/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	Version:		
	Date:		

	Script Name:	svmXFormFieldActions.js
	Created By:   	Adam Lotrowski
	Date:         	02/05/2003
	Copyright:    	Copyright 2003, SVMedia
	Purpose:      	Common javascript functions for controlling lists, forms and form fields
	
	History:
  
  	TABLE OF FUNCTIONS:
	____________________________________________________________________________________________________________________

	orderList...........................Set FormAction and OrderBy Column in hidden inputs and submits form	
	GetMultiSelectCSV...................Return csv list of multi select drop down selected	
	GetCheckedValuesCSV.................Return csv list of checkbox values checked
	emptyField..........................Sets field emply if equals specified value
	setSelectOption.....................Sets selected value for input type select based on specified value
	setRadioOption......................Sets selected value for input type select based on specified value
	setCheckOption......................Sets selected value for input type select based on specified value
	checkAll............................All checkboxes associated to 'check all' checkbox will equal 'check all' input
	AllChecked..........................Checks that the 'check all' checkbox is in consistent state with all its associated checkboxes					
	buildDropdownDate...................Build date string based on date select input parts
	buildDropdownTime...................Build time string based on date select input parts
	alternateStyle......................Alternate background style for a multi-select input
	moveLRSelect........................Switchs multiple left-right select lists
		
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Set FormAction and OrderBy Column in hidden inputs and submits form

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function orderList(objForm, intOrderByColumn, intFormOrderByAction){
	if (objForm.FormAction)			{objForm.FormAction.value		= intFormOrderByAction;}
	if (objForm.FormOrderByAction)	{objForm.FormOrderByAction.value= intFormOrderByAction;}
	if (objForm.OrderByColumn)		{objForm.OrderByColumn.value	= intOrderByColumn;}

	objForm.submit();
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Sets field emply if equals specified value

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function emptyField(fieldToBeEmptied,initialValue) {
	if (fieldToBeEmptied.value == initialValue) {
		fieldToBeEmptied.value = "";
	}
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Sets selected value for input type select based on specified value

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function setSelectOption(objFormDropDown, strValue){
	if(objFormDropDown){
		for (i=0; i <= objFormDropDown.options.length - 1 ; i++)
		{if (objFormDropDown.options[i].value == strValue)	
			{objFormDropDown.options[i].selected = true;} 
		}
	}
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Sets selected value for input type select based on specified value

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function setMultiSelect(objFormMultiSelect, strValue){
	if ((strValue != '') && (objFormMultiSelect)){
		var arrValues = strValue.split(",")
		for (j=0; j <= arrValues.length - 1 ; j++){
			for (i=0; i <= objFormMultiSelect.options.length - 1 ; i++){
				if (objFormMultiSelect.options[i].value == arrValues[j]){	
					objFormMultiSelect.options[i].selected = true; 
				}
			}
		}
	}
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Sets selected value for input type select based on specified value

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function setRadioOption(objFormRadio, strValue){
	for (var i=0; i <= objFormRadio.length - 1 ; i++){
		if (objFormRadio[i].value == strValue){objFormRadio[i].checked = true};
	} 
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Sets selected value for input type select based on specified value

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function setCheckOption(objFormCheckBox, strValue){
	var tmpValue
		
	if (objFormCheckBox){
		if (objFormCheckBox.length){
			for (var i=0; i <= objFormCheckBox.length - 1 ; i++)
				{objFormCheckBox[i].checked = false;} 
		}else{objFormCheckBox.checked = false;}
		
		if(strValue != ''){
			if (strValue.split(',') != -1){
				var arrOptions = strValue.split(',');
			}else{
				var arrOptions = new Array[0];
				arrOptions[0] = strValue;
			}
					
			for(var j=0; j<arrOptions.length; j++){ 
				
				tmpValue = arrOptions[j]
				
				if(tmpValue != ''){					
					if (objFormCheckBox.length){
						for (var i=0; i <= objFormCheckBox.length - 1 ; i++)
						{if (objFormCheckBox[i].value.indexOf(tmpValue) != -1)	
							{objFormCheckBox[i].checked = true;} 
						}
					} else {
						if (objFormCheckBox.value == tmpValue){
							objFormCheckBox.checked = true;
						}
					}
				}		
			}
		}
	}
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Return csv list of multi select drop down selected

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function GetMultiSelectCSV(objMultiSelect){
	
	var csvListOfSelectedItems 	= '';
	var intSelectedCount		= 0;

	if (objMultiSelect) {
		var formField = objMultiSelect;
		
		if(formField.length){
			for (i = 0; i < formField.length; i++){
				if (formField.options[i].selected == true){
					if (intSelectedCount == 0) {
						csvListOfSelectedItems += formField.options[i].value
						intSelectedCount += intSelectedCount + 1
					} else {
						csvListOfSelectedItems += "," + formField.options[i].value
						intSelectedCount += intSelectedCount + 1
					}
				}
			}
		}
	}	
	return csvListOfSelectedItems;
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Return csv list of checkbox values checked

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function GetCheckedValuesCSV(objCheckBox){
	
	var csvListOfSelectedItems = '';
	var intCheckedCount		   = 0;

	if (objCheckBox) {
		var formField = objCheckBox;
		
		if(formField.length){
			for (i = 0; i < formField.length; i++){
				if ((formField[i].checked) && (!(formField[i].disabled))){
					if (intCheckedCount == 0) {
						csvListOfSelectedItems += formField[i].value
						intCheckedCount += intCheckedCount + 1
					} else {
						csvListOfSelectedItems += "," + formField[i].value
						intCheckedCount += intCheckedCount + 1
					}
				}
			}
		}else{
			if (formField.checked){
				csvListOfSelectedItems += formField.value
			}
		}
	}	
	return csvListOfSelectedItems;
}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DESCRIPTION: 
	WHEN CHECK ALL IS CLICKED, ALL ASSOCIATION CHECKBOXS WILL EQUAL 
	THE STATE OF THE CHECK ALL CHECKBOX
  
PARAMETERS:
	listCheckBox.....Single/Group of Checkbox object(s) that masterCheckBox
					 controls
	masterCheckBox...Select All / Unselect All Checkbox object
   
RETURNS:
	listCheckBox state equals masterCheckBox
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

function checkAll(listCheckBox, masterCheckBox){

	if (listCheckBox){

		var formField = listCheckBox;

		if (formField.length) {
			for (i = 0; i < formField.length; i++){
				if(!formField[i].disabled){ 
					formField[i].checked = masterCheckBox.checked;
				}
			}
		}else{
			formField.checked = masterCheckBox.checked;
		}
	}
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DESCRIPTION:
	WHEN ALL ASSOCIATION CHECKBOXES ARE ALL IN THE SAME STATE THEN 
	THE CHECK ALL CHECK BOX IS IN THE SAME STATE.
  
PARAMETERS:
	listCheckBox.....Single/Group of Checkbox object(s) that masterCheckBox
					 controls
	masterCheckBox...Select All / Unselect All Checkbox object
   
RETURNS:
	masterCheckBox state equals listCheckBox if all either checked or not
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function AllChecked(listCheckBox, masterCheckBox){
	
	var blnAllChecked = true;

	if (listCheckBox) {
		var formField = listCheckBox;
	
		if(formField.length){
			for (i = 0; i < formField.length; i++){
				if ((!(formField[i].checked)) && (!(formField[i].disabled))){
					masterCheckBox.checked = false;
					blnAllChecked = false;
				}
			}
		}else{
			if (!(formField.checked)){
				masterCheckBox.checked = false;
				blnAllChecked = false;
			}
		}
	}	
	if (blnAllChecked == true){
		masterCheckBox.checked = true;
	}
}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Build Date string based on date select input parts

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function buildDropdownDate(objMonthName, objDayName, objYearName){
	var strMonth	= objMonthName.options[objMonthName.selectedIndex].value;
	var strDay 		= objDayName.options[objDayName.selectedIndex].value;
	var strYear 	= objYearName.options[objYearName.selectedIndex].value;
	var strDate 	= strMonth + "/" + strDay + "/" + strYear;
	if(strDate == '//'){strDate = '';}
	return strDate
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	PURPOSE:	Build time string based on time select input parts

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function buildDropdownTime(objHourName, objMinuteName, objAMPMName){
	var strHour		= objHourName.options[objHourName.selectedIndex].value;
	var strMinute 	= objMinuteName.options[objMinuteName.selectedIndex].value;
	var strAMPM 	= objAMPMName.options[objAMPMName.selectedIndex].value;
	var strTime 	= strHour + ":" + strMinute + " " + strAMPM;
	if(strTime == ': '){strTime = '';}
	return strTime
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DESCRIPTION: 
	ALTERNATE BACKGROUND STYLE FOR MULTIPLE SELECT
  
PARAMETERS:
	objSelectList.....MULTIPLE LIST OBJECT
   
RETURNS:
	ALTERNATING BACKGROUD STYLES FOR MULTIPLE SELECT LIST
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
// Apply alternating option backgrounds
function alternateStyle(objSelectList){ 
	for(var i=0; i<objSelectList.options.length; i++){ 
		if((objSelectList.options[i]) && (i % 2 == 0)) {
			objSelectList.options[i].style.backgroundColor = 'whitesmoke';				
		}else{
			objSelectList.options[i].style.backgroundColor = 'white';
		}
	}
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DESCRIPTION: 
	CONTROLS MULTIPLE LEFT RIGHT SELECT LIST
  
PARAMETERS:
	objSrcList.....OBJECT WITH LIST OF VALUES TO BE MOVED
	objDestList....OBJECT TO RECEIVE SELECTED LIST OF VALUES
	blnMoveAll.....IF TRUE ALL VALUES IN objSrcList WILL BE MOVED TO blnMoveAll
   
RETURNS:
	SELECTED LIST OF ITEMS MOVED TO OPPOSITE SELECT LIST 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
function moveLRSelect(objSrcList, objDestList, blnMoveAll){
	// Do nothing if nothing is selected
	if ((objSrcList.selectedIndex == -1) && (blnMoveAll == false)){return;}
	  
	var newDestList		 = new Array(objDestList.options.length);
	var newDestListText  = new Array(objDestList.options.length);
	var newDestListValue = new Array(objDestList.options.length);
	
	var len = 0;
	for(len=0; len<objDestList.options.length; len++){
		if (objDestList.options[len] != null){
			newDestListText[len]  = objDestList.options[len].text;
			newDestListValue[len] = objDestList.options[len].value;
		}
	}

	for(var i=0; i<objSrcList.options.length; i++){ 
		if (objSrcList.options[i] != null && (objSrcList.options[i].selected == true || blnMoveAll)){
			// Statements to perform if option is selected
			// Incorporate into new list
			newDestListText[len]  = objSrcList.options[i].text;
			newDestListValue[len] = objSrcList.options[i].value;
			len++;
		} 
	}
	
	// Sort out the new destination lists
	var i, j;
	for (i = newDestListText.length - 1; i >= 0; i--){
		for (j = 0; j <= i; j++){
			if (newDestListText[j+1] < newDestListText[j]){
				var temp  = newDestListText[j];
				var temp2 = newDestListValue[j];
				newDestListText[j]  = newDestListText[j+1];
				newDestListValue[j] = newDestListValue[j+1];
				newDestListText[j+1]  = temp;
				newDestListValue[j+1] = temp2;
			}
		}
	}
	
	// CREATE DESTINATION LIST OPTIONS
	for (var j=0; j<newDestListText.length; j++){
		if(newDestListText[j] != null){
			newDestList[j] = new Option(newDestListText[j], newDestListValue[j]);
		}
	}

	// Populate the destination with the items from the new array
	for (var j=0; j<newDestList.length; j++){
		if(newDestList[j] != null){
			objDestList.options[j] = newDestList[j];
		}
	}

	// Erase source list selected elements
	for(i=objSrcList.options.length-1; i>=0; i--){ 
		if(objSrcList.options[i] != null && (objSrcList.options[i].selected == true || blnMoveAll)){
		   // Erase Source
		   objSrcList.options[i].value = "";
		   objSrcList.options[i].text  = "";
		   objSrcList.options[i]       = null;
		}
	}	
}