var PxmlHttp = false;
var boxmlHttp = false;
var pDetxmlHttp = false;
var requestType;

function PcreateXmlHttp() {
	if (window.ActiveXObject)
		PxmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) {
		PxmlHttp = new XMLHttpRequest();
	}
}

function PloadXML(pName, content) {
	PcreateXmlHttp();
	PxmlHttp.onreadystatechange=function(){
		PstatusChange(pName, content);
	}
	PxmlHttp.open("GET","aptechRecord.xml",true);
	PxmlHttp.send(null);
}

function PstatusChange(pName, content) {
	if ((PxmlHttp.readyState == 4) && (PxmlHttp.status==200)) {
		PcreateXml(pName, content);
	}
}

function PcreateXml(pName, content) {
	var txt=content;
	txt = txt + '<table class="rail_left" border="1"> <caption id="caption">Batting</caption>';
	header='<tr id="row1"><th>Matches</th><th>Innings</th><th>Not Out</th><th>Runs</th><th>Highest</th><th>Avergage</th></tr>'
	txt=txt+header;
	var response = PxmlHttp.responseXML.documentElement;
	x = response.getElementsByTagName("DATA_RECORD");
	for (i=0;i<x.length;i++) {
    	xx=x[i].getElementsByTagName("Batsmen");
		yy = xx[0].firstChild.data;
		if (yy == pName) {
			txt=txt + "<tr>";
	    	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>";
	document.getElementById('railContent').innerHTML=txt;
	bowler=bowlerXML("BowlingStats.xml", pName, txt);
}


function pDetajaxpage(fname, divID, hostURL, pName){
	pDetcreateXmlHttp();
	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
			pDetxmlHttp.onreadystatechange=function(){
				pDetloadpage(pDetxmlHttp, divID, pName)
			}
		pDetxmlHttp.open('GET', fname, true)
		pDetxmlHttp.send(null)
	
}

function pDetcreateXmlHttp() {
	if (window.ActiveXObject)
		pDetxmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) {
		pDetxmlHttp = new XMLHttpRequest();
	}
}

function pDetloadpage(pDetxmlHttp, divID, pName){
	if (pDetxmlHttp.readyState == 4 && (pDetxmlHttp.status==200)) {
		document.getElementById(divID).innerHTML ="";
		document.getElementById(divID).innerHTML=pDetxmlHttp.responseText;
		content = document.getElementById(divID).innerHTML;
//		alert(content);
		var player=PloadXML(pName, content);
	}
}


function bowlerXML(file, pName, content) {
	bowlerCreateXml();
	boxmlHttp.onreadystatechange=function(){
		bowlerStatusChange(pName, content);
	}
	boxmlHttp.open("GET","BowlingStats.xml",true);
	boxmlHttp.send(null);
}

function bowlerStatusChange(pName, content) {
	if ((boxmlHttp.readyState == 4) && (boxmlHttp.status==200)) {
//		createBowling(pName, content);
		testBowling(pName, content);
	}
}

function testBowling(pName,content) {
	var bat = content;
	var boResponse = boxmlHttp.responseXML.documentElement;
	x = boResponse.getElementsByTagName("DATA_RECORD");
	var btxt="";
	for (i=0;i<x.length;i++) {
    	xx=x[i].getElementsByTagName("Bowler");
		yy = xx[0].firstChild.data;
		if (yy == pName) {
			btxt='<table class="rail_left" border="1"> <caption id="caption">Bowling</caption>';
			header='<tr id="row1"><th>Matches</th><th>Overs</th><th>Runs</th><th>Wickets</th><th>Average</th><th>Economy</th><th>3 Wkts</th></tr>'
			btxt=btxt+header;
			btxt=btxt + "<tr>";
	       	xx=x[i].getElementsByTagName("Match");
    	    btxt=btxt + "<td>" + xx[0].firstChild.data + "</td>";
       		xx=x[i].getElementsByTagName("Overs");
	        btxt=btxt + "<td>" + xx[0].firstChild.data + "</td>";
	       	xx=x[i].getElementsByTagName("Runs");
	        btxt=btxt + "<td>" + xx[0].firstChild.data + "</td>";
	       	xx=x[i].getElementsByTagName("Wickets");
    	    btxt=btxt + "<td>" + xx[0].firstChild.data + "</td>";
       		xx=x[i].getElementsByTagName("Average");
	        btxt=btxt + "<td>" + xx[0].firstChild.data + "</td>";
    	   	xx=x[i].getElementsByTagName("Economy");
        	btxt=btxt + "<td>" + xx[0].firstChild.data + "</td>";
	       	xx=x[i].getElementsByTagName("Three_Wickets");
    	    btxt=btxt + "<td>" + xx[0].firstChild.data + "</td>";
			btxt=btxt + "</tr>";
		}
	  btxt=btxt + "</table>";
      }
	  battingTxt=document.getElementById('railContent').innerHTML;
	  compTxt = battingTxt + btxt;
	  document.getElementById('railContent').innerHTML=compTxt;	
}

function bowlerCreateXml() {
	if (window.ActiveXObject) {
		boxmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		boxmlHttp = new XMLHttpRequest();
	}
}
