function drawForm(action) {
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) { return; } 
	var url="tribe.php?action="+action;
	xmlHttp.onreadystatechange=stateChanged();
	xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4) { document.getElementById("divMainForm").innerHTML=xmlHttp.responseText; } }
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getHome(elementId){
	var xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) { return; }
	var url = "tribe.pl?action=home";
	xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4) { document.getElementsByTagName(elementId)[0].innerHTML=xmlHttp.responseText; } }
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function header() {
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) { return; } 
	var url="tribe.pl?action=header";
	xmlHttp.onreadystatechange=stateChanged();
	xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4) { document.getElementById("page_header").innerHTML=xmlHttp.responseText; } }
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function plantTribesSearchAction(actionValue, locationValue) {
	//addItToHistoryUrl("url", actionValue); 	//this does not yet work with the perl version (tribe.pl)
	//addit(actionValue);				//this does not yet work with the perl version (tribe.pl)
	//getPageForTribePHP(actionValue, locationValue);
	getPageForTribePerl(actionValue, locationValue);//This will need to be created for a Perl version
	return false;
}

function getPageForTribePerl(actionValue, locationValue) {
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) { return; }
	var url = "gene.pl?action=" + actionValue;
	var targetLocation = (locationValue==null || locationValue=="") ? "mainBody" : locationValue;
	var loadImage = "<center><img src='http://www.ruseoffools.com/images/loading.gif'><center>";
	xmlHttp.onreadystatechange=function(){ 
		if(xmlHttp.readyState==4) { 						
			document.getElementById(targetLocation).innerHTML = xmlHttp.responseText;			
			
			/*this will evaluate any of the scripts that are part of the responseText.
			 * without this portion of code, the browser will not execute any javascript.
			 * Instead, it will simply just print out the javascript code and not execute it 
			 * See the following for further explanation
			 * http://www.thescripts.com/forum/thread479439.html
			 * http://www.daniweb.com/forums/thread53137.html
			 */
			listOfElements = document.getElementById(targetLocation).getElementsByTagName("script");
			for (var i = 0; i < listOfElements.length; i++){
				eval(listOfElements[i].innerHTML);
				//alert(listOfElements[i].innerHTML);
			}
			//document.getElementById("url").value = url;
			//document.title = "TESTING";						
		}else if(document.getElementById(targetLocation).innerHTML == loadImage ){	//empty block, do nothing
		}else{ document.getElementById(targetLocation).innerHTML = loadImage; } 			
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getPageForTribePHP(actionValue, locationValue) {
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) { return; }
	var url = "tribe.php?action=" + actionValue;				//this will need to be changed to handle the perl version (tribe.pl)
	var targetLocation = (locationValue==null || locationValue=="") ? "mainBody" : locationValue;
	var loadImage = "<center><img src='http://www.ruseoffools.com/images/loading.gif'><center>";
	xmlHttp.onreadystatechange=function(){ 
		if(xmlHttp.readyState==4) { 						
			document.getElementById(targetLocation).innerHTML = xmlHttp.responseText;			
			//document.getElementById("url").value = url;
			//document.title = "TESTING";						
		}else if(document.getElementById(targetLocation).innerHTML == loadImage){	//empty block, do nothing
		}else{ document.getElementById(targetLocation).innerHTML = loadImage; } 			
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function addItToHistoryUrl(elementId, url) { document.getElementById(elementId).value = url; }

function displayElementById(elementId, elementStyle) {
	elementStyle = (elementStyle==null || elementStyle == "") ? "block" : elementStyle;
	document.getElementById(elementId).style.display = elementStyle;
}

function hideElementById(elementId) { document.getElementById(elementId).style.display = "none"; }

function alterDisplayElementById(elementId, newElementStyle){
	currentElementDisplayStyle = document.getElementById(elementId).style.display;
	newElementStyle = (newElementStyle==null || newElementStyle == "") ? "block" : newElementStyle;
	if (currentElementDisplayStyle == "none"){ displayElementById(elementId, newElementStyle); }
	else if (currentElementDisplayStyle == "block"){ hideElementById(elementId);}
}

function changeValueById(elementId, value1, value2){
	currentElementValue = document.getElementById(elementId).value;
	document.getElementById(elementId).value = (currentElementValue == value1) ? value2 : value1;
}

function validateCheckBoxes(elementName){
	var arrayOfElements = document.getElementsByName(elementName);	
	var checkedFlag = false;
	for (var i=0; i<arrayOfElements.length;i++){
		if (checkedFlag == true){break;}
		if (arrayOfElements[i].checked == true){ checkedFlag = true; }
	}
	if(checkedFlag == false){
		for (var i=0; i<arrayOfElements.length;i++){		
			arrayOfElements[i].checked = true;
		}
	}
}

function getValsFromSelect(elementName, queryStringVarName){
	var response_string = "";
	for (var i = 0; i < elementName.options.length; i++){
		if (elementName.options[i].selected == true){
			response_string += "&" + queryStringVarName + "=" + elementName.options[i].value;
		}
	}	
	return response_string;	
}

function testFunction(elementId) {
	var xmlHttp=new XMLHttpRequest();
	if (xmlHttp==null){ return; }
	var url = "test.html";
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){document.getElementById(elementId).innerHTML = xmlHttp.responseText;}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function keyboardInput(e, actionValue){
	var keyNum;
	keyNum = (window.event) ? e.keyCode : e.which;		//e.keyCode is for IE, e.which is for everything else...
	if (keyNum == 13){									//'13' is the "enter" key
		plantTribesSearchAction(actionValue);
		return false;
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

/*****************************************************************************************************
*
*				These are the functions associated with historyState.js
*				They require various labels to be available on the page
*
*****************************************************************************************************/
function updateStorage() {
	if (typeof(historyState.state[historyState.currentState()]) != "string") {
		historyState.state[historyState.currentState()] = "";
	}
	document.getElementById("storage").value = historyState.state[historyState.currentState()];
}

function loadListener() {
	updateStorage();
};

function historyListener() {
	document.getElementById("currentValue").value = historyState.urlFragment();
	document.getElementById("currentIndex").value = historyState.currentState();
	updateStorage();							
	//document.getElementById("currentValue").innerHTML = historyState.urlFragment(); 	//tony
	//document.getElementById("currentIndex").innerHTML = historyState.currentState();	//tony
	if (historyState.currentState() == 0) { plantTribesSearchAction("home"); }
	else { getPageForTribePHP(document.getElementById("currentValue").value); }				//tony
};

function addit(actionValue) {							
		historyState.newState(document.getElementById("url").value);								
		document.getElementById("currentValue").value = document.getElementById("url").value;
		document.getElementById("currentIndex").value = historyState.currentState();
	//	document.getElementById("currentValue").innerHTML = document.getElementById("url").value;		//tony
	//	document.getElementById("currentIndex").innerHTML = historyState.currentState();				//tony
		updateStorage();
		document.getElementById("url").value="";				
		
};

function replaceit() {
	historyState.newState(document.getElementById("url").value,true);
	document.getElementById("currentValue").value = document.getElementById("url").value;
	document.getElementById("currentIndex").value = historyState.currentState();
	updateStorage();
	document.getElementById("url").value="";
};

function storeit() {
	historyState.state[historyState.currentState()] = document.getElementById("storage").value;
	historyState.save();
	updateStorage();
};
