/* ******************************** */
/*                                  */
/*      Custom Checkboxes           */
/*                                  */
/* ******************************** */

// Checkbox image for when not checked
var CHECKBOX_IMG_NOT_CHECKED = absolutePathForStaticResources + "Images/coche.gif";

// Checkbox image for when checked
var CHECKBOX_IMG_CHECKED = absolutePathForStaticResources + "Images/Style_R/coche_on.gif";

// Checkbox image for when not checked
var CHECKBOX_IMG_OFF_CHECKED = absolutePathForStaticResources + "Images/coche_off.gif";

// An array for checkboxes
var checkboxArray = new Array();

// -----------------------------------------------------------------
// Create a checkbox
//
// Params :
//	- aFormName : name of the form containing the checkboxes
//	- aCheckName : the name of the checkbox
//	- aLabel : the label to display
//	- aValue : the value to post when the checkbox is checked
//  - isChecked : specify if the checkbox is initialy checked or not
//  - aScript : the script string to execute on checkbox click event (in href)
//  - atabIndex : Tab index
//  - aAccesskey : Short key in order to access to this element
//  - aOnClickScript : the script string to execute on checkbox click event (in onclick). Only use with "image" checkbox
// -----------------------------------------------------------------
function createCheckbox(aFormName, aCheckName, aLabel, aValue, isChecked, aScript, atabIndex, aAccesskey, aOnClickScript) {
	if (!document.images) {
		var checked = "";
		if (eval(isChecked)) {
			checked = "checked=\"checked\"";
		}
		if (aScript == null) {
			aScript = "";
		} else if (aScript != "") {
			aScript = "onclick=\"" + aScript + ";\"";
		}
		if (atabIndex == null) {
			atabIndex = "";
		} else if (atabIndex != "") {
			atabIndex = "tabindex=\""+atabIndex+"\"";
		}
		if (aAccesskey == null || aAccesskey == "") aAccesskey = "";
		else aAccesskey = " accesskey=\"" + aAccesskey + "\" ";
		
		document.write("<input type=\"checkbox\" name=\"" + aCheckName + "\""+ checked + " " + aScript + " " + atabIndex + aAccesskey + ">" + aLabel + "<br>");
		return;
	}
	
	// If a script is defined
	if (aScript == null) {
		aScript = "";
	} else if (aScript != "") {
		aScript += ";";
	}
	
	if (aOnClickScript == null) {
		aOnClickScript = "";
	} else if (aOnClickScript != "") {
		aOnClickScript = "onclick=\"" + aOnClickScript + ";\"";
	}
	
	if (atabIndex == null) {
			atabIndex = "";
		} else if (atabIndex != "") {
			atabIndex = "tabindex=\""+atabIndex+"\"";
	}
	
	if (aAccesskey == null || aAccesskey == "") aAccesskey = "";
		else aAccesskey = " accesskey=\"" + aAccesskey + "\" ";
		
	// Store the checkbox in the array
	var checkCount = checkboxArray.length;
	checkboxArray[checkCount] = new Array(2);
	checkboxArray[checkCount][0] = aCheckName;
	checkboxArray[checkCount][1] = aValue;
	
	var checkboxString = "<div id='" + aFormName + "_" + aCheckName + "_" + aValue + "_ds' style='display:none'>"
	checkboxString += "<img src=\"" + CHECKBOX_IMG_OFF_CHECKED + "\" class=\"objCheckbox\">";
	if (aLabel != "") checkboxString += "&nbsp;<span class=\"txtCol1\">" + aLabel + "</span>";
	checkboxString += "</div>";
	checkboxString += "<div id='" + aFormName + "_" + aCheckName + "_" + aValue + "_en'><a href=\"Javascript:checkboxEvent('" + aFormName + "', '" + aCheckName + "', '" + aValue + "');"+aScript + "\"  "+ aOnClickScript + " " + atabIndex + aAccesskey +" class=\"labelCheckbox\">"
	checkboxString += "<img name=\"" + aCheckName + "_" + aValue + "\" src=\"" + CHECKBOX_IMG_NOT_CHECKED + "\" border=\"0\" class=\"objCheckbox\">";
	if (aLabel != "") checkboxString += "&nbsp;" + aLabel;
	checkboxString += "</a><input type=\"hidden\" id=\"" + aCheckName + "_" + aValue + "\" name=\"" + aCheckName + "\" value=\"" + aValue + "\"></div>";
	document.write(checkboxString);
	if (eval(isChecked)) {
	  checkboxSelect(aFormName, aCheckName, aValue);
	} else {
	  checkboxUnselect(aFormName, aCheckName, aValue);
	}
}


