// JavaScript Document
/*
	Created By  : Jansan John
	E-Mail      : jans4u@gmail.com
	Description : Ajax Functions
*/
/* --------------------------   AJAX FUNCTION STARTS HERE  -------------------------- */
/*
Return the xmlHttpObject
*/
function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null
	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		//alert("Opera Is Not Supporting. Use Some Other Browsers") 
		//return
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler;
		return objXmlHttp;
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP";
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName);
			objXmlHttp.onreadystatechange=handler; 
			return objXmlHttp;
		} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled"); 
			return; 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler; 
		return objXmlHttp;
	}
}
//--------------------------------------------------------------------------------------------------//
/*
Send an ajax request to the url mentioned
URL 	   : SERVER SIDE SCRIPT NAME
PARAMETERS : URL parameters
callback   : callback function
requesttype: POST OR GET
async      : asycronous or synchronous, TRUE or FALSE
*/
function makeRequest(url,parameters,callback,requesttype,async)
{
	
	if(requesttype=='POST')
	{
		xmlHttp=GetXmlHttpObject(callback) 
		xmlHttp.open("POST",url,async);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send(parameters);
	}else if(requesttype=="GET")
	{
		url    = url+'?'+parameters;
		xmlHttp=GetXmlHttpObject(callback) 
		xmlHttp.open("GET", url , async) 
		xmlHttp.send(null) 
	}
}
//--------------------------------------------------------------------------------------------------//
// xGetEleAtPoint, Copyright 2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
/*
Parameters :x  integer
           :y  integer
Return     : Returns an element reference, or null if no element is at the given point.
*/
function xGetEleAtPoint(x, y)
{
  var he = null, z, hz = 0;
  var i, list = xGetElementsByTagName('*');
  for (i = 0; i < list.length; ++i) {
    if (xHasPoint(list[i], x, y)) {
      z = xZIndex(list[i]);
      z = z || 0;
      if (z >= hz) {
        hz = z;
        he = list[i];
      } 
    }
  }
  return he;
}
//--------------------------------------------------------------------------------------------------//
/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects
	 for(var item in arr) {
	  var value = arr[item];
	 
	  if(typeof(value) == 'object') { //If it is an array,
	   dumped_text += level_padding + "'" + item + "' ...\n";
	   dumped_text += dump(value,level+1);
	  } else {
	   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
	  }
	 }
	} else { //Stings/Chars/Numbers etc.
	 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
} 
/* -------------------------------------------------------------
// function for sanitizing tha data
----------------------------------------------------------------- */
function sanitizeString(resText)
{
	var response = resText.replace(/\n/g, "<br/>");
    response     = response.replace(/\r/g, "\\r");	
	return response;
}
/* ----------------------------------------------------------------
// trim functions. add to native function using prototype
------------------------------------------------------------------ */
String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
   return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
   return this.replace(/\s+$/,"");
}