
FunctionsOnLoad = new Array(); 
FormAction = new Array(); 


window.onload = function() { 
    for(ii=0; ii<FunctionsOnLoad.length; ii++) 
        {FunctionsOnLoad[ii]();} 
}



function FunctionOnLoad(Funct) { 
    FunctionsOnLoad[FunctionsOnLoad.length] = Funct; 
} 

function ShowMenu(id) {
	if (typeof(window.innerWidth) == 'number') {
		/*Non-IE*/
		document.getElementById(id).style.width=document.getElementById('menu0').style.width;
	}
	document.getElementById(id).style.display='block';
}

function CloseMenu(e, id) {
	if (!e) var e = window.event; 
	var SubMenu = document.getElementById(id);
	if (typeof(window.innerWidth) == 'number') {
		/*Non-IE*/
		if (!(SubMenu.compareDocumentPosition(e.relatedTarget) & 16)) SubMenu.style.display='none';
	}
	else {
		if (!SubMenu.contains(e.toElement)) SubMenu.style.display='none';
	}
}



function addIFrame(node) {
ul = node.lastChild;
h = ul.clientHeight;
w = ul.clientWidth;
}










function hash(FormName) {

  PassWord=document.forms[FormName].password.value;
  document.forms[FormName].password.value="";
  HashedPassWord=MD5(PassWord);
  document.forms[FormName].hashed_password.value=HashedPassWord;
  return true;
}

/*function get_param(param_name) {
	param_name = param_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+param_name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec(window.location.href);
	if (results == null)
		return false;
	else
		return results[1];
}
*/


/*
function TestMsg() {

	if (document.getElementById("dp_message_span")!=null) {
		MsgBox(document.getElementById("dp_message_span").innerHTML);
	}
}

FunctionOnLoad(TestMsg);
*/


function Bulle(e,contenu) {

	MoveBulle(e);	
	document.getElementById("dp_message").innerHTML=contenu;
	document.getElementById("dp_message").className='bulle';
    document.getElementById("dp_message").style.visibility="visible";
}

function MoveBulle(e) {

	if (!e) var e = window.event; 
    MousePosition=GetMousePosition(e);
    document.getElementById("dp_message").style.left=MousePosition[0]+1+"px";
    document.getElementById("dp_message").style.top=MousePosition[1]+1+"px";

}

function ClearBulle() {

	document.getElementById("dp_message").style.visibility="hidden";
}

function NewsBulle(e,contenu) {

	if (!e) var e = window.event; 
	document.getElementById("dp_message").innerHTML=contenu;
	document.getElementById("dp_message").className='newsbulle';
	MousePosition=GetMousePosition(e);
	WindowCenter=GetWindowCenter();
	WindowSize = GetWindowSizeScrolled();
	MsgSize = GetDivSize(document.getElementById("dp_message"));
	if (MousePosition[0] >WindowCenter[0]) {
		document.getElementById("dp_message").style.left=WindowCenter[0]-90+"px";
	}
	else {
		document.getElementById("dp_message").style.left=WindowCenter[0]-330+"px";
	}
	if (MsgSize[1]+MousePosition[1]>WindowSize[1]) {
		document.getElementById("dp_message").style.top=WindowSize[1]-MsgSize[1]+"px";
	}
	else {
		document.getElementById("dp_message").style.top=MousePosition[1]-10+"px";
	}
    document.getElementById("dp_message").style.visibility="visible";
}

function AddButtonValue(FormName, Value) {

	var Form = document.getElementById(FormName);
	var NewInput = document.createElement('input');
	NewInput.setAttribute('type', 'hidden');
	NewInput.setAttribute('name', 'button');
	NewInput.setAttribute('value', Value);
	Form.appendChild(NewInput);

}

function AddParseAction(FormName, Value) {

	var Form = document.getElementById(FormName);
	if (typeof(FormAction[FormName])=='undefined') {
		FormAction[FormName] = Form.action;
	}
	Form.action = Form.action+'/'+Value;
}

function RemoveParseAction(FormName, Value) {

	var Form = document.getElementById(FormName);
	Form.action = FormAction[FormName];
}

function ChangeTarget(FormName, Value) {

	var Form = document.getElementById(FormName);
	Form.target = Value;
}

function AddType(FormName, Value) {

	var Form = document.getElementById(FormName);
	Form.action = Form.action.replace(/action_type/g,Value);	
}

