var FORM_NAME = "quick_form";
var FORM_TOP = "140px";
var FORM_LEFT = "140px";
var thePopup = null;
var inMail = null;

function DoQuickContact() {
  inMail = false;
  if ( !thePopup) {
    thePopup = new myXHR();
    thePopup.Get("quick_contact_form.xhtml");
  }
  else
    thePopup.Show();
}
function DoSubmit() {
  inMail = true;
  var frm = document.getElementById(FORM_NAME);
  var services = [];
  for ( var i=0; i < frm.services.options.length; i++) {
    if ( frm.services.options[i].selected)
      services.push(frm.services.options[i].text);
  }
  services = services.join(", ");
  var theData = "mail="+frm.mail.value+"&name="+frm.name.value+"&phone="+frm.phone.value;
  theData += "&services="+services+"&question="+frm.question.value;
  thePopup.Get("quick_mail.php?"+theData);
}

myXHR = function() {
  this.container = null;
}
myXHR.prototype = new Neaux.Ajax();

myXHR.prototype.Show = function() {
  var frm = document.getElementById(FORM_NAME);
  frm.reset();
  this.container.style.display = "block";
  var p = document.getElementById("thank_you");
  p.style.display = "none";
}
myXHR.prototype.Hide = function() {
  this.container.style.display = "none";
}

myXHR.prototype.onload = function() {
  if ( !inMail) {
    var theContainer = document.getElementById("container");
    var theDiv = document.createElement("div");
    theDiv.id = "quick_box";
    theDiv.innerHTML = this.Text;
    theDiv.style.position = "absolute";
    theDiv.style.left = FORM_LEFT;
    theDiv.style.top = FORM_TOP;
    theContainer.appendChild(theDiv);
    this.container = theDiv; 
    var frm = document.getElementById(FORM_NAME);
    frm.onsubmit = function() {
      var eFilter=/^.+@.+\..{2,3}$/
      if( this.name.value == "") {
        alert("Please enter your name");
        return false;
      }
      else if ( this.mail.value == "") {
        alert("Please enter your email address");
        return false;
      }
      else if ( !eFilter.test(this.mail.value)) {
        alert("Please input a valid email address!");
        return false;
      }
      inMail = true;
    }
    frm.action = "javascript:DoSubmit();";
    this.Show();
  }
  else if (Boolean(this.Text)) {
    var p = document.getElementById("thank_you");
    var frm = document.getElementById(FORM_NAME);
    p.style.position = "absolute";
    p.style.top = frm.offsetTop+"px";
    p.style.left = frm.offsetLeft+"px";
    p.style.width = frm.offsetWidth+"px";
    p.style.height = frm.offsetHeight+"px";
    p.style.display = "block";
  }
  else
    alert("There was a problem sending your email\n\nPlease check your internet connection\nand click on the submit button again");
}
