// 
// Validate.js
//

var LastName = "Enter a last name using the letters A-Z or a-z, digits 0-9, an underscore ( _ ), a hyphen ( - ), or an apostrophe (')."
var FirstName = "Enter a first name using the letters A-Z or a-z, digits 0-9, an underscore ( _ ), a hyphen ( - ), or an apostrophe (')."
var Email = "Enter a valid email address (in the form username@example.com)."

var NotValidName = "The name fields can contain only the letters A-Z, digits 0-9, an underscore ( _ ), or a hyphen ( - ).";
var NotValidAreaCode = "Enter a valid US area code (using 3 digits, 0 - 9).";
var NotValidExchangeCode = "Enter a valid exchange code (using 3 digits, 0 - 9).";
var NotValidUSPhone = "Enter a valid US telephone number (using up to 15 digits, 0 - 9, and the characters #, *, or a comma).";
var NotValidUSPhoneExtension = "Enter a valid extension (using the digits 0 - 9, and the characters #, *, or a comma).";
var NotValidPhoneAccessPrefix = "Enter a valid telephone prefix (using the digits 0 - 9, or the characters #, *, or a comma).";
var NoDigitsToDial = "A telephone number or extension is required.";
var NoExtType = "Because you provided an extension, you must indicate whether the call will be answered by a receptionist or an automated phone system.";
var NoExtension = "Because you indicated that the call will be answered by a receptionist or an automated phone system, you must enter an extension.";

function FocusMyself()
{
	if (_version > 1.0)
		self.focus()
}

function IsInteger(oneChar)
{
 	return ((oneChar >= "0") && (oneChar <= "9"))
}

function IsValidLetter(oneChar)
{
	return (((oneChar >= "a") && (oneChar <= "z")) || ((oneChar >= "A") && (oneChar <= "Z")) )
}

function IsValidLetterOrInteger(oneChar)
{
	return (IsValidLetter(oneChar) || IsInteger(oneChar) || (oneChar == "'") || (oneChar == "_") || (oneChar == "-"));
}

function CheckValidIntegerString(str, reqLen)
{
	if (str == "")
		return false;
	if (str.length > reqLen || str.length < 1)
		return false;
	for (var i=0; i < str.length; i++) {
		var oneChar = str.charAt(i);
		if (!(IsInteger(oneChar)))
			return false;
	}
	return true;
}

function CheckValidCharacterString(str, reqLen)
{
	if (str == "")
		return false;
	if (str.length > reqLen || str.length < 1)
		return false;
	for (var i=0; i < str.length; i++) {
		var oneChar = str.charAt(i);
		if (!(IsValidLetter(oneChar)))
			return false;
	}
	return true;
}

function CheckValidString(str, reqLen)
{
	if (str == "")
		return false;
	if (str.length > reqLen || str.length < 1)
		return false;
	for (var i=0; i < str.length; i++) {
		var oneChar = str.charAt(i);
		if (!(IsValidLetterOrInteger(oneChar)))
			return false;
	}
	return true;
}

function CheckForValidName(theField)
{
	if (CheckValidString(theField.value, 16)) {
		return true;
	}
	else {
		alert(NotValidName);
		theField.value="";
		theField.focus();
		return false;
	}
}

// replace is supported in JavaScript 1.2
// Wrote a new function "StripCharsInBag(str, bag) which
// substitutes "replace".

function StripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not 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 RemoveWhiteSpaces(theField)
{
	return StripCharsInBag (theField.value, " ")
}


function IsEmpty(str)
{
	return (str.length == 0);
}

function CheckValidStringAnySize(str)
{
	for (var i=0; i < str.length; i++) {
		var oneChar = str.charAt(i);
		if (!(IsValidLetterOrInteger(oneChar) || oneChar == "_" || oneChar == "-"))
			return false;
	}
	return true;
}


function ValidateLastName(theField)
{
	var validStr = RemoveWhiteSpaces(theField);

	if(IsEmpty(validStr))
		return true;
	if (CheckValidStringAnySize(validStr)) {
		theField.value=validStr;
		return true;
	}
	else {
		alert(LastName);
		theField.value="";
		theField.focus();
		return false;
	}
}

