﻿var ScriptVer = "1" ;

/* ***************** Global *********************** */

var EVENTTARGET   = "__EVENTTARGET"   ;
var EVENTARGUMENT = "__EVENTARGUMENT" ;

/* ************************************************ */


/* ********* Format Check Functionnalities ******** */
/* ***************** Global *********************** */

var gcharDateSep = "." ;
var gcharTimeSep = ":" ;
var defaultErrorBackcolor = "#FFAAAA";

/* ***************** Functions ******************** */


function getDocument (sender)
{
	if ( typeof(sender.document) == 'undefined' )
	{
		return document;
	}
	else
	{
		return sender.document;
	}
}


function submitForm ()
{
	theForm.submit();	
}

/* ******************** */
/* author : KG			*/
/* created : 20040704	*/
/* ******************** */
function KeyUpCheck ( htmlObject, valueFormat )
{
	if ( htmlObject.value.length == 0 )
		return true ;
	var str = "" ;
	switch (valueFormat)
	{
		case 'NUM' :
			str = "0;1;2;3;4;5;6;7;8;9;+;-;*;/;.;,;" ;
			break ;
		case 'DATE' :
			str = "0;1;2;3;4;5;6;7;8;9;.;-;/;" ;
			break ;
		default :
			return true ;
			
	}
	var lastChar = htmlObject.value.charAt (htmlObject.value.length-1) ;
	if ( str.indexOf(lastChar+";") == -1)
		htmlObject.value = htmlObject.value.substring(0, htmlObject.value.length-1);
}



/* ******************** */
/* author : KG			*/
/* created : 20040704	*/
/* ******************** */
function ValidateFormat ( htmlObject, valueFormat )
{
	if (htmlObject.value.length == 0)
		return true ;

	var reg ;
	switch (valueFormat)
	{
		case 'NUM' :
			reg = new RegExp (/^[\-\+]{0,1}\d*(\.\d+)?$/) ;
			try {
				htmlObject.value = eval ( htmlObject.value ) ;
				// ajout du seprateur de milliers
			} catch (E) 
			{}
			break ;
		case 'DATE' :
			reg = new RegExp (/^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|[048])))$/) ;
			break ;
		case 'EMAIL' :
			reg = new RegExp (/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/) ;
			break ;
		default :
			reg = new RegExp (/^\d*$/) ;
			
	}
	if ( ! reg.test ( htmlObject.value ) )
	{
		htmlObject.select () ;
		return false ;
	}
	switch (valueFormat)
	{
		case 'DATE' :
			var regDate = new RegExp ( /^(\d+)\.(\d+)\.(\d+)$/ ) ;
			if ( regDate.test ( htmlObject.value ) )
				htmlObject.value = StringFormat (RegExp.$1,'00') + "." + StringFormat(RegExp.$2,'00') + "." + StringFormat(RegExp.$3,'year') ;
			break ;	
	}
}


/* ******************** */
/* author : KG			*/
/* created : 20040704	*/
/* ******************** */
function StringFormat (strValue, strFormat)
{
	strValue = "" + strValue ;
	switch (strFormat)
	{
		case 'year':
			if ( strValue.length == 4 )
				return strValue ;
			return new Date().getFullYear() - (new Date().getFullYear() % 100) + parseInt(strValue, 10) ;
			break ;
		case 'day' :
			if ( strValue == 0 )
				strValue = new Date().getDate () ;
			return StringFormat (strValue, '00' ) ;
		default :
			if ( strValue.length >= strFormat.length )
				return strValue ;
			if ( strValue.length == 0 )
				return strFormat ;
				
			return strFormat.substring (0, (strFormat.length-strValue.length)) + strValue ;
	}
}

