/*Author: V.Seshagiri Rao @fissionlabs*/

var Ajax = {

      url:null,
      params:null,
      method:"get",
      onReady:null,
      isAsynchronous:true,
      xmlHttp:false,
      response:null,
      parameters:"",
      contentType:"text",
      useRandom:false,
      init:function(_ajax) {
        this.url = _ajax.url;
        this.params = _ajax.params;
        this.method = _ajax.method;
        this.onReady = _ajax.onReady;
        this.isAsynchronous = _ajax.isAsynchronous;
        this.contentType = _ajax.contentType;
        this.useRandom = _ajax.useRandom;
        this.createObject();
      },
      createObject:function() {
        this.createXMLHttpRequest();
        this.open();
        this.onReadyStateChange();
        this.send();
      },
      createXMLHttpRequest:function() {
	        var xmlHttp = false;
			try {
			  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			  try {
			    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			  } catch (e2) {
			    xmlHttp = false;
			  }
			}
			if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
			  xmlHttp = new XMLHttpRequest();
			}
       this.xmlHttp = xmlHttp;
      },
      open:function() {
         this.buildUrl();
         this.xmlHttp.open(
                          this.method,
                          this.url,
                          this.isAsynchronous);
         
      },
      buildUrl:function() {
         this.parameters = "";
         for(i in this.params) {
            this.parameters = this.parameters +i+"="+this.params[i]+"&"
         } 
         this.url = this.url + "?" + this.parameters;
         if(this.useRandom){
            this.url = this.url + "rand="+Math.random();
         }   
      },
      onReadyStateChange:function() {
	            this.xmlHttp.onreadystatechange = function() { 
		                if (Ajax.xmlHttp.readyState == 4 && Ajax.xmlHttp.status == 200) {
							   
							   if(Ajax.contentType == "text") {
							      Ajax.response = Ajax.xmlHttp.responseText;
							   }
							   else if(Ajax.contentType == "xml") {
							      Ajax.response = Ajax.xmlHttp.responseXML;
							   }
							   else if(Ajax.contentType == "json") {
							      Ajax.response = eval('('+Ajax.xmlHttp.responseText+')');
							   }
							    
							Ajax.onReady(Ajax.response);
					    }
					    
		        }
		  
      },
      send:function() {
           
           if(this.method == "get") {
             this.xmlHttp.send(null);
           }
           else if(this.method == "post") {
             this.setHeader();
             this.xmlHttp.send(this.parameters);
           }
         
      },
      setHeader:function() {
         
         this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		 this.xmlHttp.setRequestHeader("Content-length", this.parameters.length);
		 this.xmlHttp.setRequestHeader("Connection", "close");
      }
      
      

};