// Must include ajaxobj.js for this to work
var divContentID = "drillsData";
var divContent = "";	// Temporary place for response to go while we do setTimeout to check browser cache in IE 
var xmlData = "";		// Temporary place for the xml data to be stored
var xsltPI = "";		// Temporary place for the xslt processing instructions to be stored
var xml = "";			// Temporary place for the parsed xml data to be stored
var xslt = "";			// Temporary place for the parsed xslt to be stored
var loadmessage;		// Variable that sets whether to show or not show the loading message

function initClinic() {	// Initialize the clinic filter.
	// Preload the XSLT
	var xsltFile = xsltPath;
	loadmessage = "no"; // Do not show the loading message.
	var loadXSLT = new net.ContentLoader(xsltFile, SetXSLT, DisplayError,loadmessage);	//Ajax load the XSLT.
	
	// Load the dropdown options
	initDynamicDropDown();
	
	// Load a default clinic or one from a query string
	// Default clinic to load is set in the clinics page.
	var params = getURLParameters();	
	if(params != null){					// If there is a query string
		var p = params.split("=")[0];	// parameter name
		var pvalue = params.split("=")[1];	// paramater value
	}
	
	if ((params == null) || (pvalue == '') || (p != "xml")) {	//If there is no query string or no parameter value, load the default clinic.
		loadClinic(dflt_clinic);
	}	
	else {
		loadClinic(pvalue);
	}
}

function SetXSLT() {	// Callback function.
	xsltPI = this.req.responseText;		
	window.setTimeout("ParseXSLT()", 100); // Give the XSLT time to load then parse it
}

function ParseXSLT() {	
	xslt = xmlParse(xsltPI);
}

function ForceRefresh(){
    window.location.reload(true);
}

function loadClinic(file) {	// Load the XML data file	
	if (file == ''){
		DisplayError();
                //ForceRefresh();
	}
	else {
		var xmlFile = file;	
		loadmessage = "yes"; // Show the loading message.
		var loadXML = new net.ContentLoader(xmlFile, SetXML, DisplayError,loadmessage);	//Ajax load the XML.
	}
}

function SetXML() {	// Callback function
	xmlData = this.req.responseText;
	window.setTimeout("ParseXML()", 100); // (1) Give the XML time to load then parse it; call ParseXSLT().
}

function ParseXML() {
	xml = xmlParse(xmlData);
	window.setTimeout("ProcessData()", 1000);	// (2) Give the xml time to parse then process the XML and XSLT; call ProcessData().
}

/* Begin XML processing function */
// Google Ajaxslt 0.4
logging = false;

function ProcessData() {
	if ((xml != "") && (xslt !="")){ // If all external data is loaded and parsed.	
		//var xml = xmlParse(xmlData);
		//var xslt = xmlParse(xsltPI);
		var html = xsltProcess(xml, xslt);	
		divContent = html; 
		
		window.setTimeout("setDiv()", 1000); //Give the XML and XSLT time to process then set the div
	}
	
	else {
		ParseXML(); //Start the process over
	}
		
}
/* End XML processing function */

function setDiv() {	// Sets the clinic content 
	clean(divContentID);	// Remove all elements within container
	
	var obj = $(divContentID);
	obj.innerHTML = divContent;	//set data
	
	loadingXML('complete');	// Turn off loading message
	window.setTimeout("initRollovers()", 500);	// Wait, then add rollovers to buttons withing the new data
	xmlData = "";	// Reset xml data to nothing
	
}

function DisplayError() {	// Callback function. This is called in the event the browser doesn't support the calls we're making, or if the content we try to access does not exist. 
	var obj = $(divContentID);
	obj.innerHTML = "<span class=\"error\">" + error_msg + "</span>";
	loadingXML('complete');
}

function loadingXML(state) { // Function to show the loading message.
	var divLoadingID = "drillsDataLoading";
	switch (state) {	
		case "loading":
			$(divLoadingID).style.display="block";
			break
		case "complete":
			$(divLoadingID).style.display="none";
			break
		default:
	}
}

/* Begin dynamic drop-down functions */
var t = null;	// this is used for the timeout for the search to add slight delay
var dynamicDropDownFieldID = null;

// init function ran when page loads.
function initDynamicDropDown() {
	addEvent($("ddMainCategory"),'change',dynamicDropDownFetch);	// add "onchange" function to the Main Category drop-down
	dynamicDropDownFetch();
}

function dynamicDropDownFetch() {
	var field = "ddSubCategory";	
	dynamicDropDownFieldID =  field;
	clean($(field));	// remove all previous items in drop-down
	dynamicDropDownStart();
}

// adds a slight delay to the search
function dynamicDropDownStart() {
	if (t)	window.clearTimeout(t);
	t = window.setTimeout("dynamicDropDownSearch()",200);
}
// function which starts the dropdown process
function dynamicDropDownSearch() {
	var filter = $('ddMainCategory').options[$('ddMainCategory').selectedIndex].value;	// set variable to what main category has been selected
	
	populateDD(filter);		// function to populate the dropdown with the appropriate list of sub categories based on the selected main category
}

// function to actually add items (from array of items supplied) to dropdown
function populateDD(filter) {
	var field = dynamicDropDownFieldID;			
	
	switch(filter){		// switch the main category to choose the appropriate sub category arrays
                case "offensive":
			drillName = offense;
			drillValue = offense_values;
			break
		case "defensive":
			drillName = defense;
			drillValue = defense_values;
			break
		default:
			drillName = dflt;
			drillValue = dflt_value;
	}
	
	var len = drillName.length;										// get the total number of select options to create
	for (var i=0; i<len; i++) {										// for each item
		var option = document.createElement('option');				// create an <option> element to store the info
		option.appendChild(document.createTextNode(drillName[i]));	// create the text of the sub category to be put in option
		option.setAttribute("value",drillValue[i]);					// add a value attribute and populate with the corresponding sub category value
		$(field).appendChild(option);			// add the item to the dropdown
	}
}
/* End dynamic drop-down functions */

/* Function to remove all child nodes of an element. */
function clean(elm) {
	while($(elm).hasChildNodes()) {
		$(elm).removeChild($(elm).childNodes[0]);
	}
	return true;
}