/*
This file contains global methods that are part of SPL
web library method
*/


/*
Name : setStatus
Input: A String
Desc : This method sets the status bar to the given string
*/

function setStatus(newStatusStr){
	self.status = newStatusStr;
}

/*
Name : transferSingle
Input: 2 select elements ID: source and destination
Desc : The method transfers the selected option from the source list to
		the destination list and deletes the selected option from the 
		source list.
*/

	function transferSingle(fromBox,toBox){
		sourceType = fromBox.type;
		if (sourceType=="select-one"){
			selectedIndex = fromBox.selectedIndex;
		
			// Check that there is a selected item in the source
			if (selectedIndex > -1){
				selectedOption = fromBox.options[selectedIndex];
				selectedText = selectedOption.text;
				selectedVal  = selectedOption.value;
				//toBox[toBox.options.length] = new Option(selectedVal,selectedText);
				toBox[toBox.options.length] = new Option(selectedText,selectedVal);
				fromBox.options[selectedIndex] = null;
			}
		}
		else{
		// The source has multiple selection enabled
			sourceLen = fromBox.length;
			for (i=sourceLen-1;i>=0;i--){
				if (fromBox.options[i].selected==true){
					selectedOption = fromBox.options[i];
					selectedText = selectedOption.text;
					selectedVal  = selectedOption.value;
					//toBox[toBox.options.length] = new Option(selectedVal,selectedText);
					toBox[toBox.options.length] = new Option(selectedText,selectedVal);
					fromBox.options[i] = null;
				}
			}
		}
	}

/*
Name : transferAll
Input: 2 select elements ID: source and destination
Desc : The method transfers all the elements from the source list to 
		the destination list. the elements are removed from the source
		list
Remark : Because we are removing elements from the source list , we cannot
			refrence the elements in an incremental order, there is a need
			to refrence the first or last option only.
*/
function transferAll(fromBox,toBox){
		numOfElements = fromBox.options.length;
		for (i=0;i<numOfElements;i++){
			selectedOption = fromBox.options[0];
			selectedText = selectedOption.text;
			selectedVal  = selectedOption.value;
			//toBox[toBox.options.length] = new Option(selectedVal,selectedText);
			toBox[toBox.options.length] = new Option(selectedText,selectedVal);
			fromBox.options[0] = null;
		}
	}


/*
Name : submitSimpleForm
Input : action
Desc : The method submits the form.'action' is the 
       requested operation (button).         
*/
function submitSimpleForm(action,GroupID,Updatecounter,Description){
	if (document.forms[0].LAction!=null)
		document.forms[0].LAction.value = action;
	if (document.forms[0].hdnGroupID!=null)
		document.forms[0].hdnGroupID.value = action;
	if (document.forms[0].hdnUpdatecounter!=null)
		document.forms[0].hdnUpdatecounter.value = action;
	if (document.forms[0].hdnDescription!=null)
		document.forms[0].hdnDescription.value = action;
	if ((document.forms[0].LAction!=null) && (document.forms[0].hdnGroupID!=null) && (document.forms[0].hdnUpdatecounter!=null) && (document.forms[0].hdnDescription!=null) )
		document.forms[0].submit();	
}

/*****************************************************************
Name : setPeriodNum
Input: the input object , number f points after the period
Desc : The method sets the number of digits to show after the 
		period
Should be implemented : NO 
*******************************************************************/	
function setPeriodNum(tempObj,pointAfterPeriod){
	tempStr = new String(tempObj);
	tempPeriodIndex = tempStr.indexOf(".");
	if (tempPeriodIndex>-1){
		strLength = tempStr.length;
		endIndex = tempPeriodIndex + (pointAfterPeriod+1);
		if (endIndex < strLength)
			tempStr = tempStr.substring(0,endIndex); 
	}
	return tempStr;
}



/*****************************************************************/
/*				Getting an HTML form element value				 */
/*****************************************************************/

/*
Name : getTextVal
Input: an input object 
Desc : Gets the 'value' attirubute of the object , if the object 
		is null than "" is returned
*/
function getTextVal(elementObj){
	result = "";
	if (elementObj!=null)
		result =  elementObj.value;
	return result;
}

/*
Name : getSelectVal
Input: a select object 
Desc : Gets the value of the selected item in the given object,
		if there is no selected item or there are no items in the
		list or the object is null than "" is returned
*/

function getSelectVal(elementObj){
	result = "";
	if (elementObj!=null && elementObj.selectedIndex > -1){
		result = elementObj.options[elementObj.selectedIndex].value;
	}
	return result;
}

/*
Name : getRadioVal
Input: a Radio object 
Desc : Gets the value of a radio object only if the radio is
		checked. if the object is null or the radio is not checked
		than "" is returned.
*/
function getRadioVal(elementObj){
	result = "";
	if (elementObj!=null && elementObj.checked){
		result = elementObj.value;
	}
	return result;
}

