

function check_phone_number_syntax(the_phone_number)
{

	var response = true;

	if(the_phone_number.length < 10)
	{
		response = "Phone number must contain 10 digits.";
		return response;
	}
	else 
		{			
			if (the_phone_number.match(/^\d\d\d\d\d\d\d\d\d\d$/) || the_phone_number.match(/^\d\d\d \d\d\d \d\d\d\d$/) || the_phone_number.match(/^\d\d\d \d\d\d\d\d\d\d$/) || the_phone_number.match(/^\d\d\d-\d\d\d-\d\d\d\d$/) || the_phone_number.match(/^\d\d\d-\d\d\d\d\d\d\d$/)) 
			{
				return true;
			}
			else 
			{
				response = "Please enter phone number in one of '1111111111' or '111 111 1111' or '111 1111111' or '111-111-1111' or '111-1111111' these formats.";				
				return response;			
			}		
		}
}


function check_zip_code_syntax(the_zip_code)
{

	var response = true;

	if(the_zip_code.length < 5)
	{
		response = "Zip code must contain 5 digits.";
		return response;
	}
	else 
		{
			
			if (the_zip_code.match(/^\d\d\d\d\d$/) || the_zip_code.match(/^\d\d\d\d\d-\d\d\d\d$/)) 
			{
				return true;
			}
			else 
			{
				response = "Please enter your zip code in either '12345' or '12345-6789' format.";				
				return response;
			}		
		}
}


function check_postal_code_syntax(the_postal_code)
{

	var response = true;

	if(the_postal_code.length < 6)
	{
		response = "Postal code must contain 6 characters.";
		return response;
	}
	else 
		{
			the_postal_code=the_postal_code.toUpperCase();
			
			if (the_postal_code.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/) || the_postal_code.match(/^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$/)) 
			{
				return true;
			}
			else 
			{
				response = "Please enter your postal code in either 'A1B2C3' or 'A1B 2C3' format.";				
				return response;			
			}		
		}
}

function is_us(the_country)
{
	var us_country_values = new Array("USA","usa","US","us","United States Of America","UNITED STATES OF AMERICA","united states of america","United States of America","United States","UNITED STATES","united states","U.S.A.","840","220","1");
	
	for(var i=0; i<us_country_values.length; i++)
	{
		if(us_country_values[i] == the_country)
			return true;
	}
	return false;
}

function is_canada(the_country)
{
	var canada_country_values = new Array("CAN","can","CA","ca","Canada","canada","CANADA","124","38");
	
	for(var i=0; i<canada_country_values.length; i++)
	{
		if(canada_country_values[i] == the_country)
			return true;
	}
	return false;
}

function is_us_state(the_state)
{
	var the_state_upper_case = the_state.toUpperCase();
	var the_state_lower_case = the_state.toLowerCase();
	
	var us_state_codes = new Array("NY","PR","VI","MA","RI","NH","ME","VT","CT","NJ","AE","PA","DE","DC","VA","MD","WV","NC","SC","GA","FL","AA","AL","TN","MS","KY","OH","IN","MI","IA","WI","MN","SD","ND","MT","IL","MO","KS","NE","LA","AR","OK","TX","CO","WY","ID","UT","AZ","NM","NV","CA","AP","HI","AS","GU","PW","FM","MP","MH","OR","WA","AK");	
	var us_state_names = new Array("new york","puerto rico","virgin islands","massachusetts","rhode island","new hampshire","maine","vermont","connecticut","new jersey","armed forces - europe/africa/canada","pennsylvania","delaware","district of columbia","virginia","maryland","west virginia","north carolina","south carolina","georgia","florida","armed forces - americas","alabama","tennessee","mississippi","kentucky","ohio","indiana","michigan","iowa","wisconsin","minnesota","south dakota","north dakota","montana","illinois","missouri","kansas","nebraska","louisiana","arkansas","oklahoma","texas","colorado","wyoming","idaho","utah","arizona","new mexico","nevada","california","armed forces - pacific","hawaii","american samoa","guam","palau","federated states of micronesia","northern mariana islands","marshall islands","oregon","washington","alaska");	
	
	for(var i=0; i<us_state_codes.length; i++)
	{
		if(us_state_codes[i] == the_state_upper_case || us_state_names[i] == the_state_lower_case)
			return true;
	}
	return false;
}

