// AJAX CLASS //

/**
 * @author      Haim.Y & Daniel.O
 * @copyright   2008 SmartWD ©
 * @version     SmartWD FrameWork 2.0
 * @website     http://www.SmartWD.com
 */
 
var Ajax = {
	xmlHttp: "",//xmlHttp object to call ajax calls
	method: "GET",//the method of the call
	url: "",//url/file to call
	parameters: new Array(),//parameters to send
	onready: function() {},//functnio to do when ready
	create_xmlHttp: function()//function to create a xmlHttp object
	{
  		var xmlHttp;//set the object
  		try
  		{
  		    //try - on the most browsers will work
    		xmlHttp = new XMLHttpRequest();
  		}
  		catch(e)
  		{
  		    //if the first not work try more one array of things
    		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                            "MSXML2.XMLHTTP.5.0",
                                            "MSXML2.XMLHTTP.4.0",
                                            "MSXML2.XMLHTTP.3.0",
                                            "MSXML2.XMLHTTP",
                                            "Microsoft.XMLHTTP");
    		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    		{
      			try 
      			{
        			xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      			} 
      			catch (e) {}
    		}
  		}
  		if (!xmlHttp)//if the object not create return a error
    		alert("Error creating the XMLHttpRequest object.");
  		else //else set the xmlHttp object
    		this.xmlHttp = xmlHttp;
	},
    send: function(url, method, onready, parameters)
    {
        this.create_xmlHttp();//get the xmlHttp object
        this.url = url;//set the url
        this.method = method;//set the method
        this.onready = onready;//set the function to do when ready
        this.parameters = parameters//set the parameters to send
        var parms = "";
        for(key in this.parameters)//walk on all array keys and collect them
        {
            var value = this.parameters[key];//get the value
            parms += (String(Number(key))!="NaN")?"parm[]="+value+"&":key+"="+value+"&";//collect
        }
        parms = parms.substr(0,parms.length-1);
        if(this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0)//if the browser ready
        {
            if(this.method=="POST")//if the method is POST
            {
                this.xmlHttp.open("POST",this.url,true);//start the object
                this.xmlHttp.onreadystatechange = this.onready;//do what when ready
                this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//send headers
                this.xmlHttp.send(parms);//send the paraneters
            }
            else//ele if the method is GET
            {
                this.xmlHttp.open("GET",this.url+"?"+parms,true);//set parameters and call to the file
                vxmlHttp.onreadystatechange = this.onready;//function when ready
                vxmlHttp.send(null);//send a null object to the browser
            }
        }
        else//else if the browser not ready so try in 1 second
            setTimeout("Ajax('"+this.url+"','"+method+"','"+this.onready+"','"+this.parameters+"')",1000);
    }
};