function ConfirmBox(contenu,Confirm,ConfirmAction,Cancel,FormName) {
		
	document.getElementById("dp_message").className='ConfirmBox';
	
   	contenu+= '<br><br><form style="display:inline;"><input type="button" id="dp_message_button" value="'+Confirm+'" onClick="AddParseAction(\''+FormName+'\', \''+ConfirmAction+'\'); document.'+FormName+'.submit()">&nbsp;';
	contenu+= '<input type="button" value="'+Cancel+'" onClick="ClearBox()"></form>';
    document.getElementById("dp_message").innerHTML=contenu;

	DisplayBox();
	document.getElementById("dp_message_button").focus();	
}

function ConfirmBox2(contenu,Confirm,ConfirmAction,Cancel) {
		
	document.getElementById("dp_message").className='ConfirmBox';
	
   	contenu+= '<br><br><form style="display:inline;" method="POST" action="'+ConfirmAction+'"><input type="submit" id="dp_message_button" value="'+Confirm+'">&nbsp;';
	contenu+= '<input type="button" value="'+Cancel+'" onClick="ClearBox()"></form>';
    document.getElementById("dp_message").innerHTML=contenu;

	DisplayBox();
	document.getElementById("dp_message_button").focus();	
}

function ChoiceBox(contenu,Choice,Cancel,FormName) {
	

	document.getElementById("dp_message").className='ChoiceBox';
	
   	contenu+= '<br><br><form style="display:inline;">';
	for (i=0;i<Choice.length;i++){
		contenu+= '<input type="button" value="'+Choice[i][0]+'" onClick="AddParseAction(\''+FormName+'\', \''+Choice[i][1]+'\'); document.'+FormName+'.submit()">&nbsp;';
   	}
	contenu+= '<input type="button" value="'+Cancel+'" onClick="ClearBox()"></form>';
    document.getElementById("dp_message").innerHTML=contenu;

	DisplayBox();
}

function ChoiceBoxType(contenu,Choice,Cancel,FormName) {
	

	document.getElementById("dp_message").className='ChoiceBox';
	
   	contenu+= '<br><br><form style="display:inline;">';
	for (i=0;i<Choice.length;i++){
		contenu+= '<input type="button" value="'+Choice[i][0]+'" onClick="AddType(\''+FormName+'\', \''+Choice[i][1]+'\'); document.'+FormName+'.submit()">&nbsp;';
   	}
	contenu+= '<input type="button" value="'+Cancel+'" onClick="ClearBox()"></form>';
    document.getElementById("dp_message").innerHTML=contenu;

	DisplayBox();
}

function MsgBox(contenu) {
	
	document.getElementById("dp_message").className='MsgBox';
	
   	contenu+= '<br><br><form style="display:inline;"><input type="button" id="dp_message_button" value="OK" onClick="ClearBox()"></form>';
    document.getElementById("dp_message").innerHTML=contenu;

	DisplayBox();
	document.getElementById("dp_message_button").focus();
}

function ProcessBox(contenu, FormName) {

	PreLoad("/css/icons/processing.gif");
	ProcessBox2(contenu, FormName);
}

function ProcessBox2(contenu, FormName) {

	if (CheckLoad()) {
		document.getElementById("dp_message").className='MsgBox';
		contenu+= '<br><br><br><img src="/css/icons/processing.gif"><br><br><br>';
		document.getElementById("dp_message").innerHTML=contenu;
		DisplayBox();
		document.forms[FormName].submit();
	}
	else {
		setTimeout("ProcessBox2('"+contenu+"', '"+FormName+"')",100);	
	}
}


function PreLoad(img) {
	Preloaded = new Image();
	Preloaded.src = img;
}

function CheckLoad() {
	if (Preloaded.complete) {
		return true;
	}
	else {
		return false;
	}
}

/*function PayPalSubmit() {

	if (document.getElementById("form_paypal")!=null) {
		ImageLoading = new Image();
		document.getElementById("dp_message").className='MsgBox';
		if (window.location.pathname == '/store/catalogue/process') {
			document.getElementById("dp_message").innerHTML='Votre allez être redirigé vers le site PayPal...<br><br><br>';
		}
		else {
			document.getElementById("dp_message").innerHTML='Votre commande est en cours de validation...<br><br><br>';
		}
		document.getElementById("dp_message").innerHTML+='<img src="'+location.protocol+'//'+location.host+'/css/icons/processing.gif" alt="traitement"><br><br><br>';
		DisplayBox();
		document.form_paypal.submit();
	}
}
*/


