//--------------------------------------------------------------------------
//Name : isPresentInList        
//Parameters :  incoming --- of type Array, element --- type of variable contained in the array
//Description : Checks if the element is present in the incoming array
//Returns true if the element is present in the array else false.
//--------------------------------------------------------------------------
function isPresentInList(incoming,element)
{
	if(incoming != null && element != null)
	{
		var z;
		for(z = 0; z < incoming.length; z++)
		{
			if(incoming[z] == element)
				return true;
		}
		return false;	
	}
	return false;
}

//--------------------------------------------------------------------------
//Name :   setValueInRadioGroup
//Parameters :  radiogroup - A collection of radio buttons, not a single radio button
//				valueToBeSet - A String value to which the radio group has to be checked
//Description : This function provides a way to populate a group of radio buttons to a 
// set/default value. The value to be set to is passed in as 'valueToBeSet'
//--------------------------------------------------------------------------
function setValueInRadioGroup(radiogroup,valueToBeSet)
{
	var radiogroupLen = radiogroup.length;
	if( radiogroupLen != null && radiogroupLen != 'undefined' )
	{
		for( i = 0; i < radiogroupLen; i++ ) 
		{
			if( radiogroup[i].value == valueToBeSet.toString() )
				radiogroup[i].checked = true;
		}					
	} 
}

//--------------------------------------------------------------------------
//Name :   clearAllRadios
//Parameters :  radiogroup - A collection of radio buttons, not a single radio button
//Description : This function provides a way to clear all the radio buttons in a group.
//--------------------------------------------------------------------------
function clearAllRadios(radiogroup)
{
	var radiogroupLen = radiogroup.length;
	if( radiogroupLen != null && radiogroupLen != 'undefined' )
	{
		//Clear all.
		for( i = 0; i < radiogroupLen; i++ ) 
		{
			radiogroup[i].checked = false;
		}							
	}
}

//--------------------------------------------------------------------------
//Name :        handleClearAllElementsInDiv
//Parameters :  divId: The 'id' attribute of the <div> tag
//Description : This will clear all the elements in the passed in <div> section
//--------------------------------------------------------------------------
function handleClearAllElementsInDiv(divId) {

	var passedIndiv = document.getElementById(divId);
	
	if( passedIndiv != null && passedIndiv != 'undefined' )
	{
		var containedDivElements = passedIndiv.getElementsByTagName("*");
		
		//Iterate over each element, figure out what it is and clear it
		for( var i = 0; i < containedDivElements.length; i++ )
		{
			if( containedDivElements[i] != null && containedDivElements[i] != 'undefined' )
			{
				if( containedDivElements[i].type == 'text' ) {
					containedDivElements[i].value = '';
				} else if( containedDivElements[i].type == 'checkbox' || containedDivElements[i].type == 'radio' ) {
					containedDivElements[i].checked = false;
				} else if( containedDivElements[i].type == 'textarea' ) {
					containedDivElements[i].value = '';
				} else if( containedDivElements[i].options != null && containedDivElements[i].options.length > 0 ) {
					//Means this is a select list, iterate and de-select all the options other than the default
					for( var j = 0; j < containedDivElements[i].options.length; j++ )
					{
						if( containedDivElements[i].options[j].value != null && ( containedDivElements[i].options[j].value == '' || containedDivElements[i].options[j].value.indexOf("Select") > -1 ) )
							containedDivElements[i].options[j].selected = true;
						else
							containedDivElements[i].options[j].selected = false;
					}
				}
			}
		}
	}
	
	return true;
}

//--------------------------------------------------------------------------
//Name :        setValueToTrue
//Parameters :  Id of the element
//Description : This will set the element value to true
//--------------------------------------------------------------------------
function setValueToTrue(elementId) {

	var passedInElement = document.getElementById(elementId);
	
	if( passedInElement != null && passedInElement != 'undefined' )
	{
		if( passedInElement.type == 'checkbox' || passedInElement.type == 'radio' )
			passedInElement.checked = true;
	}
	
	return true;
}

//--------------------------------------------------------------------------
//Name :        setValueToY
//Parameters :  Id of the element
//Description : This will set the element value to Y
//--------------------------------------------------------------------------
function setValueToY(elementId) {
	var passedInElement = document.getElementById(elementId);
	if( passedInElement != null && passedInElement != 'undefined' )
	{
		passedInElement.value="Y";
	}
	return true;
}

