function validateRadio(fld,label,sRaise)
{
	var msgError = '';
	var isValid = true;
	var indexNum = -1;
	var intLength;
 
	if (isNaN(fld.length))
	{
		if (fld.checked)
  		{ indexNum = 0; }
 	}
 	else
	{ 
		for (i=0; i<=fld.length-1; i++)
		{
			if (fld[i].checked)
			{ indexNum = i; }
		}
	}
 
	 if (indexNum == -1)
	 { isValid = false; }
 
	if (!isValid)
	{
		if (String(sRaise)!='false')
		{
			msgError = raiseError(4,label);
			alert(msgError);
		}
	}
 
	return(isValid);
}

function validateCombo (fld, label)
{
	if(typeof fld != "undefined") {
		if (fld.selectedIndex == 0 || fld.selectedIndex == -1)
		{
			alert ("Please select a value for '" + label + "'.");
			fld.focus();
			return false;
		}
	}
	return true;
}

function validateText(fld,label,sRaise)
{
	var msgError = '';
	if(trim(fld.value) == '' || !(/^[a-zA-Z0-9'-.&@ ]+$/i).test(fld.value))
	{
		if (String(sRaise)!='false')
		{		
			msgError = raiseError(0,label);
			alert(msgError);
			fld.select();
			fld.focus();
		}
		return(false);
	}
	return(true);
}

function validateEmail(fld,label,sRaise){
	var msgError = '';
	if(!(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i).test(fld.value))
	{
		if (String(sRaise)!='false')
		{		
			msgError = raiseError(8,label);
			alert(msgError);
			fld.select();
			fld.focus();
		}
		return(false);
	}
	return(true);	
}

function getRadioValue(fld)
{
	for(i=0; i<=fld.length - 1; i++)
	{
		if(fld[i].checked)
		{
			 return (fld[i].value);
		}	
	}
	return ('');
}

function isOptionChecked(fld, sOption)
{
	for(i=0; i<=fld.length - 1; i++)
	{
		if(String(fld[i].value) == String(sOption))
		{
			if (fld[i].checked)
			{ return (true); }
			else
			{ return (false); }			
		}	
	}
	return (false);
}

function raiseError(elType,label,len)
{
	var errMsg = '';
	var errMsg_Txt = 'Please enter a valid value';
	var errMsg_Cbo = 'Please select an option';
	var errMsg_DteBlank = 'Please enter a date value';
	var errMsg_DteInvalid = 'Please enter a valid (current) date in the format \'MM/DD/YYYY\'';
	var errMsg_RadInvalid	= 'Please select a value';
	var errMsg_PhInvalid	= 'Please enter a valid phone number';
	var errMsg_TimeInvalid	= 'Please enter a valid time in the format hh:mm';
	var errMsg_ZipInvalid	= 'Please enter a valid zip code';
	var errMsg_NumInvalid	= 'Please enter a valid number';
	var errMsg_EmailInvalid = 'Please enter a valid email address';
	var errMsg_LengthInvalid = 'Field length must be '+len+' characters';
	var errMsg_CurrencyInvalid = 'Please enter a valid currency';
	
	switch(elType)
	{
		case 0 : 
			errMsg = errMsg_Txt; break;
		case 1 : 
			errMsg = errMsg_Cbo; break;
		case 2 : 
			errMsg = errMsg_DteBlank; break;
		case 3 : 
			errMsg = errMsg_DteInvalid; break;
		case 4 : 
			errMsg = errMsg_RadInvalid; break;
		case 5 : 
			errMsg = errMsg_PhInvalid; break;
		case 6 : 
			errMsg = errMsg_ZipInvalid; break;
		case 7 : 
			errMsg = errMsg_NumInvalid; break;
		case 8: 
			errMsg = errMsg_EmailInvalid; break;
		case 9: 
			errMsg = errMsg_LengthInvalid; break;
		case 10: 
			errMsg = errMsg_TimeInvalid; break;
		case 11: 
			errMsg = errMsg_CurrencyInvalid; break;
		default : 
			errMsg = 'Unknown Error';
	}
	
	errMsg += (label != '') ? ' for ' + label + '.' : '.'
	switch(elType)
	{
		case 11: 
			errMsg += " No commas are allowed."; break;
	}
	return(errMsg);
}

/*-----------------------------------------------------------------------
 validate types
-----------------------------------------------------------------------*/
function validateZipCode(fld,label,sRaise)
{
	var msgError = '';
  var zipPat = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  var matchArray = fld.value.match(zipPat); // is the format ok?

  if (matchArray == null) {
		msgError = raiseError(6,label);
		alert(msgError);
		fld.select();
		fld.focus();
		return(false);
  }
	return(true);
}

function validatePhone (fld,label,raise)
{  
	var msgError = '';
	var phonePat = /((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/;
	var matchArray = fld.value.match(phonePat); // is the format ok?

    if (matchArray == null)
    {
		msgError = raiseError(5,label);
		alert(msgError);
		fld.select();
		fld.focus();
		return(false);
	}
    return(true);
}

function validateTime (fld,label,raise)
{  
	var msgError = '';

    var timePat = /^\d{1,2}\:\d{2}$/;
    var matchArray = fld.value.match(timePat); // is the format ok?

    if (matchArray == null) {
		msgError = raiseError(10,label);
		alert(msgError);
		fld.select();
		fld.focus();
		return(false);
    }
	return (true);
}

function validateCurrency(fld,label,raise){
	var msgError = '';
	
	var currencyPat = /^\d*\.?(\d{1,2})?$/;
	var matchArray = fld.value.match(currencyPat);
	
	if(matchArray == null){
		msgError = raiseError(11,label);
		alert(msgError);
		fld.select();
		fld.focus();
		return(false);
	}
	return(true);

}

function validateInteger (fld,label,raise)
{  
	var msgError = '';

    var integerPattern = /^\d+$/;

    var matchArray = fld.value.match(integerPattern); // is the format ok?

    if (matchArray == null) {
		msgError = raiseError(7,label);
		alert(msgError);
		fld.select();
		fld.focus();
		return(false);
    }
	return (true);
}

function validateIntegerLength (fld, label, length)
{  
    if (fld.value.length != length) {
		alert("Please enter a " + label + " that is " + length + " characters long.");
		fld.select();
		fld.focus();
		return(false);
    }
	return (true);
}

/*-----------------------------------------------------------------------
 supporting functions
-----------------------------------------------------------------------*/
function stripChars (s, bag)
{   
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isUSPhoneNumber(s)
{   
    return (isInteger(s) && s.length == 10)
}

// Check whether string s is empty.
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

function isInteger (s)
{   
	var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}

function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"))
}

function reformatUSPhone (USPhone)
{   
	return (reformat (USPhone, "", 3, "-", 3, "-", 4))
}

function reformat (s)
{   
	var arg;
    var sPos = 0;
    var resultString = "";
    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

//is there a value in the field
function isValue( val ) {
	var temp = val.replace(/^\s+/g, '').replace(/\s+$/g, '');
	if( temp.length == 0 ) {
		return false;
	} else {
		return true;
	}
}


//is string a date?
function isDate(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        return false;
    }

    if (day < 1 || day > 31) {
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            return false;
        }
    }
    return true; // date is valid
}

// compares two dates
// returns -1 for date1 after date2
// 0 if dates are the same
// 1 if date1 before date2
function compareDates( date1, date2 ) {
	//break into values
	if( date1.indexOf("/")!=-1 ) {
		startSep = "/";
	} else {
		startSep = "-";
	}
	startVal = date1.split(startSep);
	
	if( date2.indexOf("/")!=-1 ) {
		endSep = "/";
	} else {
		endSep = "-";
	}
	endVal = date2.split(endSep);
	//creates a number in the format yyyymmdd to compare dates (i.e., 20020603)
	startNum = (parseInt((startVal[2] * 10000)) + parseInt((startVal[0] * 100)) + parseInt((startVal[1])));
	endNum = (parseInt((endVal[2] * 10000)) + parseInt((endVal[0] * 100)) + parseInt((endVal[1])));
	
	if( endNum > startNum ) {
		return 1;		
	} else if( endNum == startNum ) {
		return 0;
	} else {
		return -1;
	}
}

function trim(sValue)
{
	var r=/\b(.*)\b/.exec(sValue);
	return (r==null)?"":r[1];
}