/* ******************** */
/* author : KG			*/
/* created : 20040714	*/
/* ********************************* */
/* use this function on onBlur event */
/* ********************************* */
function OmnisDate (htmlObject, dateFormat, errorBackColor)
{
	strValue = htmlObject.value ;
	
	if ( strValue.length == 0 )
		return true ;

	var regDate = new RegExp ( /^\D*(\d{1,8})\D*(\d{0,6})\D*(\d{0,4})\D*$/ ) ;
	if ( ! regDate.test ( strValue ) )
	{
		htmlObject.value = "" ;
		return false ;
	}

	z0 = RegExp.$1 ;
	z1 = RegExp.$2 ;
	z2 = RegExp.$3 ;
	
	var strDate = z0 + gcharDateSep + z1 + gcharDateSep + z2 ;
	
	var intYear  = "" + new Date().getFullYear () ;
	var intMonth = "" + ( new Date().getMonth () + 1 ) ;
	var intDay   = -1 ;
	
	if ( z2.length > 0 )
	{
		intDay   = z0 ;
		intMonth = z1 ;
		intYear  = z2 ;
	}
	else if ( z1.length > 0 )
	{
		intDay   = z0 ;
		intMonth = z1 ;
	}
	else if ( z0.length > 0 )
	{
		if ( z0.length < 3 )
		{
			intDay = z0 ;
		}
		else if ( z0.length < 5 )
		{
			intDay = z0.substring(0,2) ;
			intMonth = z0.substring(2) ;	
		}
		else
		{
			intDay = z0.substring(0,2) ;
			intMonth = z0.substring(2,4) ;
			intYear = z0.substring(4) ;
		}
	}

	strDate = StringFormat (intDay,'day') + gcharDateSep + StringFormat(intMonth,'00') + "." + StringFormat(intYear,'year')
	
	reg = new RegExp (/^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|[048])))$/) ;
	if ( ! reg.test ( strDate ) )
	{
		htmlObject.select () ;
		htmlObject.style.backgroundColor =   (errorBackColor != null && errorBackColor.length > 0) ? errorBackColor : defaultErrorBackcolor;
		
		return false ;
	}
	else
	{
		htmlObject.style.backgroundColor = "";
	}
	htmlObject.value = strDate ;
	return true ;
}


/* ******************** */
/* author : KG			*/
/* created : 20051015	*/
/* ********************************* */
/* use this function on onBlur event */
/* ********************************* */
function OmnisTime (htmlObject, strFormat, blnDuration, errorBackColor)
{
	strValue = htmlObject.value ;
	
	if ( strValue.length == 0 )
		return true ;

	var regDate = new RegExp ( /^\D*(\d{1,8})\D*(\d{0,6})\D*(\d{0,4})\D*$/ ) ;
	if ( ! regDate.test ( strValue ) )
	{
		htmlObject.value = "" ;
		return false ;
	}

	z0 = RegExp.$1 ;
	z1 = RegExp.$2 ;
	z2 = RegExp.$3 ;
	
	var strTime = z0 + gcharTimeSep + z1 + gcharTimeSep + z2 ;
	
	var crtDate = new Date() ;
	var intHour  	= "00" ; // + StringFormat (crtDate.getHours(),'00') ;
	var intMinute	= "00" ; // + StringFormat (crtDate.getMinutes (),'00') ;
	var intSecond	= "00" ; // + StringFormat (crtDate.getSeconds (),'00') ;
	
	if ( z2.length > 0 )
	{
		intHour   	= z0 ;
		intMinute 	= z1 ;
		intSecond  	= z2 ;
	}
	else if ( z1.length > 0 )
	{
		intHour   = z0 ;
		intMinute = z1 ;
	}
	else if ( z0.length > 0 )
	{
		if ( blnDuration ) {
			intHour = z0 ;
		} else {
			if ( z0.length < 3 )
			{
				intHour = z0 ;
			}
			else if ( z0.length < 5 )
			{
				intHour = z0.substring(0,2) ;
				intMinute = z0.substring(2) ;	
			}
			else
			{
				intHour   = z0.substring(0,2) ;
				intMinute = z0.substring(2,4) ;
				intSecond = z0.substring(4)   ;
			}
		}
	}
	strTime = "" ;
	switch (strFormat)
	{

		case 'hh:mm' :
			strTime = StringFormat (intHour, '00') + gcharTimeSep + StringFormat (intMinute, '00') ;  
			break ;

		case 'hh:mm:ss' :
		default :
			strTime = StringFormat (intHour, '00') + gcharTimeSep + StringFormat (intMinute, '00') + gcharTimeSep + StringFormat (intSecond, '00') ;  
			break ;
	}
	//alert ( strTime) ;
//	reg = new RegExp (/^(20|21|22|23|[01]\d|\d)(([:.][0-5]\d){1,2})$/) ;
	reg = new RegExp (/^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/) ;
	if ( blnDuration ) {
		reg = new RegExp (/^([0-9]*):([0-5]?[0-9])(:([0-5]?[0-9]))?$/) ;
	} 

	if ( ! reg.test ( strTime ) )
	{
		htmlObject.select () ;
		htmlObject.style.backgroundColor =   (errorBackColor != null && errorBackColor.length > 0) ? errorBackColor : defaultErrorBackcolor;
		return false ;
	}
	else
	{
		htmlObject.style.backgroundColor = "";
	}
	
	htmlObject.value = strTime ;
	return true ;
}
/* ************************************************ */


/* ********* Readonly Functionnalities ************ */
/* ***************** Global *********************** */

/* ***************** Functions ******************** */

