var ajax = {};
ajax.xhr = {};

ajax.xhr.Request = function(url, params, callback, method) {
	this.url = url;
	this.params = params;
	this.callback = callback;
	this.method = method;
	this.send();
}
ajax.xhr.Request.prototype = {
	getXMLHttpRequest: function() {
		if (window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e1) { return null; }
			}
		} else if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else {
			return null;
		}		
	},
	send: function() {
		this.req = this.getXMLHttpRequest();
		
		var httpMethod = this.method ? this.method : 'GET';
		if (httpMethod != 'GET' && httpMethod != 'POST') {
			httpMethod = 'GET';
		}
		var httpParams = (this.params == null || this.params == '') ? 
		                 null : this.params;
		var httpUrl = this.url;
		if (httpMethod == 'GET' && httpParams != null) {
			httpUrl = httpUrl + "?" + httpParams;
		}
		this.req.open(httpMethod, httpUrl, true);
		this.req.setRequestHeader(
			'Content-Type', 'application/x-www-form-urlencoded');
		var request = this;
		this.req.onreadystatechange = function() {
			request.onStateChange.call(request);
		}
		this.req.send(httpMethod == 'POST' ? httpParams : null);
	},
	onStateChange: function() {
		this.callback(this.req);
	}
}



//contact form
var contact = {};

contact=function(div,param){
	target=div;
	prm=param;
	this.send();
}
contact.prototype={
	callBack:function(req){
		if (req.readyState == 4) {
			if (req.status == 200) {
				var targetID = document.getElementById(target);
				var str=req.responseText;
				targetID.innerHTML=str;
			}
		}
	},
	send:function(){
		var targetID = document.getElementById(target);
		var email=encodeURIComponent(document.getElementById('email').value);
		var first_name=encodeURIComponent(document.getElementById('first_name').value);
		var last_name=encodeURIComponent(document.getElementById('last_name').value);
		var company=encodeURIComponent(document.getElementById('company').value);
		var subject=encodeURIComponent(document.getElementById('subject').value);
		var description=encodeURIComponent(document.getElementById('description').value);
		
		var error="";
		var param="";
		if(email=="Email") error+="email ";
		else param+="email="+email;
		if(first_name=="First Name") error+="first_name ";
		else param+="&first_name="+first_name;
		if(last_name=="Last Name") param+="&last_name= "; 
		else param+="&last_name="+last_name; 
		if(company=="Company") param+="&company= "; 
		else param+="&company="+company; 
		if(company=="Subject") error+="subject ";
		else param+="&subject="+subject; 
		if(description=="Message") error+="message ";
		else param+="&description="+description;
		if(error != "") alert('Please insert '+error);
		else new ajax.xhr.Request("email_send.php",param,this.callBack, 'POST');
		
	}
}
