/**
 * @fileoverview Using in ALL module.
 *
 * WARNING: Based on 1 form per page.
 *
 */
/**
 *Use to open popup window
 *
 *@param aHTML: HTML string to write in window
 *@param name: String, name of the window
 *@param w: int, width
 *@param h: int, height
 */
function WriteWindow(aHTML,name,w,h) {
	var x=(screen.width-w)/2;
	var y=(screen.height-h)/2;
	w = open("",name,"location=no,toolbar=no,resizable=yes,scrollbars=yes,width="+w+",height="+h+",hotkeys=no,left="+x+",top="+y);
	w.focus();
	with (w.document) {
		open("text/html",name,"location=no,toolbar=no,resizable=yes,scrollbars=yes,width="+w+",height="+h+",hotkeys=no,left="+x+",top="+y);
		write(aHTML);
	}
}
/**
 *Use to open popup window
 *
 *@param url: String, complete URL to open
 *@param name: String, name of the window
 *@param w: int, width
 *@param h: int, height
 *@param stdIE: (Optionnal) String, force to add a scroll bar
 *@param isScroll: (Optionnal) String, force to standard window with adress bar, tool bar, etc...
 */
function OpenWindow(url,name,w,h) {
	var x=(screen.width-w)/2;
	var y=(screen.height-h)/2;
	var stdIE = false;
	var isScroll = 'yes';
	switch (OpenWindow.arguments.length) {
		case 6:(OpenWindow.arguments[5]!=null && OpenWindow.arguments[5]!='')?(isScroll=OpenWindow.arguments[5]):(isScroll='yes');
		case 5:(OpenWindow.arguments[4]!=null && OpenWindow.arguments[4]!='')?(stdIE=OpenWindow.arguments[4]):(stdIE=false);
	}
	if (stdIE==true || stdIE=='true') {
		w = open(url,name,"menubar=yes,location=yes,toolbar=yes, personalbar=yes, titlebar=yes, resizable=yes,scrollbars=yes,status=yes,width="+w+",height="+h+",hotkeys=no,left="+x+",top="+y);
	}else {
		w = open(url,name,"location=no,toolbar=no,resizable=yes,scrollbars="+isScroll+", width="+w+",height="+h+",hotkeys=no,left="+x+",top="+y);
	}
	w.focus();
}
/**
 *Use to set a message in the status window.
 *
 *@param aMessage: String, message to show
 */
function SetStatusText(aMessage) {
	window.status=aMessage;
}
/**
 *Use to set a message in the title window.
 *
 *@param aMessage: String, message to show
 */
function SetTitle(aMessage) {
	document.title=aMessage;
}
/**
 *Use to swap between 2 images, for onmouseover effect.
 *
 *@param id: String, name or id of image object
 *@param newSrc: String, src of the new image
 */
function SwapImage(id,newSrc) {
	var theImage=FindImage(document,id,0);
	if (theImage) theImage.src=newSrc;
}
/**
 *Use by SwapImage function, it's an internal function
 */
function FindImage(doc,name,j) {
	var theImage=false;
	if (doc.images) theImage=doc.images[name];
	if (theImage) return theImage;
	if (doc.layers)
		for (j=0;j<doc.layers.length;j++) {
			theImage=FindImage(doc.layers[j].document,name,0);
			if (theImage) return (theImage);
		}
	return (false);
}
/**
 *Use by all function, it's an internal function that return an object of element in the form
 *
 *@param objStr: String, name of element in the form
 *@return Object of single element in the form
 */
function _GetObject(objStr) {
	var obj=eval('document.forms[0].'+objStr);
	if (obj==null && objStr.indexOf('forms[')>=0) return eval(objStr);
	return obj;
}
/**
 *Use by all function, it's an internal function that return an type object of element in the form
 *
 *@param id: String, name of element in the form
 *@return Sting of single element type in the form
 */
