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() {

  $(".submit").click(function() {
		// validate and process form
		// first hide any error messages
		
	var name = $("input#name").val();
	if (name == "" || name == "enter your name")
	{
	      $("input#name").css("backgroundColor", "#ffaaaa");
	      return false;
	
	}
	else
	{
	      $("input#name").css("backgroundColor", "#fff");	
	}
	
	var contact = $("input#contact").val();
	if (contact == "" || contact == "enter your tel or email")
	{
	      $("input#contact").css("backgroundColor", "#ffaaaa");
	      return false;
	}
	else
	{
	      $("input#contact").css("backgroundColor", "#fff");	
	}
	
	var enquiry = $("textarea#enquiry").val();
	if (enquiry == "" || enquiry == "enter your enquiry")
	{
	      $("textarea#enquiry").css("backgroundColor", "#ffaaaa");
	      return false;
	}
	else
	{
	      $("textarea#enquiry").css("backgroundColor", "#fff");	
	}
		
	var dataString = 'name='+ name + '&contact=' + contact + '&enquiry=' + enquiry;
	//alert (dataString);return false;
		
	$.ajax({
      type: "POST",
      url: "contact_process.php",
      data: dataString,
      success: function() {
        $('.contact').html("<div id='message'></div>");
        $('#message').html("<h3>Contact Form Submitted!</h3>")
        .append("<p>We will be in touch soon.</p><br /><img src='images/msg_sent.jpg' alt='email sent' />")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  //$("input#name").select().focus();
});


function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}
function fillText(thefield){
if (thefield.value=="")
thefield.value = thefield.defaultValue
}
