//AJAX call functions, version 2.0

//////////////////////////////////////////////////////FROM PAGE//////////////////////////////////////////////////////

var http_request = new Array();
var results = new Array();
var xmldoc = new Array();
var root_node = new Array();

var domain = 'http://'+window.location.hostname;

//retrievs external document contents and writes to specified destination 
//index = instance of variable set - must be different for each event, location = source doc location relative to site root, target = div to load to
function ajax_page_to_div(index, location, target) 
{
	var url = domain+location;
	http_request[index] = false;
	
	if(window.XMLHttpRequest) // Mozilla, Safari,...
	{ 
		http_request[index] = new XMLHttpRequest();
		if(http_request[index].overrideMimeType) //name mime type if possible
		{
			http_request[index].overrideMimeType('text/xml');
		}
	} 
	else if(window.ActiveXObject) //IE...
	{ 
		try 
		{
			http_request[index] = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
				http_request[index] = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e) 
			{}
		}
	}
	if(!http_request[index]) //no support...
	{
		alert('Your current browser does not support the JavaScript used in this part of the website. Please upgrade to a current browser to continue.');
		return false;
	}
	http_request[index].open('GET', url, false); //true = asynchronous load
	http_request[index].send(null);
	embedpage(index, target);
}

//load contents from external page to div
function embedpage(index, target)
{
	if(window.location.href.indexOf("http")==-1 || http_request[index].status==200)
	{
		results[index] = http_request[index].responseText; //get source page contents
		document.getElementById(target).innerHTML = results[index]; //write results to page element

	}
}


//////////////////////////////////////////////////////FROM XML//////////////////////////////////////////////////////

//retrievs XML document contents and writes to specified destination 
//index = instance of variable set - must be different for each event, location = location of source of XML relative to site root, 
//tag_name = tag to look for in XML, item_num = number of tag instance, target = div to load to
function ajax_xml_to_div(index, location, tag_name, item_num, target ) 
{
	var url = domain+location;
    http_request[index] = false;
    
    if(window.XMLHttpRequest) // Mozilla, Safari,...
    { 
        http_request[index] = new XMLHttpRequest();
        if(http_request[index].overrideMimeType) //name mime type if possible
        {
            http_request[index].overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) //IE...
    { 
        try 
        {
        http_request[index] = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            try 
            {
                http_request[index] = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) 
            {}
        }
    }
	if(!http_request[index]) //no support...
    {
        alert('Your current browser does not support the JavaScript used in this part of the website. Please upgrade to a current browser to continue.');
        return false;
    }
    http_request[index].open('GET', url, true); //true = asynchronous load
    http_request[index].onreadystatechange = function(){embed_xml(item_num, tag_name, target, index)};
    http_request[index].send(null);
}

//load contents from XML to div
function embed_xml(item_num, tag_name, target, index)
{
    if(http_request[index].readyState == 4) //ready state = loaded
    {
        if(http_request[index].status == 200 || http_request[index].status == 0) //status = OK
        {
            xmldoc[index] = http_request[index].responseXML;
            root_node[index] = xmldoc[index].getElementsByTagName(tag_name).item(item_num); //define XML item
            document.getElementById(target).innerHTML = root_node[index].firstChild.data; //get and place XML item into HTML
        } 
        else //status != OK
        {
            alert('Request error '+http_request[index].status);
        }
    }
}