//--------------------------------------------------------------------------
//Name :        setValueToFalse
//Parameters :  Id of the element
//Description : This will set the element value to false
//--------------------------------------------------------------------------
function setValueToFalse(elementId) {

	var passedInElement = document.getElementById(elementId);
	
	if( passedInElement != null && passedInElement != 'undefined' )
	{
		if( passedInElement.type == 'checkbox' || passedInElement.type == 'radio' )
			passedInElement.checked = false;
	}
	
	return true;
}

//--------------------------------------------------------------------------
//Name :        isAnyElementEnteredInDiv
//Parameters :  divId: The 'id' attribute of the <div> tag
//Description : Returns 'true' if at least one element in the <div> is selected, else returns 'false'
//--------------------------------------------------------------------------
function isAnyElementEnteredInDiv(divId) {
	var passedIndiv = document.getElementById(divId);
	var atleastOneChecked = false;	

	if( passedIndiv != null && passedIndiv != 'undefined' )
	{
		var containedDivElements = passedIndiv.getElementsByTagName("*");
		
		//Iterate over each element, figure out if it is populated
		for( var i = 0; i < containedDivElements.length; i++ )		
		{
			if( containedDivElements[i] != null && containedDivElements[i] != 'undefined' )
			{
				if( containedDivElements[i].type == 'text' || containedDivElements[i].type == 'textarea'  ) {
					if( containedDivElements[i].value != '' ) {
						atleastOneChecked = true;
						break;
					}
				} else if( containedDivElements[i].type == 'checkbox' || containedDivElements[i].type == 'radio' ) {
					if( containedDivElements[i].checked ) {
						atleastOneChecked = true;
						break;
					}
				} else if( containedDivElements[i].options != null && containedDivElements[i].options.length > 0 ) {
					//Means this is a select list, see if anything other than the default was selected
					for( var j = 0; j < containedDivElements[i].options.length; j++ )
					{
						if( containedDivElements[i].options[j].value != null && ( containedDivElements[i].options[j].value != '' || containedDivElements[i].options[j].value.indexOf("Select") == -1 ) ) {
							atleastOneChecked = true;
							break;						
						}
					}
					
					if(atleastOneChecked) {
						break;
					}					
				}
			}			
		}
	}
	
	return atleastOneChecked;
}

//--------------------------------------------------------------------------
//Name :   getCheckedRadioValue
//Parameters :  radiogroup - A collection of radio buttons, not a single radio button
//				
//Description : This function returns the value of the checked radio button in the
// group.
//--------------------------------------------------------------------------
function getCheckedRadioValue(radiogroup)
{
	if( radiogroup == null || radiogroup == 'undefined' )
		return null;

	var radiogroupLen = radiogroup.length;
	if( radiogroupLen != null && radiogroupLen != 'undefined' )
	{
		for( i = 0; i < radiogroupLen; i++ ) 
		{
			if( radiogroup[i].checked )
				return radiogroup[i].value;
		}					
	} 
	
	return null;
}

//--------------------------------------------------------------------------
//Name :   IsNumeric
//Parameters :  sText - string value to test
//				
//Description : This function performs a numeric test on the sText string value.
//--------------------------------------------------------------------------
function IsNumeric(sText) {
	var ValidChars = "0123456789";
	var IsNumber = true;
	var Char;
 
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

//--------------------------------------------------------------------------
//Name :   populateElement
//Parameters :  fieldValue - string value to set
//              id - id of element to set
//				
//Description : This function places the fieldValue string within the
// element id.
//--------------------------------------------------------------------------
function populateElement(fieldValue, id) {
	if (fieldValue == null) {
		fieldValue = '';
	}

	var element = document.getElementById(id);
	if (element && document.createTextNode) {
		var replacement = document.createTextNode(fieldValue);
		if (element.hasChildNodes()) {
			var replaced = element.childNodes[0];
			if (element.replaceChild) {
				element.replaceChild(replacement, replaced);
			}
			else {
				element.replaceNode(replacement, replaced);
			}
		}
		else {
			if (element.appendChild) {
				element.appendChild(replacement);
			}
			else {
				element.appendNode(replacement);
			}
		}
	}
	else {
		eval('element.innerHTML="'+fieldValue+'";');
	}
}

//Call only for straight element not elements or arrays
function isElementNullOrUndefined(elementId) {
	if(elementId == null || elementId == undefined )
		return true;
		
	var element = document.getElementById(elementId);
	
	if(element != null && element != undefined ) {
		return false;
	}
	
	return true;
}
