/********************************************************

Date:June 12 2007
Filename:ajax.js
Company:
Purpose:Collection of ready to use ajax functions
*********************************************************/
/************ IMPORTANT NOTE ****************************
req.onreadystatechange=FUNCTION NAME in 
callServerGet() AND callServerPost() has to be explicitly
mentioned which will change every time.

**********************************************************/
/**********************************************************
FOR CREATING AN EMPTY REQUEST OBJECT
***********************************************************/
var req;
req = false;

function getObject() 
{    
    if(window.XMLHttpRequest && !(window.ActiveXObject)) 
	{
    	try {
				req = new XMLHttpRequest();
            } catch(e) 
			{
				req = false;
        	}
    
    } else if(window.ActiveXObject) 
	{
       	try {
        		req = new ActiveXObject("Msxml2.XMLHTTP");
      	    } catch(e) {
        	try 
			{
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) 
			{
          		req = false;
        	}
		}
    }
	
	
	if(!req)
		{
			alert('Cannot create request object');
		}
	else
		{
			return req;
		}
}




/**********************************************************
SEND REQUEST TO SERVER USING GET,ALSO PASS THE URL AND
THE FUNCTION TO BE CALLED ON 400 OK
***********************************************************/
function callServerGet(url) 
{
	req = getObject();
    
	if(req) 
	{
		req.open("GET", url, true);
		req.onreadystatechange = getAjaxResponseGet;
		req.send("");
	}
}



/**********************************************************
SEND REQUEST TO SERVER USING POST,ALSO PASS THE URL AND
THE FUNCTION TO BE CALLED ON 400 OK
(THE RESPONSE HANDLING FUNCTION SHOULD ALWAYS BE 
 getAjaxResponse FOR PROCESSING RESPONSE IN THE SPECIFIC PAGE)
***********************************************************/
function callServerPost(url,params) 
{
	req = getObject();
	
	if(req) 
	{   
		req.open("POST", url, true);
		req.onreadystatechange = getAjaxResponsePost;
		
		//Send the proper header information along with the request
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", params.length);
		req.setRequestHeader("Connection", "close");
		
		req.send(params);
	}
}


/********************************************************
FUNCTION BELOW THIS LINE ARE DUPLICATES OF THE ABOVE
WITH SAME FUNCTIONALITY BUT DIFFERENT NAME
*********************************************************/
function callServerForImages(url) 
{
	req = getObject();
    
	if(req) 
	{
		req.open("GET", url, true);
		req.onreadystatechange = getAjaxResponseForImages;
		req.send("");
	}
}

function callServerForFavourites(url) 
{
	req = getObject();
    
	if(req) 
	{
		req.open("GET", url, true);
		req.onreadystatechange = getAjaxResponseForFavourites;
		req.send("");
	}
}
