
// This'll hold our XMLHttpRequest method
var xmlHttp;


// This makes our http request object, it's used internally
// and doesn't need to be called directly in the html
function ajax_start()
{

	// XMLHttpRequest is the correct method,
	// however, it is not supported by IE, because they
	// like to do things a "little" different.  
	try
	{

		xmlHttp=new XMLHttpRequest();
	
	}
	// try -> catch statements will attempt to do something and
	// then fall into the catch block when it runs into a problem
	// instead of erroring. In this case it will be because the user is in
	// IE and doesn't support XMLHttpRequest
	catch (e)
	{
	
		// This one is IE 6.0+
		try
		{
		
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			
		}
		catch (e)
		{
	
			// This one is IE 5.5+
			try
			{
			
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			
			}
			catch (e)
			{
	
				// There's nothing else to fall back on,
				// the user is using a pretty outdated browser, so 
				// we error them.		
				alert(
					"Your browser does not support AJAX."
					+ "Please consider upgrading or the asynchronous features will not be supported."
				);

				return false;
	
			}

		}
		
	}
	
}


// This function is one we send to a URL with some
// data, it should be a script that deals with our
// data and returns the text that jscript will deal with.
// Could go in something like onClick for buttons etc
//
// Be aware that URL -has- to be on the same domain as the
// Javascript is, just a little oddity I came accross that
// would have you stumped otherwise.
//
// Third parameter can be null to use the default name.
function ajax_send_data_get(url, data, statechange)
{

	// First get the xmlHttpRequest object
	ajax_start();

	// How Ajax works is that when calls are sent to the 
	// server, the onreadystatechange function will fire depending
	// on where the current action is. We specify a function to call
	// when the state changes which will do things at specific states.
	if(statechange == null)
		xmlHttp.onreadystatechange = ajax_statechange;
	else
		xmlHttp.onreadystatechange = statechange;

	// This opens the request, this one we set to GET method.
	// The third parameter defines if the request is asynchronous or not,
	// if this is false, the browser can crash because it will wait for the 
	// request to come back, and sometimes it may never come back!
    xmlHttp.open("GET", url, true);
    
    // This one sends our data of course.. Then it's all up to ajax_statechange.
    xmlHttp.send(data);

}


// Same as above but uses post
function ajax_send_data_post(url, data, statechange)
{

	// First get the xmlHttpRequest object
	ajax_start();
	
	if(statechange == null)
		xmlHttp.onreadystatechange = ajax_statechange;
	else
		xmlHttp.onreadystatechange = statechange;

    xmlHttp.open("POST", url, true);

	// Post needs the correct header type to work
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    
    xmlHttp.send(data);

}



/*
----------------------------------------------------------------------
**********************************************************************
----------------------------------------------------------------------


This is an outline of an ajax_statechange, we can just
copy paste this and edit it in other jscript files.


function ajax_statechange()
{

	// Request has not been started.
	if(xmlHttp.readyState == 0)
	{
	
	
	}

	// Request has been started
	if(xmlHttp.readyState == 1)
	{
	
	
	}
	
	// Request has been sent to the server
	if(xmlHttp.readyState == 2)
	{
	
	
	}

	// Request is being dealt with server-side
	if(xmlHttp.readyState == 3)
	{
	
	
	}

	// Request is finished, now use the data.
	if(xmlHttp.readyState == 4)
	{
	
		// Data returned is stored in xmlHttp.responseText
		// with PHP, we just use echo to get our response.
	
	}	

}

*/



// This bit is a function for getting an element we want by id.
// There's a lot of cross browser stuff going on, but in practice all 
// you do is.
// var my_element = find_dom("foo");
// This will return whichever element on our page has the ID "foo". 
var is_dhtml = 0;
var is_layers = 0;
var is_all = 0;
var is_id = 0;

if(document.getElementById)
{
	is_id = 1;
	is_dhtml = 1;
}
else
{

        if(document.all)
        {
        	is_all = 1;
        	is_dhtml = 1;
        }
        else
        {
                browserVersion = parseInt(navigator.appVersion);
                if((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4))
                {
                	is_layers = 1;
                	is_dhtml = 1;
                }
        }
        
}

function find_dom(objectID, withStyle)
{

        if(withStyle == 1)
        {
        
                if(is_id)
                	return (document.getElementById(objectID).style);
                else
                {

                        if(is_all)
                        	return (document.all[objectID].style);
                        else
                                if(is_layers)
                                	return (document.layers[objectID]);

                }
                
        }
        else
        {
        
                if(is_id)
                	return (document.getElementById(objectID));
                else
                {
                
                        if(is_all)
                        	return(document.all[objectID]);
                        else
                                if(is_layers)
                                	return (document.layers[objectID]);
                        
                }
                
        }
        
        return false;
        
}