/* Cross-browser Ajax call */
function getPage(uri,holder) 
{
	if(typeof(holder) == 'string')
	{
		holder = document.getElementById(holder);
	}
	
	document.body.style.cursor = "wait";
	if (window.XMLHttpRequest) 
	{
		xmlhttp= new XMLHttpRequest();
	}
	else if (window.ActiveXObject) 
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(a) 
	{
		if(xmlhttp.readyState==4) 
		{
			holder.innerHTML = xmlhttp.responseText;
			document.body.style.cursor = "default";
		}
	}
 
	xmlhttp.open("GET",uri,true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(null);
}

function getPage2(uri, form, holder) 
{
	var requestURL;
	var value;
	var ajaxreq;

	if(typeof(holder) == 'string')
	{
		holder = document.getElementById(holder);
	}
	
	holder.innerHTML = 'Processing...';

	// Determine if we want to retrieve by state or by staff by looking at the URL passed
	if (uri.search("state") > 0)
	{
		value = form.stateID[form.stateID.selectedIndex].text;
	}
	else if (uri.search("staff") > 0)
	{
		value = form.staffID[form.staffID.selectedIndex].text;
	}

	try 
	{
		// Firefox / IE7 / Others
		ajaxreq= new XMLHttpRequest();
	} 
	catch (error) 
	{
		try 
		{
			// IE 5 / IE 6
			ajaxreq = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (error) 
		{
			return false;
		}
	}

	requestURL = uri + value;

	ajaxreq.onreadystatechange=function(a) 
	{
		if(ajaxreq.readyState==4) 
		{
			holder.innerHTML = ajaxreq.responseText;
			document.body.style.cursor = "default";
		}
	}

	ajaxreq.open("GET",requestURL,true);
	ajaxreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajaxreq.send(null);
}

/* Cross-browser Ajax call that uses createTextNode instead of innerHTML*/
function getPageDOM(uri,holder) 
{
	if(typeof(holder) == 'string')
	{
		holder = document.getElementById(holder);
	}
	
	document.body.style.cursor = "wait";
	if (window.XMLHttpRequest) 
	{
		xmlhttp= new XMLHttpRequest();
	}
	else if (window.ActiveXObject) 
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(a) 
	{
		if(xmlhttp.readyState==4) 
		{
			text = document.createTextNode(xmlhttp.responseText);
			holder.appendChild(text);
			document.body.style.cursor = "default";
		}
	}
 
	xmlhttp.open("GET",uri,true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(null);
}


function addContent(uri,odd,neighbor)
{
	page = uri;
	var row = document.createElement("tr");
	var destination = document.createElement("td");
	destination.colSpan = 3;
	destination.setAttribute("class","descrip");

	if(odd)
	{
		row.setAttribute("class","odd");
		row.setAttribute("className","odd");
	}

	getPageDOM(page,destination);
	destination.onclick = function() {hideContent(row); neighbor.onclick = function(){addContent(uri,odd,neighbor);}};
	neighbor.onclick = function() {hideContent(row); neighbor.onclick = function(){addContent(uri,odd,neighbor);}};

	row.appendChild(destination);
	neighbor.parentNode.insertBefore(row,neighbor.nextSibling);
}

function hideContent(content)
{
	content.parentNode.removeChild(content);
}

/* Cross-browser function to import an XML file using */
/* XMLHttpRequest - code from apple.com's Developer section */
var state;
var tableToggle = 1;

function ajaxCall(url, holder) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {

        req = new XMLHttpRequest();
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
	    req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
    	document.write("This feature requires the use of Javascript.  Please enable it or upgrade your browser to one that supports Javascript to use this feature.");
    }
    
   req.onreadystatechange = function() {

   	if (req.readyState == 4) {
   		document.getElementById(holder).innerHTML = req.responseText;
   	}
   };
   req.open("GET", url, true);
   req.send(null);
}

function loadXMLDoc() 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", "fairs.xml", true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", "fairs.xml", true);
            req.setRequestHeader('Accept','text/xml')
            req.send();
        }
    }
    else {
    	document.write("This feature requires the use of Javascript.  Please enable it or upgrade your browser to one that supports Javascript to use this feature.");
    }
}

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
          createTable(req.responseXML);
       
    }
}

/* Table creation function.  Adapted from quirksmode.org */

function createTable(xmlDoc)
{
	var x = xmlDoc.getElementsByTagName('event');
	var newEl = document.createElement('table');
	newEl.setAttribute('cellPadding',5);
	newEl.setAttribute('id','traveltable');
	var tmp = document.createElement('tbody');
	newEl.appendChild(tmp);
	var row = document.createElement('tr');
	for (j=0;j<x[0].childNodes.length;j++)
	{
		if (x[0].childNodes[j].nodeType != 1) continue;
		var container = document.createElement('th');
		var theData = document.createTextNode(x[0].childNodes[j].nodeName);
		container.appendChild(theData);
		row.appendChild(container);
	}
	tmp.appendChild(row);
	for (i=0;i<x.length;i++)
	{
			var drawRow = 0;
			var row = document.createElement('tr');
			for (j=0;j<x[i].childNodes.length;j++)
			{	
				if (x[i].childNodes[j].nodeType != 1) continue;
				var container = document.createElement('td');
				var theData = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
				container.appendChild(theData);
				row.appendChild(container);
				if (x[i].childNodes[j].firstChild.nodeValue == state) {	
					drawRow = 1;
				}
			}
			if (drawRow == 1) {
				tmp.appendChild(row);
			}
	}
	if (tableToggle == 0) {
		document.getElementById('calendar').removeChild(document.getElementById('traveltable'));
	}
	document.getElementById('calendar').appendChild(newEl);
	tableToggle = 0;
}