/*******************************************************************************
*			 ______________________________________________________
			/ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ\
			|     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('loaded'); elemOn('rightdiv')",
    errorfunc:"ajaxError()" }
  );

*/

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 || "";

  if( startfunc != "" )
    eval( startfunc );

  var url_with_param = url+( params != "" ? "?"+params : "" );
//alert(url_with_param);
  loadXMLDoc_new();
//----------------------------------------------------------------
	var xmlhttp1
	function loadXMLDoc_new()
	{
		// code for Mozilla, etc.
		if (window.XMLHttpRequest)
		  {
			  xmlhttp1=new XMLHttpRequest()
			  xmlhttp1.onreadystatechange=xmlhttpChange
			  xmlhttp1.open(meth,url_with_param,async)
			  xmlhttp1.send('')
		  }
		// code for IE
		else if (window.ActiveXObject)
		  {
			xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP")
			if (xmlhttp1)
			{
				xmlhttp1.onreadystatechange=xmlhttpChange
				xmlhttp1.open(meth,url_with_param,async)
				xmlhttp1.send()
					return false;
			  }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;
			  }
		  }
	}

	function xmlhttpChange()
	{

	// if xmlhttp shows "loaded"
	if (xmlhttp1.readyState==4)
	  {
		//alert("a");
		  if (xmlhttp1.status==200)
			{
			 var objXML = xmlhttp1.responseXML;
			 var objXML1 = xmlhttp1.responseText;
			//alert(objXML1);
			 document.getElementById(elemid).innerHTML = objXML1;
			 if( endfunc != "" )
				eval( endfunc );
		  }
		  else
			{
				alert("Problem retrieving XML data")
				if( endfunc != "" )
					eval( endfunc );
			  if( errorfunc != "" )
					eval( errorfunc );
				  return false;
			}
		}
	}
}
//END OF AJAX FUNCTIONS.