$(document).ready(function() {
	$(".ajax").hide();
	$("#frm").submit(function() {
		var i = 0;
		
		$(".ajax").show();
		
		$("input[@name=txtfirstname],input[@name=txtlastname],input[@name=txtcompany],input[@name=txtemail]")
		.each(function() {
			if($(this).val() == "") {
			$(".loading-info").empty().append("<p>Please fill in the required field</p>");
			$(this).focus();
			$(".ajax").hide();
			return false;
			}
			else i++;
		});
		
		
		if($("input[@name=contactby]:checked").val() == 2 && $("input[@name=txtphone]").val() == "") {
			$(".loading-info").empty().append("<p>Please fill in the required field</p>");
			$("input[@name=txtphone]").focus();
			$(".ajax").hide();
			return false;
		}
		else i++;
		
		$("textarea[@name=txtcomment]")
		.each(function() {
			if($(this).val() == "") {
			$(".loading-info").empty().append("<p>Please fill in the required field</p>");
			$(this).focus();
			$(".ajax").hide();
			return false;
			}
			else i++;
		});
		
		$("input[@name=type]").attr({value: $("input[@name=contactby]:checked").val()});
		
		
		
		if(i == 6) {
			// start ajax here
			
			var inputs = [];
			$(":input",this).each(function() {
				inputs.push(this.name + "=" + this.value);
			});
			
			$.ajax({
				url: "submitdata.php",
				type: "post",
				data: inputs.join("&"),
				success: function(m) {
					if(m == 1) {
						$(".loading-info").empty().append("<p>Your form has been submitted. Thank you!</p>");
						$(".ajax").hide();
						$(":input").attr("disabled","disabled");
					}
					else if(m == 0) {
						$(".loading-info").empty().append("<p>Something went wrong.</p>");
						$(".ajax").hide();
					}
					else {
						$(".loading-info").empty().append("<p>"+m+"</p>");
						$(".ajax").hide();
					}
				}
			});
			
			return false;
			
		}
		else return false;
	});
	
	
	$(".prev a").attr({href:"javascript:history.go(-1)"});
	//$(".next a").attr({href:"javascript:history.go(1)"});
	
	$("input[@name=contactby]:first").attr({checked:"checked"});
	$("input[@name=contactby]").click(function() {
		if($("input[@name=contactby]:checked").val() == 2) $(".phone").show();
		else $(".phone").hide();
	});
	
	
	$(".thedata").hide();
	$("#zipform").hide();
	$(".controller a.ziplist").toggle(function() {
		$(".thedata").fadeIn("slow").animate({top:"-630px"}).append("<img src='images/close.gif' class='imgclose' alt='close' />");
		$(".zipforms").show();
		
		$(".imgclose").click(function() {
			$(".thedata").slideUp("slow");
		});
		
	},function() {
		$(".thedata").slideUp("slow");
	});
	
	$(".controller a.zipcode").toggle(function() {
		$("#zipform").slideDown("slow");
	},function() {
		$("#zipform").slideUp("slow");
	});
	
	
	
	
	//zip form
	$("input[@name=zipcode]").focus(function() {
		if($(this).val() == "enter zip code") $(this).attr({value:""});
	})
	$("input[@name=zipcode]").blur(function() {
		if($(this).val() == "") $(this).attr({value:"enter zip code"});
	});
});

function validate_email(stremail) {
	var objRegExp  = /(.*)@(.*){3,}\.(.*)/i;
	
	return objRegExp.test(stremail);
}
