var result ;
//<--! fonctions communes -->

// Permet de réinitialiser une liste
function resetList(theList){
	while(theList.length>0){
		theList.options[0]=null;
	}
}

// Permet d'ordonner une liste Select par ordre alphabétique
function orderListByLabel(theList){			
	var labelList=new Array(theList.length);
	
	for(i=0;i<theList.length;i++){
		labelList[i]=theList.options[i].text;
	}
	
	labelList.sort();
	
	for(j=0;j<labelList.length;j++){
        for(i=j;i<theList.length;i++){ 
            if(labelList[j] == theList.options[i].text && (i != j) ){
				//on décale tous les champs
                for(k=theList.length;k>j;k--){
                    
                    theList.options[k] = new Option(theList.options[(k-1)].text,theList.options[(k-1)].value);
                }
            	// On insère la nouvelle position
            	theList.options[j] = new Option(theList.options[i+1].text,theList.options[i+1].value);
				break;
            }
        }
    }
    
    // Suppression des champs décalés
	while(theList.length > labelList.length){
		theList.options[theList.length-1] = null;
	}  	      	        
}

// Permet d'ordonner une liste Select par indexes croissants
function orderListByIndex(theList){			
	var indexList=new Array(theList.length);
	
	for(i=0;i<theList.length;i++){
		indexList[i]=theList.options[i].value.replace("/","");
	}
	
	indexList.sort(triNumerique);
	
	for(j=0;j<indexList.length;j++){
        for(i=j;i<theList.length;i++){
            if(indexList[j] == theList.options[i].value.replace("/","") && (i != j) ){
				//on décale tous les champs
                for(k=theList.length;k>j;k--){
                    
                    theList.options[k] = new Option(theList.options[(k-1)].text,theList.options[(k-1)].value);
                }
            	// On insère la nouvelle position
            	theList.options[j] = new Option(theList.options[i+1].text,theList.options[i+1].value);
				break;
            }
        }
    }
    
    // Suppression des champs décalés
	while(theList.length > indexList.length){
		theList.options[theList.length-1] = null;
	}     	        
}

// Cette fonction est utilisée pour ordonner un tableau de valeurs numériques ( monTableau.sort(triNumerique) )
function triNumerique(p_nombre1, p_nombre2)
{
 	return p_nombre1 - p_nombre2;
}

// Permet de sélectionner tous les élément d'une liste
function select_all(theList){
    var selectSize = theList.length;
    		    
    for(i=0;i<selectSize;i++){
        theList.options[i].selected = true;
    }
}

// Permet de désélectionner tous les élément d'une liste
function unselect_all(theList){
    var selectSize = theList.length;
    
    for(i=0;i<selectSize;i++){
        theList.options[i].selected = false;
    }
}

function set_visibility_status(div , status ){
	if(document.getElementById(div)!=null){
		document.getElementById(div).style.visibility = status ;
	}
	
}

//<--! fonctions d affichage ou de masquage des info dans la fenetre orange lors des erreurs de login  -->	

function hide(){
	set_visibility_status('calk'   	,	 'hidden' );
	set_visibility_status('validation', 'visible' );
	
}

function pop_calk(msg){
	document.getElementById('errorMessage').innerHTML = msg;
	set_visibility_status('calk' 		, 'visible' );
	set_visibility_status('validation'  , 'hidden'  );
	
}

//<--! fonctions d affectation des informations vers les minifenetres de la page d acceuil -->

function assign_content(div , content ){
	document.getElementById(div).innerHTML = content;
	
}

//<--! fonctions de modification pour les radiobutton-->
function check_button( val ){
if (val == 'all') 		{document.forms.radioForm.listType[0].checked='true' ;}
if (val == 'active') 	{document.forms.radioForm.listType[1].checked='true' ;}
if (val == 'inactive') 	{document.forms.radioForm.listType[2].checked='true' ;}
if (val == 'disabled') 	{document.forms.radioForm.listType[3].checked='true' ;}

}


//<--! fonctions pour les check box a 3 etats-->

var STATUS_ACTIVE    = "ACTIVE"		;
var STATUS_INACTIVE  = "INACTIVE"	;
var STATUS_DISABLE   = "DISABLED"	;
var STATUS_TRUE    	 = "true"		;
var STATUS_FALSE  	 = "false"		;


function updateCheckbox(idImg,idInput)
{
	var img = document.getElementById(idImg);
	var input = document.getElementById(idInput);
	
	if(input.value == STATUS_ACTIVE)	
	{
		img.src = "images/radio_inactive.gif";
		input.value = STATUS_INACTIVE;
	}	
	else if(input.value == STATUS_INACTIVE)
	{
		img.src = "images/radio_disable.gif";
		input.value = STATUS_DISABLE;
	}

	else if(input.value == STATUS_DISABLE)
	{
		img.src = "images/radio_active.gif";
		input.value = STATUS_ACTIVE;
	}
	else if(input.value == STATUS_FALSE)
	{
		img.src = "images/radio_active.gif";
		input.value = STATUS_TRUE;
	}
	else if(input.value == STATUS_TRUE)
	{
		img.src = "images/radio_inactive.gif";
		input.value = STATUS_FALSE;
	}	
	
}