function _GetObjectType(obj) {
	if ((obj.length!=null)&&(obj.type!='text')&&(obj.type!='password')&&(obj.type!='hidden')&&(obj.type!='select-one')&&(obj.type!='select-multiple')) return _GetObjectType(obj[0]);
	return obj.type;
}
/**
 *Use by all function, it's an internal function to set value in the form
 *
 *@param objStr: String, name of element in the form
 *@param valStr: String, value to set to the element
 */
function SetValue(objStr,valStr) {
	var obj=_GetObject(objStr);
	if (_GetObjectType(obj)=='text') SetText(objStr,valStr);
	else if (_GetObjectType(obj)=='hidden') SetText(objStr,valStr);
	else if (_GetObjectType(obj)=='select-one') SetSelect(objStr,valStr);
	else if (_GetObjectType(obj)=='checkbox') SetCheckBox(objStr,valStr);
	else if (_GetObjectType(obj)=='radio') SetRadioBox(objStr,valStr);
	else if (_GetObjectType(obj)=='textarea') SetText(objStr,valStr);
	else if (_GetObjectType(obj)=='password') SetText(objStr,valStr);
	else if (_GetObjectType(obj)=='select-multiple') SetSelect(objStr,valStr);
	else alert('Type inconnu : '+_GetObjectType(obj));
}
/**
 *Use by all function, it's an internal function to get a value in the form
 *
 *@param objStr: String, name of element in the form
 *@return String, value of the element
 */
function GetValue(objStr) {
	var obj=_GetObject(objStr);
	if (_GetObjectType(obj)=='text') return GetText(objStr);
	else if (_GetObjectType(obj)=='hidden') return GetText(objStr);
	else if (_GetObjectType(obj)=='select-one') return GetSelect(objStr);
	else if (_GetObjectType(obj)=='checkbox') return GetCheckBox(objStr);
	else if (_GetObjectType(obj)=='radio') return GetRadioBox(objStr);
	else if (_GetObjectType(obj)=='textarea') return GetText(objStr);
	else if (_GetObjectType(obj)=='password') return GetText(objStr);
	else if (_GetObjectType(obj)=='select-multiple') return GetSelect(objStr);
	alert('Type inconnu : '+_GetObjectType(obj));
	return null;
}
/**
 *Use to get a value from a SELECT-MULTI
 *
 *@param objStr: String, name of element in the form
 *@return String array separate with '#'
 */
function GetSelectMulti(objStr) {
	var obj=_GetObject(objStr);
	var StrRtn = '';
	for (i=0;i<obj.length;i++){
		if (obj.options[i].selected){
			StrRtn = StrRtn + obj.options[i].value + '#';
		}
	}
	return StrRtn;
}
/**
 *Use to set multiple values to a SELECT-MULTI
 *
 *@param objStr: String, name of element in the form
 *@param valStr: String, values separate with '#'
 */
function SetSelectMulti(objStr,valStr) {
	var objA=_GetObject(objStr);
	var found=false;
	var tmpTab=valStr.split('#');
	for (i=0;i<tmpTab.length;i++) {
		for (j=0;j<objA.length;j++) {
			if (objA.options[j].value==tmpTab[i] && tmpTab[i]!='') {
				objA.options[j].selected=true;
				found=true;
				break;
			}
		}
	}
	if (!found) objA.options[0].selectedIndex=0;
}
/**
 *Use to set value to TEXT field
 *
 *@param objStr: String, name of element in the form
 *@param valStr: String, value to set
 */
function SetText(objStr,valStr) {
	var obj=_GetObject(objStr);
	obj.value=valStr;
}
/**
 *Use to get value from TEXT field
 *
 *@param objStr: String, name of element in the form
 *@return String value
 */
function GetText(objStr) {
	var obj=_GetObject(objStr);
	return obj.value;
}
/**
 *Use to set value to SELECT field
 *
 *@param objStr: String, name of element in the form
 *@param valStr: String, value to set
 */
