﻿var preloadsPic = new Object();
var KError = new Array();

function openVideo(p_video,p_type)
{
//haute 420/315
//    if(p_type == 'haute')
	    window.open("engine/videoPlayer.aspx?video="+p_video+"&width=420&height=315","video","width=440,height=325");
	    
	return false;
}

function press_region(nr)
{
  document.location.href="default.aspx?docId=2J0JQ%2flul8Q%3d&loadfile=flash/filiales_monotag.xml&region=" + nr;
}

function enlarge(height, box)
{
	var target = document.getElementById(box);
	target.style.height = height;
}

function searchNextElm(elm)
{
	while(elm != null)
	{
		if(elm.nodeType == 1)
			return elm;
					
		elm = elm.nextSibling;
  }
}
function searchPrevElm(elm)
{
	while(elm != null)
	{
		if(elm.nodeType == 1)
			return elm;
					
		elm = elm.previousSibling;
  }
}

function openShowShowReport(p_docId, p_type)
{
	var win;
    
	if(p_type=='xls')
		win = window.open('engine/ShowReport.aspx?docType='+p_type+'&docId='+p_docId,'Media','resizable=1,titlebar=1,menubar=1,scrollbars=1,width=800,height=600');
	else
		win = location.href='engine/ShowReport.aspx?docType='+p_type+'&docId='+p_docId;
		//win = window.open('engine/ShowReport.aspx?docType='+p_type+'&docId='+p_docId,'Media','width=800,height=600');
	
	win.focus();
	return false;
}

function cleanTabs(p_tabName)
{
	var first =  searchNextElm(document.getElementById(p_tabName).firstChild);
	var last =  searchPrevElm(document.getElementById(p_tabName).lastChild);
	var elm =  first;
	
	while(elm != null)
	{
		if(elm.nodeType == 1)
		{
			if(elm == first)
				elm.className = 'first';
			else if(elm == last)
				elm.className = 'empty';
			else
				elm.className = '';
		}	
		
		elm = elm.nextSibling;
  }
  
}

function selectTab(p_tabName,p_obj,p_divId)
{
	var last =  searchPrevElm(document.getElementById(p_tabName).lastChild);
	var elm = p_obj.parentNode;
	
	var div = document.getElementById(p_divId);
	var dispDivName = readCookie(p_tabName);
	var dispDiv = null;
	
	if(dispDivName != null)
	    dispDiv = document.getElementById(dispDivName);
	    
	cleanTabs(p_tabName);
	
	elm.className = 'active';
	
	if(searchNextElm(elm.nextSibling) != last)
		searchNextElm(elm.nextSibling).className = 'postactive';
		
	if(dispDiv != null)
	{
		dispDiv.style.display = 'none';
		deleteCookie(p_tabName);
	}
		
	if(div != null)
	{
		div.style.display = 'block';
		writeCookie(p_tabName,p_divId);
	}
	
	return false;
}

function initTab(p_tabName)
{
	var last =  searchPrevElm(document.getElementById(p_tabName).lastChild);
	var selected =  null;
	var divName  = readCookie(p_tabName);
	
	cleanTabs(p_tabName);

    if(divName != null)
        selected = document.getElementById('tab_'+divName);
        
    if(selected == null)
        selected =  searchNextElm(document.getElementById(p_tabName).firstChild);
    
    divName = selected.id.substring(4,selected.id.length);

	var elm = searchNextElm(selected.nextSibling);
	
	selected.className = 'active';
	if(elm != last)
	    elm.className = 'postactive';
	    
	var div = document.getElementById(divName);
    div.style.display = 'block';
    writeCookie(p_tabName,divName);
}

//Donne la valeur du parametre parameterName de la chaine queryString
function getParameter ( p_parameterName ) 
{

	// Add "=" to the parameter name (i.e. parameterName=value)
	var parameterName = p_parameterName + "=";
	var queryString = location.href;
	
	if ( queryString.length > 0 ) 
	{
		// Find the beginning of the string
		begin = queryString.indexOf ( parameterName );
		// If the parameter name is not found, skip it, otherwise return the value
		if ( begin != -1 ) 
		{
			// Add the length (integer) to the beginning
			begin += parameterName.length;
			// Multiple parameters are separated by the "&" sign
			end = queryString.indexOf ( "&" , begin );
			if ( end == -1 ) 
			{
				end = queryString.length
			}
			// Return the string
			return queryString.substring ( begin, end );
		}
		// Return "null" if no parameter has been found
		return "null";
	}
}

