
//machine locator code

function countrySel(){
if (document.forms[0].country[0].checked == true){
	return "US"
	}
if (document.forms[0].country[1].checked == true){
	return "CA"
	}
if (document.forms[0].country[2].checked == true){
	return "UK"
	}
}



function changeTextBox() {
if (document.Search.country[0].checked == true) {
document.Search.zip.value = "ZIP code";
}
if (document.Search.country[1].checked == true) {
document.Search.zip.value = "Postal code";

}
if (document.Search.country[2].checked == true) {
document.Search.zip.value = "Post code";
}
}


// clears quick search input
function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
  if (document.Search.zip.value = "Enter ZIP code")el.value = "";
  if (document.Search.zip.value = "Postal code")el.value = "";
  if (document.Search.zip.value = "Post code")el.value = "";
  }



// form validation stuff

//trims whitespace before validating
function trim(s) {
while (s.substring(0,1) == ' ') {
s = s.substring(1,s.length);
}
while (s.substring(s.length-1,s.length) == ' ') {
s = s.substring(0,s.length-1);
}
return s;
}

// US postal code check
function isUSzip(val){
	if (val.match(/^([0-9]{5})+$/)){
		return true;
		}else{
		return false;
	}	
}

// Canadian postal code check
function isCaZip(val) {
    if (val.match(/^(([A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9])|([A-Za-z][0-9][A-Za-z]\s[0-9][A-Za-z][0-9])|([A-Za-z][0-9][A-Za-z](|-)[0-9][A-Za-z][0-9]))+$/)){
        return true;
    	}else{
        return false;
		}
}

// UK postal code check
function isUKzip(val){
	if (val.match(/^(([A-Za-z]{1})([A-Za-z0-9]{1,3})\s(([0-9]{1})[A-Za-z]{2}))$/)){
		return true;
		}else{
		return false;
	}	
}



function runSearch(){
var dontGo=0;
	if ((document.Search.country[0].checked == true) && (!isUSzip(document.Search.zip.value))){
	alert("Please enter a valid US Zip code or choose another Country");
	dontGo =1;
	}	
	else if ((document.Search.country[1].checked == true) && (!isCaZip(document.Search.zip.value))){
	alert("Please enter a valid Canadian Postal code or choose another Country");
	dontGo =1;
	}	
	else if ((document.Search.country[2].checked == true) && (!isUKzip(document.Search.zip.value))){
	alert("Please enter a valid Post code or choose another Country");
	dontGo =1;
	}	
	else {
		if (dontGo == 0){
		document.Search.submit()
		
		}
	}
}

// force form validation when enter key is pressed
function detectKey(evt){
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13 || charCode == 3) {
        runSearch();
		return false;
    } 
}