function SetSelect(objStr,valStr) {
	var obj=_GetObject(objStr);
	var found=false;
	for (i=0;i<obj.length;i++) {
		if (obj.options[i].value==valStr) {
			obj.options[i].selected=true;
			found=true;
			break;
		}
	}
	if (!found) obj.selectedIndex=0;
}
/**
 *Use to get value from SELECT field
 *
 *@param objStr: String, name of element in the form
 *@return String value
 */
function GetSelect(objStr) {
	var obj=_GetObject(objStr);
	return obj.options[obj.selectedIndex].value;
}
/**
 *Use to get value from SELECT field
 *
 *@param objStr: String, name of element in the form
 *@return String value
 */
function GetSelectText(objStr) {
	var obj=_GetObject(objStr);
	return obj.options[obj.selectedIndex].text;
}
/**
 *Use to clear selected value from SELECT field
 *
 *@param objStr: String, name of element in the form
 */
function ClearSelect(objStr) {
	var obj=_GetObject(objStr);
	for (var i=obj.length; i>0;i--) obj.options[i]=null;
}
/**
 *Use to set value to RADIO field
 *
 *@param objStr: String, name of element in the form
 *@param valStr: String, value to set
 */
function SetRadioBox(objStr,valStr) {
	var obj=_GetObject(objStr);
	if (obj.length!=null)
		for (i=0;i<obj.length;i++)
			if (obj[i].value==valStr) obj[i].checked=true; else obj[i].checked=false;
	else obj.checked=true;
}
/**
 *Use to get value from RADIO field
 *
 *@param objStr: String, name of element in the form
 *@return String value
 */
function GetRadioBox(objStr) {
	var obj=_GetObject(objStr);
	if (obj.length!=null) {
		for (i=0;i<obj.length;i++)
			if (obj[i].checked) return obj[i].value;
		return null;
	} else return obj.value;
}
/**
 *Use to set value to CHECKBOX field
 *
 *@param objStr: String, name of element in the form
 *@param valStr: String, value to set
 */
function SetCheckBox(objStr,valStr) {
	var obj=_GetObject(objStr);
	if (valStr=='on') obj.checked=true;
	else obj.checked=false;
}
/**
 *Use to get value from CHECKBOX field
 *
 *@param objStr: String, name of element in the form
 *@return String value
 */
function GetCheckBox(objStr) {
	var obj=_GetObject(objStr);
	return obj.value;
}
/**
 *Use to get the state from CHECKBOX field
 *
 *@param objStr: String, name of element in the form
 *@return boolean, true if check, false if not
 */
function GetCheckBoxState(objStr) {
	var obj=_GetObject(objStr);
	return obj.checked;
}
/**
 *Use to set the state to CHECKBOX field
 *
 *@param objStr: String, name of element in the form
 *@param valStr: String, true to check, false to uncheck
 */
function SetCheckBoxState(objStr,valStr) {
	var obj=_GetObject(objStr);
	obj.checked=eval(valStr);
}
/**
 *Use to focus on a element in the form
 *
 *@param objStr: String, name of element in the form
 */
function SkipOnTab(objStr) {
	var obj=_GetObject(objStr);
	obj.focus();
}
/**
 *Use to select an element in the form, overlight text in TEXT field
 *
 *@param objStr: String, name of element in the form
 */
function SelectAll(objStr) {
	var obj=_GetObject(objStr);
	obj.select();
}
/**
 *Use to convert date object to text for template 'yyyy', 'mm.yyyy', 'dd.mm', 'dd.mm.yyyy'
 *
 *@param objDate: Date, name of element in the form
 *@param strMask: String, template 'yyyy', 'mm.yyyy', 'dd.mm', 'dd.mm.yyyy'
 *@return String date
 */