/* ******************** */
/* author : KG		*/
/* created : 20040715	*/
/* ******************** */
function _RD_CheckBoxList (sender)
{
	var i = -1 ;
	var chkbx = document.getElementById ( sender.getAttribute ('id') + '_' + (++i) ) ;
	while ( chkbx != null ) {
	    chkbx.checked = chkbx.defaultChecked ;
		chkbx = document.getElementById ( sender.getAttribute ('id') + '_' + (++i) ) ;
	}
}

/* ******************** */
/* author : KG		*/
/* created : 20040715	*/
/* ******************** */
function _RD_List (sender) 
{
	for ( i=0 ; i<sender.length ; i++)
		sender[i].selected = sender[i].defaultSelected ; 
}

/* ******************** */
/* author : KG		*/
/* created : 20040715	*/
/* ******************** */
function _RD_Radio (sender) 
{
	var rdObject = eval ('sender.form.' + sender.getAttribute ('name') ) ;
	for ( i=0 ; i<rdObject.length ; i++)
		rdObject[i].checked = rdObject[i].defaultChecked ; 
}

/* ******************** */
/* author : KG		*/
/* created : 20040715	*/
/* ******************** */
function _RD_RadioButtonList (sender) 
{
	_RD_CheckBoxList (sender) ;
}
/* ************************************************ */

/* ********* Image Functionnalities *************** */
/* ***************** Global *********************** */

/* ***************** Functions ******************** */


/* ******************** */
/* author : KG		*/
/* created : 20040722	*/
/* ******************** */
function ChangeImage ( htmlObject, imageSrc )
{
	if ( imageSrc.length > 0 )
		htmlObject.src = imageSrc ;	
}

/* ************************************************ */



/* ********* CheckboxTimer Functionnalities ******* */
/* ***************** Global *********************** */
var ckbxTimer ;
/* ************ 
0 : OK, do what you want
1 : Ask a refresh
************ */
var timerOpe = 0 ;
/* ***************** Functions ******************** */
function WebTimer (ckbx, formId)
{
	if ( timerOpe == 0 && ckbx.checked ) {
		// -> LAUNCH THE AUTO-REFRESH
		WebTimerRequest (formId, ckbx.id) ;
	} else if (timerOpe != 1 && ! ckbx.checked ) {
		// -> DISPLAY THE STOPPED MESSAGE
		ckbx.nextSibling.innerText = ckbx.parentNode.getAttribute('StopMsg') ;
		// -> STOP THE TIMER
		WebTimerStopper () ;
	} else if (timerOpe == 1) {
		ckbx.checked = true ;
		alert ('Wait the end of the refresh before to change the checkbox state') ;	
	}
}
function WebTimerStarter (ckbxId, formId, intDelay)
{
	ckbxTimer = setInterval("WebTimerRequest ('" + formId + "', '" + ckbxId + "')", intDelay) ;
}
function WebTimerStopper ()
{
	// -> STOP THE TIMER
	if ( typeof(ckbxTimer) != 'undefined' && ckbxTimer != null )
	{
		clearTimeout (ckbxTimer) ;
		ckbxTimer = null ;
	}
}
function WebTimerRequest (formId, ckbxId)
{
	// -> LOCK ACTION FOR THE NEXT OPERATION
	timerOpe = 1 ;
	// -> STOP THE TIMER
	WebTimerStopper ()
	
	var ckbx = document.getElementById (ckbxId) ;
	if ( ckbx == null ) {
		alert ('Refresh timer error (1), contact your administrator') ;
	}

	// -> DISPLAY MESSAGE TO WAIT THE REFRESH
	ckbx.nextSibling.innerText = "Wait the answer" ;
	// -> FIND THE FORM
	var theform = document.getElementById (formId) ;
	if ( theform == null ) {
		alert ('Refresh timer error (2), contact your administrator') ;
	}
	// -> LAUNCH THE AUTO-REFRESH
	theform.__EVENTTARGET.value		= ckbxId + "timer" ;
	theform.__TIMERTARGET.value		= ckbxId + "timer" ;
	theform.__TIMERARGUMENT.value	= "refresh" ;
	// -> SUBMIT THE FORM
	theform.submit () ;	
}

/* ************************************************ */


/* ******************** */
/* author : KG			*/
/* created : 20040930	*/
/* ******************** */
function DisplayInformation (htmlObject)
{
	alert ("Create by :\t" + htmlObject.getAttribute ("CU") + " (" + htmlObject.getAttribute ("CD") + ")\nModified by :\t" + htmlObject.getAttribute ("MU") + " (" + htmlObject.getAttribute ("MD") + ")") ;
}


/* ************************************************ */