function ValidateFirstName(theField)
{
	var validStr = RemoveWhiteSpaces(theField);

	if(IsEmpty(validStr))
		return true;
	if (CheckValidStringAnySize(validStr)) {
		theField.value=validStr;
		return true;
	}
	else {
		alert(FirstName);
		theField.value="";
		theField.focus();
		return false;
	}
}

function IsEmail (s)
{   
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@")) {
	i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != ".")) {
   	 i++;
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;

    return true;
}


function ValidateEmail(theField)
{
	if(IsEmpty(theField.value)) return true;

	if (IsEmail(theField.value))
		return true;
	else {
		alert(Email)
		theField.value="";
		theField.focus();
		return false;
	}
	return true;
}


function CheckForAreaCode(str)
{
	for (var i=0; i < str.length; i++) {
		var oneChar = str.charAt(i);
		if (!(IsInteger(oneChar)))
			return false;
	}
	return true;
}

function CheckForValidPhoneDialPad(str)
{
	for (var i=0; i < str.length; i++) {
		var oneChar = str.charAt(i);
		if (!(IsInteger(oneChar) || (oneChar == "*") || (oneChar == "#") || (oneChar == ",")))
			return false;
	}
	return true;
}

function CheckForExtension(str)
{
	for (var i=0; i < str.length; i++) {
		var oneChar = str.charAt(i);
		if (!(IsInteger(oneChar)))
			return false;
	}
	return true;
}

function MakeValidAreaCode(str)
{
	var newstr = StripCharsInBag(str, "+")
	newstr = StripCharsInBag(newstr, "-")
	newstr = StripCharsInBag(newstr, "(")
	newstr = StripCharsInBag(newstr, ")")
	newstr = StripCharsInBag(newstr, " ")
	return newstr;
}

function ValidateUSPhoneAreaCode(theField)
{
	if(IsEmpty(theField.value)) return true;

	var str=MakeValidAreaCode(theField.value);
	if (CheckForAreaCode(str)) {
		if (str.length != 3) {
			alert(NotValidAreaCode);
			theField.value="";
			theField.focus();
			return false;
		}
		theField.value=str;
		return true;
	}
	else {
		alert(NotValidAreaCode);
		theField.value="";
		theField.focus();
		return false;
	}
}

function ValidateUSPhoneNumber(theField)
{
	if(IsEmpty(theField.value)) return true;

	var str=MakeValidAreaCode(theField.value);
	if (CheckForValidPhoneDialPad(str)) {
		theField.value=str;
		return true;
	}
	else {
		alert(NotValidUSPhone);
		theField.value="";
		theField.focus();
		return false;
	}
}

function ValidatePhoneExtension(theField)
{
	if(IsEmpty(theField.value)) return true;

	var str=MakeValidAreaCode(theField.value);
	if (CheckForValidPhoneDialPad(str)) {
		theField.value=str;
		return true;
	}
	else {
		alert(NotValidUSPhoneExtension);
		theField.value="";
		theField.focus();
		return false;
	}
}

function ValidateCustomerInfo(lastName, firstName, isPSTNCall, phNumber, phExtension, extType)
{
	var str = "";
	var i = 1;
	// this matches form name in HTML <form>
	var form = document.forms[0];
	
	if (firstName == "") 
		str = str + "\n\n" + "*   " + FirstName;

	if (lastName == "") 
		str = str + "\n\n" + "*   " + LastName;

 	if (isPSTNCall == true) {
		if ((phNumber == "") && (phExtension == "")) 
		{
			str = str + "\n\n" + "*   " + NoDigitsToDial;
		}
		else if (phNumber == "")
			{
				str = str + "\n\n" + "*   " + NotValidUSPhone;
			}
		else 
		{
			// if there is an extension make sure we have 
			// an extension type (not "You" (1)).
			if ("" != phExtension)
			{
				if (("" == extType) || (1 == extType) || (null == extType))
				{
					str = str + "\n\n" + "*   " + NoExtType;
					
					
				}
			}
			else // no extension...
			{
				// ... so make sure the extension type is "You" (1) 
				if ((extType != "") && (extType != 1) && (extType != null))
				{
					str = str + "\n\n" + "*   " + NoExtension;
				}
			}
		}
	}

	if (str != "") {
		alert(str + "\n")
		return false;
	}

	if (str == "") 
	   	return true;
}