function writeCookie(p_nom, p_valeur, p_expires, p_path, p_domain, p_secure)
{	
	document.cookie=p_nom+"="+escape(p_valeur)+
		((p_expires==null) ? "" : ("; expires="+p_expires.toGMTString()))+
		((p_path==null) ? "" : ("; path="+p_path))+
		((p_domain==null) ? "" : ("; domain="+p_domain))+
		((p_secure==true) ? "; secure" : "");
}

function deleteCookie(name,path,domain) 
{
	if (readCookie(name)) 
	{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function _getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	
	return unescape(document.cookie.substring(offset, endstr));
}

function readCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	
	while (i<clen)
	{
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return _getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	
	return null;
}

function addToCart(p_cartName,p_data)
{
	window.open('front/page/DocCartPopupPage.aspx?docId='+p_data,'Popup','width=500,height=500');
}

function refreshDiv(p_div,p_data)
{
    var div = document.getElementById(p_div);
    
    if(div != null)
        div.innerHTML = p_data;
}

function assertValid(_obj, _msg)
{
	if (_msg==null)
		throw new Error("invalid error message");
	if (_obj==null)
		throw new Error(_msg);
	return true;
}

function invokeServer(_method, _url, _message)
{
	try {
		var objHTTP = (window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"));
		assertValid(objHTTP, "can not create a XMLHTTP object");
		
		if (_message!=null && _message!="" && _method=="POST") 
		{
			objHTTP.open(_method,_url,false);
			objHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
			objHTTP.send(_message);
		} 
		else 
		{
		    //alert('get');
			objHTTP.open("GET",_url+'?'+_message,false);
			objHTTP.send(null);
		}
		
		//alert(objHTTP.responseText);
		//alert(_message + ' '+_url+' rep=' +objHTTP.responseXML.xml);
		
		//return objHTTP.responseXML;
		return objHTTP.responseText;
	} catch (e) {
		alert('ERROR ' + e.message);
		throw Error("can not contact the server: "+e.message);
	}
}


/*function removeAllFromCart(p_cartId)
{
	if (confirm('Confirmez-vous la suppression de tous les éléments de votre panier?') != true)
        return false;
        
    var r="<?xml version='1.0' encoding='UTF-8'?>"
    var response;
    r=r+"<Request order='emptyBasket'>";
           
    var list = document.getElementsByTagName('input');
    for(i=0;i<list.length;i++)
    {
        var ctrl = list[i];
        r=r+"<selected id='"+p_cartId+"' />";
                
        r=r+"</Request>";
                
        response = invokeServer("POST", './engine/tools/AddToCart.aspx','req='+r);
        //alert(response);
        if(response.indexOf('ERROR') > 0)
        {
            alert(response);
        } 
	
	    document.forms["Form1"].submit();
	}
}*/

/*function removeFromCart(p_cartName,p_data)
{
	var list = readCookie(p_cartName);

	if(list == null)
		list = "";
	
	if(list.indexOf("&"+p_data) >= 0)
	{
		var reg = new RegExp("&"+p_data,"g");
		list = list.replace(reg,"");
	}
	
	if (confirm('Confirmez-vous la suppression de cet élément de votre panier?') == true)
	{
		date=new Date;
		date.setFullYear(date.getFullYear()+10);

		if(list == '')
			deleteCookie(p_cartName,"/",null);
		else
			writeCookie(p_cartName,list,date,"/",null,null);
			
		alert("L'élément a été enlevé de votre panier.");
		
		document.forms["Form1"].submit();
	}
}
*/

function addPreloadPic(p_control,p_normalImgId,p_mouseOverImgId)
{
	var img = new Image;
	preloadsPic['n'+p_control.id]= new Image;
	preloadsPic['n'+p_control.id].src = 'engine/getFile.aspx?fileId='+p_normalImgId;

	preloadsPic['o'+p_control.id]= new Image;
	preloadsPic['o'+p_control.id].src = 'engine/getFile.aspx?fileId='+p_mouseOverImgId;

	preloadsPic['o'+p_control.id].onerror = function () {this.src='images/p.gif'}

	p_control.onmouseover = function () { this.src=preloadsPic['o'+this.id].src;}
	p_control.onmouseout = function () { this.src=preloadsPic['n'+this.id].src;}
}

function alertError()
{
	if(KError.length  > 0)
	{
		var message='';
		for(var i = 0; i < KError.length; i++)
			message+=KError[i]+"\n";
		
		alert(message);
	}
}

function openAdmin(p_ext,p_docId,p_modId)
{
	if(p_docId == null)
		window.open('front/security/MainPage.aspx?ext='+p_ext+'&modId='+p_modId,'Administration','width=800,height=600,scrollbars=yes,resizable=yes');
	else
		window.open('front/security/MainPage.aspx?ext='+p_ext+'&docId='+p_docId+'&modId='+p_modId,'Administration','width=800,height=600,scrollbars=yes,resizable=yes');
	return false;
}

function openOrderAdmin(p_ext,p_grpId,p_modId)
{
	if(p_grpId != null)
		window.open('front/security/MainPage.aspx?ext='+p_ext+'&modId='+p_modId+'&grpId='+p_grpId,'ordre','width=800,height=600,scrollbars=yes,resizable=yes');
	else
		window.open('front/security/MainPage.aspx?ext='+p_ext+'&modId='+p_modId,'ordre','width=800,height=600,scrollbars=yes,resizable=yes');
	return false;
}

function openTreeAdmin(p_ext,p_grpId,p_modId)
{
	window.open('front/security/MainPage.aspx?aut=Bcz72mutUUyuH7FuRBcs8Q%3d%3d&ext='+p_ext+'&moduleId='+p_modId+'&grpId='+p_grpId,'Administration','width=800,height=600,scrollbars=yes,resizable=yes');
	return false;
}

function openParamAdmin(p_ext,p_permName,p_paramCode,p_paramIndice)
{
	//alert('front/security/MainPage.aspx?ext='+p_ext+'&aut='+p_permName+'&paramCode='+p_paramCode);
	window.open('front/security/MainPage.aspx?ext='+p_ext+'&aut='+p_permName+'&paramCode='+p_paramCode+
			'&paramIndice='+p_paramIndice,'Administration','width=800,height=600,scrollbars=yes,resizable=yes');

	return false;
}

function openShowPic(p_fileId,p_width,p_height)
{
	var win = window.open('engine/getFile.aspx?fileId='+p_fileId,'Media','width='+(p_width+20)+',height='+(p_height+20));
	win.focus();
	return false;
}

function openShowDoc(p_fileId,p_width,p_height,p_fileName)
{
	var win = window.open('engine/getFile/pdf/'+escape(p_fileId)+'/'+p_fileName,'Media','width='+(p_width+20)+',height='+(p_height+20));
	//var win = window.open('engine/getFile.aspx?fileId='+p_fileId,'Administration','width=800,height=600,scrollbars=yes,resizable=yes');
	win.focus();
	return false;
}

function clearWebcodeField(elm){
    if(elm.value=="Code web"){
            elm.value = '';
    }
}


function fnTrapKD(btnID, event)
{
            //btn = findObj(btnID);
            var btn = document.getElementById(btnID)
            if (document.all)
            {
                        if (event.keyCode == 13)
                        {
//                                 alert(btnID);
                                   event.returnValue=false;
                                   event.cancel = true;
                                   if(btn.href != null && btn.href.indexOf('javascript:__doPostBack') >= 0)
                                               eval(btn.href);
                                   else
                                               btn.click();
                        }
            }
            else if (document.getElementById)
            {
                        if (event.which == 13)
                        {
                                   event.returnValue=false;
                                   event.cancel = true;
                                   if(btn.href != null && btn.href.indexOf('javascript:__doPostBack') >= 0)
                                               eval(btn.href);
                                   else
                                               btn.click();
                        }
            }

            else if(document.layers)

            {
                        if(event.which == 13)

                        {
                                   event.returnValue=false;
                                   event.cancel = true;
                                   if(btn.href != null && btn.href.indexOf('javascript:__doPostBack') >= 0)
                                               eval(btn.href);
                                   else
                                               btn.click();
                        }
            }
}




function onLoadHandler()
{
	alertError();
}


// DOM2
if ( typeof window.addEventListener != "undefined" )
	window.addEventListener( "load", onLoadHandler, false );
else if ( typeof window.attachEvent != "undefined" ) 
	window.attachEvent( "onload", onLoadHandler );

else 
{
	if ( window.onload != null ) 
	{
		var oldOnload = window.onload;
		window.onload = function ( e ) {
			oldOnload( e );
			onLoadHandler();};
	}
	else 
		window.onload = onLoadHandler;
}

// AffPoste
function clearWebcodeField(elm){
    if(elm.value=="Code web"){
	elm.value = '';
    }
}

var popUp = null;
			
function openPopUp(p_url)
{
	if(popUp==null)
		popUp=window.open(p_url,'mail','height=200,width=300');
	else
		popUp.location=p_url;
}

function closePopup()
{
	if(popUp != null)
		popUp.close();
}