function DisplayBox() {

	ToggleSelect('hidden');
	DisplayTransparent();
	
	WindowCenter = GetWindowCenter();	
	var PosX = WindowCenter[0] - document.getElementById("dp_message").offsetWidth/2;
	var PosY = WindowCenter[1] - document.getElementById("dp_message").offsetHeight/2;
		
    document.getElementById("dp_message").style.left = PosX + "px";
    document.getElementById("dp_message").style.top = PosY + "px";
    document.getElementById("dp_message").style.visibility="visible";	
	
	document.getElementById("ombre").style.height = document.getElementById("dp_message").offsetHeight;
	document.getElementById("ombre").style.width = document.getElementById("dp_message").offsetWidth;
	document.getElementById("ombre").style.left = PosX - 10 + "px";
	document.getElementById("ombre").style.top = PosY + 10 + "px";
    document.getElementById("ombre").style.visibility="visible";	
}


function ClearBox() {

	ToggleSelect('visible');
	document.getElementById("dp_message").style.visibility="hidden";
	document.getElementById("dp_transparent").style.visibility="hidden";
	document.getElementById("ombre").style.visibility="hidden";
}

function ToggleSelect(visibility) {

	var selects=document.getElementsByTagName("select");
	for (i=0;i<selects.length;i++){
		selects[i].style.visibility=visibility;
	}
}
	
function DisplayTransparent() {

	DocumentSize=GetDocumentSize();

	document.getElementById("dp_transparent").style.width=DocumentSize[0]+"px";
	document.getElementById("dp_transparent").style.height=DocumentSize[1]+"px";
	document.getElementById("dp_transparent").style.visibility="visible";
}

function GetDocumentSize() {

	var DocumentSize = new Array();

	if( typeof( window.innerWidth ) == 'number' ) {
		/*Non-IE*/
		DocumentSize[0] = document.width;
		DocumentSize[1] = document.height;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		/*IE 6+ in 'standards compliant mode' */
		DocumentSize[0] = document.documentElement.scrollWidth;
		DocumentSize[1] = document.documentElement.scrollHeight;
	} else if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
		/*IE 4 compatible*/
		DocumentSize[0] = document.body.scrollWidth;
		DocumentSize[1] = document.body.scrollHeight;
	}

	var WindowSize = GetWindowSize();
	if (WindowSize[0]>DocumentSize[0]){DocumentSize[0]=WindowSize[0]}
	if (WindowSize[1]>DocumentSize[1]){DocumentSize[1]=WindowSize[1]}
	
	return DocumentSize;
}

function GetWindowSize() {

	var WindowSize = new Array();
	
	if( typeof( window.innerWidth ) == 'number' ) {
		/*Non-IE*/
		WindowSize[0] = window.innerWidth;
		WindowSize[1] = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		/*IE 6+ in 'standards compliant mode'*/
		WindowSize[0] = document.documentElement.clientWidth;
		WindowSize[1] = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		/*IE 4 compatible*/
		WindowSize[0] = document.body.clientWidth;
		WindowSize[1] = document.body.clientHeight;
	}
		
	return WindowSize;
}

function GetWindowSizeScrolled() {

	var WindowSize = new Array();
	
	if( typeof( window.innerWidth ) == 'number' ) {
		/*Non-IE*/
		WindowSize[0] = window.innerWidth + document.documentElement.scrollLeft;
		WindowSize[1] = window.innerHeight + document.documentElement.scrollTop;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		/*IE 6+ in 'standards compliant mode'*/
		WindowSize[0] = document.documentElement.clientWidth + document.documentElement.scrollLeft;
		WindowSize[1] = document.documentElement.clientHeight + document.documentElement.scrollTop;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		/*IE 4 compatible*/
		WindowSize[0] = document.body.clientWidth + document.documentElement.scrollLeft;
		WindowSize[1] = document.body.clientHeight + document.documentElement.scrollTop;
	}
		
	return WindowSize;
}

function GetWindowCenter() {

	var WindowCenter = new Array();
	
	var WindowSize = GetWindowSize();
		
	WindowCenter[0] = document.documentElement.scrollLeft + WindowSize[0]/2;
	WindowCenter[1] = document.documentElement.scrollTop + WindowSize[1]/2;
	
	return WindowCenter;
}

function GetDivSize(div) {

	var DivSize = new Array();
	
		DivSize[0] = div.offsetWidth;
		DivSize[1] = div.offsetHeight;
		
	return DivSize;
}

function GetMousePosition(e) {

	var MousePosition = new Array();

	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		MousePosition[0] = e.pageX;
		MousePosition[1] = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
/*		window.alert(document.documentElement.scrollTop);
		MousePosition[0] = e.clientX + document.body.scrollLeft;
		MousePosition[1] = e.clientY + document.body.scrollTop;*/
		MousePosition[0] = e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		MousePosition[1] = e.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
    
	if (MousePosition[0]<0) MousePosition[0]=0;
    if (MousePosition[1]<0) MousePosition[1]=0;

	return MousePosition;
}


