function FindXY(obj){
	var x=0,y=0;
	while ((obj !=null)&&(obj.id != 'content')){
		x+=obj.offsetLeft-obj.scrollLeft;
		y+=obj.offsetTop-obj.scrollTop;
		obj=obj.offsetParent;
	}
	return {x:x,y:y};
}

var timeNew = new Array;
function showInfo(status,caller,strCat){
	var divinfo = document.getElementById('divInfo'+strCat);
	var top = 0;
	var left = 0;

	if (undefined != caller){
		top = FindXY(caller).y + caller.offsetHeight;
		left = FindXY(caller).x;
		divinfo.style.top = top+'px';
		divinfo.style.left = left+'px';
	}

	if (status == 0) {
		if (typeof(arguments[3]) != 'undefined') {
			if (typeof(timeNew[strCat]) != 'undefined' && null != timeNew[strCat]) { clearTimeout(timeNew[strCat]);}
			var func = function hideDivInfo(){ divinfo.style.display="none"; };
			timeNew[strCat] = setTimeout(func,10);
		} else {
			divinfo.style.display="none";
		}
	} else {
		clearTimeout(timeNew[strCat]);
		divinfo.style.display='block';
	}
}

function showHide(status,obj){
	if (null == status) {
		switch (obj.style.display){
		case "none":
			obj.style.display = "block";
			break;
		case "block":
			obj.style.display = "none";
			break;
		default:
			obj.style.display = "block";
			break;
		}
	} else	if (status == 0) {
		obj.style.display="none";
	} else if (status == 2) {
		obj.style.display="inline";
	} else {
		obj.style.display='block';
	}
	
}

function expandContract(id,e){
   if (!e) var e = window.event;

   var divObj = document.getElementById(id)

   var eventSource = (window.event) ? e.srcElement : e.target;
   if (e.type == "mouseout" && eventSource.nodeName != "UL")
       return;

   // prevent event bubbling
   var relTarg = e.relatedTarget || e.toElement || e.fromElement;

   try
   {
       while (relTarg && relTarg != divObj)
           relTarg = relTarg.parentNode;

       if (relTarg == divObj)
           return;

       divObj.style.display = (divObj.style.display == "block") ? "none" : "block";
   }
   catch(e)
   {}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	
	if(arguments[3] != undefined){
		alert(arguments[3]);
		cookiepath = "path=/"+arguments[3];
	} else {
		cookiepath = "path=/";
	}
	document.cookie = name+"="+value+expires+"; "+cookiepath;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	if(arguments[1] != undefined){
		createCookie(name,"",-1,arguments[1]);
	} else {
		createCookie(name,"",-1);
	}
}

function changeRegion(reg) {
	selectDpt = document.getElementById( 'departement' );
	selectDpt.options.length = 0; // clear out existing items 
//	blanckOpt = new Option("département", "") ;
//	blanckOpt.style.color = '#a0a0a0';
//	selectDpt.options.add(blanckOpt) ;
	if(reg.value != '' && reg.value != 0){
		for(i=0; i < depts[reg.value].length; i++) { 
			d = depts[reg.value][i]; 
			selectDpt.options.add(new Option(d.text.replace(/&#039;/gi,"'"), d.value)) ;
		} 
	} else {
		for(j=0; j < depts.length; j++) {
//			alert(typeof(depts[j]));
			if (typeof(depts[j]) != 'undefined'){
				for(i=0; i < depts[j].length; i++) { 
					d = depts[j][i]; 
					selectDpt.options.add(new Option(d.text.replace(/&#039;/gi,"'"), d.value)) ;
				}
			}
		}
	}
}
function trim (myString) {
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}

function looksLikeMail(str) {
    var lastAtPos = str.lastIndexOf('@');
    var lastDotPos = str.lastIndexOf('.');
    return (lastAtPos < lastDotPos && lastAtPos > 0 && str.indexOf('@@') == -1 && lastDotPos > 2 && (str.length - lastDotPos) > 2);
}

function notShorterThan(str,minlength,whipewhitespace) {
	if(whipewhitespace){
		str = str.replace(/ /gi,'');
	}

	if(str.length >= minlength){
		return true;
	} else {
		return false;
	}
}

function alertElemnt(){
	if(arguments.length >= 1){
		formulaire = arguments[0];
	} else {
		formulaire = document.editForm;
	}
	flagReturn = true;
	for(i=0;i<formulaire.length;i++) {
		obj = formulaire[i];
		if(typeof(obj) == 'object' && obj.className.indexOf('mandatory') >= 0 ){
			if(trim(obj.value).length <= 0){
				flagReturn = false;
			    obj.style.backgroundColor = "#F5E585";
			} else {
				if(obj.className.indexOf('email') >= 0 ){
					if (!looksLikeMail(obj.value)) {
						flagReturn = false;
						obj.style.backgroundColor = "#F5E585";
					} else {
						obj.style.backgroundColor = "";
					}
				}
				
				if(obj.className.indexOf('tel10') >= 0 ){
					if (!notShorterThan(obj.value,10,true)) {
						flagReturn = false;
						obj.style.backgroundColor = "#F5E585";
					} else {
						obj.style.backgroundColor = "";
					}
				}
				if(obj.className.indexOf('tel10') === false && obj.className.indexOf('email') === false  ){
					obj.style.backgroundColor = "";
				}
			}
		}
	}
	if(!flagReturn) alert('Des champs ne sont pas correctement remplis');
	return flagReturn;
}

