// whitespace characters
var whitespace = " \t\n\r";
var digits = "0123456789";


//The control passed in requires a value
//Return true if value is present
function RequiresValue(strCTLName, strMSG)
{
	var ctl;
	ctl = eval("document.reg." + strCTLName)
	if (isWhitespace(ctl.value)==true)
	{
		alert(strMSG);
		ctl.value = "";
		ctl.focus();
		return false;
	}
	return true;
}

//RadioButton requires a selection
function RadioRequiresSelection(strCTLName, strMSG)
{
	var ctl;
	ctl = eval("document.reg." + strCTLName)
	if (getRadioButtonValue(ctl)== "")
	{
		alert(strMSG);
		ctl[0].focus();
		return false;
	}
	return true;
}

// Get checked value from radio button.
function getRadioButtonValue (radio)
{   
	var strReturn="";
		for (var i = 0; i < radio.length; i++)
    {   if (radio[i].checked) 
				{ 
					strReturn = radio[i].value;
					break;
				}
    }
    return strReturn;
}

// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


// reformat (TARGETSTRING, STRING, INTEGER, STRING, INTEGER ... )       
//
// Handy function for arbitrarily inserting formatting characters
// or delimiters of various kinds within TARGETSTRING.
//
// reformat takes one named argument, a string s, and any number
// of other arguments.  The other arguments must be integers or
// strings.  These other arguments specify how string s is to be
// reformatted and how and where other strings are to be inserted
// into it.
//
// reformat processes the other arguments in order one by one.
// * If the argument is an integer, reformat appends that number 
//   of sequential characters from s to the resultString.
// * If the argument is a string, reformat appends the string
//   to the resultString.
//
// NOTE: The first argument after TARGETSTRING must be a string.
// (It can be empty.)  The second argument must be an integer.
// Thereafter, integers and strings must alternate.  This is to
// provide backward compatibility to Navigator 2.0.2 JavaScript
// by avoiding use of the typeof operator.
//
// It is the caller's responsibility to make sure that we do not
// try to copy more characters from s than s.length.
//
// EXAMPLES:
//
// * To reformat a 10-digit U.S. phone number from "1234567890"
//   to "(123) 456-7890" make this function call:
//   reformat("1234567890", "(", 3, ") ", 3, "-", 4)
//
// * To reformat a 9-digit U.S. Social Security number from
//   "123456789" to "123-45-6789" make this function call:
//   reformat("123456789", "", 3, "-", 2, "-", 4)
//
// HINT:
//
// If you have a string which is already delimited in one way
// (example: a phone number delimited with spaces as "123 456 7890")
// and you want to delimit it in another way using function reformat,
// call function stripCharsNotInBag to remove the unwanted 
// characters, THEN call function reformat to delimit as desired.
//
// EXAMPLE:
//
// reformat (stripCharsNotInBag ("123 456 7890", digits),
//           "(", 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;
}

// Removes all characters which do NOT appear in string bag 
// from string s.
function stripCharsNotInBag (s, bag)
{   
	var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to 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 FormatPhone(strCTLName)
{
	var ctl;
	var strNumber="";
	
	ctl = eval("document." + strCTLName);
	if (ctl.value == "") return;
	strNumber = stripCharsNotInBag (ctl.value, digits);
	//alert(strNumber);
	
	if (strNumber.length != 10)
	{
		alert("Please enter your area code and phone number.");
		ctl.value = "";
		ctl.focus();
		return;
	}
	ctl.value = reformat(strNumber,  "",3, "-", 3, "-", 4)
	return;
}

function FormatZip(strCTLName)
{
	var ctl;
	var strNumber="";
	
	ctl = eval("document." + strCTLName);
	if (ctl.value == "") return;
	strNumber = stripCharsNotInBag (ctl.value, digits);

	if (strNumber.length == 9)
	{
		strNumber = reformat(strNumber, "", 5, "-", 4);
		ctl.value = strNumber
		return;
	}	
	if ((strNumber.length == 5))
	{
		ctl.value = strNumber
		return;
	}
	alert("Please enter a 5 or 9 digit zip code.");
	ctl.value = "";
	ctl.focus();
	return;
}

function FormatDate(strCTLName)
{
	var ctl;
	var strNumber="";
	
	ctl = eval("document." + strCTLName);
	if (ctl.value == "") return;
	strNumber = stripCharsNotInBag (ctl.value, digits);

	if (strNumber.length == 8)
	{
		strNumber = reformat(strNumber, "", 2, "/", 2, "/", "4");
		ctl.value = strNumber
		return;
	}	
	if ((strNumber.length == 6))
	{
		strNumber = reformat(strNumber, "", 2, "/", 2, "/", "2");
		ctl.value = strNumber
		return;
	}
	alert("Please enter a correct date.");
	ctl.value = "";
	ctl.focus();
	return;
}


function FormatDate2(strCTLName)
{
	var ctl;
	var strNumber="";
	
	ctl = eval("document." + strCTLName);
	if (ctl.value == "") return;
	strNumber = stripCharsNotInBag (ctl.value, digits);

	if (strNumber.length == 8)
	{
		strNumber = strNumber.substring(0,2) + "/" + strNumber.substring(2,4) + "/" + strNumber.substring(4,8);
		ctl.value = strNumber
		return;
	}	
	if ((strNumber.length == 10))
	{
		strNumber = strNumber.substring(0,2) + "/" + strNumber.substring(3,5) + "/" + strNumber.substring(6,10);
		ctl.value = strNumber
		return;
	}
	alert("Please enter the date in the format 'MM/DD/YYYY'.");
	ctl.value = "";
	ctl.focus();
	return;
}


function FormatSSN(strCTLName)
{
	var ctl;
	var strNumber="";
	
	ctl = eval("document." + strCTLName);
	if (ctl.value == "") return;
	strNumber = stripCharsNotInBag (ctl.value, digits);

	if (strNumber.length == 9)
	{

		strNumber = reformat(strNumber, "", 3, "-", 2, "-", "4");
		ctl.value = strNumber
		return;
	}	
	alert("Please enter a 9 digit SSN or Tax ID.");
	ctl.value = "";
	ctl.focus();
	return;
}


function TextAreaMaxSize(strCTLName, intSize, strFieldName)
{
	var ctl;
	var strValue="";	
	ctl = eval("document.reg." + strCTLName);
	strValue = ctl.value;
	if (strValue == "") return;
	if (strValue.length > intSize)
	{
		alert(strFieldName + " is limited to " + intSize + " characters.");
		ctl.value = strValue.slice(0, intSize);
		ctl.focus();
		return;
	}
	return;
}

function isInteger (s)
{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // 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 (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"));
}

function CheckAge(strCTLName)
{
	var ctl;
	ctl = eval("document.reg." + strCTLName);
	if (isInteger(ctl.value, true) == false)
	{
		alert("Please only enter numeric values for age.");
		ctl.value = "";
		ctl.focus();
		return;
	}
	return;
}


