function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }
    
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;

$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".form-submit").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();


	var title = $("select#title").val();
	if (title == "") 
	{
		$("label#title_error").show();
		$("select#title").focus();
		return false;
	}
	var name = $("input#name").val();
	if (name == "") 
	{
		$("label#name_error").show();
		$("input#name").focus();
		return false;
	}
	var email = $("input#email").val();
	if (email == "") 
	{
		$("label#email_error").show();
		$("input#email").focus();
		return false;
	}
	var telephone = $("input#telephone").val();
	if (telephone == "") 
	{
		$("label#telephone_error").show();
		$("input#telephone").focus();
		return false;
	}
	var address1 = $("input#address1").val();
	var address2 = $("input#address2").val();
	var enquiry = $("textarea#enquiry").val();
	if (address1 == "") 
	{
		$("label#address1_error").show();
		$("input#address1").focus();
		return false;
	}
	var town = $("input#town").val();
	if (town == "") 
	{
		$("label#town_error").show();
		$("input#town").focus();
		return false;
	}
	var county = $("input#county").val();
	if (county == "") 
	{
		$("label#county_error").show();
		$("input#county").focus();
		return false;
	}
	var postcode = $("input#postcode").val();
	if (postcode == "") 
	{
		$("label#postcode_error").show();
		$("input#postcode").focus();
		return false;
	}
	var house_type = $("select#house_type").val();
	if (house_type == "") 
	{
		$("label#house_type_error").show();
		$("select#house_type").focus();
		return false;
	}
	var beds = $("select#beds").val();
	if (beds == "") 
	{
		$("label#beds_error").show();
		$("select#beds").focus();
		return false;
	}
		
		var dataString = 'title='+ title + '&name='+ name + '&email='+ email + '&telephone='+ telephone + '&enquiry='+ enquiry + '&address1='+ address1 + '&address2='+ address2 + '&town='+ town + '&county='+ county + '&postcode='+ postcode + '&house_type='+ house_type + '&beds='+ beds;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "inc/getaquote_process.php",
      data: dataString,
      success: function() {
        $('#main-contact').html("<div id='message'></div>");
        $('#message').html("<h3>Congratulations you have successfully submitted a quote to Wheldon EPC</h3>")
        .append("<p>Thank you for your time, we will be in touch soon.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#title").select().focus();
});
