// ------------------------ BEGIN ZIP CODE CHECK -------------------------

//ZipVerify.js

/*
This file checks US Zip codes - verifying that they are numeric
Place onChange=" return checkUSZip(this, true);" in appropriate input box

use true if the input box can be left empty, use false if it is required.
*/


// ---------------------- VARIABLE DECLARATIONS -------------------------------

//define available digits
var ZIPdigits = "0123456789";

//a zip contains 5 digits
//this script does not accomodate for specific zip codes like 57252-3101
var ZIPnumofdigits = 5;

var ZIPalert = "Please enter a 5 digit U.S. zip code.";

var ZIPdefaultEmptyOK = true;


//************************* checkUSZip Function *******************************
//called from form 

// Check that string theField.value is a valid US Zip Code.

function checkUSZip(theField, emptyOK)
{  

if(theField.value.length == 0 && (emptyOK))
	{
		return true;
	}
else
{
 
 var normalized = theField.value
 if (!isUSZip(normalized, false)) 
    return ZIPwarnInvalid(theField, ZIPalert);
 else 
 { 
    return true;
 }

} 

}

//************************* isUSZip Function *******************************
//called from checkUSZip function

function isUSZip (s)
{   
    return (ZIPisInteger(s) && s.length == ZIPnumofdigits)
}

//************************* ZIPisInteger Function *******************************
//called from isUSZip function

function ZIPisInteger (s)
{   var i;

    if (ZIPisEmpty(s)) 
       if (ZIPisInteger.arguments.length == 1) return ZIPdefaultEmptyOK;
       else return (ZIPisInteger.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 (!ZIPisDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

//************************* ZIPisEmpty Function *******************************
//called from ZIPisInteger function

// Check whether string s is empty.
function ZIPisEmpty(s)
{   return ((s == null) || (s.length == 0))
}

//************************* ZIPisDigit Function *******************************
//called from ZIPisInteger function

// Returns true if character c is a digit 
// (0 .. 9).
function ZIPisDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


//************************* ZIPwarnInvalid Function ****************************
//called from checkUSZip function

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, put focus in it, and return false.
function ZIPwarnInvalid (theField, s)
{   	
	alert(s);
        theField.focus();
    	theField.select();
        return false;
}

// END ZIP CODE CHECK 