/* ********* Complex ListBox Fonctionnalities ***** */
/* ***************** Global *********************** */
var gcharCplxSltSep = ';';
/* ***************** Functions ******************** */

/* ******************** */
/* author : KG			*/
/* created : 20050228	*/
/* ******************** */
function FromToAdd (sender, fromId, toId, all, order)
{
 	var objFrom = sender.document.getElementById ( fromId ) ;
 	var objTo   = sender.document.getElementById ( toId ) ;
 	
 	if ( objFrom == null || objTo == null )
 	{
 		alert ("Lists not find") ;
 		return ;
 	}
 	if ( objFrom.options.length < 1 || ( !all && objFrom.selectedIndex == -1 ) )
 	{
 		return ;
 	}
 
	var start = (all)?0:objFrom.selectedIndex ;
	var end   = (all || objFrom.multiple)?objFrom.options.length:start+1 ;

	for (var i=start ; i<end ; i++ )
	{
		if ( all || objFrom.options(start).selected )
		{
			var selItem = objFrom.options ( start ) ;
			if ( ! order )
			{
		 		objTo.options.add ( new Option (selItem.text,selItem.value) ) ;
		 		objTo.selectedIndex  = objTo.options.length - 1 ;
		 	}
		 	else
		 	{
		 		blnNotAffect = true ;
		 		for ( var j=0 ; j<objTo.options.length && blnNotAffect ; j++ )
		 		{
		 			if ( objTo.options(j).text > selItem.text )
		 			{
			 			objTo.options.add ( new Option (selItem.text,selItem.value), j ) ;
			 			objTo.selectedIndex = j ;
			 			blnNotAffect = false ;
			 		}
		 		}
		 		if ( blnNotAffect )
		 		{
		 			objTo.options.add ( new Option (selItem.text,selItem.value) ) ;
		 			objTo.selectedIndex  = objTo.options.length - 1 ;
		 		}
		 	}
		 	objFrom.remove ( start ) ;
		 }
	}
	
	objFrom.selectedIndex = start ;
	
	UpdateValueField (sender, objTo) ;
	UpdateValueField (sender, objFrom) ;

}
 
/* ******************** */
/* author : KG			*/
/* created : 20050228	*/
/* ******************** */
function UpDown (sender, id, up)
{
 	var obj = sender.document.getElementById ( id ) ;
 	if ( obj == null || obj.selectedIndex == -1 ) { return ; }

	start   = obj.selectedIndex ;
	end     = start + ((up)?1:0) ;
	selItem = obj.options ( start ) ;
	
	if ( up && start < 1 ) { return ; }
	if ( up )
	{
		obj.add ( new Option (selItem.text,selItem.value), start-1 ) ;
	}
	if ( ! up && ( start == obj.options.lenght || obj.options.lenght == 1 ) ) { return ; }
	if ( ! up ) 
	{
		obj.add ( new Option (selItem.text,selItem.value), start+2 ) ;
	}
	obj.remove (end) ;
	obj.selectedIndex = start + ((up)?-1:1) ;
	
	UpdateValueField ( sender, obj ) ;
}
 
/* ******************** */
/* author : KG			*/
/* created : 20050228	*/
/* ******************** */
function UpdateValueField (sender, objLst)
{
	var objValue = sender.document.getElementById ( objLst.id + '_value') ;
	if ( objValue == null ) { return ; }
	objValue.value = '' ;
	var nbOptions = objLst.options.length ;
	for ( var i=0 ; i<nbOptions ; )
	{
		objValue.value += objLst.options(i).value + (++i==nbOptions?'':gcharCplxSltSep) ;
	}
}



/* ************************************************ */


/* ********* Anchor Position Functionnalities ***** */
/* ***************** Global *********************** */


/* ***************** Functions ******************** */

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;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }

	// Logic to find position
 	if (use_gebi && document.all) 
 	{
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	}
	else 
	{
		coordinates.x=0; coordinates.y=0; return coordinates;
	}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}


/* ************************************************ */
/** HidablePanel **/
function ChangeVisibilty(strZoneId, hiddenFieldId, titlePicId, picPathClosed, picPathOpen)
{
    zone     = document.getElementById(strZoneId);
    input    = document.getElementById(hiddenFieldId);
    titlePic = document.getElementById(titlePicId);
    
    if(zone.style.display != 'none')
    {
        zone.style.display = 'none';
        input.value = 'hidden';
        
        if(titlePic != null && picPathClosed != null)
        {
            titlePic.src = picPathClosed;
        }
    }
    else
    {
        zone.style.display = 'block';
        input.value = 'visible';
        
        if(titlePic != null && picPathOpen != null)
        {
            titlePic.src = picPathOpen;
        }
    }
}

