// JavaScript Document

function resetPage() 
{
	$(".panel").hide();
	$("#divSteps").hide();
	$("#divSlogan").fadeIn(1000);
	$("#imgWelcome").fadeIn(1200);
	$('#divButtons').fadeIn();
	
	$("#btnCalculator").unbind("click").text('Mortgage Calculator').toggle(
		function() {
			$(".panel").hide();
			$("#divMainPanel").animate({ height: "380px" }, 1000 );
			setTimeout( $("#divCalculator").fadeIn(1200), 20000);	
		},
		function() {
			$(".panel").hide();
			$("#divMainPanel").animate({ height: "350px" }, 1000 );		
			$("#divSlogan").fadeIn(1200);
	});
	$("#btnDislcosure").click(function() {
		$(".panel").hide();
		$("#divDisclosure").fadeIn();
	});
	$("#btnAbout").click(function() {
		$(".panel").hide();
		setTimeout($("#divAbout").fadeIn(), 10000);
	});
	$("#btnStart").unbind("click").text('Start Here').click(function() {
		$(".panel").hide();	
		$("#divForm").fadeOut().html("").load("step1.php").fadeIn();
	});
}
 
 function getData()
{
	var dataString;
	$.each($("#divForm input"), function(){
		switch ($(this).attr('type'))
		{
		case 'checkbox':
			if ($(this).attr('checked') == true) dataString += "&" + $(this).attr("name") + "=" + $(this).val();
			break;
		case 'radio':
			if ($(this).attr('checked') == true) dataString += "&" + $(this).attr("name") + "=" + $(this).val();
			break;
		default:
		    dataString += "&" + $(this).attr("name") + "=" + $(this).val();
		}
	});	
	$.each($("#divForm select"), function(){
		dataString += "&" + $(this).attr("name") + "=" + $(this).children(":selected").val();
	});
	$.each($("#divForm textarea"), function(){
		dataString += "&" + $(this).attr("name") + "=" + $(this).val();
	});
	return dataString;
}

function loadStep(step)
{
	var dataString = getData();
	$.ajax({
		   type: "POST",
		   url: "step" + step + ".php",
		   data: dataString,
		   success: function(msg) {
			$("#divForm").hide()
			.html(msg)
			.fadeIn(1500, function()
								   {
									//alert(dataString);   
								   });   
		   }
	});
}

 function formStart()
 {
	 $("#divMainPanel").animate({ height: "380px" }, 1000 );
	 $("#btnCalculator").text('Back').unbind("click").click(function() {
		resetPage();												
	});	
	 $("#btnStart").text('Next').unbind("click").click(function() {
		loadStep("2");
	});
	 $("#divSteps").fadeIn().children("span").removeClass("currentStep").filter("#spStep1").addClass("currentStep");
 }
 
 function step2()
 {
	 $("#btnStart").unbind("click").click(function() {
		loadStep("3");
	})
	 $("#btnCalculator").unbind("click").click(function() {
		$("#divForm").fadeOut().load("step1.php", {}, function() {	$("#divForm").fadeIn();	});											
	});	
	 $("#divSteps").children("span").removeClass("currentStep").filter("#spStep2").addClass("currentStep");
 }
 
  function step3()
 {
	 $("#btnStart").unbind("click").click(function() {
		loadStep("4");
	})
	 $("#btnCalculator").unbind("click").click(function() {
		$("#divForm").fadeOut().load("step2.php", {}, function() {	$("#divForm").fadeIn();	});
	});
	 $("#divSteps").children("span").removeClass("currentStep").filter("#spStep3").addClass("currentStep");
 }
 
   function step4()
 {
	 $("#btnStart").text("Submit").unbind("click").click(function() {
		var dataString = getData();
		$.ajax({
		   type: "POST",
		   url: "sendmail.php",
		   data: dataString,
		   success: function(msg) {
			$("#divForm").hide()
			.html(msg)
			.fadeIn(1500);   
		   }
		});
		//resetPage();
		//submit ajax  
	})
	 $("#btnCalculator").unbind("click").click(function() {
		$("#divForm").fadeOut().load("step3.php", {}, function() {	$("#divForm").fadeIn();	});											
	});
	 $("#divSteps").children("span").removeClass("currentStep").filter("#spStep4").addClass("currentStep");
 }
 
 /* from old js file
 
 function loadStep(step)
{
	var dataString = getData();
	$.ajax({
		   type: "POST",
		   url: "step" + step + ".php",
		   data: dataString,
		   success: function(msg) {
			$("#divForm").hide()
			.html(msg)
			.fadeIn(1500);   
		   }
	});
}

 function formStart()
 {
	 $("#divMainPanel").animate({ height: "380px" }, 1000 );
	 $("#btnCalculator").text('Back').unbind("click").click(function() {
		resetPage();												
	});	
	 $("#btnStart").text('Next').unbind("click").click(function() {
		loadStep("2");
	});
	 $("#divSteps").fadeIn().children("span").removeClass("currentStep").filter("#spStep1").addClass("currentStep");
 }
 
 function step2()
 {
	 $("#btnStart").unbind("click").click(function() {
		loadStep("3");
	})
	 $("#btnCalculator").unbind("click").click(function() {
		loadStep("1");											
	});	
	 $("#divSteps").children("span").removeClass("currentStep").filter("#spStep2").addClass("currentStep");
 }
 
  function step3()
 {
	 $("#btnStart").text("Next").unbind("click").click(function() {
		loadStep("4");
	})
	 $("#btnCalculator").unbind("click").click(function() {
		loadStep("2");
	});
	 $("#divSteps").children("span").removeClass("currentStep").filter("#spStep3").addClass("currentStep");
 }
 
   function step4()
 {
	 $("#btnStart").text("Submit").unbind("click").click(function() {
	    var dataString = getData();
		$.ajax({
		   type: "POST",
		   url: "sendmail.php",
		   data: dataString,
		   success: function(msg) {
			$("#divForm").hide()
			.html(msg)
			.fadeIn(1500);   
		   }
		});
	});
	 $("#btnCalculator").unbind("click").click(function() {
		loadStep("3");								
	});
	 $("#divSteps").children("span").removeClass("currentStep").filter("#spStep4").addClass("currentStep");
 }
 */