$(document).ready(function() {

	//Step 1 Validation
	var clocation = $('#clocation');
	var capacity = $('#capacity');
	var span = $('#span');
	var obstruction = $('#obstruction');
	var lift = $('#lift');
	var step1form = $('#step1form');
	var step1submit = $('#step1submit');
	var msg = "";

	function validateclocation() {
	
	var a = clocation.val();
	
	if(a != "" && a != undefined) {
		clocation.prev().removeClass("error");
		msg = msg.replace(/-Please enter your Crane Location\n/, "");
		return true;
	} else {
		clocation.prev().addClass("error");
		if(msg.search("Crane Location") == -1) {
		  msg+= "-Please enter your Crane Location\n";
		}
		return false;
	}
	}
	
	function validatecapacity() {
		
	var a = capacity.val();
	
	if(a != "" && a != undefined) {
		capacity.prev().removeClass("error");
		msg = msg.replace(/-Please enter your Capacity\n/, "");
		return true;
	} else {
		capacity.prev().addClass("error");
		if(msg.search("Capacity") == -1) {
		  msg+= "-Please enter your Capacity\n";
		}
		return false;
	}
	}
	
	function validatespan(){
	var a = span.val().length;
	
	if(a < 1) {
		span.parent().prev().addClass("error");
		if(msg.search("Span") == -1) {
		  msg+= "-Please enter your Span\n";
		}
		return false;
	} else {
		span.parent().prev().removeClass("error");
		msg = msg.replace(/-Please enter your Span\n/, "");
		return true;
	}
	}
	
	function validateobstruction(){
	var a = obstruction.val().length;
	
	if(a < 1 && lift.val().length < 1) {
		obstruction.parent().prev().addClass("error");
		if(msg.search("Lowest Obstruction") == -1) {
		  msg+= "-Please enter your Lift OR Lowest Obstruction\n";
		}
		return false;
	} else {
		obstruction.parent().prev().removeClass("error");
		msg = msg.replace(/-Please enter your Lift OR Lowest Obstruction\n/, "");
		return true;
	}
	}

	clocation.change(validateclocation);
	capacity.change(validatecapacity);
	span.blur(validatespan);
	span.keyup(validatespan);
	obstruction.blur(validateobstruction);
	obstruction.keyup(validateobstruction);
	lift.keyup(validateobstruction);
	lift.blur(validateobstruction);

	// Validation Submit Functions
	step1submit.click(function(){
		if(validateclocation() & validatecapacity() & validatespan() & validateobstruction()) {
			step1form.submit();
			return true
		} else {
			$.scrollTo(180, 400, {onAfter:function() {
			  msg+= "\nPlease fill out the fields marked in red";
			  alert(msg);
			  msg = "";
			}});
			return false;
		}
	});
	
	// Step 2 Validation
	var runway = $('#runway');
	var rlength = $('#rlength');
	var scenters = $('#scenters');
	var asce = $('#asce');
	var electrification = $('#electrification');
	var step2form = $('#step2form');
	var step2submit = $('#step2submit');
	var msg = "";
	
	function validaterunway() {
		
	var a = runway.val();
	
	if(a != "" && a != undefined) {
		runway.prev().removeClass("error");
		msg = msg.replace(/-Please select if you need a Runway\n/, "");
		return true;
	} else {
		runway.prev().addClass("error");
		if(msg.search("a Runway") == -1) {
		  msg+= "-Please select if you need a Runway\n";
		}
		return false;
	}
	}
	
	function validaterlength(){
	var a = rlength.val().length;
	
	if(a < 1 && (runway.val() == "Yes" || (runway.val() == "No" && (asce.val() == "Yes" || electrification.val() == "Yes")))) {
		rlength.parent().prev().addClass("error");
		if(msg.search("Length") == -1) {
		  msg+= "-Please enter your Runway Length\n";
		}
		return false;
	} else {
		rlength.parent().prev().removeClass("error");
		msg = msg.replace(/-Please enter your Runway Length\n/, "");
		return true;
	}
	}
	
	function validatescenters(){
	var a = scenters.val().length;
	
	if(a < 1 && runway.val() == "Yes") {
		scenters.parent().prev().addClass("error");
		if(msg.search("Support") == -1) {
		  msg+= "-Please enter your Support Centers\n";
		}
		return false;
	} else {
		scenters.parent().prev().removeClass("error");
		msg = msg.replace(/-Please enter your Support Centers\n/, "");
		return true;
	}
	}
	
	function validateasce() {
		
	var a = asce.val();
	
	if(a != "" && a != undefined || runway.val() != "No") {
		asce.prev().removeClass("error");
		msg = msg.replace(/-Please select if you need ASCE Rail\n/, "");
		return true;
	} else {
		asce.prev().addClass("error");
		if(msg.search("ASCE") == -1) {
		  msg+= "-Please select if you need ASCE Rail\n";
		}
		return false;
	}
	}
	
	function validateelectrification() {
		
	var a = electrification.val();
	
	if(a != "" && a != undefined || runway.val() != "No") {
		electrification.prev().removeClass("error");
		msg = msg.replace(/-Please select if you need Electrification\n/, "");
		return true;
	} else {
		electrification.prev().addClass("error");
		if(msg.search("Electrification") == -1) {
		  msg+= "-Please select if you need Electrification\n";
		}
		return false;
	}
	}
	
	runway.change(validaterunway);
	rlength.blur(validaterlength);
	rlength.keyup(validaterlength);
	scenters.blur(validatescenters);
	scenters.keyup(validatescenters);
	asce.change(validateasce);
	electrification.change(validateelectrification);
	
	// Validation Submit Functions
	step2submit.click(function(){
		if(validaterunway() & validaterlength() & validatescenters() & validateasce() & validateelectrification()) {
			step2form.submit();
			return true
		} else {
			$.scrollTo(180, 400, {onAfter:function() {
			  msg+= "\nPlease fill out the fields marked in red";
			  alert(msg);
			  msg = "";
			}});
			return false;
		}
	});
	
	// Step 3 Validation
	var installation = $('#installation');
	var zipcode = $('#zipcode');
	var step3form = $('#step3form');
	var step3submit = $('#step3submit');
	var msg = "";
	
	function validateinstallation() {
		
	var a = installation.val();
	var b = zipcode.val().length;
	
	if(a == "Yes" && b < 5) {
		zipcode.prev().addClass("error");
		if(msg.search("Zip") == -1) {
		  msg+= "-Please enter your Zip Code\n";
		}
		return false;
	} else {
		zipcode.prev().removeClass("error");
		msg = msg.replace(/-Please enter your Zip Code\n/, "");
		return true;
	}
	}
	
	zipcode.blur(validateinstallation);
	zipcode.keyup(validateinstallation);
	
	// Validation Submit Functions
	step3submit.click(function(){
		if(validateinstallation()) {
			step3form.submit();
			return true
		} else {
			$.scrollTo(180, 400, {onAfter:function() {
			  msg+= "\nPlease fill out the fields marked in red";
			  alert(msg);
			  msg = "";
			}});
			return false;
		}
	});
	
	// Step 4 Validation
	var fname = $('#fname');
	var lname = $('#lname');
	var email = $('#email');
	var company = $('#company');
	var phone = $('#phonenumber');
	var step4form = $('#step4form');
	var step4submit = $('#step4submit');
	var msg = "";
	
	function validatefname(){
	var a = fname.val().length;

	if(a < 2) {
		fname.prev().addClass("error");
		if(msg.search("First Name") == -1) {
		  msg+= "-Please enter your First Name\n";
		}
		return false;
	} else {
		fname.prev().removeClass("error");
		msg = msg.replace(/-Please enter your First Name\n/g, "");
		return true;
	}
	}
	
	function validatelname(){
	var a = lname.val().length;
	
	if(a < 2) {
		lname.prev().addClass("error");
		if(msg.search("Last Name") == -1) {
		  msg+= "-Please enter your Last Name\n";
		}
		return false;
	} else {
		lname.prev().removeClass("error");
		msg = msg.replace(/-Please enter your Last Name\n/g, "");
		return true;
	}
	}
	
	function validatecompany(){
	var a = company.val().length;
	
	if(a < 2) {
		company.prev().addClass("error");
		if(msg.search("Company") == -1) {
		  msg+= "-Please enter your Company\n";
		}
		return false;
	} else {
		company.prev().removeClass("error");
		msg = msg.replace(/-Please enter your Company\n/g, "");
		return true;
	}
	}
	
	function validateemail() {
	
		var a = email.val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9-.]+.[a-z]{2,4}$/;
		var b = filter.test(a);
			
		if(b){
			email.prev().removeClass("error");
			msg = msg.replace(/-Please enter your Email Address\n/g, "");
			return true;
		} else {
			email.prev().addClass("error");
			if(msg.search("Email") == -1) {
		      msg+= "-Please enter your Email Address\n";
			}
			return false;
		}
	}
	
	function validatephone() {
	var a = phone.val().length;
	
	if(a < 7) {
		phone.prev().addClass("error");
		if(msg.search("Phone") == -1) {
		  msg+= "-Please enter your Phone Number\n";
		}
		return false;
	} else {
		phone.prev().removeClass("error");
		msg = msg.replace(/-Please enter your Phone Number\n/g, "");
		return true;
	}
	}
	
	// Validation while typing	
	fname.blur(validatefname);
	fname.keyup(validatefname);
	lname.blur(validatelname);
	lname.keyup(validatelname);
	company.blur(validatecompany);
	company.keyup(validatecompany);
	email.blur(validateemail);
	email.keyup(validateemail);
	
	// Validation Submit Functions
	step4submit.click(function(){
		if(validatefname() & validatelname() & validatecompany() & validateemail()) {
			step4form.submit();
			return true
		} else {
			$.scrollTo(180, 400, {onAfter:function() {
			  msg+= "\nPlease fill out the fields marked in red";
			  alert(msg);
			  msg = "";
			}});
			return false;
		}
	});
	
	// Incentive Validation
	var incentiveform = $("#fincentive");
	var incentivesubmit = $("#step6submit");
	var gift = $('#gift');
	var address = $("#iaddress");
	var city = $("#icity");
	var state = $("#istate");
	var zip = $("#izip");
	
	function validategift(){
	
	var a = gift.val();
	
	if(a != "" && a != undefined) {
		gift.prev().removeClass("error");
		msg = msg.replace(/-Please select your Complimentary Gift\n/, "");
		return true;
	} else {
		gift.prev().addClass("error");
		if(msg.search("Gift") == -1) {
		  msg+= "-Please select your Complimentary Gift\n";
		}
		return false;
	}
	}
	
	function validateaddress(){
	var a = address.val().length;
	
	if(a < 1) {
		address.prev().addClass("error");
		if(msg.search("Address") == -1) {
		  msg+= "-Please enter your Address\n";
		}
		return false;
	} else {
		address.prev().removeClass("error");
		msg = msg.replace(/-Please enter your Address\n/g, "");
		return true;
	}
	}
	
	function validatecity(){
	var a = city.val().length;
	
	if(a < 1) {
		city.prev().addClass("error");
		if(msg.search("City") == -1) {
		  msg+= "-Please enter your City\n";
		}
		return false;
	} else {
		city.prev().removeClass("error");
		msg = msg.replace(/-Please enter your City\n/g, "");
		return true;
	}
	}
	
	function validatestate(){
	var a = state.val().length;
	
	if(a < 1) {
		state.prev().addClass("error");
		if(msg.search("State") == -1) {
		  msg+= "-Please enter your State\n";
		}
		return false;
	} else {
		state.prev().removeClass("error");
		msg = msg.replace(/-Please enter your State\n/g, "");
		return true;
	}
	}
	
	function validatezipcode(){
	var a = zip.val().length;
	
	if(a < 1) {
		zip.prev().addClass("error");
		if(msg.search("Zip") == -1) {
		  msg+= "-Please enter your Zip\n";
		}
		return false;
	} else {
		zip.prev().removeClass("error");
		msg = msg.replace(/-Please enter your Zip\n/g, "");
		return true;
	}
	}
	
	gift.change(validategift);
	address.blur(validateaddress);
	address.keyup(validateaddress);
	city.blur(validatecity);
	city.keyup(validatecity);
	state.blur(validatestate);
	state.keyup(validatestate);
	zip.blur(validatezipcode);
	zip.keyup(validatezipcode);
	
	incentivesubmit.click(function(){
		if(validategift() & validateaddress() & validatecity() & validatestate() & validatezipcode()) {
		  $.post("rfqprocess.php", incentiveform.serialize(), function() {
			incentiveform.slideUp("fast", function() {
			  incentiveform.after("<div id=\"thank\">Thank you, your free gift will be sent out shortly.</div>");
			  $('#thank').fadeIn();
			});
		  });
			//incentiveform.submit();
			return true
		} else {
			$.scrollTo(180, 400, {onAfter:function() {
			  msg+= "\nPlease fill out the fields marked in red";
			  alert(msg);
			  msg = "";
			}});
			return false;
		}
	});

	// Step 2 Functions
	$('.yes').hide();
	$('.no').hide();
	$('.runlen').hide();
	
	var runway = $("#runway");
	
	runway.change(function() {
	  
	  if(runway.val() == "Yes") {
	    $('.runlen').show();
	  	$('.yes').show();
	  	$('.no').hide();
	  } else if(runway.val() == "No") {
	  	$('.no').show();
	  	$('.yes').hide();
	  	$('.runlen').hide();
	  } else {
	  	$('.no').hide();
	  	$('.yes').hide();
	  	$('.runlen').hide();
	  }
	});
	
	electrification.change(function() {
		if(electrification.val() == "Yes" || asce.val() == "Yes") {
			$('.runlen').show();
		} else {
			$('.runlen').hide();
		}
	});
	
	asce.change(function() {
		if(electrification.val() == "Yes" || asce.val() == "Yes") {
			$('.runlen').show();
		} else {
			$('.runlen').hide();
		}
	});
	
	// Step 3 Functions
	$('#voltage').hide();
	$('#zip').hide();
	
	var bridge = $("#bridgepower");
	var trolley = $("#trolleypower");
	var hoist = $("#hoistpower");
	
	$('.power').change(function() {
	  
	  if(bridge.val() == "Electric" || trolley.val() == "Electric" || hoist.val() == "Electric") {
	  	$("#voltage").show();
	  } else {
	    $("#voltage").hide();
	  }
	  
	});
	
	var install = $("#installation");
	
	install.click(function() {
	
	if(install.val() == "Yes") {
		$('#zip').show();
	} else {
		$('#zip').hide();
	}
	});
	
});