function SelectChange(SelectObj) {
	
	var SelectedValues = new Array();
	var selects=document.getElementsByTagName("select");
	var mySelectedIndex=SelectObj.options.selectedIndex;
	
	if (typeof(InitialValues)=='undefined') {
		InitialValues = new Array();
		for (i=0;i<selects[0].options.length;i++){
			InitialValues[i] = selects[0].options[i].value;
		}
	}

	for (i=0;i<selects.length;i++){
		SelectedValues[i]=selects[i].options[selects[i].options.selectedIndex].value;
		for (j=selects[i].options.length;j>0;j--){
			selects[i].options[j] = null;
		}
	}
	
	for (i=0;i<selects.length;i++){
		for (j=1;j<InitialValues.length;j++){
			var flag = 0;		
			for (k=0;k<SelectedValues.length;k++) {
				if (InitialValues[j] == SelectedValues[k]) {
					if (i != k) {
						flag = 1;
					}
					else {
						flag =2;
					}
				}
			}
			if (flag != 1) {
				selects[i].options[j]=new Option(InitialValues[j], InitialValues[j]);	
			}
			if (flag == 2) {
				selects[i].options.selectedIndex=j;	
			}
		}
	}

}

function openwindow(page) {
  window.open(page,"popup","menubar=yes, status=yes, scrollbars=yes,width=850, height=800");
}

function display_template(FormName,Field) {
	var Selects=document.getElementsByName("template");
	var Template=(Selects[0].options[Selects[0].options.selectedIndex].value);
	Href = window.location.href;
	Href=Href.replace(/#/g,'');	
	openwindow(Href+'?template='+Template+'&field='+Field);
}















/*4 caracters desc: 1 for required or not and 3 for valid_type (tel, str, num, etc.) */

function checkForm(form) {
	
	var error_msg='';
	var inputs = form.getElementsByTagName('input');
	var selects = form.getElementsByTagName('select');
	
	if (typeof(color) != "undefined") {
		for (i=0;i<inputs.length;i++) {
			var field = inputs[i];
			if (field.style.background == 'red') {
				field.style.background = color;
				break;
			}
		}
		for (i=0;i<selects.length;i++) {
			var field = selects[i];
			if (field.style.background == 'red') {
				field.style.background = color;
				break;
			}
		}
	}
	
	for (i=0;i<inputs.length;i++){
		if (inputs[i].type == 'text' || inputs[i].type == 'password') {
			var field = inputs[i];
			if (field.name.substring(0,3) == '%%%') {
			
				var required = field.name.substring(3,4);
				var valid_type = field.name.substring(4,7);
				var value = field.value.replace(/^\s+|\s+$/g,''); /*trim()*/

				if ((required == '1') && (value.length == 0)) {
					error_msg='Ce champ doit impérativement être rempli';
					break;		
				}
				
				if (valid_type == 'mel') {
					var filter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
					var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
					if ((!filter.test(value)) || (value.match(illegalChars))) {
						error_msg='L\'adresse email est invalide';
						break;
					}
				}

				if (valid_type == 'tel') {
					var stripped = value.replace(/[\(\)\.\-\+\ ]/g, ''); 
					if ((isNaN(parseInt(stripped))) || (!(stripped.length == 10))) {
						error_msg='Le numéro de téléphone est invalide (xx xx xx xx xx)';
						break;				
					}
				}
				
				if (valid_type == 'num') {
					if (isNaN(value)) {
						error_msg='Cette valeur doit inpérativement être un nombre';
						break;				
					}
				}
			}
		}
	}
	if (error_msg != '') {
		color = field.style.background; 
		if (color =='') {
			color = 'white';
		}
		field.style.background = 'red'; 
		MsgBox(error_msg);
		return false;
	}
	else {
		for (i=0;i<selects.length;i++){
			var field = selects[i];
			if (field.name.substring(0,3) == '%%%') {
				var required = field.name.substring(3,4);
				if (required == '1') {
					var value = field.options[field.selectedIndex].value.replace(/^\s+|\s+$/g,''); /*trim()*/
					if (value.length == 0) {
						error_msg='Ce champ doit impérativement être rempli';
						break;		
					}		
				}
			}
		}
		if (error_msg != '') {
			color = field.style.background; 
			if (color =='') {
				color = 'white';
			}
			field.style.background = 'red'; 
			MsgBox(error_msg);
			return false;
		}
		else {
			return true;
		}
	}
}

