//////////////////////////////
////  General Functions  /////
//////////////////////////////

function initTabs() {
	var foundIt = false;
	var divID = getQueryVariable("divID");
	if ((divID != null) && (divID.length > 0)) { 
		if (document.getElementById(divID)) { 
			showLayer(divID);
		}
	}
	else { 
		if (document.getElementById('whatWeDo')) { 
			showLayer('whatWeDo');
		}
		else { 
			return false;
		}
	}
}

// Left side navigation menu
function getElementsByClassName(nodeId, className) {
	node = document.getElementById(nodeId);
	var a = [];
	var re = new RegExp('\\b' + className + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
	if(re.test(els[i].className))a.push(els[i]);
	return a;
}
function showLayer(layerToShow) { 
	var x = 0;
	var arr = getElementsByClassName('leftSide','sect');
	var len = arr.length;
	for (x=0; x<len; x++) {
		if (arr[x].id != layerToShow) { 
			arr[x].style.display = 'none';
			arr[x].style.visibility = 'hidden';
		}
		else { 
			arr[x].style.display = 'block';
			arr[x].style.visibility = 'visible';
		}
	}
}
							
//Adds a function to be run when the page finally loads
//Adoped from http://www.tek-tips.com/faqs.cfm?fid=4862
function addOnloadEvent(fnc) {
	if (typeof window.addEventListener != "undefined") {
 		window.addEventListener("load", fnc, false);
	} else if (typeof window.attachEvent != "undefined") {
		window.attachEvent("onload", fnc);
	} else {
		if (window.onload != null) {
			var oldOnload = window.onload;
			window.onload = function (e) {
				oldOnload(e);
				window[fnc]();
			};
		} else  {
			window.onload = fnc;
		}
	}
}
addOnloadEvent(initTabs);
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
// (From microsoft.com)
function getInternetExplorerVersion() {
	var rv = -1; // Return value assumes failure
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null) {
			rv = parseFloat(RegExp.$1);
		}
 	}
	return rv;
}

//IE 6/7 use a different attribute name for the ID attribute of tags than other browsers
function getElementClassAttributeName() {
	//Get the version of IE, as version 6 requires a different attribute name
	var ver = getInternetExplorerVersion();
	var classId = "class";
	if (ver >= 6.0) { 
		classId = "className";
	}
	return classId;
}

// Gets the query strong from browser.
// Author: Pete Freitag (pete@cfdev.com)
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

var colorList;
var colorClass;
function initColorClass() {
	colorList = new Array();
	colorList[0] = "tableBG_Blue";
	colorList[1] = "tableBG_Gold";
	colorClass;
}
initColorClass();

function getNextColorClass() {
	//Determine index
	var currentIndex;
	for (var i = 0; i < colorList.length; i++) {
		if (colorList[i] == colorClass) { currentIndex = i; }
	}
	
	currentIndex = (currentIndex + 1) % colorList.length;
	colorClass = colorList[currentIndex];
	return colorClass;
}

//////////////////////////////
////    AJAX Functions    ////
//////////////////////////////

var blankJoinRow;
function initAJAX() {
	try {
		blankJoinRow = blankJoinRowTemplate;
		colorClass = initialColorClass;
	} catch (e) {}
}

var eventHandler;
var httpRequest;
function makeAJAXRequest(url, eventHandler_rhs) {
	var result = true;
	httpRequest = false;
	eventHandler = eventHandler_rhs;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		try {
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) {
				httpRequest.overrideMimeType('text/xml'); //Deals with Firefox bug
			}
		} catch (e) {}
	} else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		result = false;
	} else {
		httpRequest.onreadystatechange = alertContents;
		httpRequest.open('GET', url, true);
		httpRequest.send(null);
	}
	
	return result;
}

function alertContents() {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) { //HTTP Code 200 == OK
			eval(eventHandler + "('" + httpRequest.responseText + "');");
		} else {
			alert('There was a problem with the request.');
		}
	}
}

var rowID;
function changedJoinRow(keyFieldName, rowID_rhs) {
	//Determine which row we're talking about
	rowID = rowID_rhs;
	
	//Get input from key field
	var value = null;
	eval("value = document.mainForm." + keyFieldName + ".value;");
	
	//Make AJAX request
	//makeAJAXRequest('/recreation/scripts/RentalCenter/AJAX_SFFacility_SFSport_join.php?ID=' + value , "populateJoinRow");
	
	//Determine how many rows we current have
	var rows = document.getElementById("joinTable").children;
	var numRows = rows.length - 1; //Don't include the header row
	
	//If the current ID is one less than the number of rows, we need to add a new one
	if (rowID == numRows -  1) {
		//Insert a new row
		insertBlankJoinRow(numRows);
		
		//Set focus on new row
		//eval("document.testForm.txtID" + numRows + ".focus();");
	}
}

function populateJoinRow(responseText) {
	document.getElementById("joinTable").rows[document.getElementById("joinTable").rows.length].innerHTML = responseText;
}

function insertBlankJoinRow(rowID) {
	//Replace dummy ID with real ID and next color class
	var newBlankRow = blankJoinRow;
	newBlankRow = newBlankRow.replace(new RegExp("65534", "gi"), rowID);
	newBlankRow = newBlankRow.replace(new RegExp(colorClass, "gi"), getNextColorClass());
	
	//Create new div
	var newRowDiv = document.createElement("div");
	newRowDiv.setAttribute("className", "tableRow");
	newRowDiv.innerHTML = newBlankRow;
	document.getElementById("joinTable").appendChild(newRowDiv);
}
