
/**
 * Functions for validation of input fields.
 * Note: Header.jsp has convenience methods to use these functions!
 * 
 * Usage:
 * (1) Input Dollar Amount: <input id="dollarField" onKeyPress="return validateDollar(this, VAL_KEYPRESS);" onPaste="return validateDollar(this, VAL_KEYPRESS);" type=text>
 * (2) if (!validateDollar(form.dollarField, VAL_FIELD)) alert('That is not a dollar amount.');
 * (3) if (!validateDollar("1500.99", VAL_STRING)) alert('That is not a dollar amount.');
 */

/**
 * List of valid actions.
 */
var VAL_KEYPRESS	= 1;
var VAL_FIELD		= 2;
var VAL_STRING		= 3;

var _errorColor = "red";
var _normalColor = "black";

//This function added by GGB on 2009/10/28.
//Returns a URL query string parameter, given it's name.
function getParameter (parameterName ) 
{   
    //var queryString = window.top.location.search.substring(1);
    var queryString = window.location.href;
    
    // Add "=" to the parameter name (i.e. parameterName=value)   
    var parameterName = parameterName + "=";   
    if ( queryString.length > 0 ) 
    {      
        // Find the beginning of the string      
        begin = queryString.indexOf ( parameterName );      
        // If the parameter name is not found, skip it, otherwise return the value      
        if ( begin != -1 ) 
        {         
            // Add the length (integer) to the beginning         
            begin += parameterName.length;         
            // Multiple parameters are separated by the "&" sign         
            end = queryString.indexOf ( "&" , begin );      
            if ( end == -1 ) 
            {         
                end = queryString.length     
            }      
            
            // Return the string      
            return unescape ( queryString.substring ( begin, end ) );   
        }   
        
        // Return "null" if no parameter has been found
        //return "null";
        return null;
    }
}

//This function added by GGB on 02/25/2009 to support conversion to master pages.
function document_getElementById_Dynamic(sControlId)
{
    TargetControl = document.getElementById(sControlId);
    if (TargetControl == null)
    {
        //GGB.  Switch on 04/01/2009.
        //TargetControl = document.getElementById("_ctl0_CP1_" + sControlId);
        TargetControl = document.getElementById("ctl00_CP1_" + sControlId);
    }
    return TargetControl;
}

//This function added by GGB on 02/25/2009 to support conversion to master pages.
function document_getElementById_Dynamic_WUC(sControlId, sWebUserControlName)
{
    //Regular getElementById.
    TargetControl = document.getElementById(sControlId);
    if (TargetControl == null)
    {
        //Now try with Master Page ID and Content Page Id Prepended.
        //TargetControl = document.getElementById("_ctl0_CP1_" + sControlId);
        TargetControl = document.getElementById("ctl00_CP1_" + sControlId);
        if (TargetControl == null)
        {
            //Now try with WebUserControlID Prepended also.
            //TargetControl = document.getElementById("_ctl0_CP1_" + sWebUserControlName + "1_" + sControlId);
            TargetControl = document.getElementById("ctl00_CP1_" + sWebUserControlName + "1_" + sControlId);
            if (TargetControl == null)
            {
                //Finally, try with only WebUserControlID prepended.  This happens in the case where the control was dynamically created after a postback.
                TargetControl = document.getElementById(sWebUserControlName + "1_" + sControlId);
            }
        }
    }
    return TargetControl;
}

/**
 * Returns true if input contains only digits 0-9.
 * field = String or Field to validate (depends on action)
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 */
function validateNumber(field, action) 
{
	return validateRegex(field, /^-?[0-9]{0,10}$/g, action);
}
      
/**
 * Returns true if input is an integer.
 * field = String or Field to validate (depends on action)
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 */
function validateInteger(field, action) 
{
	return validateRegex(field, /^-?[0-9]{0,9}$/g, action);
}

/**
 * Returns true if input is a dollar amount.
 * field = String or Field to validate (depends on action)
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 */
function validateDollar(field, action) 
{
	return validateRegex(field, /^-?[0-9]{0,9}\.?[0-9]{0,2}$/g, action);
}

/**
 * Returns true if input is a decimal.
 * field = String or Field to validate (depends on action)
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 * maxPrecision = maximum number of decimal places.
 */
function validateDecimal(field, action, maxPrecision) 
{
	return validateRegex(field, new RegExp("^-?[0-9]{0,9}\\.?[0-9]{0,"+maxPrecision+"}$","g"), action);
}

/**
 * Returns true if input has no special characters.
 * field = String or Field to validate (depends on action)
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 */
function validateNoSpecialChars(field, action) 
{
	return validateRegex(field, /^[\n\ra-zA-Z0-9 \(\),\.@-]*$/g, action);
}

/**
 * Returns true if input only contains letters, dashes, or spaces.
 * field = String or Field to validate (depends on action)
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 */
function validateLettersOnly(field, action) 
{
	return validateRegex(field, /^[\n\ra-zA-Z \-]*$/g, action);
}

/**
 * Returns true if input is a valid zip code format.
 * field = String or Field to validate (depends on action)
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 */
function validateZip(field, action) 
{
	if (action == VAL_KEYPRESS) {
		return validateRegex(field, /^([0-9]{0,5}|[0-9]{5}-[0-9]{0,4}|[a-zA-Z]|[a-zA-Z][0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][a-zA-Z][0-9]|[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9])$/g, action);
	}
	else {
		return validateRegex(field, /^([0-9]{5}|[0-9]{5}-[0-9]{4}|[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9])$/g, action);
	}
}

