/* ******************************** */
/*                                  */
/*      Custom radio buttons        */
/*                                  */
/* ******************************** */

// Radio image for when not checked
var RADIO_IMG_NOT_CHECKED = absolutePathForStaticResources +"Images/radio.gif";

// Radio image for when checked
var RADIO_IMG_CHECKED = absolutePathForStaticResources +"Images/radio_on.gif"; 

// Radio image for when inactive
var RADIO_IMG_INACTIVE = absolutePathForStaticResources +"Images/radio_off.gif"; 


// Global definitions
var radioCount = 0;
var radioArray = new Array();
var radioNamesArray = new Array();
var radioSelectedImg;
var radioUnSelectedImg;

// Initialize image objects
if (document.images) {
   	radioSelectedImg = new Image();
    radioSelectedImg.src = RADIO_IMG_CHECKED;
   	radioUnselectedImg = new Image();
    radioUnselectedImg.src = RADIO_IMG_NOT_CHECKED;
   	radioInactiveImg = new Image();
    radioInactiveImg.src = RADIO_IMG_INACTIVE;
}
  
  
//-----------------------------------------------------------
// Insert a radio button
//
// Params :
//	- aFormName : name of the form containing radio buttons
//	- aName : name of the radio button field
//  - aValue : value of the radio button
//  - aLabel : label of the radio button
//  - isSelected : specify if the radio is initially selected
//  - aScript : the script string to execute on checkbox click event (in href)
//  - atabIndex : Tab index
//  - aOnClickScript : the script string to execute on checkbox click event (in onclick)
//-----------------------------------------------------------
function createRadio(aFormName, aName, aValue, aLabel, isSelected, aScript, atabIndex, aOnClickScript)  {
  
	var imageName = null;
   	var checked = "";        	
	// If document.images not handled by browser
	// use standard radio button
    if (!document.images) {

		if(isSelected == -1) {
			imageName = RADIO_IMG_INACTIVE;
        	checked = "";
        } else if(isSelected) {
				imageName = RADIO_IMG_CHECKED;
				checked = "CHECKED";
			} else {
				imageName = RADIO_IMG_NOT_CHECKED;        
				checked = "";
			}
		if (aScript == null) {
			aScript = "";
		} else if (aScript != "") {
			aScript = "onclick=\"Javascript:" + aScript + ";\"";
		}
		if (atabIndex == null) {
			atabIndex = "";
		} else if (atabIndex != "") {
			atabIndex = "tabindex=\""+atabIndex+"\"";
		}
        document.write('<input type="radio" name="' + aName + '" value="' + aValue + '" ' + imageName + ' checked="' + checked + " " + aScript + " " + atabIndex + '">' + aLabel);      	
      	return;
    }    

    var hiddenDefined = false;
    var hiddenFieldIndex = 0;
    var hiddenFieldString = "";

    // Check if the hidden field corresponding to
    // the radio button is already defined
    for (var i = 0; i < radioNamesArray.length; i++) {
       	if (radioNamesArray[i] == aName) {
       		hiddenDefined = true;
       		hiddenFieldIndex = i;
    	}
    }
     
    // If the hidden field is not already defined
    if(hiddenDefined == false) {
    	hiddenFieldString = '<input type="hidden" name="' + aName + '" value="" style="vertical-align:bottom;height:0;margin:0;padding:0;border:0">';
		var radioNamesCount = radioNamesArray.length;
		radioArray[radioNamesCount] = new Array();
		radioArray[radioNamesCount][0] = new Array();
		radioArray[radioNamesCount][0][0] = aName;
		radioArray[radioNamesCount][0][1] = 'radio_' + aFormName + '_' + aName + '_' + aValue;
		radioNamesArray[radioNamesArray.length] = aName;
	}
	// If it is already defined
    else {
       	hiddenFieldString = "";
	    var radioArrayCount = radioArray[hiddenFieldIndex].length
        radioArray[hiddenFieldIndex][radioArrayCount] = new Array();
       	radioArray[hiddenFieldIndex][radioArrayCount][0] = aName;
	    radioArray[hiddenFieldIndex][radioArrayCount][1] = 'radio_' + aFormName + '_' + aName + '_' + aValue;
    }
	
	// If a script is defined
	if (aScript == null) {
		aScript = "";
	} else if (aScript != "") {
		aScript += ";";
	}
    
		if (aOnClickScript == null) {
			aOnClickScript = "";
		} else if (aOnClickScript != "") {
			aOnClickScript = "onclick=\"Javascript:" + aOnClickScript + ";\"";
		}
    
    if (atabIndex == null) {
			atabIndex = "";
		} else if (atabIndex != "") {
			atabIndex = "tabindex=\""+atabIndex+"\"";
	}
    // Display image depending on initial state 
    if(isSelected == -1) {
       	imageName = RADIO_IMG_INACTIVE;
    }
		else if(isSelected) {
			imageName = RADIO_IMG_CHECKED;
		}
		else {
			imageName = RADIO_IMG_NOT_CHECKED;
		}
      	
    if(isSelected == -1)
		document.write("<img src=\"" + imageName + "\" name=\"radio_" + aFormName + "_" + aName + "_" + aValue + "\" id=\"" + aValue + "\" class=\"objRadio\">"
			+ aLabel);
	else
		document.write("<a href=\"javascript:radioEvent('" + aFormName + "', '" + aName + "', '" + aValue + "');"+ aScript + "\" "+ aOnClickScript + " " + atabIndex +" class=\"labelRadio\">"
			+ "<img src=\"" + imageName + "\" name=\"radio_" + aFormName + "_" + aName + "_" + aValue + "\" id=\"" + aValue + "\" border=\"0\" "+" class=\"objRadio\">"
			+ aLabel
			+ "</a>" + hiddenFieldString);
    // If this radio is selected by default, set the hidden field value 
	if(isSelected != -1)        
		if(isSelected) {
			document.forms[aFormName].elements[aName].value = aValue;
		}
    radioCount++
}

// ------------------------------------------------
// Function isRadioChecked :
// Checks if the specified radio is checked or not.
//
// Params :
//	- aFormName : name of the form containing the checkboxes
//	- aRadioName : the name of the checkbox
//  - aValue : the value of the checkbox 
//
// Returns : true if the radio state is checked
// and false if it is not checked.
// ------------------------------------------------
function isRadioChecked(aFormName, aRadioName, aValue) {
	if (document.forms[aFormName].elements[aRadioName].value == aValue) {
		return true;
	} else {
		return false;
	}
}

  
// --------------------------------------------------------
// Handle radio button click event
//
// Params :
//	- aFormName : name of the form containing radio buttons
//	- aName : name of the radio button field
//  - aValue : value of the radio button
// --------------------------------------------------------
function radioEvent(aFormName, aName , aValue) {
  
  	// If the hidden field value does not correspond to the value 
  	// of the checkbox, unselect all radio buttons
    if(document.forms[aFormName].elements[aName].value != aValue) {
    	for (i = 0; i < radioArray.length; i++) {
        	for (j = 0; j < radioArray[i].length; j++) {
          		if(radioArray[i][j][0] == aName) {            
          			getImage(radioArray[i][j][1]).src = eval('radioUnselectedImg.src');
          		}
        	}
      	}
      
      	// Select the clicked radio :
      	// set image
      	getImage('radio_' + aFormName + '_' + aName + '_' + aValue).src = eval('radioSelectedImg.src');
      	// set hidden field value
      	document.forms[aFormName].elements[aName].value = aValue;
    }
 }
