	/*************************************************************************
	 *
	 * This file contains functions used throughout the samariis web site:
	 * 1. Functions to manage layers drop down elements
	 * 2. Functions to check forms content
	 *
	 * Copyright samariis
	 *
	 *************************************************************************/
	
	
	/*************************************************************************
	 * Functions to manage layers drop down elements
	 *************************************************************************/
	function getEl(n, d) {
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x = getEl(n,d.layers[i].document);
	  if(!x && document.getElementById) x=document.getElementById(n); return x;
	}
	
	function setTextOfDD(objName,x,newText) {
		if ((obj=getEl(objName))!=null) with (obj)
		if (document.layers) {document.write(unescape(newText)); document.close();}
		else innerHTML = unescape(newText);	
	}
	
	check = []; //this is an array that stores all the true/false values for each checkbox 
	function checkBox(id) {
		if(check[id] != true) {
			getEl('imgCB_' + id).src = "/images/cb1.gif";
			getEl(id + 'Field').value = "true";
			check[id] = true;
		}
		else {
			getEl('imgCB_' + id).src = "/images/cb0.gif"; 
			getEl(id + 'Field').value = "false"; 
			check[id] = false;
		}
	}


	function dispSearchClan(ddName) {
		getEl('searchClan' + ddName).style.display = "block";
		setLayerVisibility(ddName + 'Div', 'hidden');
	}
	
	function hideSearchClan(ddName) {
		getEl('searchClan' + ddName).style.display = "none";
	}
	
	var isBusy = false;
	var curDDName = false;
	function searchClanLst(ddName, formName, typeSearch) {
		if (isBusy) {
			xmlhttp.onreadystatechange = function () {}
			xmlhttp.abort();
		}
		xmlhttp.open("GET", "/scripts/ajax/getClansDD.php?name=" + ddName + "&formName=" + formName + "&typeSearch=" + typeSearch + "&searchClan=" + getEl("searchClanField" + ddName).value, true);
		isBusy = true;
		curDDName = ddName;
		xmlhttp.onreadystatechange = getSearchClanResult;
		xmlhttp.send(null);
	}
	
	
	function getSearchClanResult() {
		if (xmlhttp.readyState == 4) {
			results = xmlhttp.responseText;
			if (results == "0") {
				alert("Aucun clan ne correspond à votre critère de recherche !");
			}
			else if (results == "-1") {
				alert("Plus de 50 clans correspondent à votre critère de recherche ! Merci d'affiner votre recherche !");
			}
			else {
				getEl(curDDName + "SearchClanDiv").innerHTML = results;
			}
			
			isBusy = false;
		}
	}
	
	
	function getHTTPObject() {
		var xmlhttp;
		/*@cc_on
		@if (@_jscript_version >= 5)
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) {
					xmlhttp = false;
				}
			}
		@else
		xmlhttp = false;
		@end @*/
	
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			try {
				xmlhttp = new XMLHttpRequest();
			} catch (e) {
				xmlhttp = false;
			}
		}
		return xmlhttp;
	}
	var xmlhttp = getHTTPObject();

	function getPageOffsetLeft (el) {
		var ol = el.offsetLeft;
		while ((el=el.offsetParent) != null) {
			ol += el.offsetLeft;
		}
		return ol;
	}
	
	function getPageOffsetTop (el) {
		var ot = el.offsetTop;
		while((el=el.offsetParent) != null) {
			ot += el.offsetTop;
		}
		return ot;
	}
	
	function getAnchorPosition(anchorname) {
		// This function will return an Object with x and y properties
		var useWindow = false;
		var coordinates = new Object();
		var x = 0, y = 0;
		
		// Logic to find position
		if (window.top.use_gebi && document.all) {
			x = getPageOffsetLeft(document.all[anchorname]);
			y = getPageOffsetTop(document.all[anchorname]);
		}
		else if (window.top.use_gebi) {
			var o = document.getElementById(anchorname);
			x = getPageOffsetLeft(o);
			y = getPageOffsetTop(o);
		}
		else if (window.top.use_css) {
			x = getPageOffsetLeft(document.all[anchorname]);
			y = getPageOffsetTop(document.all[anchorname]);
		}
		else if (window.top.use_layers) {
			var found = 0;
			for (var i = 0; i < document.anchors.length; i++) {
				if (document.anchors[i].name == anchorname) {
					found = 1;
					break;
				}
			}
			if (found==0) {
				coordinates.x = 0;
				coordinates.y = 0;
				return coordinates;
			}
			x = document.anchors[i].x;
			y = document.anchors[i].y;
		}
		else {
			coordinates.x = 0;
			coordinates.y = 0;
			return coordinates;
		}
		coordinates.x = x;
		coordinates.y = y;
		//alert(x + ' - ' + y);
		return coordinates;
	}
	
	function setLayerVisibility(layerName, visibility) {
		if (window.top.use_gebi) {
			document.getElementById(layerName).style.visibility = visibility;
		}
		else if (window.top.use_css) {
			document.all[layerName].style.visibility = visibility;
		}
		else if (window.top.use_layers) {
			document.layers[layerName].visibility = visibility;
		}
	}
	
	function setLayerSize(divName, height) {
		if (window.top.use_gebi) {
			document.getElementById(divName).style.height = height;
		}
		else if (window.top.use_css) {
			document.all[divName].style.height = height;
		}
	}
	
	function showList(anchorName, deltaX, deltaY) {
		var coordinates = getAnchorPosition(anchorName + 'ImgA');
		var divName = anchorName + 'Div';
		
		var x = coordinates.x + deltaX;
		var y = coordinates.y + deltaY;
		
		y = y + 20;
		x = x - 200;
		// Show the DIV object
		if (window.top.use_gebi) {
			document.getElementById(divName).style.left = x + "px";
			document.getElementById(divName).style.top = y + "px";
		}
		else if (window.top.use_css) {
			document.all[divName].style.left = x;
			document.all[divName].style.top = y;
		}
		else if (window.top.use_layers) {
			x = x - document.layers[divName].clip.width;
			document.layers[divName].left = x;
			document.layers[divName].top = y;
		}
		setLayerVisibility(divName, "visible");
	}
	
	function getSelEl(object, dropDownName) {
		setLayerVisibility(dropDownName + 'Div', 'hidden');
	}
	
	function updDDVal(field, label, value) {
		if (label != '') {
			getEl(field + "Field").value = label.innerHTML;
		}
		else { 
			getEl(field + "Field").value = '';
		}
		getEl(field).value = value;
		setLayerVisibility(field + 'Div', 'hidden');
	}
	
	//listOldEventListener = null;
	listObjects = new Array();
	
	function registerLayer(divName) {
		listObjects[listObjects.length] = divName;
	}
	
	function attachListener() {
		if (document.layers) {
			document.captureEvents(Event.MOUSEUP);
		}
		/*listOldEventListener = document.onmouseup;
		if (listOldEventListener != null) {
			document.onmouseup = new Function("listOldEventListener(); hideListIfNotClicked();");
		}
		else {*/
			document.onmouseup = hideListIfNotClicked;
		//}
	}
	
	function isClicked(divName, e) {
		if (divName != null) {
			if (window.top.use_layers) {
				var clickX = e.pageX;
				var clickY = e.pageY;
				var t = document.layers[divName];
				if ((clickX > t.left) && (clickX < t.left + t.clip.width) && (clickY > t.top) && (clickY < t.top + t.clip.height)) {
					return true;
				}
				else {
					return false;
				}
			}
			else if (document.all) { // Need to hard-code this to trap IE for error-handling
				var t = window.event.srcElement;
				while (t.parentElement != null) {
					if (t.id == divName) {
						return true;
					}
					t = t.parentElement;
				}
				return false;
			}
			else if (window.top.use_gebi && e) {
				var t = e.originalTarget;
				while (t.parentNode != null) {
					if (t.id == divName) {
						return true;
					}
					t = t.parentNode;
				}
				return false;
			}
			return false;
		}
		return false;
	}
	
	function hideListIfNotClicked(e) {
		for (var i = 0; i < listObjects.length; i++) {
			if (listObjects[i] != null) {
				var layerName = listObjects[i];
				if (!isClicked(e, layerName)) {
					setLayerVisibility(layerName, "hidden");
				}
			}
		}
	}

	
	/**
	 * This function checks if elements of css class className have been filled
	 * by user. In case some are empty, a javascript alert prevents the user from
	 * saving the data and lists the fields to be completed.
	 */
	function checkElements(formNameParam) {
		var className = 'champObligatoire';
		var formName = 'form1';
		if (formNameParam != null) {
			formName = formNameParam;
		}
		
		if (getEl(formName).getAttribute('formSent') == 'yes') {
			alert('Votre requête est en train d\'être traitée... Vous devez attendre la réponse du serveur avant d\'en envoyer une autre !');
			return false;
		}
		
		var all = document.all ? document.all : document.getElementsByTagName('*');
		var go = true;
		var msg = '';
		
		for (var e = 0; e < all.length; e++) {
			var curField = all[e];
			
			if (curField.className == className) {
				var parentTag = curField.parentNode;
				
				while (parentTag.tagName != "FORM") {
					parentTag = parentTag.parentNode;
				}
				
				if (parentTag.name == formName) {
					if (curField.value == "") {
						msg = msg + curField.getAttribute("errMsg") + "\n";
						if (go) {
							try {
								curField.focus();
							}
							catch (er) {}
							go = false;
						}
					}
				}
			}
			
			var errorOnType = false;
			var typeField = "";
			if (curField.tagName == "INPUT") {
				var curVarType = curField.getAttribute("varType");
				if ((typeof curVarType != "undefined") && (curVarType != "") && (curField.value != "")) {
					if ((curVarType == "double") || (curVarType == "int") || (curVarType == "intPos")) {
						var oldstring = curField.value;
						var newstring;
						if (curVarType == "double") {
							newstring = parseFloat(oldstring).toString();
							typeField = "nombre";
						}
						else if (curVarType == "int") {
							newstring = parseInt(oldstring).toString();
							typeField = "nombre entier";
						}
						else if (curVarType == "intPos") {
							newstring = parseInt(oldstring).toString();
							typeField = "nombre entier positif";
						}
						if ((oldstring.length != newstring.length) || (newstring == "NaN")) {
							errorOnType = true;
						}
						else if ((curVarType == "intPos") && (parseInt(oldstring) < 0)) {
							errorOnType = true;
						}
					}
					else if (curVarType == "URL") {
						if (curField.value.substring(0, 7) != "http://") {
							errorOnType = true;
							typeField = "site web (http://...)";
						}
					}
					
						
					if (errorOnType) {
						parentTag = curField.parentNode;
						while ((parentTag.tagName != "TR") && (parentTag.tagName != "FORM")) {
							parentTag = parentTag.parentNode;
						}
						if (parentTag.tagName == "TR") {
							msg = msg + "Le champ *" + parentTag.firstChild.innerText + "* requiert la saisie d'un " + typeField + " !\n";
						}
						
						if (go) {
							try {
								curField.focus();
								curField.select();
							}
							catch (er) {}
							go = false;
						}
					}
				}
			}
		}
		
		if (!go) {
			alert(msg);
		}
		else {
			getEl(formName).setAttribute('formSent', 'yes');
		}
		
		return go;
	}