//////////////////////////////////////////////////////////////////////////
function valEmail(objSource)
//////////////////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////////////////
{
	var RegExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	var strEmail = objSource.value
	if(!RegExp.test(strEmail))
	{
		alert("Email address must be valid.");
		objSource.focus();
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////
function valCombo( blnBlank, objSource, strName )
//////////////////////////////////////////////////////////////////////////
//
//  blnBlank = Tells whether the field can be blank or not.
//				0 = Field must be field in.
//				1 = Field can be filled in or left blank.
//
//  objSource = form object that holds the date in question.
//
//	strName = The name of the field to be used in Error messages 
//
//////////////////////////////////////////////////////////////////////////
{
	var strValue = getComboSelectedValue(objSource)
	var strFirstChar = strName.charAt(0)
	var strIdentifier
	var intSelIdx = objSource.selectedIndex
	
	if(blnBlank==0)
	{
		if( objSource[intSelIdx].value=="")
		{
			if(isVowel(strFirstChar))
			{
				strIdentifier = " an "
			}
			else
			{
				strIdentifier = " a "
			}
			alert( "Please select" + strIdentifier + strName + " from the drop down list!" );		
			objSource.focus();
			return false;
		}
		return true;
	}
	return true;
}


/////////////////////////////////////////////////////////////////////////
function valBegDate( blnBlank, objSource, strName )
/////////////////////////////////////////////////////////////////////////
//
//  blnBlank = Tells whether the field can be blank or not.
//				0 = Field must be field in.
//				1 = Field can be filled in or left blank.
//
//  objSource = form object that holds the date in question.
//
//	strName = The name of the field to be used in Error messages 
//
//////////////////////////////////////////////////////////////////////////
{
	var src = objSource.value
	var strName = capitalizeFirst( strName );
		
	if( blnBlank == 0 )
	{
		if( isEmpty(src) )
		{
			alert( strName + " cannot be blank!" );
			objSource.focus();
			return false;
		}
	}
		
	if(!isEmpty(src) )
	{
		if(!isValidDate(src, strName) )
		{
			objSource.focus();
			return false;
		}
		return true;	
	}
	return true;	
}

/////////////////////////////////////////////////////////////////////////
function valEndDate( objSourceBeg, objSourceEnd, strName )
/////////////////////////////////////////////////////////////////////////
//
//  objSourceBeg = Form object that holds the Beginning date in question.
//
//  objSourceEnd = Form object that holds the Ending date in question.
//
//	strName = The name of the field to be used in Error messages 
//
//////////////////////////////////////////////////////////////////////////
{
	var srcBeg = objSourceBeg.value
	var srcEnd = objSourceEnd.value
	var strName = capitalizeFirst( strName );
		
	if( isEmpty(srcEnd) )
	{
		alert( strName + " cannot be blank!" );
		objSourceEnd.focus();
		return false;
	}
	else
	{
		if(!isValidDate(srcEnd, strName) )
		{
			objSourceEnd.focus();
			return false;
		}
		else
		{
			var dtmBeg = new Date(srcBeg);
			var dtmEnd = new Date(srcEnd);
			
			if( dtmBeg > dtmEnd)
			{
				alert( "Ending Date cannot be before Beginning Date!" );
				objSourceEnd.focus();
				return false;
			}
			return true;
		}
		return true;
	}
}

/////////////////////////////////////////////////////////////////////////
function valNumber( blnBlank, objSource, strName )
/////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////
{
	var src = objSource.value
	var strName = capitalizeFirst( strName );
	
	if( blnBlank == 0 )
	{
		if( isEmpty(src) )
		{
			alert( strName + " cannot be blank!" );
			objSource.focus();
			return false;
		}
	}
	if(!isEmpty(src) ) 
	{
		if(!isNumber(src) )
		{
			alert( strName + " must be a valid number!" );
			objSource.focus();
			return false;
		}
		return true;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////
function valCurrency( blnBlank,objSource, strName )
/////////////////////////////////////////////////////////////////////////
//
//  blnBlank = Tells whether the field can be blank or not.
//				0 = Field must be field in.
//				1 = Field can be filled in or left blank.
//
//  objSource = form object that holds the date in question.
//
//	strName = The name of the field to be used in Error messages 
//
/////////////////////////////////////////////////////////////////////////
{
	var src = objSource.value
	var strCurrency
	var nIndex
	var strName = capitalizeFirst( strName );
		
	if(blnBlank==0)
	{
		if( isEmpty(src) )
		{
			alert( strName + " cannot be empty!" )
			objSource.focus();
			return false;
		}
	}
	
	if(!isEmpty(src) )
	{
		if(src.charAt(0) == '.')
		{
			src = "0" + src
		}
		
		for (i = 0; i < src.length; i++)
		{
			c = src.charAt(i)
		
			if( true == isDigit( c ))
			{
				nIndex = i
				break
			}
		}
	
		strCurrency = src.substring(nIndex)
	
		if(!isFloat(strCurrency) )
		{
			alert( strName + " must be a valid number!");
			objSource.focus();
			return false;
		}
		objSource.value = strCurrency
		return true;
		
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////
function valCheckBox( blnBlank, intMinCheck, objSource, strName )
/////////////////////////////////////////////////////////////////////////
//
//  blnBlank = Tells whether the field can be blank or not.
//				0 = Field must be field in.
//				1 = Field can be filled in or left blank.
//
//  objSource = form object that holds the date in question.
//
//	strName = The name of the field to be used in Error messages 
//
/////////////////////////////////////////////////////////////////////////
{
	var intLength
	var intCounter
	var intCount = 0
	var strFirstChar = strName.charAt(0)
	var strIdentifier
	var strPlural = ""
	
	if(blnBlank==0)
	{
		if(intMinCheck > 1)
		{
			strPlural = "s"
		} 
		strIdentifier = " at least " + intMinCheck + " "
	}
	
	intLength = (objSource.length);
				
	for(intCounter=0;intCounter<intLength;intCounter++)
	{
		if(objSource[intCounter].checked)
		{
			intCount++
		}
	}
		
	if((intMinCheck>0)&&(intCount<intMinCheck))
	{
		alert( "Please select" + strIdentifier + strName + strPlural )
		objSource[0].focus();
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////
function valText( blnBlank, objSource, strName )
/////////////////////////////////////////////////////////////////////////
//
//  blnBlank = Tells whether the field can be blank or not.
//				0 = Field must be field in.
//				1 = Field can be filled in or left blank.
//
//  objSource = form object that holds the string in question.
//
//	strName = The name of the field to be used in Error messages 
//
/////////////////////////////////////////////////////////////////////////
{
	var src = objSource.value
	var strName = capitalizeFirst( strName );
	
	src = stripChar(src,"'");
	src = src.toUpperCase();
		
	if(blnBlank==0)
	{	
		if( isEmpty( src ) )
		{
			alert( strName + " cannot be blank!" );
			objSource.focus();
			return false;
		}
	}
	objSource.value = src;
	return true;
}

/////////////////////////////////////////////////////////////////////////
function valRadio( objSource, strName )
/////////////////////////////////////////////////////////////////////////
//
//  objSource = form object that holds the string in question.
//
//	strName = The name of the field to be used in Error messages 
//
/////////////////////////////////////////////////////////////////////////
{
	var bChecked = false;
	var nIndex;
	var strIndentifier;
	var strFirstChar = strName.charAt(0);
		
	for ( nIndex = 0; nIndex < objSource.length; nIndex++)
    {   
		if ( objSource[nIndex].checked) 
		{
			bChecked = true;
			break;
		}
    }
    
    if(!bChecked)
    {
		if(isVowel(strFirstChar))
			{
				strIdentifier = " an "
			}
			else
			{
				strIdentifier = " a "
			}
			alert( "Please select" + strIdentifier + strName );
			objSource[0].focus();
			return false;
    }
    return true;

	

}

/////////////////////////////////////////////////////////////////////////
function valPercentage( objForm, strPreFix )
/////////////////////////////////////////////////////////////////////////
//
//  objForm = form object that holds the string in question.
//
//	strName = The name of the field to be used in Error messages 
//
/////////////////////////////////////////////////////////////////////////
{
	var objElement
	var strName
	var intTotal = 0

	for(i=0;i<objForm.length;i++)
	{
		objElement = objForm.elements[i]
		strName = objElement.name
		strValue = objElement.value
		if(strName.substr(0,3)==strPreFix)
		{
			if(!valNumber(0,objElement,"Product percentage" ))
			{
				return false;
			}
			intTotal += parseInt(objElement.value);
		}
	}
	if(intTotal!=parseInt(100) )
	{
		alert( "The percentage totals do not add up to 100%.  Please adjust.");
		return false
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////
function valPercentage2( objElement, strName )
/////////////////////////////////////////////////////////////////////////
//
//  objElement = form object that holds the string in question.
//
//  strName = The name of the field to be used in Error messages 
//
/////////////////////////////////////////////////////////////////////////
{
	var intValue = parseInt(objElement.value)
	
	if( valNumber(0,objElement,"HP Reimbursement") )
	{
		if(intValue>100)
		{
			alert( strName + " cannot be greater than 100%" );
			objElement.focus();
			return false;
		}
		return true;
	}
	return false;

}

/////////////////////////////////////////////////////////////////////////
function valTotal( objForm, strPreFix, numTotal )
/////////////////////////////////////////////////////////////////////////
//
//  objForm = form object that holds the string in question.
//
//	strName = The name of the field to be used in Error messages 
//
//	numTotal = Total amount that fields should add up to
//
/////////////////////////////////////////////////////////////////////////
{
	var objElement
	var strName
	var intTotal = 0

	for(i=0;i<objForm.length;i++)
	{
		objElement = objForm.elements[i]
		strName = objElement.name
		strValue = objElement.value
		if(strName.substr(0,3)==strPreFix)
		{
			if(!valNumber(0,objElement,"Product percentage" ))
			{
				return false;
			}
			intTotal += parseInt(objElement.value);
		}
	}
	if(intTotal!=parseInt(numTotal) )
	{
		alert( "The amounts do not add up to $" + numTotal + ".  Please adjust.");
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////
// Begin Support Functions  //////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

var decimalPointDelimiter = "."

function upshiftThis( oField )
{
	// oField should be input type of text or textbox.
	// Primary use is input onchange.
	//
	// Upshifts the value of a text field.
	
	oField.value = oField.value.toUpperCase();
}

function isVowel(strChar)
{
	var strUChar = strChar.toUpperCase();
	
	if( strUChar == "A" || 
	    strUChar == "E" || 
	    strUChar == "I" || 
	    strUChar == "O" )
	{
		return true;
	}
	return false;
}

function isDigit(c)
{   
	return ((c >= "0") && (c <= "9"))
}

function isEmpty(s)
{   
	return ( (s == null) || (s.length == 0))
}

function isNumber( sValue )
{
	var c;

	for( nIndex = 0; nIndex < sValue.length; nIndex++ )
	{
		c = sValue.charAt( nIndex );

		if( !isDigit( c ) )
		{	
			return false;
		}
	}

	return true;

}

function getRadioValue( obj )
{
	var bChecked = false;
	var nIndex;
	var sRadio;
		
	for ( nIndex = 0; nIndex < obj.length; nIndex++)
    {   
		if ( obj[nIndex].checked) 
		{
			bChecked = true;
			break;
		}
    }

	if ( bChecked == true )
		sRadio = obj[nIndex].value;
	else
		sRadio = "";

	return sRadio;

}

function checkRadio( obj, sCheck )
{
	for( nIndex = 0; nIndex < obj.length; nIndex++ )
	{
		if ( obj[nIndex].value == sCheck )
			obj[nIndex].checked = true;
	}

}

function getComboSelectedText( obj )
{
	var nSelected;
	var sCombo;

	nSelected = obj.selectedIndex;
	if (nSelected != -1 )
		sCombo = obj.options[nSelected].text;
	return sCombo;

}

function getComboSelectedValue( obj )
{
	var nSelected;
	var sCombo;

	nSelected = obj.selectedIndex;
	if (nSelected != -1)
		sCombo = obj.options[nSelected].value;
	return sCombo;

}

function selectComboValue( obj, sValue )
{
	
	for( var nIndex = 0; nIndex < obj.options.length; nIndex++ )
	{

		if ( obj.options[nIndex].value == sValue )
		{
			obj.options[nIndex].selected = true;
			return true;
		}

	}

	return false;
}

function isBetween( nNum, nStart, nEnd )
{
	if( nNum >= nStart & nNum <= nEnd )
		return true;
	else
		return false;
}

var nArrMonth = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
var sArrMonth = new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
function isValidDate( s, sDateName )
{
	var nIndex1;
	var nIndex2;
	var sMonth;
	var sDay;
	var sYear;
	var nMonth;
	var nDay;
	var nYear;
	var sAlert;
	var sOut;

	// Look for dashes
	nIndex1 = s.indexOf( "-" );

	if( nIndex1 == -1 )
	{
		// No dashes, look for slashes
		nIndex1 = s.indexOf( "/" );
		if( nIndex1 == -1 )
		{	
			alert( "You have entered an invalid date format in " + sDateName + ".\nPlease use MM-DD-YY, MM/DD/YY, MM-DD-YYYY, etc." );
			return false;
		}
		else
		{
			nIndex2 = s.indexOf( "/", nIndex1 + 1 );
			if( nIndex2 == -1 )
			{
				alert( "You have entered an invalid date format in " + sDateName + ".\nPlease use MM-DD-YY, MM/DD/YY, MM-DD-YYYY, etc." );
				return false; 
			}
		}
	}
	else
	{
		nIndex2 = s.indexOf( "-", nIndex1 + 1 );
		if( nIndex2 == -1 )
		{
			alert( "You have entered an invalid date format in " + sDateName + ".\nPlease use MM-DD-YY, MM/DD/YY, MM-DD-YYYY, etc." );
			return false;
		}
	}


	sMonth = s.substring( 0, nIndex1 );
	sDay = s.substring( nIndex1 + 1, nIndex2 );
	sYear = s.substring( nIndex2 + 1 );

	// Verify that we have only numbers in the date
	for( i=0; i < sMonth.length; i++ )
	{
		var c = sMonth.charAt( i );
		if( false == isDigit( c ) )
		{
			alert( "Please use only valid numbers for the month in " + sDateName + "." );
			return false;
		}

	}

	for( i=0; i < sDay.length; i++ )
	{
		var c = sDay.charAt( i );
		if( !isDigit( c ) )
		{
			alert( "Please use only valid numbers for the day in " + sDateName + "." );
			return false;
		}
	}
	
	for( i=0; i < sYear.length; i++ )
	{
		var c = sYear.charAt( i );
		if( !isDigit( c ) )
		{
			alert( "Please use only valid numbers for the year in " + sDateName + "." );
			return false;
		}
	}

	nMonth = parseInt( sMonth, 10 );
	nDay = parseInt( sDay, 10 );
	nYear = parseInt( sYear, 10 );

	if( !isBetween( nMonth, 1, 12 ) )	
	{
		alert( "You have entered an invalid month in " + sDateName + ".\nPlease enter a month between 1 and 12." );
		return false;
	}

	// Special case for leap year
	if( nDay == 29 && nMonth == 2 )
	{
		var nResult = nYear / 4;
		if( nResult != Math.round( nResult ) )
		{
			sOut = "The year ";
			sOut += nYear;
			sOut += " is not a valid leap year, therefore there are only 28 days in February.";
			alert( sOut );
			return false;
		}
	}
	else
	{
		if( !isBetween( nDay, 1, nArrMonth[nMonth - 1] ) )
		{
			sOut = "There are only ";
			sOut += nArrMonth[nMonth-1];
			sOut += " days in ";
			sOut += sArrMonth[nMonth-1];
			sOut += "\nPlease enter a valid day between 1 and ";
			sOut += nArrMonth[nMonth-1];
			sOut += " in " + sDateName + ".";
	
			alert( sOut );
			return false;
		}
	}

	if(	!( isBetween( nYear, 95, 99 ) | isBetween( nYear, 1995, 2050 ) ) )
	{
		if( isBetween( nYear, 0, 20 ) )
		{
			alert( "Please use 4 digit years for 2000 and up in " + sDateName + "." );
			return false;
		}
		alert( "You have entered an invalid year in " + sDateName + "." );
		return false;
	}

	return true;

}

function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       return true;

    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function capitalizeFirst( strName )
{
var strFC = strName.charAt(0)
var strFirst = strFC.toUpperCase();

	if(strName.length > 0)
	{
		return strFirst + strName.substr( 1 );
	}
}

function stripChar( sString, sChar )
{
	var c;
	var nIndex;

	nIndex = sString.indexOf( sChar );
	
	while( nIndex != -1 )
	{
		sString = sString.substring( 0, nIndex ) + sString.substring( nIndex + 1 );
		nIndex = sString.indexOf( sChar );
	}

	return sString;
	
}
