
//Constants
var c_available = 1;
var c_unavailable = 0;
var debug = false;

var data = [];
// initialize XMLHttpRequest object
var xmlobj=null;
var xhr = []; // ARRAY OF XML-HTTP REQUESTS
var xhrRequestAvailability = []; // ARRAY OF XML-HTTP REQUEST INDEXES
var xhrRequestTarget = []; // Array of XML-HTTP Request divs to write to
xhrRequestAvailability[0] = c_available; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE
// set global location to write to
//var xmlWriteTarget = "xmlCareersCurrentOps";
// display data at a given time interval
function displayData( dataText, xmlWriteTarget )
{
    // write to the xml data container
    if( document.getElementById(xmlWriteTarget) )
    {
	    document.getElementById(xmlWriteTarget).innerHTML = dataText;
    }

    // update headlines each 1 hour
    //setTimeout("sendRequest('http://www.qa.recruitasp.com.au/jobtools/xml_feeds.current_jobs?in_orgi=14679')",5*1000);
   //setTimeout("sendRequest('http://syddev03.corp.aal.au/aalaus/aalaus.nsf/test?openagent')",5*1000);
}

// check request status
function stateChecker( xhrsend )
{
    // if request is completed
    if(xhr[xhrsend].readyState==4)
    {
        // if status == 200 display text file
        if(xhr[xhrsend].status==200){
            // display XML data
            displayData( xhr[xhrsend].responseText, xhrRequestTarget[xhrsend] );
        }
        else
        {
            displayData( 'We could not find a match for your search criteria. ('+ xhr[xhrsend].statusText + ')<br /><br />', xhrRequestTarget[xhrsend] );
        }
    }
}

// send http request
function sendRequest( doc, targetDivId )
{
	// xhrsend IS THE xi POSITION THAT GETS PASSED BACK
	// INITIALIZED TO THE LENGTH OF THE ARRAY(LAST POSITION + 1)
	// IN CASE A FREE RESOURCE ISN'T FOUND IN THE LOOP
	var xhrsend = xhrRequestAvailability.length;
	// GO THROUGH AVAILABLE xi VALUES
	for (var i=0; i < xhrRequestAvailability.length; i++)
	{
		// IF IT'S 1 (AVAILABLE), ALLOCATE IT FOR USE AND BREAK
		if( xhrRequestAvailability[i] == c_available )
		{
			xhrRequestAvailability[i] = c_unavailable;
			xhrsend = i;
			break;
		}
	}
	// SET TO 0 SINCE IT'S NOW ALLOCATED FOR USE
	xhrRequestAvailability[xhrsend] = 0;
	// set the location to write the results to
	xhrRequestTarget[xhrsend] = targetDivId;
	try
	{
       	// instantiate object for Mozilla, Nestcape, etc.
		xhr[xhrsend] = new XMLHttpRequest();
	}
	catch(err1)
	{
		try
		{
			// instantiate object for Internet Explorer
			xhr[xhrsend] = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch(err2)
		{
			// Ajax is not supported by the browser
			//xmlobj=null;
			return false;
		}
	}
    // assign state handler
    xhr[xhrsend].onreadystatechange = function()
					    {
							stateChecker( xhrsend );
					    };
    // open socket connection
    //xmlobj.open('GET',doc,true);
    xhr[xhrsend].open('GET',doc,true);
    // send GET request
    try
    {
       //xmlobj.send(null);
	xhr[xhrsend].send(null);
    }
    catch(e)
    {
        alert( 'error' );
    }
}
function initXMLRequest( xmlLocation, divId )
{
    // exit if we don't have a div to write to.
    //if( !divId || divId = "" ) { return false; }
    // check if browser is DOM compatible
    if(document.getElementById && document.getElementsByTagName && document.createElement)
    {
        // load XML file
        sendRequest( xmlLocation, divId );
    }
}
function initOnloadXMLRequest( origLoad, xmlUrl )
{
   origLoad( );
   initXMLRequest( xmlUrl );
}

