// JavaScript Document
function US_CAN_Validator_Selector(){
	var CountryID = document.getElementsByName("Country");
    if(CountryID[0].selectedIndex == 1){
			return ZipCodeValidator('extended','CA');
	}
	else{
			return ZipCodeValidator('extended','US');
	}
}
			
 function global_postalcode_selector() {
	 
	     if (document.locator.country[1].checked){
			return ZipCodeValidator('extended', 'CA');
		 }
			 
		 else{
			return ZipCodeValidator('extended', 'US');
		 }
	}

function ZipCodeValidator(size, country){
	var number = document.getElementById("ltsZipcode").value;
	
	number = remove_spaces_hyphen(number);
	
	if(country == "US")
	{
		if(size == "extended")
		{
			var valid_characters = "0123456789-"
			var num_hyphens = 0;
			if(number.length!=5 && number.length != 10) {
				alert("The ZIP code is not a valid US ZIP Code");
				return false;
			}
			for(var i = 0; i < number.length; i ++)
			{
				temp = "" + number.substring(i, i+1);
				if (temp == "-")
				{
					num_hyphens++;
				}
				if (valid_characters.indexOf(temp) == "-1")
				{
					alert("The ZIP code is not a valid US ZIP code");
					return false;
				}
				if ((num_hyphens > 1) || ((number.length==10) && ""+number.charAt(5)!="-")) 
				{
					alert("The ZIP code is not a valid US ZIP code");
					return false;
	   			}
			}
			return true;
		}
		else if(size == "normal")
		{
			var valid_characters = "0123456789";
			if(number.length!=5) {
				alert("The ZIP code is not a valid US ZIP Code");
				return false;
			}
			for(var j = 0; j < number.length; j ++)
			{
				temp = "" + number.substring(j, j+1);

				if (valid_characters.indexOf(temp) == "-1")
				{
					alert("The ZIP code is not a valid US ZIP code");
					return false;
				}

			}
			return true;		
		}
	}
	else if(country == "CA")
	{
			var valid_chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
			var valid_nums = "0123456789";
			var spacer = " ";
			var num_spaces = 0;
			
			
			
			if(number.length!=6) {
				alert("The POSTAL code is not a valid CA POSTAL Code");
				return false;
			}
			for(var k = 0; k < 6; k ++)
			{	
				if(k == 0 || k == 2 || k == 4){
					temp = "" + number.substring(k, k+1);
					if (valid_chars.indexOf(temp) == "-1")
					{
						alert("The POSTAL code is not a valid CA POSTAL Code");
						return false;
					}
				
				}
				if(k == 1 || k == 3 || k == 5){
					temp = "" + number.substring(k, k+1);
					if (valid_nums.indexOf(temp) == "-1")
					{
						alert("The POSTAL code is not a valid CA POSTAL Code");
						return false;
					}
				} 
				
			}
			return true;
		}
	}
	
	function remove_spaces_hyphen(string){
	
		var tstring = "";
		
		
		string = '' + string;
		splitstring = string.split(" ");
		for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];
		
	
		
		
		if (tstring.length != 10){
		string = tstring;
		tstring = "";
		string = '' + string;
		splitstring = string.split("-");
		for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];
		}
		
		
		return tstring;
}