// JavaScript Document
// Submits form data

$(function() {
	// Need to pre cache part3 background image so the fade in animation runs smoothely
	image1 = new Image();
	image1.src = "images/home/app/part-3.gif";
	image2 = new Image();
	image2.src = "images/home/app/update-popup.gif";
	
	// Set up replacement, adding in javascript elements as java is enabled
	$("#get-started").attr("href","javascript:apanim1();");
	$("#try-again").attr("href","javascript:apanim1();");
	//$("#index-aplication-form").attr("action","javascript:apanim2();");
	$("#index-aplication-form").attr("action","javascript:apanim1();");
	$("#close-ap").attr("href","javascript:apanim2();");
	$("#part1-slider").html("<div id='slider'></div> &euro; <input name='how-much' id='how-much'>");
	$("#part2-slider").html("<div id='slider2'></div> <input name='how-many-creditors' id='how-many-creditors'>");
	
	// Set up "how much do you owe" slider
	$("#slider").slider({
		value:10000,
		min: 0,
		max: 100000,
		step: 500,
		slide: function(event, ui) {
			$("#how-much").val(ui.value);
		}
	});
	$("#how-much").val($("#slider").slider("value"));
	
	// Set up "how many creditors do you have" slider
	$("#slider2").slider({
		value:2,
		min: 0,
		max: 20,
		step: 1,
		slide: function(event, ui) {
			$("#how-many-creditors").val(ui.value);
		}
	});
	$("#how-many-creditors").val($("#slider2").slider("value"));
	
});

// Application animation, sliding from part one to part two
function apanim1()
{
	// by default hide the error message
	$("#debt-worried-help").css("display","none");
	
	// check if we are on stage two, if so slide back to stage one
	if ($("#index-ap-content").css("marginLeft")=="-439px")
	{
		$("#index-aplication-form").attr("action","javascript:apanim1();");
		$("#index-ap-content").animate({ 
        	marginLeft: "0"
      	}, 400 );
	}
	// otherwise slide to stage two
	else
	{
		// if the user has not selected yes or no
		if(($("#debt-worried-yes").attr("checked")==false)&&($("#debt-worried-no").attr("checked")==false))
		{
			// display the error box
			$("#debt-worried-help").fadeIn("fast");
		}
		else
		{
			$("#index-aplication-form").attr("action","javascript:apanim2();");
			$("#index-ap-content").animate({ 
				marginLeft: "-439"
			}, 400 );
		}
	}
}
// Application animation, displaying (by fade) of part 3
function apanim2()
{
	// by default hide the error message
	$("#whereinuk-help").css("display","none");
	
	// Check if user has selected a value for where they live
	if($("#app-live").prop("value")=="")
	{
		// display the error box
		$("#whereinuk-help").fadeIn("fast");
	}
	else
	{
		// get the values from the form
		var dataArray = $('#index-aplication-form').serialize();  
		
		// post to index-table and then display the results in the right div
		$.post("includes/debt_check/part3.php", dataArray,
		function(data){
			$("div#index-ap-part3-hidden").html(data);
		  });
	
		if ($("div#index-ap-part3-hidden").css("display")=="none")
		{
			// scroll the screen so that the ap is the main focus
			$('html, body').animate({
				scrollTop: $("#index-bottom-content").offset().top - 20
			}, 1000);
			
			// fade in part three
			$("div#index-ap-part3-hidden").fadeIn("slow");
		}
		else
		{
			$("div#app-updated").fadeIn(1000);
			$("div#app-updated").fadeOut(1000);
		}
		
	}
}
// Application animation, hiding (by fade) of part 3
function apanim3()
{
	$("div#index-ap-part3-hidden").fadeOut("slow");
}
// Call me form submision, on application
function indexFormSend()
{
	// some basic form validation
	var formvalidate = true;
	var errorMsg = "Please enter:";
	
	// Reset all inputs to standard border
	$("#index-email-form input").css("border","1px solid #767676");
	
	if ($("#ap-email-name").prop("value")=="")
	{
		errorMsg += ", your name";
		$("#ap-email-name").css("border","1px solid #990000");
		formvalidate = false;
	}
	if ($("#ap-email-tel").prop("value")=="")
	{
		errorMsg += ", your telephone number";
		$("#ap-email-tel").css("border","1px solid #990000");
		formvalidate = false;
	}
	if ($("#ap-email-email").prop("value")=="")
	{
		errorMsg += ", your email address";
		$("#ap-email-email").css("border","1px solid #990000");
		formvalidate = false;
	}
	else if (!validateEmail($("#ap-email-email").prop("value")))
	{
		errorMsg += ", a valid email address";
		$("#ap-email-name").css("border","1px solid #990000");
		formvalidate = false;	
	}
	if ($("#ap-email-time").prop("value")=="")
	{
		errorMsg += ", the best time to contact you";
		$("#ap-email-time").css("border","1px solid #990000");
		formvalidate = false;
	}
	// Because the first error will produce "Please Enter:, text" replace ":," with ":"
	errorMsg = errorMsg.replace(":,",":");
	// Add a full stop to the end of the msg
	errorMsg += ".";
	
	// if the validtion throws up any errors then display them
	if (formvalidate==false)
	{
		$("div#ap-email-error").html(errorMsg);
		$("div#ap-email-error").fadeIn(500);
	}
	// otherwise post the form
	else
	{
		// get the values from the form
		var dataArray = $('#index-email-form').serialize();  
			
		// post to index-table and then display the results in the right div
		$.post("includes/debt_check/part3.php", dataArray,
		function(data){
			$("div#index-ap-part3-hidden").html(data);
		});
	}
}

// validation
function validateEmail(value)
{
	var filter = /^.+@.+\..+$/;
	
	if ( !filter.test(value) )
	{
		return false;
	}
	
	return true;
}

function validateTelephone(value)
{
	var filter = /^([0-9 ]{7,})$/;

	if ( !filter.test(value) )
	{
		return false;
	}
	
	return true;

}
