$(function(){

	$("#trial_title").live("keyup", function(){
		formValid("trial");
	});
	
	$("#trial_first_name").live("keyup", function(){
		formValid("trial");
	});
	
	$("#trial_last_name").live("keyup", function(){
		formValid("trial");
	});
	
	$("#trial_primary_address_street").live("keyup", function(){
		formValid("trial");
	});
	
	$("#trial_primary_address_postalcode").live("keyup", function(){
		formValid("trial");
	});
	
	$("#trial_phone_home").live("keyup", function(){
		formValid("trial");
	});
	
	$("#trial_email1").live("keyup", function(){
		formValid("trial");
	});
	
	$("#trial_confirm-email").live("keyup", function(){
		formValid("trial");
	});
	
	$("#register-trial").live("click", function(){
		window.location.href="#";
		$("#lightbox").fadeIn(500);
		$("#lightbox_panel").fadeIn(500);
	});
	
	$("#register-trial-btn").live("click", function(){
	if (formValid("trial") == true){
		submitFORM($("#trialForm").serialize(), "trial");
	} 
	return false;
	});
	
	$("#lightbox").live("click", function(){
		$(this).fadeOut(500);
		$("#lightbox_panel").fadeOut(500);
	});
		
	$("#register-interest").click(function(){
	if (formValid("ri") == true){
		submitFORM($("#registerInterest").serialize(), "ri");
	} 
	return false;
	});

	$("#search-btn").click(function(){
		if (($("#number").val() != "House") && ($("#number").val() != "") && ($("#postcode").val() != "Postcode") && ($("#postcode").val() != "")){
			checkStatus($("#number").val(), $("#postcode").val());
			return false;
		}
		return false;
	});
	
	$("#number").click(function(){
		if ($(this).val() == "House"){
			$(this).val("");
		}
	});
	
	$("#number").blur(function(){
		if ($(this).val() == ""){
			$(this).val("House");
		}
	});
	
	$("#postcode").click(function(){
		if ($(this).val() == "Postcode"){
			$(this).val("");
		}
	});
	
	$("#postcode").blur(function(){
		if ($(this).val() == ""){
			$(this).val("Postcode");
		}
	});
	
	$(".close-btn").live("click", function(){
		$("#lightbox").fadeOut(500);
		$("#lightbox_panel").fadeOut(500);
	});
	
});

function checkStatus(house, postcode){			

	$(".search-result").html("<h4 class=\"pcs-title\">Please wait, searching...</h4>");

	$(document).scrollTo("#search-result");
	
	$.ajax({
		//url: "http://localhost/status.php?postcode=" + postcode + "&house=" + house,
		url: "http://www.fibreband.co.uk/status.php?postcode=" + postcode + "&house=" + house,
		success: function(data){
			$(".search-result").html(resultText(data));
		}
	});
	
	return false;
};

function resultText(result){
	if (result == "no"){
		html = "<h4 class=\"pcs-title\">Sorry,</h4>";
		html += "<p>Our service is being deployed to Bournemouth and certain surrounding areas. At present there are no plans to connect your postcode.</p>";
		$("#primary_address_street").val("");
		$("#primary_address_postalcode").val("");
	} else if (result == "available") {
		html = "<h4 class=\"pcs-title\">Congratulations...you're through!</h4>";
		html += "<p>We need a few more details to confirm we can connect you. Please click the button below, fill in the form and we'll be in touch as soon as possible.</p>";
		html += "<p><input type=\"image\" src=\"images/btn-trial.png\" id=\"register-trial\" name=\"register-trial\" />";
		$("#trial_primary_address_postalcode").val($("#postcode").val());
		$("#trial_primary_address_street").val($("#number").val());
		//$("#primary_address_street").val($("#number").val());
		//$("#primary_address_postalcode").val($("#postcode").val());
	} else if (result == "when-live"){
		html = "<h4 class=\"pcs-title\">We're almost there,</h4>";
		html += "<p>We're looking to connect you're area soon.  We won't be able to offer you a free trial, but please submit your details so that we can notify you when the service becomes available in your area.</p>";
		$("#primary_address_street").val("");
		$("#primary_address_postalcode").val("");
	}
	
	return html;
};

