/*******************************************************************************
*			 ______________________________________________________
			/ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ\
			|     COMMON FUNCTION FOR IMPLEMENTATION OF AJAX	   |
			|                                                      |
			|			Developed By : Sameer Pal Singh			   |
			|			Created on   : 8/7/2006 3:58 PM			   |
			|													   |
			|			E-mail me :                                |
			|				- 'sameers@e-lixirweb.com'	           |
			|                                                      |
			\______________________________________________________/
			 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
*
* The "options" parameter is an anonymous object which includes the following
* available options:
*
* params:    Parameters for the requested url in the format p1=1&p2=0&p3=2
* meth:      The request method. Can be "get" or "post". Default is "post".
* async:     Toggles asynchronous mode. Default is true.
* startfunc: A function or list of functions to be called before the AJAX
*            request is made. A list of functions must be separated by the
*            semi-colon like this: "showLoad(); animateText(); hideDiv('divname')".
*            You can pass parameters into the functions.
* endfunc:   A function or list of functions to be called after a successful
*            AJAX request. Uses the same format as "startfunc".
* errorfunc: A function or list of functions to be called when the AJAX request
*            is unsuccessful. Uses the same format as "startfunc".
*
* Returns true on success and false on failure.
*
* Example Usage:
*
  callAjax( "rightdiv", "getData.php", {
    params:"id=12&name=sameer",
    meth:"post",
    async:true,
    startfunc:"elemOn('loading')",
    endfunc:"elemOff('loading'); elemOn('rightdiv')",
    errorfunc:"ajaxError()" }
  );

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~VERSION HISTORY~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Author                                  Date                                    Change History
Sameer Pal Singh                        8/7/2006                                Initial Development
Manmeet Kaur							6/4/2007                                Separate post and get conditions +
																				call ActiveXObject constructor for old 																					versions of IE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

function callAjax( elemid, url, options )
{
	var params = options.params || "";
	var meth = options.meth || "post";
	var async = options.mode || true;
	var startfunc = options.startfunc || "";
	var endfunc = options.endfunc || "";
	var errorfunc = options.errorfunc || "";
//	 alert(elemid);

	if( startfunc != "" )
		eval( startfunc );
	
	var url_with_param = url+( params != "" ? "?"+params : "" );
 	//alert(url_with_param);
	loadXMLDoc_rand();
	var xmlhttp_rand
	function loadXMLDoc_rand()
	{
       	// code for Mozilla, Safari, Netscape etc. a simple call to object's constructor
	    if (window.XMLHttpRequest)
	    {
        	xmlhttp_rand=new XMLHttpRequest()
	        xmlhttp_rand.onreadystatechange=xmlhttpChange_rand
        }
	    // code for IE
        // For the ActiveX branch, pass the name of the object to the ActiveX constructor
	    else if (window.ActiveXObject)
	    {
      	 // alert(1);
	        try{//for old versions of IE
             //	alert(2);
	            xmlhttp_rand = new ActiveXObject("Msxml2.XMLHTTP");
	        }
	        catch(e){
            	try{
	          //  alert(3);
	            xmlhttp_rand=new ActiveXObject("Microsoft.XMLHTTP");
	        	}
	        	catch(e){}
	        }
	        if (xmlhttp_rand)
	        {
              	xmlhttp_rand.onreadystatechange=xmlhttpChange_rand
	        }
	        else
	        {
           		alert( "Your browser cannot perform the requested action. "+
	             "Either your security settings are too high or your "+
	             "browser is outdated. Try the newest version of "+
	             "Internet Explorer or Mozilla Firefox." );
	            return false;
	        }

	    }
        if(xmlhttp_rand)
        {
            if(meth=="get")
            {
                xmlhttp_rand.open('GET',url_with_param,async);
                xmlhttp_rand.send('');
            }
            else if(meth=="post")
            {
                xmlhttp_rand.open('POST',url,async);
                xmlhttp_rand.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
                xmlhttp_rand.setRequestHeader("Content-length", params.length);
                xmlhttp_rand.setRequestHeader("Connection", "close");
                xmlhttp_rand.send(params);
            }
        }
        return false;
	}

	function xmlhttpChange_rand()
	{
	// if xmlhttp shows "complete status"

   	 if (xmlhttp_rand.readyState==4)
	    {
	        if (xmlhttp_rand.status==200)
	        {// OK status
	             var objXML = xmlhttp_rand.responseXML;
	             var objXML1 = xmlhttp_rand.responseText;

            	if(elemid != "")
	            {
	                    // alert(objXML1);
	                document.getElementById(elemid).innerHTML = objXML1;
					
	            }
	            if( endfunc != "" )
	                eval( endfunc );
	        }
	        else
	        {
	            alert("Problem retrieving XML data:\n"+xmlhttp_rand.statusText);
	            if( endfunc != "" )
	                eval( endfunc );
	            if( errorfunc != "" )
	                eval( errorfunc );
	            return false;
	        }
	    }
	}
}

//END OF AJAX FUNCTIONS.