//<!--

//----------------------------------------------
// DisplayErrorMessage()
//----------------------------------------------
function DisplayErrorMessage(strErrorHeader,arrErrors)
{
	// Variable Declaration
	var strErrorMessage = "";

	// Build error message pop-up alert box
	strErrorMessage = strErrorHeader + "\n";
	for (var i=0; i < arrErrors.length; i++)
	{
		strErrorMessage += "\n" + arrErrors[i];
	}

	alert(strErrorMessage);
}

//----------------------------------------------
// TrimTextInputs
//----------------------------------------------
function TrimTextInputs(oForm){
	// loop thru all form elements
	for (var i=0; i < oForm.elements.length;i++){
		if (oForm.elements[i].type == "text"){
			// Trim all leading and trailing spaces
			oForm.elements[i].value = AllTrim(oForm.elements[i].value);
		}
	}
}


//-------------------------------------------------------------------------
// IsDate()
//-------------------------------------------------------------------------
function IsDate(strDate){
	return CheckPattern("isDate", AllTrim(strDate));
}

//-------------------------------------------------------------------------
// MakeDate()
//-------------------------------------------------------------------------
function MakeDate(strDate)
{
	// Date 1
	var str1  = strDate;
	

	var mon1   = parseInt(Left(str1, InStr(str1, "/")),10);
	
	
	mon1 = parseInt(mon1 - 1);
	var dt1  = Mid(str1, ((InStr(str1, "/")+1)), 2);

	if (Left(dt1, 1) == 0)
	{
		dt1 = parseInt(Right(dt1,1),10);
	}
	else
	{
		dt1 = parseInt(dt1,10);
	}

	var yr1   = parseInt(Right(str1, 4),10);
	
	var date1 = new Date(yr1, mon1, dt1);
	
	return date1;
}

//-------------------------------------------------------------------------
// CompareDateTimes()
//-------------------------------------------------------------------------
function CompareDateTimes(strDate1, strTime1, strDate2, strTime2)
{
	if (CompareDates(strDate1, strDate2) == 0)
	{
		return CompareTimes(strTime1, strTime2);
	}
	else
	{
		return CompareDates(strDate1, strDate2);
	}
}

