// FormChek.js

// VARIABLE DECLARATIONS

var sEmail = "Email"
// whitespace characters
var whitespace = " \t\n\r";

// decimal point character differs by language and culture
var decimalPointDelimiter = "."

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

// i is an abbreviation for "invalid"

var iEmail = "This field must be a valid email address (like jim@mycompany.com). Please reenter."

// p is an abbreviation for "prompt"

var pEntryPrompt = "Please enter a "
var pEmail = "valid email address (like jim@mycompany.com)."

var defaultEmptyOK = false

// 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;
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    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;
    else return true;
}

/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */

// Display prompt string s in status bar.

function prompt (s)
{   window.status = s
}

// Display data entry prompt string s in status bar.

function promptEntry (s)
{   window.status = pEntryPrompt + s
}

// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */

// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else return true;
}

// Function to see  if input is valid US phone number
function isPhone(strInput)
{
    // Phone numbers can be 
    // AAA.EEE.NNNN
    
    var inputStr = strInput.toString()
    var strFormatMsg = "The phone entry is not in an acceptable format.\n\nPlease enter phone numbers in the following format nnn.nnn.nnnn."
  
	if (inputStr.length != 12) {return false;}

    // convert dot delimiters to dashes
    /* while (inputStr.indexOf(".") != -1) 
        {
        inputStr = replaceString(inputStr,".","-")
        }
     */   
    // Find the delimiters, if any    
    var delim1 = inputStr.indexOf(".")
    var delim2 = inputStr.lastIndexOf(".")
    if (delim1 != -1 && delim1 == delim2) 
        {
        // there is only one delimiter in the string
        //alert(strFormatMsg)
        return false
        }
        
    // dashes must be in the right places
    if (delim1 != 3 || delim2 != 7)
        {
        //alert(strFormatMsg)
        return false;
        }
    
    if (delim1 != -1) 
        {
        // there are delimiters; extract component values
        var AreaCode = parseInt(inputStr.substring(0, delim1), 10)
        var CO = parseInt(inputStr.substring(delim1 + 1, delim2), 10)
        var Number = parseInt(inputStr.substring(delim2 + 1, inputStr.length), 10)
        }
    else 
        {
        // there are no delimiters; extract component values
        //alert(strFormatMsg)
        return false
        }
        
    if (isNaN(AreaCode) || isNaN(CO) || isNaN(Number)) 
        {
        // there is a non-numeric character in one of the component values
        //alert(strFormatMsg)
        return false
        }
    
    if (AreaCode < 100 || AreaCode > 999) 
        {
        //alert(strFormatMsg)
        return false
        }
    if (CO < 100 || CO > 999) 
        {
        //alert(strFormatMsg)
        return false
        }
	
    if (Number < 0 || Number > 9999) 
        {
        //alert(strFormatMsg)
        return false
        }

    return true;

}