function formValid(type) {
	
	valid = true;
	
	$("#" + type + "_title").removeClass("invalid");
	$("#" + type + "_first_name").removeClass("invalid");
	$("#" + type + "_last_name").removeClass("invalid");
	$("#" + type + "_primary_address_street").removeClass("invalid");
	$("#" + type + "_primary_address_postalcode").removeClass("invalid");
	$("#" + type + "_phone_home").removeClass("invalid");
	$("#" + type + "_email1").removeClass("invalid");
	$("#" + type + "_confirm-email").removeClass("invalid");
	
	if ($("#" + type + "_title").val() == ""){
		$("#" + type + "_title").addClass("invalid");
		valid = false;
	} 
	
	if ($("#" + type + "_first_name").val() == ""){
		$("#" + type + "_first_name").addClass("invalid");
		valid = false;
	} 
	
	if ($("#" + type + "_last_name").val() == ""){
		$("#" + type + "_last_name").addClass("invalid");
		valid = false;
	} 
	
	if ($("#" + type + "_primary_address_street").val() == ""){
		$("#" + type + "_primary_address_street").addClass("invalid");
		valid = false;
	} 
	
	if ($("#" + type + "_primary_address_postalcode").val() == ""){
		$("#" + type + "_primary_address_postalcode").addClass("invalid");
		valid = false;
	} 
	
	if ($("#" + type + "_phone_home").val() == ""){
		$("#" + type + "_phone_home").addClass("invalid");
		valid = false;
	} 
	
	if ($("#" + type + "_email1").val() == ""){
		$("#" + type + "_email1").addClass("invalid");
		valid = false;
	} 
	
	if ($("#" + type + "_confirm-email").val() == ""){
		$("#" + type + "_confirm-email").addClass("invalid");
		valid = false;
	} 
	
	if ($("#" + type + "_email1").val() !== $("#" + type + "_confirm-email").val()){
		$("#" + type + "_email1").addClass("invalid");
		$("#" + type + "_confirm-email").addClass("invalid");
		valid = false;
	}
	
	return valid;

};

function completeForm(fOrm) {
	if (fOrm == "trial"){
		html = "<h4 class=\"pcs-title\" style=\"text-align: left;\">Thank You!</h4>";
		html += "<p style=\"text-align: left;\">Thank you for submitting your details, one of our team will be in touch soon.</p>";
		$(".search-result").html(html);
		$("#trialForm")[0].reset();
		$("#lightbox").fadeOut(500);
		$("#lightbox_panel").fadeOut(500);
	} else {
		html = "<h4 class=\"pcs-title\" style=\"text-align: left;\">Thank You!</h4>";
		html += "<p style=\"text-align: left;\">Thank you for registering you're interest. We'll keep you up to date.</p>";
		$(".search-result").html(html);
		$("#registerInterest")[0].reset();
	}
	
};

function submitFORM(dataString, fOrm){
	
	if (fOrm == "trial"){
		//html1 = "<h3 class=\"black\">Trial request pending</h3>";
		//html1 += "<p class=\"black\">Your trial request is being recorded, please wait..</p>"; 
		//$("#lightbox_panel").html(html1);
		$("#lightbox_panel").fadeOut(500);
		$("#lightbox").fadeOut(500);
	} 
	html = "<h4 class=\"pcs-title\" style=\"text-align: left;\">Please wait...</h4>";
	html += "<p style=\"text-align: left;\">You're request is being submitted.</p>";
	$(".search-result").html(html);
	
	$.ajax({
		url: "http://www.fibreband.co.uk/scripts/garedirect.php",
		//url: "http://localhost/scripts/garedirect.php",
		data: dataString,
		type: "POST",
		async: false
	});	
	
	completeForm(fOrm);
};
