// AjaxRequest object constructor
function AjaxRequest() {
  // Try the XMLHttpRequest object first
  if (window.XMLHttpRequest) {
    try {
      this.request = new XMLHttpRequest();
    } catch(e) {
      this.request = null;
    }
  // Now try the ActiveX (IE) version
  } else if (window.ActiveXObject) {
    try {
      this.request = new ActiveXObject("Msxml2.XMLHTTP");
    // Try the older ActiveX object for older versions of IE
    } catch(e) {
      try {
        this.request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        this.request = null;
      }
    }
  }

  // If the request creation failed, notify the user
  if (this.request == null)
    alert("Ajax error creating the request.\n" + "Details: " + e);
}

// Send an Ajax request to the server
AjaxRequest.prototype.send = function(type, url, handler, postDataType, postData) {
  if (this.request != null) {
    // Kill the previous request
    this.request.abort();

    // Tack on a dummy parameter to override browser caching
    url += "?dummy=" + new Date().getTime();

    try {
      this.request.onreadystatechange = handler;
      this.request.open(type, url, true); // always asynchronous (true)
      if (type.toLowerCase() == "get") {
        // Send a GET request; no data involved
        this.request.send(null);
      } else {
        // Send a POST request; the last argument is data
        this.request.setRequestHeader("Content-Type", postDataType);
        this.request.send(postData);
      }
    } catch(e) {
      alert("Ajax error communicating with the server.\n" + "Details: " + e);
    }
  }
}

AjaxRequest.prototype.getReadyState = function() {
  return this.request.readyState;
}

AjaxRequest.prototype.getStatus = function() {
  return this.request.status;
}

AjaxRequest.prototype.getResponseText = function() {
  return this.request.responseText;
}

AjaxRequest.prototype.getResponseXML = function() {
  return this.request.responseXML;
}

// Mootools functions
window.addEvent('domready', function(){
			var target = $('mail');
			var fx = new Fx.Morph(target, {transition: Fx.Transitions.Sine.easeOut, duration: 1500, wait: false});
			var anchy = $('clicker');
			var closer = $('closer');
				
			anchy.addEvent('click', function(event){
				event = new Event(event);
				fx.start({
					'bottom': 25,
					'left': 100
				});
				event.stop();
			});
			
			closer.addEvent('click', function(event){
				event = new Event(event);
				fx.start({
					'bottom': 25,
					'left': -410
				});
				event.stop();
			});
			
			});
			
// Form validation and ajax request/response
		
var ajaxReq = new AjaxRequest();

function validateEmail(field,help) {
	if (!validateNonEmpty(field,help))
	return false;
	return validateRegEx(/^[\w\.-_\+]+@[\w-]+(\.\w{2,4})+$/, field.value, help, "* Please enter a valid email address.");
	}

function validateNonEmpty(field,help) {
	if (field.value.length == 0) {
		if (help != null)
			help.innerHTML = "* Please enter a value.";
		return false;
	} else {
	     if (help != null) 
		 help.innerHTML = "";
	return true;
	}
}

function validateRegEx(regex, inputStr, help, helpMessage) {
	if (!regex.test(inputStr)) {
		if (help != null)
			help.innerHTML = helpMessage;
		return false;
	} else  {
	if (help != null)
		help.innerHTML = "";
	return true;
	}
}

function tellaFriend () {
	if(!validateRegEx(/^[\w\.-_\+]+@[\w-]+(\.\w{2,4})+$/, document.getElementById('email1').value, document.getElementById('mail1help'), "* Please enter a valid email address."))
	return false;
	if(!validateRegEx(/^[\w\.-_\+]+@[\w-]+(\.\w{2,4})+$/, document.getElementById('email2').value, document.getElementById('mail2help'), "* Please enter a valid email address."))
	return false;
	if(!validateNonEmpty(document.getElementById('name'),document.getElementById('namehelp')))
	return false;
	if(!validateNonEmpty(document.getElementById('comment'),document.getElementById('commenthelp')))
	return false; 

  document.getElementById('sendit').disabled = true;
  document.getElementById('status').innerHTML = "<img src='http://www.elanman.co.uk/js/ajax-loader.gif' alt='Loading..' />";
  
  ajaxReq.send("POST", "http://www.elanman.co.uk/js/form.php", handleRequest, "application/x-www-form-urlencoded; charset=UTF-8", "name=" + document.getElementById("name").value + "&email1=" + document.getElementById("email1").value + "&email2=" + document.getElementById("email2").value + "&comment=" + document.getElementById("comment").value + "&url=" + document.location);
}

  
function handleRequest() {
    if (ajaxReq.getReadyState() == 4 && ajaxReq.getStatus() == 200) {
      document.getElementById('status').innerHTML = ajaxReq.getResponseText();
  	}
  }
window.onload = function() {
document.getElementById('mail').style.display = 'block';
  document.getElementById('clicker').style.display = 'inline';
  document.getElementById('email1').onblur = function(evt) { validateEmail(this, document.getElementById('mail1help'));};
  document.getElementById('email2').onblur = function(evt) { validateEmail(this, document.getElementById('mail2help'));};
  document.getElementById('name').onblur = function(evt) { validateNonEmpty(this, document.getElementById('namehelp'));};
  document.getElementById('comment').onblur = function(evt) { validateNonEmpty(this, document.getElementById('commenthelp'));};
  document.getElementById('sendit').onclick = function(evt) { tellaFriend();};
}
