function initialiseContactForm()
{
  var ele = document.getElementById("stdbutton");
  if (ele != null) {ele.style.display = "none";}
  ele = document.getElementById("jsbutton");
  if (ele != null) {ele.style.display = "block";}
  setElementValue("fjs","yes");
}

function setElementValue(elementid,elementvalue)
{
  var ele = document.getElementById(elementid);
  if (ele != undefined)
  {
    ele.value = elementvalue;
  }
}
function elementToStr(elementid)
{
  var ele = document.getElementById(elementid);
  if (ele != undefined)
  {
    return ele.value;
  }
  else
  {
    return "";
  }
}

function validEnquiry()
{
  var sError = "";
  if (elementToStr("cname") == "")
  {
    sError += "<li>You haven't entered your name</li>";
  }
  if ((elementToStr("cemail") == "") && (elementToStr("cphone") == ""))
  {
    sError += "<li>You haven't entered an email address or phone number</li>";
  }
  if (elementToStr("cenq") == "")
  {
    sError += "<li>You haven't entered any enquiry</li>";
  }
  return sError;
}

function submitEnquiry()
{
  var sError = validEnquiry();
  if (sError == "")
  {
    showDialog('Sending Enquiry','<p>Your enquiry is being sent... please wait</p><p align="center"><img src="images/loading.gif" width="32" height="32" alt="Working"></p>','working');
    new Ajax.Request('sendenquiry.php', {
      method: 'post',
      parameters: $('contactform').serialize(true),
      onSuccess: successSending,
      onFailure: failedSending
      });
  }
  else
  {
    showDialog('Contact Form','<p>Sorry - the contact form was not filled in correctly:<br /><ul>' + sError + '</ul></p>' +
      '<div class="cen"><div id="nav"><ul><li><a href="javascript:hideDialog();" title="Close"><span>&nbsp;&nbsp;Close&nbsp;&nbsp;</span></a></li></ul></div></div>','error');
  }
}

function successSending(transport)
{
  if (transport.responseText == '')
  {
    hideDialogImmediate();
    showDialog('Enquiry Sent','<p><strong>Your enquiry has been sent</strong> - we\'ll get back to you as soon as we can.</p><p>Thank you for your interest in the Aviator.</p>' +
      '<div class="cen"><div id="nav"><ul><li><a href="javascript:finishedContact();" title="Close"><span>&nbsp;&nbsp;Close&nbsp;&nbsp;</span></a></li></ul></div></div>','success');
  }
  else
  {
    failedSending(transport);
  }
}

function finishedContact()
{
  hideDialogImmediate();
  window.location = "home.php";
}

function failedSending(transport)
{
  hideDialogImmediate();
  showDialog('Error Sending','<p><strong>Sorry, there was an error when sending your enquiry.</strong></p><p>Please try phoning or emailing us instead. Thank you for your interest in the Aviator.</p>' +
    '<p>' + transport.responseText + '</p>' +
    '<div class="cen"><div id="nav"><ul><li><a href="javascript:hideDialog();" title="Close"><span>&nbsp;&nbsp;Close&nbsp;&nbsp;</span></a></li></ul></div></div>','error');
}