var theExpert = null;
var inMail = null;
function DoAskTheExpert( aLeft, aTop) {
  inMail = false;
  if ( !theExpert) {
    theExpert = new myHttpRequest( aLeft, aTop);
    theExpert.Get("expert_form.xhtml");
  }
  else
    theExpert.Show();
}
function SubmitTheQuestion() {
  inMail = true;
  var frm = document.getElementById("expert_form");
  var theData = "from="+frm.from.value+"&name="+frm.name.value+"&question="+frm.question.value;
  theExpert.Get("ask_the_expert.php?"+theData);
}


Neaux.Ajax = function() {
  this._HttpRequest = this._CreateNewRequest();
  this._TimerID = null;

  this.Timeout = null;  
  this.Status = null;
  this.State = null;
  this.Text = null;
  this.XML = null;
}
// Public Methods
Neaux.Ajax.prototype.Get = function( aUrl) { this._Send( aUrl, "GET"); }
Neaux.Ajax.prototype.Post = function( aUrl, aData) { this._Send( aUrl, "POST", aData); }
Neaux.Ajax.prototype.SetTimer = function ( aTimerMS) {
  this.Timeout = aTimerMS ? aTimerMS : null;
  if ( !this.Timeout && this._TimerID)
    this._TimerID = Neaux.Event.ClearTimeout();
}
// Event Handlers
Neaux.Ajax.prototype.onload = function() {};
Neaux.Ajax.prototype.onerror = function() {};
Neaux.Ajax.prototype.onprogress = function() {};
Neaux.Ajax.prototype.ontimeout = function() {};

// XML HTTP States 
Neaux.Ajax.prototype.ReadyState = { 
  UNSENT           : 0,
  OPENED           : 1,
  HEADERS_RECEIVED : 2,
  LOADING          : 3,
  DONE             : 4,
  TIMED_OUT        : 99
}

// HTTP Status Codes
Neaux.Ajax.prototype.HttpStatus = { 
  INITIALIZED   : 0,
	OK            : 200,
	CREATED       : 201,
	ACCEPTED      : 202,
	NO_RESPONSE   : 204,	
	BAD_REQUEST   : 400,
	UNAUTHORIZED  : 401,
	FORBIDDEN     : 403,
	NOT_FOUND     : 404,		
	SERVER_ERROR  : 500,
  APP_TIMEOUT   : 999
};

// Private Methods
Neaux.Ajax.prototype._CreateNewRequest = function() {
  try { return new XMLHttpRequest(); } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
  throw new Error("The User Agent (Browser) does not support XML Http Requests.");
}


Neaux.Ajax.prototype._Send = function( aUrl, aMethod, aData) {
  this._InitState();
  var theData = null;
  if ( aMethod == "POST") {
    this._HttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    theData = aData;
  }
  this._InitState();
  if ( this.Timeout)
    this._TimerID = Neaux.Event.SetTimout(this, this._TimeoutHandler, this.Timeout);
  this._HttpRequest.open(aMethod, aUrl, true);
  try {
    var _this = this;
    this._HttpRequest.onreadystatechange = function() {
      var xmlHttp = _this._HttpRequest;
      if ( xmlHttp.readyState == _this.ReadyState.DONE) {
        _this._UpdateState();
        if ( _this._TimerID)
          _this._TimerID = Neaux.Event.ClearTimeout(_this._TimerID);
        _this.onload(this);
      }
      else
        _this.onprogress(this);
    } 
  }
  catch(e) {
    alert("failed to connect");
    this.onload(this);
  }
  this._HttpRequest.send(theData);
}
Neaux.Ajax.prototype._InitState = function() {
  this.Status = this.HttpStatus.INITIALIZED;
  this.State = this.ReadyState.UNSENT;
  this.Text = null;
  this.XML = null;
}
Neaux.Ajax.prototype._UpdateState = function() {
  this.Status = this._HttpRequest.status;
  this.State = this._HttpRequest.readyState;
  this.Text = this._HttpRequest.responseText;
  this.XML = this._HttpRequest.responseXML;
}
Neaux.Ajax.prototype._TimeoutHandler = function() {
  this._HttpRequest.abort();
  this._TimerID = null;
  this.ontimeout(this);
}

myHttpRequest = function( aLeft, aTop) {
  this.ExpertBlock = null;
  this.Left = aLeft || 140;
  this.Top = aTop || 120;
}
myHttpRequest.prototype = new Neaux.Ajax();

myHttpRequest.prototype.onload = function() {
  if ( !inMail) {
    var theContainer = document.getElementById("container");
    var theDiv = document.createElement("div");
    theDiv.id = "expert_box";
    theDiv.innerHTML = this.Text;
    theDiv.style.left = this.Left+"px";
    theDiv.style.top = this.Top+"px";
    theContainer.appendChild(theDiv);
    this.ExpertBlock = theDiv; 
    this.Show();
  }
  else {
    if (Boolean(this.Text)) {
      var p = document.getElementById("thank_you");
      var frm = document.getElementById("expert_form");
      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 and try again");
  }
}
myHttpRequest.prototype.Show = function() {
  var p = document.getElementById("thank_you");
  var frm = document.getElementById("expert_form");
  frm.reset();
  p.style.display = "none";
  this.ExpertBlock.style.display = "block";
}
myHttpRequest.prototype.Hide = function() {
  this.ExpertBlock.style.display = "none";
}
myHttpRequest.prototype.DoSubmit = function() {
  var frm = document.getElementById("expert_form");
  if( frm.from.value == "" || frm.name.value == "" || frm.question.value == "") {
    alert("You must enter all fields");
    return;
  }
  var eFilter=/^.+@.+\..{2,3}$/
  if ( !eFilter.test(frm.from.value)) {
    alert("Please input a valid email address!");
    return;
  }
  frm.submit();
}


