/*
 * JavaScript functions to validate various user entered data
 *
 * Author: Ryan Gilfether <ryan@webgen-inc.com>
 * Date: 05/01/07
 * Copyright 2007 WebGen, Inc.
 * http://www.webgen-inc.com
 */

/*
 * Checks for a valid zip code
 */
function validateZip(zip)
{
	if(zip=='33333'||zip=='66666'||zip=='77777'||zip=='88888'||zip=='99999'||zip=='00000')
		return false;

	// if the string contains anything other than numbers, return false
	var reg = new RegExp("^[0-9]{5}$");
	if(!reg.test(zip))
		return false;

	return true;
}

/*
 * Validates Candian Postal Codes
 */
function validatePostalCode(pc)
{
	if(pc.length == 6 && pc.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1)
		return true;
		
	return false;
}


/*
 * Checks for a valid email
 */
function validateEmail(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (str.indexOf(at)==-1)
		return false;

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false;

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false;

	if (str.indexOf(at,(lat+1))!=-1)
		return false;

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;

	if (str.indexOf(dot,(lat+2))==-1)
		return false;

	if (str.indexOf(" ")!=-1)
		return false;

	return true;
}

/*
 * Evaluates the string to make sure its a 10 digit long number
 */
function validatePhone(phone)
{
	// if the string is not 10 digits long, return false
	if(phone.length != 10)
		return false;

	// if the string contains anything other than numbers, return false
	// make sure to account for the leading '1' which may or may not be entered
	var reg = new RegExp("^[0-9]{10}$");
	if(!reg.test(phone))
		return false;

	// if the phone number area code starts with a 0 or 1, it is invalid
	if(phone.substring(0,1) == '0' || phone.substring(0,1) == '1')
		return false;

	// extract the area code and exchange
	ac = phone.substring(0, 3); // area code
	ex = phone.substring(3, 6); // exchange

	// check for invalid area codes
	if(ac == "000" || ac == "111" || ac == "222" || ac == "333" || ac == "444" ||
	ac == "555" || ac == "666" || ac == "777" || ac == "888" || ac == "999" ||
	ac == "911" || ac == "411" || ac == "123" || ac == "012" || ac == "321")
		return false;

	// check for a valid exchange, below are exchanges that are invalid
	if(ex == "000" || ex == "555" || ex == "911")
		return false;

	// check to see if the area code and exchange are the same, if they are then return false
	// the only exception to this is 787
	if(ex == ac && ac != "787")
		return false;

	// now check for any other possible invalid numbers that may not have been caught above
	if(phone == "9876543210")
		return false;

	return true;
}

/*
 * Validates a name. A name should not be blank and
 * be longer than 1 character in length
 */
function validateName(str)
{
	if(str == "" || str.length <= 1)
		return false;

	return true;
}