//*****************************************************************************//
//*****************************************************************************//
//************************Scripting By Achintya Agarwal************************//
//*****************************************************************************//
//*****************************************************************************//
/*This Javascript file provides users with a method to load data using AJAX
   from a server side script.
  The function to be used here is 'sendAjaxQuery()'.
  It has 4 parameters out of which 1 is mandatory.

  Syntax:
   sendAjaxQuery(url,dataToSend,method,htmlElementId)

  Parameters:
   url: Compulsory. The url of the server side file to load data from.
   dataToSend: The data to be sent to the server. To be formatted in the form of
    'variable1'='value'&'variable2'='value'&'variable3'='value'&....
    No quotes are to be put and no '&' is needed after the last value.
   method: The method by which data should be transferred (GET or POST).
   htmlElementId: The ID of the HTML element into which data should be fed into.

  If the ID isnt specified, the data is stored inside a variable called
   'responseData'. The data can be used for further manipulations from this
   variable.
  After the response has been recieved 'afterResponse' function is executed. This
   function can be overwritten. Make sure that the overwriting (overriding)
   function is included after this file is included otherwise it will not work.

  EXAMPLES of use:

   sendAjaxQuery("servertime.php")

   sendAjaxQuery("db.php","name=Abc&age=25&sex=m","POST","confirmbox")

   function counter(){
   sendAjaxQuery("counter.php","","GET");
   alert("Page has been visited " + responseData + " times.");
   }
*/
//*****************************************************************************//
//*****************************************************************************//
//*****************************************************************************//

//****DO NOT EDIT UNTIL VERY SURE****//

var xmlHttp,responseData,htmlElementId1				//Global Variable Declaration

function afterResponse(){					//Function Executed after response recieved
}

////***********************//
///****MAIN FUNCTION******//
//***********************//

function sendAjaxQuery(url,dataToSend,method,htmlElementId){

responseData="";

if(url == "")							//Check if URL is specified
{
	alert="Please Specify valid url";
	return;
}

if(dataToSend == ""){						//To bypass cache add a random number
dataToSend = "rnd=" + Math.random();
}else{
dataToSend = dataToSend + "&rnd=" + Math.random();
}

htmlElementId1=htmlElementId;

xmlHttp=GetXmlHttpObject();					//Create XMLHTTP Object
if (xmlHttp==null)						//Check if XMLHTTP Objct is created or no
{
	alert ("Browser does not support HTTP Request!");
	return;
}
xmlHttp.onreadystatechange=stateChanged;			//function to be executed when state changes

switch(method)							//Check for method specified
{
	case "GET":
	XmlHttpGET(url, dataToSend);				//GET Called
	break;
	case "POST":
	XmlHttpPOST(url, dataToSend);				//POST Called
	break;
	default:						//Default method
	XmlHttpGET(url, dataToSend);
}
}


////*******************//
///****AJAX AREA******//
//*******************//

//********************//
//***Browser check & create XMLHTTP Object***//
//********************//

function GetXmlHttpObject()
{
var objXmlHttp=null;
try								// Firefox, Opera 8.0+, Safari
{
	objXmlHttp=new XMLHttpRequest();
}
catch (e)
{
	try 							// Internet Explorer
	{
		objXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		objXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}
return objXmlHttp;
}


//********************//
//***Send request via either of the two methods***//
//********************//

function XmlHttpPOST(url, dataToSend)
{
try{
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(dataToSend);
} catch (ex){
	//Do nothing
}
}

function XmlHttpGET(url, dataToSend)
{
try{
	url = url + "?" + dataToSend;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} catch (ex){
	//Do nothing
}
}


//********************//
//**Wait For Server Response**//
//********************//

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
	responseData=xmlHttp.responseText;			//Server response data stored in 'responseData'
	afterResponse();					//Function Executed after response recieved
	if(htmlElementId1!=null){
		document.getElementById(htmlElementId1).innerHTML=responseData;
	}
}
}