/*
Name : getCheckboxVal
Input: an Radio object 
Desc : Gets the value of a checkbox object. 
		if the object is null than "" is returned.
*/

function getCheckboxVal(elementObj){
	result = "";
	if (elementObj!=null){
		result = elementObj.value;
	}
	return result;
}

/*
Name : getFieldValue
Input: a form element object 
Desc : redirects the object to the relevant method according to the 
		method types and returns its value.
*/

function getFieldValue(elementObj){
	var result = "";
	switch(elementObj.type){
		case "text"		: 
		case "password" :
		case "textarea" : 
		case "hidden"	:
		case "file"		: result = getTextVal(elementObj);break;
		case "checkbox" : result = getCheckboxVal(elementObj);break;
		case "radio"	: result = getRadioVal(elementObj);break;
		case "select-one" : 
		case "select-multiple" : result = getSelectVal(elementObj);break;
	}
	return result;
}

/*****************************************************************/
/*				Setting an HTML form element value				 */
/*****************************************************************/
/*
Name : setFieldValue
Input: a form element object,value 
Desc : sets the value of the given object to the given value.
		if the given object is null than false is retured otherwise
		true is returned.
		If the given value is null than the value is set to ""
*/
function setTextVal(elementObj,value){
	result = false;
	if (elementObj!=null){
		if (value == null)
			elementObj.value = "";
		else
			elementObj.value = value;
		result = true;
	}
	return result;
}
/*
Name : setSelectVal
Input: a select object,value 
Desc : Sets the selected value in the list to the element whose
		value is the given value.
		if the given value is null than the first elments is selected
		if the given element is null or there is no element in the 
		given element with the given value than false is returned, otherwise
		true is returned.		
*/

function setSelectVal(elementObj,value){
	result = false;
	count = 0;
	if (elementObj!=null && elementObj.length > 0)
		if (value==null){
			elementObj.selectedIndex = 0;
			result = true;
		}
		else{
			tempLength = elementObj.length;
			for (j=0;j<tempLength;j++)
				if (elementObj.options[j].value==value){
					elementObj.selectedIndex = j;
					result = true;	
				}
		}			
	return result;
}

/*
Name : setRadioVal
Input: a select object,value 
Desc : If the value of radio is the given value then the radio is
		checked. If the given value is null the radio is unchecked.
		if the given element is null false is returned, otherwise
		true is returned.		
*/

function setRadioVal(elementObj,value){
	result = false;
	if (elementObj!=null){
		result = true;
		if (value==null)
			elementObj.checked = false;
		else
			if (elementObj.value==value)
				elementObj.checked = true;
			else
				elementObj.checked = false;
	}
	return result;
}

/*
Name : setCheckboxVal
Input: a select object,value 
Desc : If the value of radio is the given value then the radio is
		checked. If the given value is null the radio is unchecked.
		if the given element is null false is returned, otherwise
		true is returned.		
*/


function setCheckboxVal(elementObj,value){
	result = false;
	if (elementObj!=null){
		result = true;
		if (value==null)
			elementObj.checked = false;
		else
			if (elementObj.value==value)
				elementObj.checked = true;
			else
				elementObj.checked = false;
	}
	return result;
}

/*
Name : setFieldValue
Input: a form element object , value
Desc : redirects the object to the relevant method according to the 
		method types and returns the value (true or false)
*/
function setFieldValue(elementObj,value){
	var result = false;
	switch(elementObj.type){
		case "text"		: 
		case "password" :
		case "textarea" : 
		case "hidden"	:
		case "file"		: result = setTextVal(elementObj,value);break;
		case "checkbox" : result = setCheckboxVal(elementObj,value);break;
		case "radio"	: result = setRadioVal(elementObj,value);break;
		case "select-one" : 
		case "select-multiple" : result = setSelectVal(elementObj,value);break;
	}
	return result;
}


/*
Name : ViewOnly
Input: 
Desc : Disables all the fields on the form
*/
function ViewOnly(){
		//alert("view");
		var numOfElements;
		var index;
		var elementType
		if (document.forms[0]!=null){
			numOfElements = document.forms[0].length;
			//alert(numOfElements);
			for (index=0;index<numOfElements;index++){
				
				//document.forms[0][index].readOnly = true;
				elementType=document.forms[0][index].type;
				
				switch(elementType){
					case "text"		: 
					case "password" :
					case "textarea" : 
					case "hidden"	:
					case "file"		: document.forms[0][index].className = "HH_text_disable";
									  document.forms[0][index].readOnly = true;break;
					case "checkbox" :
					case "select-one" : 
					case "select-multiple" : document.forms[0][index].className = "HH_select_disable";
											 document.forms[0][index].Disabled = true;break;
				}
			}
			//	if (elementType = "text")
			//		document.forms[0][index].className = "HH_text_disable";
			//	else if (elementType = "text")						
		}
		
	}
	
