$(function(){
  // Document is ready
  doContact(document.contact_us.contact_me);
  doBrochure(document.contact_us);
});

function checkOther(theSelect) {
	var theOther = document.getElementById("other");
	if(theOther) {
		if(theSelect.options[theSelect.selectedIndex].value=="Other") {
			$("#other").show().val("Please specify").select().focus();
		} else {
			$("#other").hide();
		}
	}
}
function validate(theForm){
	var brochReq = false;
	var contactReq = false;
	var needName = false;
	if ((theForm.stationary.checked == true)||(theForm.vertical_tank_mounted.checked == true)||(theForm.clean_dry_air.checked == true)||(theForm.rotary_screw.checked == true)) {
		brochReq = true;
		if (theForm.name.value == "" || theForm.name.value.length < 2){
			alert("Please enter your name in order to receive brochures by mail.");
			theForm.name.focus();
			theForm.name.setAttribute("style","border:1px solid #FF0000;");
			return false;
		}
		if (theForm.country.value == "choose"){
			alert("Please select your country from the list in order to receive brochures by mail.");
			theForm.country.focus();
			theForm.country.setAttribute("style","border:1px solid #FF0000;");
			theForm.country.setAttribute("style","border:1px solid #FF0000;");
			return false;
		} else if ((theForm.country.value != "USA")&&(theForm.country.value != "Canada")) {
			alert("Sorry, we can only mail brochures within the USA and Canada, please use one of our online brochures.");
			theForm.country.focus();
			var nodeObj = document.getElementById('country');
			theForm.stationary.checked = false;
			theForm.vertical_tank_mounted.checked = false;
			theForm.clean_dry_air.checked = false;
			theForm.rotary_screw.checked == false;
			theForm.rotary_screw.focus();
			//theForm.rotary_screw.setAttribute("style","border:1px solid #FF0000;");
			return false;
		} else {
			if (theForm.address.value == "" || theForm.address.value.length < 6){
				alert("Please enter your street address in order to receive brochures by mail.");
				theForm.address.focus();
				theForm.address.setAttribute("style","border:1px solid #FF0000;");
				return false;
			}
			if (theForm.city.value == "" || theForm.city.value.length < 2){
				alert("Please enter the name of your city in order to receive brochures by mail.");
				theForm.city.focus();
				theForm.city.setAttribute("style","border:1px solid #FF0000;");
				return false;
			}
			if (theForm.state.value == "choose"){
				alert("Please select your state or province in order to receive brochures by mail.");
				theForm.state.focus();
				theForm.state.setAttribute("style","border:1px solid #FF0000;");
				return false;
			}
		}
	}
	
	
	if(theForm.contact_me.options[theForm.contact_me.selectedIndex].value=="Yes, please email") {
		contactReq = true;
		if (theForm.email.value == "" || theForm.email.value.indexOf('@',1)== -1 || theForm.email.value.indexOf('.',2)==-1){
			alert("Please give your email address to be contacted via email.");
			theForm.email.focus();
			theForm.email.setAttribute("style","border:1px solid #FF0000;");
			return false;
		}
	} else if (theForm.contact_me.options[theForm.contact_me.selectedIndex].value=="Yes, please phone") {
		contactReq = true;
		if (theForm.phone.value == "" || theForm.phone.value.length < 10){
			alert("Please give your number to be contacted by phone.");
			theForm.phone.focus();
			theForm.phone.setAttribute("style","border:1px solid #FF0000;");
			return false;
		}
	}
	if((contactReq == false)&&(brochReq == false)) {
		if (theForm.comments.value==""){
			alert("Please enter your comments.");
			theForm.comments.focus();
			return false;
		}
	} else {
		if (theForm.name.value==""){
			alert("Please enter your name.");
			theForm.name.focus();
			theForm.name.setAttribute("style","border:1px solid #FF0000;");
			return false;
		}
	}
	return true;
}
function clearStyle(id) {
	var nodeObj = document.getElementById(id);
	if(nodeObj) { 
		nodeObj.setAttribute("style","");
	}
}
function doContact(theSelect) {
	if (theSelect.options[theSelect.selectedIndex].value=="Yes, please phone") {
		$("#phone_row").show();
		$("#email_row").hide();
	} else if (theSelect.options[theSelect.selectedIndex].value=="Yes, please email") {
		$("#phone_row").hide();
		$("#email_row").show();
	} else if (theSelect.options[theSelect.selectedIndex].value=="Yes, either phone or email") {
		$("#phone_row").show();
		$("#email_row").show();
	} else {
		$("#phone_row").hide();
		$("#email_row").hide();
	}
}

function doBrochure(theForm) {
	if ((theForm.stationary.checked == true)||(theForm.vertical_tank_mounted.checked == true)||(theForm.clean_dry_air.checked == true)||(theForm.rotary_screw.checked == true)) {
		// show address stuff
		$("#address_row").show();
		$("#city_row").show();
		$("#state_row").show();
		$("#zip_row").show();
		$("#country_row").show();
	} else {
		// hide address stuff
		$("#address_row").hide();
		$("#city_row").hide();
		$("#state_row").hide();
		$("#zip_row").hide();
		$("#country_row").hide();
	}
}