//function for validating a proper zip code value			
function formatZip(strField)
{
	var intCurrentDigit;
	var strDigit;
	var strZIP;
	var strFormatMessage;
	var strInput = document.getElementById(strField).value;

	strFormatMessage = "Please enter the postal code using the following format: 99999-9999 (USA), 99999 (USA) or Z9Z9Z9 (Canada).";
	if (strInput == "")
		return strFormatMessage;
		
	strZIP = strInput;
	strZIP = strZIP.toUpperCase();
	
	if (strZIP.length == 6)
	{
		// Canadian ZIP code.
		for (intCurrentDigit = 0; intCurrentDigit < strZIP.length; intCurrentDigit++)
		{
			strDigit = strZIP.substring(intCurrentDigit, intCurrentDigit + 1);

			if (intCurrentDigit % 2)
			{
				// Odd digits are numeric (zero indexed)
				if ((strDigit != "0") &&
					(strDigit != "1") &&
					(strDigit != "2") &&
					(strDigit != "3") &&
					(strDigit != "4") &&
					(strDigit != "5") &&
					(strDigit != "6") &&
					(strDigit != "7") &&
					(strDigit != "8") &&
					(strDigit != "9"))
				{
					document.getElementById(strField).focus();
					return strFormatMessage;
				}
			}
			else
			{
				// Even digits are alphabetic (zero indexed
				if ((strDigit < "A") || (strDigit > "Z"))
				{
					document.getElementById(strField).focus();
					return strFormatMessage;
				}
			}
		}

		// Now update the field's value if the formatted value differs from what was actually entered.
		if (strZIP != document.getElementById(strField).value)
			document.getElementById(strField).value = strZIP;

		return "";
	}

	// US ZIP code.
	if (strZIP.indexOf("-", 0) == -1)
	{
		if (strZIP.length != 5 && strZIP.length !=9 )
		{
			document.getElementById(strField).focus();
			return strFormatMessage;
		}
		else if (strZIP.length == 5)
		{
			// If the element's max-length is 5 or 6, don't extend the ZIP
			if (document.getElementById(strField).maxLength > 6)
				strZIP = strZIP + "-" + "0000";
		}
		else
		{
			strZIP = strZIP.substring(0, 5) +	"-" +	strZIP.substring(5);
		}
	}
	else if (strZIP.length != 10)
	{
		document.getElementById(strField).focus();
		return strFormatMessage;
	}

	for (intCurrentDigit = 0; intCurrentDigit < strZIP.length; intCurrentDigit++)
	{
		strDigit = strZIP.substring(intCurrentDigit, intCurrentDigit + 1);

		if (intCurrentDigit == 5)
		{
			if (strDigit != "-")
			{
				document.getElementById(strField).focus();
				return strFormatMessage;
			}
		}
		else
		{
			if ((strDigit != "0") &&
				(strDigit != "1") &&
				(strDigit != "2") &&
				(strDigit != "3") &&
				(strDigit != "4") &&
				(strDigit != "5") &&
				(strDigit != "6") &&
				(strDigit != "7") &&
				(strDigit != "8") &&
				(strDigit != "9"))
			{
				document.getElementById(strField).focus;
				return strFormatMessage;
			}
		}
	}

	// If we extended the ZIP for internal reasons then remove that extension now.
	if (strZIP.substring(5) == "-0000")
		strZIP = strZIP.substring(0, 5);
	
	// Now update the field's value if the formatted value differs from what was actually entered.
	if (strZIP != document.getElementById(strField).value)
		document.getElementById(strField).value = strZIP;

	return "";
}
