$(document).ready(function() {
	
	
	function page3(){
		var thank_you = "<div id='thank_container'>"+
    	"<p id='thank'>Thank you for your request.</p>"+
    	"<p id='we_will'>We will mail you your $15.00 coupon booklet shortly.</p>"+
		"<a id='close' class='button block' href=''>close</a>"+
    	"</div>";
		$("#container").fadeOut(500, function (){$('body').prepend(thank_you).fadeIn(500);
														$("#close").click(function(event){
																				   event.preventDefault();
																				   self.close();});						   
																			  
										  });
	}
	
	//when key is pressed in the textbox
	$("#zip").keyup(function (e)
	{
	  this.value = this.value.replace(/[^0-9\.]/g,'');
	  var len = this.value.length;
                if (len >= 5) {
                    this.value = this.value.substring(0, 5);
                }
	});
	
	$("#phone").keyup(function (e)
	{
	  this.value = this.value.replace(/[^0-9\-\.]/g,'');
	  var len = this.value.length;
                if (len == 3) {
                    this.value = this.value + "-";
                }
				if (len == 7) {
                    this.value = this.value + "-";
                }
				if (len >= 12) {
                    this.value = this.value.substring(0, 12);
                }
	});
	
	$('#reset').click(function(event){
							   $("#error").remove();
							   $(".text").animate({
												  borderBottomColor: '#96afc8',
												  borderTopColor: '#96afc8',
												  borderLeftColor: '#96afc8',
												  borderRightColor: '#96afc8'}, 300);
							   $('#select_container').animate({ 
																	   borderBottomColor: '#e4f1ff',
																	   borderTopColor: '#e4f1ff',
																	   borderLeftColor: '#e4f1ff',
																	   borderRightColor: '#e4f1ff'}, 300);
							   event.preventDefault();
							   $('#first_name').val("");
							   $('#last_name').val("");
							   $('#address1').val("");
							   $('#address2').val("");
							   $('#city').val("");
							   $('#state').attr("selectedIndex", "0");;
							   $('#zip').val("");
							   $('#phone').val("");
							   $('#email').val("");
							   });
	
    $("#submit").click(function() {
								 
								 $("#error").remove();
								 var first_name_orig = $('#first_name').val();
								  var last_name_orig = $('#last_name').val();
								  var address1_orig = $('#address1').val();
								  var address2_orig = $('#address2').val();
								  var city_orig = $('#city').val();
								  var state_orig = $('#state').val();
								  var zip_orig = $('#zip').val();
								  var phone_orig = $('#phone').val();
								  var email_orig = $('#email').val();
								 
								 $.ajax({
									  url: "checkform.php",
									  type: "POST",
									  cache: false,
									  data: "first_name="+$('#first_name').val()+
									  		"&last_name="+$('#last_name').val()+
											"&address1="+$('#address1').val()+
											"&address2="+$('#address2').val()+
											"&city="+$('#city').val()+
											"&state="+$('#state').val()+
											"&zip="+$('#zip').val()+
											"&phone="+$('#phone').val()+
											"&email="+$('#email').val()+
											"&ajax=yes",
									  dataType: "json",
									  success: function(data){
										  var first_name = data.first_name;
										  var last_name = data.last_name;
										  var address1 = data.address1;
										  var address2 = data.address2;
										  var city = data.city;
										  var state = data.state;
										  var zip = data.zip;
										  var phone = data.phone;
										  var email = data.email;
										  var missing = (first_name == "missing" || last_name == "missing" || address1 == "missing" || city == "missing" || state == "missing" || zip == "missing" || email == "missing");
										  var invalidmail = (email == "invalidmail");
										  if (missing || invalidmail){
											  var error_string = "<div id='error'><div id='inner'><ul>";
											  if (missing)
											  	error_string += "<li>Please fill in all fields.</li>";
											  if (invalidmail)
											  	error_string += "<li>Please enter a valid email address.</li>";
											  //if (missing && inapp && invalidmail)
											  $('body').prepend(error_string);
											  if ((first_name == "missing")){
												$("#first_name").animate( {  borderBottomColor: '#F00',
																	   borderTopColor: '#F00',
																	   borderLeftColor: '#F00',
																	   borderRightColor: '#F00'}, 500);
											  }
											  if ((last_name == "missing")){
											  	$("#last_name").animate( { borderBottomColor: '#F00',
																	   borderTopColor: '#F00',
																	   borderLeftColor: '#F00',
																	   borderRightColor: '#F00'}, 500);
											  }
											  if ((address1 == "missing")){
												$("#address1").animate( { borderBottomColor: '#F00',
																	   borderTopColor: '#F00',
																	   borderLeftColor: '#F00',
																	   borderRightColor: '#F00'}, 500);
											  }
											  if ((city == "missing")){
												$("#city").animate( { borderBottomColor: '#F00',
																	   borderTopColor: '#F00',
																	   borderLeftColor: '#F00',
																	   borderRightColor: '#F00'}, 500);
											  }
											  if ((state == "missing")){
												$("#select_container").animate( { borderBottomColor: '#F00',
																	   borderTopColor: '#F00',
																	   borderLeftColor: '#F00',
																	   borderRightColor: '#F00'}, 500);
											  }
											  if ((zip == "missing")){
												$("#zip").animate( { borderBottomColor: '#F00',
																	   borderTopColor: '#F00',
																	   borderLeftColor: '#F00',
																	   borderRightColor: '#F00'}, 500);
											  }
											  if ((email == "missing") || (email == "invalidmail")){
												$("#email").animate( { borderBottomColor: '#F00',
																	   borderTopColor: '#F00',
																	   borderLeftColor: '#F00',
																	   borderRightColor: '#F00'}, 500);
											  }
											  $(".text").focus(function(){$(this).animate({ 
																	   borderBottomColor: '#96afc8',
																	   borderTopColor: '#96afc8',
																	   borderLeftColor: '#96afc8',
																	   borderRightColor: '#96afc8'}, 300);});
											  $("select").focus(function(){$(this).parent().animate({ 
																	   borderBottomColor: '#e4f1ff',
																	   borderTopColor: '#e4f1ff',
																	   borderLeftColor: '#e4f1ff',
																	   borderRightColor: '#e4f1ff'}, 300);});
										  }
										  else{
											  $.ajax({
													  url: "sendmail.php",
													  type: "POST",
													  cache: false,
													  data: "first_name="+first_name_orig+"&last_name="+last_name_orig+"&address1="+address1_orig+"&address2="+address2_orig+"&city="+city_orig+"&state="+state_orig+"&zip="+zip_orig+"&phone="+phone_orig+"&email="+email_orig+"&ajax=yes",
													  dataType: "json",
													  success: function(data){
														  page3();
														  },
													  error: function(e, xhr){alert("Error in sending mail.  Please try again.");}
																						   });
										  }
										  
									  },
									  error: function(e, xhr){
											alert("Error.  Please try again.");
									  }
									});
				return false;
			});				 
});
