var fb_form = {
	errorFree: {},
	errorCheck: function(){
		this.nameCheck();
		this.emailCheck();
		this.phoneCheck();
		this.commentsCheck();
		for(var i=0; i<3; i++)
		{
			if(this.errorFree[i] == false){
				return;
			}
		}
		this.formSubmission();
	},
	formSubmission: function (){
		this.sendPage('contactForm', 'gs_mail.php', 'name='+document.getElementById('name').value+'&email='+document.getElementById('email').value+'&phone='+document.getElementById('phone').value+'&comments='+document.getElementById('comments').value);
	},
	emailCheck: function(){
		var email = document.getElementById('email').value;
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if(email.match(illegalChars) || !(emailPattern.test(email))){
			document.getElementById('email_error').innerHTML = '<p class="required">We need your email address. We wont spam you or sell it or anything like that.<p>';
			this.errorFree[1] = false;
		}else{
			this.errorFree[1] = true;
			document.getElementById('email_error').innerHTML = '';
		}
	},
	nameCheck:function(){
		var name = document.getElementById('name').value;
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
		if(name.match(illegalChars) || name.length < 1){
			document.getElementById('name_error').innerHTML = '<p class="required">We need to know your name.<p>';
			this.errorFree[0] = false;
		}else{
			this.errorFree[0] = true;
			document.getElementById('name_error').innerHTML = '';
		}
	},
	phoneCheck:function(){
		var phone = document.getElementById('phone').value;
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
		if(phone.match(illegalChars) || phone.length < 1){
			document.getElementById('phone_error').innerHTML = '<p class="required">We would like to know your phone number.<p>';
			this.errorFree[0] = false;
		}else{
			this.errorFree[0] = true;
			document.getElementById('phone_error').innerHTML = '';
		}
	},
	commentsCheck:function(){
		var comments = document.getElementById('comments').value;
		var illegalChars= /[\(\)\<\>\;\\\/\"\[\]]/;
		if(comments.match(illegalChars)){
			document.getElementById('comments_error').innerHTML = '<p class="required">You have used a special character that our system does not like. Please remove it and try again.<p>';
			this.errorFree[2] = false;
		}else{
			this.errorFree[2] = true;
			document.getElementById('comments_error').innerHTML = '';
		}
	},
	AJAXInteraction: function(url, callback) {
	    var req = init();
	    req.onreadystatechange = processRequest;
	     function init() {
	      if (window.XMLHttpRequest) {
	        return new XMLHttpRequest();
	      } else if (window.ActiveXObject) {
	        return new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    }
	    function processRequest () {
	      if (req.readyState == 4) {
	        if (req.status == 200) {
	          if (callback) callback(req);
	        }
	      }
	    }
	    this.doGet = function() {
	      req.open("GET", url, true);
	      req.send(null);
	    }
	    this.doPost = function(body) {
	      req.open("POST", url, true);
	      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	      req.send(body);
	    }
	},
	sendPage: function(callbackID, url, body){
		var ai = new this.AJAXInteraction(url, function(x) { if(x.responseText){(x.responseText.substring(0,4) == "http") ? window.location = x.responseText  : document.getElementById(callbackID).innerHTML=x.responseText;}});
		ai.doPost(body);
	}
};