
/*** COOKIES HANDLING ***/

function setCookie(cookieName, cookieValue, nDays) 
{
	 var today = new Date();
	 var expire = new Date();
	 if (nDays==null || nDays==0) nDays=1;
	 expire.setTime(today.getTime() + 3600000*24*nDays);
	 document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/";
}

function deleteCookie(cookieName, cookieValue) 
{
	setCookie(cookieName, cookieValue, -100); 
}

function getCookie(name) 
{
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}



/*** FORM HANDLING METHODS ***/

/* Method adds to the action URL the specified parameters and submits the form.
   Note: Works with POST  method only!	
 */
/*function submitForm(formName, params, stringParams)
{	
	var par = "";
	
	if(params!=null)
	{
		for(i=0; i<params.length; i++)
		{
			par = par + "&" + params[i] + "=" +  document.getElementById(params[i]).value;
		}	
	}
	if (stringParams != null){
		par = par + stringParams;
	}
	document.forms[formName].action = document.forms[formName].action + par;
	
	document.forms[formName].submit();
}
*/

function submitForm(formName, params, stringParams){	
	var par = "";
	if(params!=null){
		for(i=0; i<params.length; i++){
			obj = eval("document.forms['" + formName + "']." + params[i]);
			if (obj.type == null){
				//this is an array
				temppar = "";
				for (j = 0; j < obj.length; j++){
					if (obj[j].type == "checkbox" || obj[j].type == "radio"){
						if (obj[j].checked){
							temppar = temppar + obj[j].value + ",";
						}
					} else {
						temppar = temppar + obj[j].value + ",";
					}
				}
				if (temppar.length > 0){
					par = par + "&" + params[i] + "=" + temppar.substring(0, temppar.length - 1);
					
				}
			} else {
				//this is an object
				if (obj.type == "checkbox" || obj.type == "radio"){
					if (obj.checked){
						par = par + "&" + params[i] + "=" + obj.value;
					}
				} else {
					par = par + "&" + params[i] + "=" + obj.value;
				}
			}
		}	
	}
	if (stringParams != null){
		par = par + stringParams;
	}
	document.forms[formName].action = document.forms[formName].action + par;
	
	document.forms[formName].submit();
}






/*** CLIENT-SIDE CHECKS ***/

// Verifies if the submitted file (using <input type="file" ...) is an image file with an accepted extension.

function checkImageFileExtension(formName, pictureInputName) 
{	
  var pInputName = eval("document.forms['" + formName + "']." + pictureInputName);
  var ext = pInputName.value;
  if(ext=='') return true;
  
  ext = ext.substring(ext.length-4,ext.length);
  ext = ext.toLowerCase();
  if( (ext != '.jpg') && (ext != '.gif') && (ext != '.bmp') && (ext != 'jpeg') ) {
    alert('You selected a '+ext+
          ' file. Please select a .jpg, .jpeg, .gif or .bmp image file instead!');
    return false; 
  }
  else
    return true;
}




/*** IMAGES EFFECTS SCRIPTS ***/

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



/*** CONTROLS ACTIONS HANDLING ***/

/*
Call this metod on checkboxes and checkboxes selector, using [onClick] event.

selectorAction = true if the method is triggered by selector
			   = false if the method is triggered by any checkbox in the group				   
*/	
function checkSelector(selectorAction, selectorName, checkBoxesName)
{	
    var selector = null;
    var checkBoxes = null;
	
	try	
	{ selector = document.getElementById(selectorName);	}	
	catch(e){}
	
	if(selector==null)
		alert("Checkbox having id '" + selectorName + "' could not be found in the document!");
		
	checkBoxes = document.getElementsByName(checkBoxesName);		
	if(checkBoxes==null)
		alert("CheckBoxes group  having name '" + checkBoxesName + "' could not be found in the document!");
	 	
 	if(selectorAction)
 	{
 		for (i = 0; i < checkBoxes.length; i++)
			checkBoxes[i].checked = selector.checked;
	}
	else
	{ 	
	 	selector.checked = true;
		for (i = 0; i < checkBoxes.length; i++)
		{
			selector.checked = checkBoxes[i].checked && selector.checked;	
		}	
	}
}

function checkEnter(e, formObject) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;	
	
	if (code == 13){
		formObject.submit()
	}	
}


function setPageTitle(title) {
	document.title = title;
}

function trim(sInString) 
{
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  sInString = sInString.replace( /\s+$/g, "" );// strip trailing
  return sInString;
}

function goSearch(searchURL, searchText) 
{
	var query = trim(searchText);
		
	if (query.length > 0) 
	{
		location.href = searchURL + escape(query);
	}
}

function goConfirm(goUrl, confirmMsg)
{
	var answer = confirm(confirmMsg);
	if (answer)
	{
		location.href = goUrl;
	}
}

/*** QUICK ACCESS ***/

function disableOnChange(elem){
	elem.onchange=function(){
		return true
	}
}

/*
A select list that works without a submit button for also for keyboard users.
Works like this:
- If a select is clicked then we assume that the user is using a mouse and don't wait for enter.
- Else we assume that the user is not using a mouse and wait for enter to get pressed...

by MVDR
*/
function accesSelectOnchange (selectId){
	if (selectElem = document.getElementById(selectId)) {
		if (selectElem.type=="select-one")
		{
			selectElem.onclick=function()
			{
				this.onchange=function()
				{
					if (this.value) 
					{
						//this.form.submit();
						top.location.href = this.value;
					}
				}
				/*
				this.onclick=function(){
					if (this.value) {
						//this.form.submit();
						alert(this.value);						
					}
				}
				*/
			}
			selectElem.onblur=function(){
				disableOnChange(this); //il faut rajouter un disable onclick car sous IE redirection + back + click + onblur + click = redirection immédiate
			}
			selectElem.onkeydown = function(e){
				disableOnChange(this);
				if (e){	//if !IE
					theEvent = e
				} else {	//if IE 
					theEvent  = event
				};
				if (theEvent.keyCode==13)
				{	//if key = enter
					this.onchange = function () 
					{
						if (this.value) 
						{
							//this.form.submit();
							top.location.href = this.value;
						}
					}
					if (this.value) 
					{
							//this.form.submit();
							top.location.href = this.value;
					}
					
				}
            }			
		}
	}
}