function DateToText(objDate,strMask) {
	var _jour;
	var _mois;
	var _annee;
	if (objDate==null) return '';
	_annee=''+objDate.getFullYear();
	if (strMask=='yyyy')	return _annee;
	if (objDate.getMonth()<9) _mois='0'+(objDate.getMonth()+1); else _mois=''+(objDate.getMonth()+1);
	if (strMask=='mm.yyyy') return _mois+'.'+_annee;
	if (objDate.getDate()<10) _jour='0'+objDate.getDate(); else _jour=''+objDate.getDate();
	if (strMask=='dd.mm') return _jour+'.'+_mois;
	return _jour+'.'+_mois+'.'+_annee;
}
/**
 *Use to convert text object to date for template 'yyyy', 'mm.yyyy', 'dd.mm', 'dd.mm.yyyy'
 *
 *@param strObj: String, name of element in the form
 *@param strMask: String, template 'yyyy', 'mm.yyyy', 'dd.mm', 'dd.mm.yyyy'
 *@return Date date
 */
function TextToDate(strObj,strMask) {
	return AsDate(strObj,strMask);
}
/**
 *INTERNAL: Use to test string date for template 'yyyy', 'mm.yyyy', 'dd.mm', 'dd.mm.yyyy'
 *
 *@param strObj: String, name of element in the form
 *@param strMask: String, template 'yyyy', 'mm.yyyy', 'dd.mm', 'dd.mm.yyyy'
 *@return Date or null
 */
function _AsDate(strDate,strMask) {
	var _mask=strMask.toLowerCase();
	if (_mask=='mm.yyyy') strDate='01.'+strDate;
	else if (_mask=='yyyy') strDate='01.01.'+strDate;
	else if (_mask=='dd.mm') strDate=strDate+'.'+(new Date()).getFullYear();
	if (strDate.length>10) return null;
	var _regle=/(\d{1,2})[ .,-,\/](\d{1,2})[ .,-,\/](\d{4})/;
	var _resultat=_regle.exec(strDate);
	if (_resultat!=null) {
		var _jour=parseInt(_resultat[1],10);
		var _mois=parseInt(_resultat[2]-1,10);
		var _annee=parseInt(_resultat[3],10);
		var _date=new Date(_annee,_mois,_jour);
		if (_annee!=_date.getFullYear()) return null;
		if (_mois!=_date.getMonth()) return null;
		if (_jour!=_date.getDate()) return null;
		return _date;
	}
	return null;
}
/**
 *Use to test string date for template 'yyyy', 'mm.yyyy', 'dd.mm', 'dd.mm.yyyy'
 *
 *@param strObj: String, name of element in the form
 *@param strMask: String, template 'yyyy', 'mm.yyyy', 'dd.mm', 'dd.mm.yyyy'
 *@return Date or null
 */
function AsDate(objStr,strMask) {
	var obj=_GetObject(objStr);
	if (obj!=null) return _AsDate(obj.value,strMask);
	else return null;
}
/**
 *Use to replace a string by another string
 *
 *@param string: String, original text
 *@param text: String, search text
 *@param by: String, replace by text
 *@param cs: boolean, case sensitive
 *@return String result
 */
function RemplaceString(string,text,by,cs){
	// Replaces text with by in string : cs == Case Sensitive
	if ((string.length == 0) || (text.length == 0)) return string;
	var opt = '';
	if (!(cs)) opt = 'gi';
	else opt = 'g';
	return string.replace(new RegExp(text,opt),by);
}
/**
 *Use to set a maxlength on a TEXTAREA, put this method in onKeypress, onKeydown, onKeyup
 *
 *@param objStr: String, name of element in the form
 *@param nb: int, limit of character
 */
function SetMaxLength(objStr, nb){
	var aText = GetText(objStr);
	if (aText !=null && aText.length>nb) SetText(objStr,aText.substring(0,nb));
}

/**
 * Trim functions
 *   Returns string with whitespace trimmed
 */
function LTrim(str){
	if (str==null){return null;}
	for(var i=0;str.charAt(i)==" ";i++);
	return str.substring(i,str.length);
	}
function RTrim(str){
	if (str==null){return null;}
	for(var i=str.length-1;str.charAt(i)==" ";i--);
	return str.substring(0,i+1);
	}
function Trim(str){return LTrim(RTrim(str));}

/**
 * Validate email string
 *   Returns true false
 */
function isValidEmail(str){
	if(str == "") return true

	var reEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return reEmail.test(str);
}
