function skDoAjaxRequest(url){
	$.ajax({
		type: "GET",
		url: url,
		success:function(msg){process(msg);}
	});
}
function resetForm(){
	$("#starting_zipcode").val()="";
	$("#comp_submit").val()="TRY AGAIN";
	return false;
}
function process(msg){
	if (msg=="Y"){
		return true;
	} else if (msg=="I"){
		alert("The zipcode does not appear to be valid.");
		resetForm();
		return false;
	} else {
		alert("The zipcode was not found in the database, please try another.");
		resetForm();
		return false;
	}
}
function skPostForm(){
	return false;
}

function strlen(string){return (string+'').length;}
function checkCountry(){if (country=$('starting_country')){if (country.value=="GB"){alert("Support for Great Britain is not 100%, we are working to improve upon it.");if (zipCode=$('starting_zipcode')){zipCode.focus();}}}}
function isWhitespace(charToCheck){var whitespaceChars=" \t\n\r\f";return (whitespaceChars.indexOf(charToCheck) != -1);}
function ltrim(str){if(str){for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);return str.substring(k, str.length);}}
function rtrim(str){if(str){for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)); j--);return str.substring(0,j+1);}}
function trim(str){return ltrim(rtrim(str));}
function findRadiusCities(){
	if (zipCode=$("#starting_zipcode").val()) {
		if (zipCode!="") {
			zipCode = trim(zipCode);
			if (selectedCountry = $("#starting_country").val()) {
				if (selectedCountry == "US") {
					var GBMatch = /[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}/.test(zipCode.toUpperCase());
					//var CAMatch = /^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/.test(zipCode.value);
					var CAMatch = /^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/.test(zipCode.toUpperCase());
					if (GBMatch) {
						selectedCountry = "GB";				
					} else if (CAMatch) {
						selectedCountry = "CA";				
					} else if (strlen(zipCode)==4) {
						selectedCountry = "AU";										
					} else {
						selectedCountry = "US";
					}
				}
				if (selectedCountry == "GB") {
					if ($('#contract_zips').attr('checked')) {
					} else {
						if (confirm("Not Restricting Post Codes To One Per City In Great Britain May Result In A Very Long List!  Continue?")) {
							$('#contract_zips').removeAttr('checked');
						} else {
							$('#contract_zips').attr('checked','checked');
						}
					}
				}
				if (radiusValue = $("#radius").val()) {
					if (radiusValue > 50) {
						radiusValue = "50";
					}
				}
			}
			$("#submit_but").val("CALCULATING...");
			url = "http://www.studiokraft.com/index.php/action/find_cities_in_radius/aj/1/z/" + zipCode + "/sc/" + selectedCountry;
			skDoAjaxRequest(url);
		} else {
			alert('Please enter the zip code into the form.');
		}
	}
	return false;
}	
