/* syd_ajax.js
 * purpose: ajax wrapper for sydneyplus knowledge portal
 *
	Usage: syd_ajax_start(url, call_back_fn)

	url: http:// address or a script file name relative to current folder.
	call_back_fn: javascript callback function, place null if there's no call back is needed.

	note: error will be displayed if url failed to process regardless of callback function.
		inside call_back_fn

	Examples:

	1. Calling without callback function
	syd_ajax_start("unselect_session_all_records.asp?rpt_session_guid=<%= strReportSessionGUID%>&template_guid=<%= strTemplateGUID %>", null);

	2. Calling with a callback function

	var fn_unselect_callback = new Function("Unselect_CallBack();");
	syd_ajax_start("unselect_session_all_records.asp?rpt_session_guid=<%= strReportSessionGUID%>&template_guid=<%= strTemplateGUID %>", fn_unselect_callback);

	... [somewhere outside of this calling function]
    function Unselect_CallBack()
	{
	// global variable, syd_ajax_requester, can be used inside data to find out return values.
	//  eg. syd_ajax_requester.responseText to get all returned value.
	}
 */

var syd_ajax_requester;
var syd_ajax_fnexecute;

function syd_ajax_start(page, fnCall)
{
 /* Check for running connections */ 
 if (syd_ajax_requester != null && syd_ajax_requester.readyState != 0 && syd_ajax_requester.readyState != 4) 
 { 
   syd_ajax_requester.abort(); 
 } 

 try 
 { 
   syd_ajax_requester = new XMLHttpRequest(); 
 } 
 catch (error) 
 { 
   try 
   { 
     syd_ajax_requester = new ActiveXObject("Microsoft.XMLHTTP"); 
   } 
   catch (error) 
   { 
     syd_ajax_requester = null; 
   } 
 } 

 if (syd_ajax_requester != null)
 {
     syd_ajax_fnexecute = fnCall;
     
     syd_ajax_requester.onreadystatechange = syd_ajax_state_change; 

    //work around for IE6 where it's default caching will not allow page to be added multiple times

    if (page.indexOf("?") == -1) // not found
        page += "?"
    else
        page += "&"

    page += "waie6=" + (Math.random() * Date.parse(new Date()))

     syd_ajax_requester.open("GET", page);
     syd_ajax_requester.send(null); 
 }
}

function syd_ajax_state_change()
{
 /* If XMLHR object has finished retrieving the data */ 
 if (syd_ajax_requester.readyState == 4) 
 { 
   /* If the data was retrieved successfully */ 
   if (syd_ajax_requester.status == 200) 
   { 
     if (syd_ajax_fnexecute)
        syd_ajax_fnexecute();
   } 
   /* IE returns a status code of 0 on some occasions, so ignore this case */ 
   else if (syd_ajax_requester.status != 0) 
   { 
     alert("There was an error while retrieving the URL: " + syd_ajax_requester.statusText); 
   } 
 } 

 return true; 
}