// ------------------------------------------------
// Function isCheckboxChecked :
// Checks if the specified checkbox is checked or not.
//
// Params :
//	- aFormName : name of the form containing the checkboxes
//	- aCheckboxName : the name of the checkbox
//  - aValue : the value of the checkbox 
//
// Returns : true if the checkbox state is checked
// and false if it is not checked.
// ------------------------------------------------
function isCheckboxChecked(aFormName, aCheckboxName, aValue) {
	var tempValue=false;
	if ((aCheckboxName == "") || (aValue == "")) {
		return false;
	}
	if (document.getElementById) {
		tempValue = document.getElementById(aCheckboxName + "_" + aValue).value;
	}
	else if (document.all) {
		tempValue = document.all[aCheckboxName + "_" + aValue].value;
	}
	else if(document.layers) {
		tempValue = document.forms[aFormName].elements[aCheckboxName + "_" + aValue].value;
	}
	if (tempValue == aValue) {
		return true;
	} else {
		return false;
	}
}


// ------------------------------------------------
// Select the specified checkbox.
//
// Params :
//	- aFormName : name of the form containing the checkboxes
//	- aCheckboxName : the name of the checkbox
//  - aValue : the value of the checkbox 
// ------------------------------------------------
function checkboxSelect(aFormName, aCheckboxName, aValue) {
	if (document.images) {
		getImage(aCheckboxName + "_" + aValue).src = CHECKBOX_IMG_CHECKED;
	}
	if (document.getElementById) {
		//Il semble que la ligne passant par getElementByID ne fonctionne pas correctement sour IE donc elle a ete remplacee par la commande globale
		document.getElementById(aCheckboxName + "_" + aValue).value = aValue;
		document.forms[aFormName].elements[aCheckboxName + "_" + aValue].value = aValue;
	}
	else if (document.all) {
		document.all[aCheckboxName + "_" + aValue].value = aValue;
	}
	else if(document.layers) {
		document.forms[aFormName].elements[aCheckboxName + "_" + aValue].value = aValue;
	}
}


// ------------------------------------------------
// Unselect the specified checkbox.
//
// Params :
//	- aFormName : name of the form containing the checkboxes
//	- aCheckboxName : the name of the checkbox
//  - aValue : the value of the checkbox 
// ------------------------------------------------
function checkboxUnselect(aFormName, aCheckboxName, aValue) {
	if (document.images) {
		getImage(aCheckboxName + "_" + aValue).src = CHECKBOX_IMG_NOT_CHECKED;
	}	
	if (document.getElementById) {
		//Il semble que la ligne passant par getElementByID ne fonctionne pas correctement sour IE donc elle a ete remplacee par la commande globale
		document.getElementById(aCheckboxName + "_" + aValue).value = "";
		document.forms[aFormName].elements[aCheckboxName + "_" + aValue].value = "";
	}
	else if (document.all) {
		document.all[aCheckboxName + "_" + aValue].value = "";
	}
	else if(document.layers) {
		document.forms[aFormName].elements[aCheckboxName + "_" + aValue].value = "";
	}
}

// ------------------------------------------------
// Enabled the specified checkbox.
//
// Params :
//      - aFormName : name of the form containing the checkboxes
//      - aCheckboxName : the name of the checkbox
//  - aValue : the value of the checkbox
// ------------------------------------------------
function checkboxEnable(aFormName, aCheckboxName, aValue) {
	document.getElementById(aFormName + "_" + aCheckboxName + "_" + aValue + "_ds").style.display = "none";
	document.getElementById(aFormName + "_" + aCheckboxName + "_" + aValue + "_en").style.display = "block";
}

// ------------------------------------------------
// Disabled the specified checkbox.
//
// Params :
//      - aFormName : name of the form containing the checkboxes
//      - aCheckboxName : the name of the checkbox
//  - aValue : the value of the checkbox
// ------------------------------------------------
function checkboxDisable(aFormName, aCheckboxName, aValue) {
	document.getElementById(aFormName + "_" + aCheckboxName + "_" + aValue + "_en").style.display = "none";
	document.getElementById(aFormName + "_" + aCheckboxName + "_" + aValue + "_ds").style.display = "block";
}

// ------------------------------------------------
// Handle click on the image checkbox.
//
// Params :
//	- aFormName : name of the form containing the checkboxes
//	- aCheckboxName : the name of the checkbox
//  - aValue : the value of the checkbox 
// ------------------------------------------------
function checkboxEvent(aFormName, aCheckboxName, aValue) {		
	if (isCheckboxChecked(aFormName, aCheckboxName, aValue)) {
		checkboxUnselect(aFormName, aCheckboxName, aValue);
	} else {
		checkboxSelect(aFormName, aCheckboxName, aValue);
	}		
}
