$(document).ready(function() {

 function showElement(elemID) {
  showOverlayDiv();
  showLoadingImage();

  arrPageSize = getPageSize();
  arrPageScroll = getPageScroll();

  var objElement = $('#' + elemID);

  if ($.browser.msie) {
   if ((arrPageSize[2] < arrPageSize[0])) {
    objElementTop = (((arrPageSize[3] - objElement.height()) / 2) + 9);
   } else {
    objElementTop = (((arrPageSize[3] - objElement.height()) / 2));
   }//if

   if ((arrPageSize[3] < arrPageSize[1])) {
    objElementLeft = (((arrPageSize[2] - objElement.width()) / 2) + 9);
   } else {
    objElementLeft = (((arrPageSize[2] - objElement.width()) / 2));
   }//if
  } else {
   objElementTop = (((arrPageSize[3] - objElement.height()) / 2) - 9);
   objElementLeft = (((arrPageSize[2] - objElement.width()) / 2) - 9);
  }//if

  if (objElementTop < 0) { objElementTop = 0; }//if
  if (objElementLeft < 0) { objElementLeft = 0; }//if

  objElement.css("top", arrPageScroll[1] + objElementTop + "px");
  objElement.css("left", arrPageScroll[0] + objElementLeft + "px");

  objElement.show();
  hideLoadingImage();
 }//function

 function hideElement(elemID) {
  var objElement = $('#' + elemID);
  objElement.hide();
  hideOverlayDiv();
 }//function

 function showLoadingImage () {
  arrPageSize = getPageSize();
  arrPageScroll = getPageScroll();

  $("#loadingimage").css("position", "absolute");
  $("#loadingimage").css("z-index", "2");
  $("#loadingimage").css("top", (arrPageScroll[1] + ((arrPageSize[3] - 16) / 2)) + "px");
  $("#loadingimage").css("left", (arrPageScroll[0] + ((arrPageSize[2] - 16) / 2)) + "px");
  $("#loadingimage").show();
 }//function

 function hideLoadingImage () {
  $("#loadingimage").hide();
 }//function

 function showOverlayDiv () {
  arrPageSize = getPageSize();

  overlayDiv = document.createElement("div");
  $(overlayDiv).attr("id", "overlay");
  $(overlayDiv).css("height", arrPageSize[1] + "px");
  $(overlayDiv).css("position", "absolute");
  $(overlayDiv).css("top", "0");
  $(overlayDiv).css("left", "0");
  $(overlayDiv).css("width", "100%");
  $(overlayDiv).css("z-index", "1");

  $("body").append(overlayDiv);

  $("select").hide();
 }//function

 function hideOverlayDiv () {
  $("div#overlay").remove();
  $("select").show();
 }//function

 // Core code from - quirksmode.org
 function getPageScroll(){
 	var yScroll;

 	if (self.pageYOffset) {
 		yScroll = self.pageYOffset;
 	} else if (document.documentElement && document.documentElement.scrollTop) {
 		yScroll = document.documentElement.scrollTop;
 	} else if (document.body) {
 		yScroll = document.body.scrollTop;
 	}//if

 	arrayPageScroll = new Array('', yScroll)
 	return arrayPageScroll;
 }

 // Core code from - quirksmode.org
 // Edit for Firefox by pHaez
 function getPageSize(){
 	var xScroll, yScroll;

 	if (window.innerHeight && window.scrollMaxY) {
 		xScroll = document.body.scrollWidth;
 		yScroll = window.innerHeight + window.scrollMaxY;
 	} else if (document.body.scrollHeight > document.body.offsetHeight) {
 		xScroll = document.body.scrollWidth;
 		yScroll = document.body.scrollHeight;
 	} else {
 		xScroll = document.body.offsetWidth;
 		yScroll = document.body.offsetHeight;
 	}//if

 	var windowWidth, windowHeight;
 	if (self.innerHeight) {
 		windowWidth = self.innerWidth;
 		windowHeight = self.innerHeight;
 	} else if (document.documentElement && document.documentElement.clientHeight) {
 		windowWidth = document.documentElement.clientWidth;
 		windowHeight = document.documentElement.clientHeight;
 	} else if (document.body) {
 		windowWidth = document.body.clientWidth;
 		windowHeight = document.body.clientHeight;
 	}//if

 	if(yScroll < windowHeight){
 		pageHeight = windowHeight;
 	} else {
 		pageHeight = yScroll;
 	}//if

 	if(xScroll < windowWidth){
 		pageWidth = windowWidth;
 	} else {
 		pageWidth = xScroll;
 	}//if

 	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
 	return arrayPageSize;
 }

 $('ul#nav a[@href="enquiry.php"]').click( function() {
  showElement('enquiry');
  return false;
 });

 $('div#enquiry img#closebutton').click( function() {
  $('div.field input').each(function() {
   $(this).val('');
  });
  $('div.field textarea').each(function() {
   $(this).val('');
  });
  $('div.field input.checkbox').each(function() {
   $(this).attr('checked', false);
  });
  hideElement('enquiry');
 });

 $("form#form-enquiry").submit(function() {

  $('div.field.error').each(function() {
   $(this).removeClass('error');
  });

  strErrors = '';
  if ($('#input-enquiry-name').val() == null || $('#input-enquiry-name').val() == '') {
   $('#input-enquiry-name').parents('div').addClass('error');
   strErrors += " - Name is a required field\r\n" ;
  }//if
  if ($('#input-enquiry-email').val() == null || $('#input-enquiry-email').val() == '') {
   $('#input-enquiry-email').parents('div').addClass('error');
   strErrors += " - Email is a required field\r\n" ;
  }//if

  if (strErrors.length > 0) {
   strErrors = "Please correct the errors with your form:\r\n\r\n" + strErrors;
   alert(strErrors);
   return false;
  }//if

  //Now post form results to form handler
  $.post('enquiry.php', $(this).serialize(), function(xmlData) {
   strMessages = '';

   $(xmlData).find('message').each( function() {
    strMessages += $(this).text() + "\r\n";
   });

   alert(strMessages);

   if ($(xmlData).find('sent').text() == 'true') {
    $('div#enquiry img#closebutton').click();
   }//if
  });

  return false;

 });

});