function showPixCheckBox(idImg,idInput) {
	var img = document.getElementById(idImg);
	var input = document.getElementById(idInput);

	switch (input.value) {
			case STATUS_ACTIVE: img.src = "images/radio_active.gif"; break;
			case STATUS_TRUE: img.src = "images/radio_active.gif"; break;
			
			case STATUS_INACTIVE: img.src = "images/radio_inactive.gif"; break;
			case STATUS_FALSE: img.src = "images/radio_inactive.gif"; break;
			
			case STATUS_DISABLE: img.src = "images/radio_disable.gif"; break;
			default:break;		
		}
}

//<--! fonction d'affichage de la date actuelle-->

function getCurrentDate()
{
	var date = new Date();
	return (date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear())
}

function compareDates(date1,date2){

		// Compare 2 french dates with format dd/MM/yyyy
		// Return 0 if date1 == date2
		// Return 1 if date1 > date2 
		// Else return 2
		
		
		var enDate1 = convertToEnglishDate(date1,"/");	
		var enDate2 = convertToEnglishDate(date2,"/");
		tableDate1 = Date.parse(enDate1);
		tableDate2 = Date.parse(enDate2);
		
		if (tableDate1 == tableDate2) { 
			return 0;
		}
		
		if (tableDate1 > tableDate2){
			return 1;
		}else{
			return 2;
		}
	}

function convertToEnglishDate(date,separator){
	
	arrayDate = new Array(3);
	arrayDate = parseDate(date);
	
	englishDate = arrayDate[2]+separator+arrayDate[1]+separator+arrayDate[0];
	return englishDate;
}

function parseDate(date){ 

	// Returns a date table

	result = new Array(3);
	var day="";
	var month="";
	var year="";
	
	// Get the day
	i=0;
	while((date.charAt(i)!="/")&&(i<10)){
	day+=date.charAt(i);
	i++;
	}
	if(day.charAt(0)=="0"){
	day=day.charAt(1);
	}
	date=date.substring(i+1,date.length);
	
	// Get the month
	i=0;
	while((date.charAt(i)!="/")&&(i<10)){
	month+=date.charAt(i);
	i++;
	}
	if(month.charAt(0)=="0"){
	month=month.charAt(1);
	}
	date=date.substring(i+1,date.length);
	
	
	// Get the year
	year=date;
	result[0]=day;
	result[1]=month;
	result[2]=year;
	return result;
}

function applyStyleForIEReadOnlyFields() {
		var input,i = 0;
		var inputs = document.getElementsByTagName('input');
		while (input = inputs.item(i++)) {
			if (input.readOnly)
				input.className = 'readOnly';
		}
		
		i = 0;
		
		var textareas = document.getElementsByTagName('textarea');
		while (textarea = textareas.item(i++)) {
			if (textarea.readOnly)
				textarea.className = 'readOnly';
		}
}

// resize the help zone and other contents
function MaximizeAndMinimize(div)
{
 var divResized = document.getElementById(div);
 
 	if(divResized){
		if (divResized.style.visibility == "visible"){		
			divResized.style.visibility = "hidden";
			document.getElementById('zoneTableau').style.width = '100%' ;
			document.getElementById('zoneInfo').style.width = '0%' ;
			
			if(document.getElementById('iframeZone')){
				document.getElementById('iframeZone').style.width = '100%' ;
			}
			if(document.getElementById('zonePrincepsDetails')){
				document.getElementById('zonePrincepsDetails').style.width = '100%' ;
			}
			if(document.getElementById('zoneWaiting')){
				document.getElementById('zoneWaiting').style.width = '100%' ;
			}
		}else{	
			divResized.style.visibility = "visible";
			document.getElementById('zoneTableau').style.width = '75%' ;
			document.getElementById('zoneInfo').style.width = '24%' ;		
			
			if(document.getElementById('iframeZone')){
				document.getElementById('iframeZone').style.width = '100%' ;
			}
			if(document.getElementById('zonePrincepsDetails')){
				document.getElementById('zonePrincepsDetails').style.width = '75%' ;
			}
			if(document.getElementById('zoneWaiting')){
				document.getElementById('zoneWaiting').style.width = '75%' ;
			}
		}	
	}
} 

function affectExportLink(copyright)
{
	if(document.getElementById('iframeZone'))
	{
	
		var src = document.getElementById('iframeZone').src.replace('&rs:Format=HTML4.0','') ;
				
		src = src.replace('&fl_imp=0','');
		src = src.replace('_N','');
		
		if(document.getElementById('exportExcel'))
		{
			document.getElementById('exportExcel').href 	= src 	+	'&fl_imp=2&rs:Format=EXCEL&nm_ocs=' + copyright ;
		}
		
		if(document.getElementById('exportPDF'))
		{
			document.getElementById('exportPDF').href 	= src	+	'&fl_imp=1&rs:Format=PDF&nm_ocs=' + copyright ;
		}
	
		if(document.getElementById('printable'))
		{
			document.getElementById('printable').href 	= src + '&fl_imp=1&nm_ocs=' + copyright ;
			document.getElementById('printable').target	=	"_blank" ;
		}
	
	}

}

// To set maxlength in some fields like Struts Textarea
function textCounter(field,maxlimit) {
	if (field.value.length > maxlimit)
	field.value = field.value.substring(0, maxlimit);
}