var loadstatustext="Requesting content from server, please wait..."
var xmlHttp = false;
var requestType;
var sxmlHttp;
var bxmlHttp;

function createXmlHttp() {
	if (window.ActiveXObject)
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function ajaxpage(fname, divID, hostURL){
//	alert("fname: " + fname + "\n divID: " + divID + "\n hostURL:" + hostURL);
	if (fname.match("external2.htm")) {
		loadXML("aptechRecord.xml");
	}
	else if (fname.match("calen.htm")) {
		window.location.href="calen.htm";
	}	
	else if (fname.match("external3.htm")) {
		recordXmlRequest();
	}	
	else if (fname.match("external1.htm") || fname.match("external.htm") || fname.match("external5.htm") || fname.match("external4.htm") || fname.match("joinUs.htm"))
//	else 
	{
		createXmlHttp();
		var ullist=hostURL.parentNode.parentNode.getElementsByTagName("li")
		for (var i=0; i<ullist.length; i++)
			ullist[i].className=""  //deselect all tabs
			hostURL.parentNode.className="selected"  //highlight currently clicked on tab
			document.getElementById(divID).innerHTML=loadstatustext
			xmlHttp.onreadystatechange=function(){
				loadpage(xmlHttp, divID)
			}
			xmlHttp.open('GET', fname, true)
		xmlHttp.send(null)
	}
}

function loadpage(xmlHttp, divID){
	if (xmlHttp.readyState == 4 && (xmlHttp.status==200)) {
		document.getElementById(divID).innerHTML ="";
		document.getElementById(divID).innerHTML=xmlHttp.responseText;
	}
}

function navigation(){
	var ulTag=document.getElementById("nav");
	var ulist=ulTag.getElementsByTagName("li")
		for (var x=0; x<ulist.length; x++){ 
			var ulistlink=ulist[x].getElementsByTagName("a")[0]
			ulistlink.onclick=function(){
					ajaxpage(this.getAttribute("href"), this.getAttribute("rel"), this)
					return false
			}
		}
}

function loadXML(file) {
	createXmlHttpRequest();
	sxmlHttp.onreadystatechange=function(){
		statusChange();
	}
	sxmlHttp.open("GET",file,true);
	sxmlHttp.send(null);
}

function statusChange() {
	if ((sxmlHttp.readyState == 4) && (sxmlHttp.status==200)) {
		createXml();
	}
}

function createXml() {
	var response = sxmlHttp.responseXML.documentElement;
	var header;
	header = '<div id="tab" style="width:395px;"><ul id="tabmenu"><li><a href="#" rel="tcontent1" onclick="battingXmlRequest();">Batting</a></li><li><a href="#" rel="tcontent1" onclick="bowlingXmlRequest();">Bowling</a></li></ul><div id="tcontent1" style="display: block;"></div></div>';
	txt = header + '<table border=1" id="statsTable">';
	x = response.getElementsByTagName("DATA_RECORD");
	header='<tr id="row1"><th>Batsman</th><th>Matches</th><th>Innings</th><th>Not Out</th><th>Runs</th><th>Highest</th><th>Average</th></tr>'
	txt=txt+header;
	for (i=0;i<x.length;i++) {
		txt=txt + "<tr>";
    	xx=x[i].getElementsByTagName("Batsmen");
	    txt=txt + '<td style="font: bold 10px Verdana, Arial, Helvetica, sans-serif;">' + xx[0].firstChild.data + "</td>";
    	xx=x[i].getElementsByTagName("Match");
        txt=txt + "<td>" + xx[0].firstChild.data + "</td>";
    	xx=x[i].getElementsByTagName("Innings");
        txt=txt + "<td>" + xx[0].firstChild.data + "</td>";
    	xx=x[i].getElementsByTagName("NotOut");
        txt=txt + "<td>" + xx[0].firstChild.data + "</td>";
    	xx=x[i].getElementsByTagName("Runs");
        txt=txt + "<td>" + xx[0].firstChild.data + "</td>";
    	xx=x[i].getElementsByTagName("HS");
        txt=txt + "<td>" + xx[0].firstChild.data + "</td>";
    	xx=x[i].getElementsByTagName("Ave");
        txt=txt + "<td>" + xx[0].firstChild.data + "</td>";      
		txt=txt + "</tr>"
      }
  txt=txt + "</table>";
//  bowling=loadXMLBowling("BowlingStats.xml");
  document.getElementById('railContent').innerHTML=txt
//  alert(document.getElementById('railContent').innerHTML);
}


function createXmlHttpRequest() {
	if (window.ActiveXObject) {
		sxmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		sxmlHttp = new XMLHttpRequest();
	}
}

var recordXmlHttp = false;

function recordXmlRequest() {
	recordXmlHttpRequest();
	recordXmlHttp.onreadystatechange = function() {
		recordStateChange();
	}
	recordXmlHttp.open("GET", "record.xml", true);
	recordXmlHttp.send(null);
}

function recordXmlHttpRequest() {
	if (window.ActiveXObject) {
		recordXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		recordXmlHttp = new XMLHttpRequest();
	}
}

function recordStateChange() {
	if ((recordXmlHttp.readyState == 4) && (recordXmlHttp.status==200)) {
		displayRecord();
	}
}


function displayRecord() {
var recHeader;
var fieldTxt;
var responseRec = recordXmlHttp.responseXML.documentElement;
var rec = responseRec.getElementsByTagName("record");
recHeader = '<table class="rail_left" border="1"> <caption id="caption">Team Record</caption>';
recHeader = recHeader + '<tr id="row1"><th>Type</th><th>Played</th><th>Won</th><th>Lost</th><th>Tied</th><th>Abandoned</th></tr>'
fieldTxt=recHeader;

	for (i=0;i<rec.length;i++) {
		fieldTxt=fieldTxt + "<tr>";
		xx=rec[i].getElementsByTagName("type");
		fieldTxt=fieldTxt + '<td style="font: bold 10px Verdana, Arial, Helvetica, sans-serif;">' + xx[0].firstChild.data + "</td>";
		xx=rec[i].getElementsByTagName("matches");
		fieldTxt=fieldTxt + "<td>" + xx[0].firstChild.data + "</td>";
		xx=rec[i].getElementsByTagName("won");
		fieldTxt=fieldTxt + "<td>" + xx[0].firstChild.data + "</td>";
		xx=rec[i].getElementsByTagName("lost");
		fieldTxt=fieldTxt + "<td>" + xx[0].firstChild.data + "</td>";
		xx=rec[i].getElementsByTagName("tied");
		fieldTxt=fieldTxt + "<td>" + xx[0].firstChild.data + "</td>";
		xx=rec[i].getElementsByTagName("abandoned");
		fieldTxt=fieldTxt + "<td>" + xx[0].firstChild.data + "</td>";      
		fieldTxt=fieldTxt + "</tr>"
	}
fieldTxt=fieldTxt + "</table>";
document.getElementById('railContent').innerHTML=fieldTxt
}

