//JavaScript functions (http://www.siteexperts.com/tips/functions/ts23/demo.asp)

function ValidateElement(blnValue,strElement)
	{
	var strHelpText = 'ctl00_MainContent_HelpText' + strElement
	strElement = 'ctl00_MainContent_' + strElement
	
	if (blnValue == false)
		{
		document.getElementById(strElement).className = 'invalid';
		document.getElementById(strHelpText).className = 'ShowHelpText';
		}
	else
		{
		document.getElementById(strElement).className = 'valid';
		document.getElementById(strHelpText).className = 'HideHelpText';
		}
	}

function IsValidOneOrZero(strValue)
	{
	if (strValue == '1' || strValue == '0' || strValue == 'False' || strValue == 'True')
		{
		return true;
		}
	else
		{
		return false;
		}
	}

function IsValidRequiredText(strValue)
	{
	if (strValue == '')
		{
		return false;
		}
	else
		{
		return true;
		}
	}


function IsValidDate(strValue)
	{
	if (CheckDate(strValue) == 'N')
		{
		return false;
		}
	else
		{
		return true;
		}
	}


function IsValidInteger(strValue)
	{
	if (validateInteger(strValue) == 'N')
		{
		return false;
		}
	else
		{
		return true;
		}
	}
	
	
function IsValidNumeric(strValue)
	{
	if (validateNumeric(strValue) == 'N')
		{
		return false;
		}
	else
		{
		return true;
		}
	}


function IsValidSelectList(strValue)
	{
	if (strValue == '')
		{
		return false;
		}
	else
		{
		return true;
		}
	}	



function removeCommas(strValue)
	/*
	Description:	Strips commas out from a value
	Parameters:		strValue - the value to check
	Returns:		A string without commas
	*/
	
	{
	var objRegExp = /,/g;	//search for commas globally
	
	return strValue.replace(objRegExp,'');	//replace all matches with empty strings
	}


function validateInteger(strValue)
	{
	/*
	Description:	Validates that a string contains only valid integer numbers
	Parameters:		strValue - the value to check
	Returns:		Y if valid, else N
	*/
	
	var objRegExp  = /(^-?\d\d*$)/;
	 
	//check for integer characters
	var blnAnswer = objRegExp.test(removeCommas(strValue));
	

	if (blnAnswer == true)
		{
		return 'Y'
		}
	else
		{
		return 'N'
		}
	}

function  validateNumeric(strValue) 
	{
	/*
	Description:	Validates that a string contains only valid numbers
	Parameters:		strValue - the value to check
	Returns:		Y if valid, else N
	*/
	
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
	 
	//check for numeric characters
	var blnAnswer = objRegExp.test(removeCommas(strValue));
	
	if (blnAnswer == true)
		{
		return 'Y'
		}
	else
		{
		return 'N'
		}
	}


function CheckDate(strValue)
	{
	/*
	Description:	Validates a date. Date must be in mm/dd/yyyy format.
					Date separator can be a comma, dash or slash
	Parameters:		strValue - the value to check
	Returns:		Y if valid, else N
	*/

	var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	 
	//check to see if in correct format
	if(!objRegExp.test(strValue))
		return 'N'; //doesn't match pattern, bad date
	else
		{
		var arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
		var intDay = parseInt(arrayDate[1],10); 
		var intYear = parseInt(arrayDate[2],10);
		var intMonth = parseInt(arrayDate[0],10);
		
		//check for valid month
		if(intMonth > 12 || intMonth < 1) 
			{
			return 'N';
			}
		
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
							'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
	  
		//check if month value and day value agree
		if(arrayLookup[arrayDate[0]] != null) 
			{
			if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
				return 'Y'; //found in lookup table, good date
			}
			
		//check for February
		var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
		if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
		return 'Y'; //Feb. had valid number of days
		}
	return 'N'; //any other values, bad date
	}


function rightTrim(strValue) 
	{
	var objRegExp = /^([\w\W]*)(\b\s*)$/;
	 
		if(objRegExp.test(strValue)) 
			{
			//remove trailing a whitespace characters
			strValue = strValue.replace(objRegExp, '$1');
			}
			
	return strValue;
	}


function leftTrim(strValue) 
	{
	var objRegExp = /^(\s*)(\b[\w\W]*)$/;
	 
		if(objRegExp.test(strValue)) 
			{
			//remove leading a whitespace characters
			strValue = strValue.replace(objRegExp, '$2');
			}
			
	return strValue;
	}


function trimAll(strValue) 
	{
	var objRegExp = /^(\s*)$/;

		//check for all spaces
		if(objRegExp.test(strValue)) 
			{
			strValue = strValue.replace(objRegExp, '');
			if( strValue.length == 0)
				return strValue;
			}
	    
	//check for leading & trailing spaces
	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	if(objRegExp.test(strValue)) 
		{
		//remove leading and trailing whitespace characters
		strValue = strValue.replace(objRegExp, '$2');
		}
		
	return strValue;
	}