//-------------------------------------------------------------------------
// CompareDates()
//-------------------------------------------------------------------------
function CompareDates(strDate1, strDate2)
{
	// Dates
	var date1 = MakeDate(strDate1);
	var date2 = MakeDate(strDate2);
	
	//alert("Date 1:" + date1 + " Date 2:" + date2);

	if (date1 < date2)
	{
		return -1;
	}
	else if (date1 > date2)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//-------------------------------------------------------------------------
// CompareTimes()
//-------------------------------------------------------------------------
function CompareTimes(strTime1, strTime2)
{
	var time1  = parseInt(strTime1,10);
	var time2  = parseInt(strTime2,10);
	
	if (time2 > time1)
	{
		return -1;
	}
	else if (time2 < time1)
	{
		return 1;
	}
	else if (time2 == time1)
	{
		return 0;
	}
}

//------------------------------------------------------------------------
// IsValidEmail()
//------------------------------------------------------------------------
function IsValidEmail(strEmail)
{
	return CheckPattern("isEmail", AllTrim(strEmail));
}

//------------------------------------------------------------------------
// IsValidTime()
//------------------------------------------------------------------------
function IsValidTime(strTime)
{

	//return true;
	if (!(/^([01]?[0-9]|[2][0-3])[0-5][0-9]$/.test(strTime)))
	{
		return false;
	}
	else
	{
		return true;
	}

}

//------------------------------------------------------------------------
// IsValidPhone()
//------------------------------------------------------------------------
function IsValidPhone(strPhone)
{
	return CheckPattern("isUSPhone", AllTrim(strPhone));
}
//------------------------------------------------------------------------
// IsValidFax()
//------------------------------------------------------------------------
function IsValidFax(strFax)
{
	return CheckPattern("isUSPhone", AllTrim(strFax));
}

//------------------------------------------------------------------------
// LTrim()
//------------------------------------------------------------------------
function LTrim(strVal) {
	for (var i=0; (i<=strVal.length); i++){
		if (strVal.charAt(i)!=" "){
			break;
		}
	}
	return strVal.substr(i);
}

//------------------------------------------------------------------------
// RTrim()
//------------------------------------------------------------------------
function RTrim(strVal) {
	for (var i=(strVal.length-1); (i>=0); i--){
		if (strVal.charAt(i)!=" "){
			break;
		}
	}
	return strVal.substr(0,i+1);
}

//------------------------------------------------------------------------
// AllTrim()
//------------------------------------------------------------------------
function AllTrim(strVal) {
	return LTrim(RTrim(strVal));
}

//------------------------------------------------------------------------
// IsNumeric()
//------------------------------------------------------------------------
function IsNumeric(strVal)
{
	return CheckPattern("isInteger", AllTrim(strVal));
}

//------------------------------------------------------------------------
// IsPositiveNumber()
//------------------------------------------------------------------------
function IsPositiveNumber(strValue){
	var bDecimal = false;

	strValue = AllTrim(strValue);

	var strInput = strValue.toString();

	for (var i=0; i < strInput.length; i++){
		var strChar = strInput.charAt(i);
		if (strChar == "." && !bDecimal){
			bDecimal = true;
			continue;
		}
		if (strChar < "0" || strChar > "9"){
			return false;
		}
	}
	return true;
}

//------------------------------------------------------------------------
// IsPositiveInteger()
//------------------------------------------------------------------------
function IsPositiveInteger(strValue){

	return CheckPattern("isPositiveInteger", AllTrim(strValue));

}

//---------------------------------------------------
// GetDefaultValueForSelect()
//---------------------------------------------------
function GetDefaultValueForSelect(oSelect){
	for (i=0;i < oSelect.options.length;i++){
		if (oSelect.options[i].defaultSelected){
			return oSelect.options[i].value;
		}
	}
	return "";
}


//-------------------------------------------------------------------------
// TextContainsTag()
//-------------------------------------------------------------------------
function TextContainsTag(strText, strTagName){

	//***********************
	// variable Declaration
	//***********************
	//regexp = new RegExp ("<" + strTagName + "[^>]*>(.|\n)*<[^<]*" + strTagName + ">", "gi");
	regexp = new RegExp ("<" + strTagName + "[^>]*>", "gi");

	if (strText.match(regexp) == null){
		return false;
	}else{
		return true;
	}

}

//-------------------------------------------------------------------------
// IsCheckboxGroupSelected()
//-------------------------------------------------------------------------
function IsCheckboxGroupSelected(oCheckbox)
{

	return IsRadioButtonSelected(oCheckbox);
}

//-------------------------------------------------------------------------
// IsRadioButtonSelected()
//-------------------------------------------------------------------------
function IsRadioButtonSelected(oRadio)
{

	for (var i = 0;  i < oRadio.length;  i++)
	{

		if (oRadio[i].checked)
		{
			return true;
		}
	}

	return false;
}

//-------------------------------------------------------------------------
// GetRadioButtonSelectedValue()
//-------------------------------------------------------------------------
function GetRadioButtonSelectedValue(oRadio)
{

	for (var i = 0;  i < oRadio.length;  i++)
	{

		if (oRadio[i].checked)
		{
			return oRadio[i].value;
		}
	}

	return null;
}

//-------------------------------------------------------------------------
// Left()
//-------------------------------------------------------------------------
function Left(str, n)
{
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}

//-------------------------------------------------------------------------
// Right()
//-------------------------------------------------------------------------
function Right(str, n)
{
	if (n <= 0)
	   return "";
	else if (n > String(str).length)
	   return str;
	else
	{
	   var iLen = String(str).length;
	   return String(str).substring(iLen, iLen - n);
	}
}

//-------------------------------------------------------------------------
// Mid()
//-------------------------------------------------------------------------
function Mid(str, start, len)
{
	if (start < 0 || len < 0)
		return "";

	var iEnd, iLen = String(str).length;

	if (start + len > iLen)
		iEnd = iLen;
	else
		iEnd = start + len;

	return String(str).substring(start,iEnd);
}

//-------------------------------------------------------------------------
// Len()
//-------------------------------------------------------------------------
function Len(str)
{
	return String(str).length;
}

//-------------------------------------------------------------------------
// InStr()
//-------------------------------------------------------------------------
function InStr(strSearch, charSearchFor)
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}

//-------------------------------------------------------------------------
// ctlHasSelection()
//-------------------------------------------------------------------------
	function ctlHasSelection(id_of_select) {
	// ctlHasSelection() checks to see if a control has any values selected
	// returns true or false

		var options_select_object = document.getElementById(id_of_select).options;
		var toReturn = false;

		for (var i = 0; i < options_select_object.length; i++) {
   			if(options_select_object[i].selected) {
				toReturn = true;
				break;
			}
		}
		return toReturn;
	}

//-->