function is_province_canada(the_province)
{
	var the_province_upper_case = the_province.toUpperCase();
	var the_province_lower_case = the_province.toLowerCase();
	
	var canada_province_codes = new Array("NL","NS","PE","NB","BC","QC","NU","NT","YT","ON","MB","SK","AB");
	var canada_province_names = new Array("newfoundland","nova scotia","prince edward island","new brunswick","british columbia","quebec","nunavut","northwest territories","yukon territory","ontario","manitoba","saskatchewan","alberta");

	for(var i=0; i<canada_province_codes.length; i++)
	{
		if(canada_province_codes[i] == the_province_upper_case || canada_province_names[i] == the_province_lower_case)
			return true;
	}
	return false;
}


function check_state_zip (the_state,the_zip)
{
	var response = true;
	var us_state = true;
	var canada_province = false;
	
	if(the_state == '')
	{
		response = "Please select your state or province.";
		return response;
	}
	if(the_zip == '')
	{
		response = "Please enter your zip or postal code";
		return response;
	}

	us_state = is_us_state(the_state);
	canada_province = is_province_canada(the_state);
	
	if(!us_state && !canada_province)
	{
		response = "The selected state is neither a US state nor a Canadian province.";	
		return response;
	}
	else 
	{
		if(us_state)
		{
			response = check_zip_code_syntax(the_zip);
		}
		if(canada_province)
		{
			response = check_postal_code_syntax(the_zip);
		}
		if(response == true)
			response = sync_zip_state_http_post("sync_zip_state_check.cfm",us_state,canada_province,the_state,the_zip);
		return response;
	}
	return response;
}

function check_state_zip_country (the_country,the_state,the_zip)
{
	var response = true;
	var country_us = true;
	var country_canada = false;
	var us_state = true;
	var canada_province = false;
	
	if(the_country == '')
	{
		response = "Please select your country.";
		return response;
	}
	else
	{
		country_us = is_us(the_country);
		country_canada = is_canada(the_country);
	}
	
	if(the_state == '')
	{
		response = "Please select your state or province.";
		return response;
	}
	else
	{
		us_state = is_us_state(the_state);
		canada_province = is_province_canada(the_state);
		
	}	

	if(country_us && !us_state)
	{
		response = "Please select an US state or change the country you selected.";
		return response;
	}
	if(country_canada && !canada_province)
	{
		response = "Please select a Canadian province or change the country you selected.";
		return response;
	}

	if(!country_us && us_state)
	{
		response = "You have selected an US state, please select United States as your country.";
		return response;
	}
	if(!country_canada && canada_province)
	{
		response = "You have selected a Canadian province, please select Canada as your country.";
		return response;
	}
	if(!country_canada && !canada_province && !country_us && !us_state)
	{
		return true;
	}

	if(the_zip == '')
	{
		response = "Please enter your zip or postal code";
		return response;
	}
	
	response = check_state_zip(the_state,the_zip);
	return response;	
}

function sync_zip_state_http_post(strURL,us_state,canada_province,the_state,the_zip) 
{
	var xmlHttpReq = false;
	var self = this;
	var http_response = '';
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, false);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.send('us_state=' + us_state + '&canada_province=' + canada_province + '&the_state=' + the_state + '&the_zip=' + the_zip);		
	http_response = self.xmlHttpReq.responseText;
	if(http_response.search(/true/i) >= 0)
		return true;
	else 
		return http_response;
}