/**
 * Returns true if input is a valid zip code format. (No +4 extension allowed).
 * field = String or Field to validate (depends on action)
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 */
function validateZipShort(field, action) 
{
	if (action == VAL_KEYPRESS) {
		return validateRegex(field, /^([0-9]{0,5}|[a-zA-Z]|[a-zA-Z][0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][a-zA-Z][0-9]|[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9])$/g, action);
	}
	else {
		return validateRegex(field, /^([0-9]{5}|[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9])$/g, action);
	}
}

/**
 * Returns true if input is a valid date format (mm/dd/yyyy, m/d/yyyy).
 * field = String or Field to validate (depends on action)
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 */
function validateDate(field, action) {
	var rc = true;

	if (action == VAL_KEYPRESS) {
		var checkAll = false;
		var keyCode = window.event.keyCode;
		if (keyCode) {
			if (13 == keyCode) return true;
			if (keyCode < 47 || keyCode > 57) rc = false;
			if ((field.value.charAt(field.value.length-1) == "/") && (keyCode == 47)) rc = false;
			if ((field.value.length == 0) && (keyCode == 47)) rc = false;
			if (!rc) {
				_setErrorField(field, action);
				return false;
			}
		}

		var sOldText = field.value;
		var objRange = document.selection.createRange();
		var sOldRange = objRange.text;


		if (keyCode) objRange.text = String.fromCharCode(keyCode); 
		else objRange.text = window.clipboardData.getData("Text");

		if (keyCode) objRange.moveStart('character', -1);
		var sNewText = field.value;
		objRange.text = sOldRange;

	} 
	else if (action == VAL_FIELD) {
		var checkAll = true;
		var sNewText = field.value;
	}
	else if (action == VAL_STRING) {
		var checkAll = true;
		var sNewText = field;
	}
	else {
		alert ("ERROR: Validate action not specified/invalid.");
		return false;
	}

	var s = sNewText.split("/");

	if (s[0]) {
		if (s[0].length > 2) {
			rc = false;
		}
		else {
			if (s[0] != "00" && s[0].substring(0,1) == "0") {
				s[0] = s[0].substring(1,1);

			}
			if (s[0]) rc = (s[0] > 0 && s[0] < 13);
		}
	}
		
	if (rc && s[1]) {
		if (s[1].length > 2) {
			rc = false;
		}
		else {
			if (s[1] != "00" && s[1].substring(0,1) == "0") {
				s[1] = s[1].substring(1,1);

			}
			if (s[1]) rc = (s[1] > 0 && s[1] < 32);
		}
	}

	if (rc && s[2]) {
		if (s[2].length > 4) {
			rc = false;
		}
		else {
			if (checkAll) rc = (s[2] >= 1995 && s[2] <= 2099);
			else rc = s[2] == "1" || s[2] == "2" || (s[2] >= 19 && s[2] <= 20) || (s[2] >= 199 && s[2] <= 209) || (s[2] >= 1995 && s[2] <= 2099);
		}

		if (sNewText.charAt(sNewText.length-1) == "/") rc = false;
	}

	if (checkAll && !s[2]) rc = false;
	if (!rc) _setErrorField(field, action);
	return rc;
}

function validateHours24(field, action) 
{
	if (action == VAL_KEYPRESS) {
        return validateRegex(field, /^(0[0-9]|1[0-9]|2[0-3]|[0-9])$/g, action);
    }
    else {
        return validateRegex(field, /^0[0-9]|1[0-9]|2[0-3]$/g, action);
    }
}

function validateHours12(field, action) 
{
	if (action == VAL_KEYPRESS) {
        return validateRegex(field, /^(0[1-9]|1[0-2]|[0-9])$/g, action);
    }
    else {
        return validateRegex(field, /^[1-9]|0[1-9]|1[0-2]$/g, action);
    }
}

function validateMinutes(field, action) 
{
	if (action == VAL_KEYPRESS) {
        return validateRegex(field, /^([0-5][0-9]|[0-9])$/g, action);
    }
    else {
        return validateRegex(field, /^[0-5][0-9]$/g, action);
    }
}

/**
 * Returns true if input is validated against the regular expression.
 * field = String or Field to validate (depends on action)
 * regex = regular expression to validate against
 * action = VAL_KEYPRESS, VAL_FIELD, VAL_STRING.
 */
function validateRegex(field, regex, action) 
{

	if (action == VAL_KEYPRESS) {
		var keyCode = window.event.keyCode;
		if (13 == keyCode) return true;

		var sOldText = field.value;
		var objRange = document.selection.createRange();
		var sOldRange = objRange.text;

		if (keyCode) {
			objRange.text = String.fromCharCode(keyCode); 
		}
		else objRange.text = window.clipboardData.getData("Text");

		if (keyCode) objRange.moveStart('character', -1);
		var sNewText = field.value;
		
		objRange.text = '';
	} 
	else if (action == VAL_FIELD) {
		var sNewText = field.value;
	}
	else if (action == VAL_STRING) {
		var sNewText = field;
	}
	else {
		alert ("ERROR: Validate action not specified/invalid.");
		return false;
	}

	var rc = regex.test(sNewText);
	if (!rc) _setErrorField(field, action);

	return rc;
}

/**
 * Sets the error "flash" color when invalid key pressed.
 * Default is "red".
 * color = flash color.
 */
function setErrorColor(color) 
{
	_errorColor = color;
}

function _setErrorField(field, action) 
{
	if (action == VAL_KEYPRESS) {
		var normalColor = field.style.color;
		if (!normalColor) normalColor = _normalColor;
		var sField = field.id;
		if (!sField) sField = field.name;
		if (sField && normalColor != _errorColor) {
			field.style.color = _errorColor;
			setTimeout("_clearErrorField('"+sField+"','"+normalColor+"');", 100);
		}
	}
}

function _clearErrorField(fieldId, normalColor) 
{
	var field = document.getElementById(fieldId);
	field.style.color = normalColor;
}

