var xmlhttp;
var p_templateUrl;
var p_season_id;
var p_get_all;

function stateChanged(){
	if(xmlhttp.readyState == 4){
		document.getElementById("scorecard").innerHTML = xmlhttp.responseText;
		document.getElementById("cmb_fixtures").disabled = false;
		getFixturesForSeason(p_templateUrl,p_season_id,p_get_all);
		//document.getElementById('b_report').style.visibility = 'visible';
	}
}

function stateChanged_player(){
	if(xmlhttp.readyState == 4){
		document.getElementById("player_details").innerHTML = xmlhttp.responseText;
	}
}

function stateChanged_stats_list(){
  if(xmlhttp.readyState == 4){
    document.getElementById("list_statistics").innerHTML = xmlhttp.responseText;
  }
}

function stateChanged_fixtures_list(){
  if(xmlhttp.readyState == 4){
    document.getElementById("list_fixtures").innerHTML = xmlhttp.responseText;
  }
}

function GetXmlHttpObject(){
	if(window.XMLHttpRequest){
		return new XMLHttpRequest();
	}

	if(window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}


function getPlayerDetails(templateUrl, year, player_id) {
 p_templateUrl = templateUrl;
 xmlhttp = GetXmlHttpObject();
 document.getElementById("cmb_players").value =  player_id;
 if(xmlhttp == null){
   alert("Browder does not support HTTP Request");
   return;
 }

 var url = templateUrl + "/IndPlayerStats.php?id=" + player_id;
 if (year != null)
   url += "&year=" + year;
   
 xmlhttp.onreadystatechange = stateChanged_player;
 xmlhttp.open("GET",url,true);
 xmlhttp.send(null);
}

function getStatistics(templateUrl) {
 p_templateUrl = templateUrl;
 xmlhttp = GetXmlHttpObject();
 // document.getElementById("cmb_years").value =  year;
 if(xmlhttp == null){
   alert("Browder does not support HTTP Request");
   return;
 }

 var url = templateUrl + "/statistics_year.php?year=" + document.getElementById("cmb_years").value + "&allplayerstats=" + document.getElementById("allplayerstats").checked;
 xmlhttp.onreadystatechange = stateChanged_stats_list;
 xmlhttp.open("GET",url,true);
 xmlhttp.send(null);
}

function getFixtures(templateUrl, year) {
 p_templateUrl = templateUrl;
 xmlhttp = GetXmlHttpObject();
 document.getElementById("cmb_years").value =  year;
 if(xmlhttp == null){
   alert("Browder does not support HTTP Request");
   return;
 }

 var url = templateUrl + "/fixtures_year.php?year=" + year;
 xmlhttp.onreadystatechange = stateChanged_fixtures_list;
 xmlhttp.open("GET",url,true);
 xmlhttp.send(null);
}
 
function getScoreCard(templateUrl, fixture_id, season_id) {
 //alert(templateUrl.concat(" | ").concat(fixture_id).concat(" | ").concat(season_id));
 p_season_id = season_id;
 p_templateUrl = templateUrl;
 p_get_all = 0;
 xmlhttp = GetXmlHttpObject();
 document.getElementById("cmb_seasons").value =  season_id;
 document.getElementById("cmb_fixtures").value =  fixture_id;
 if(xmlhttp == null){
   alert("Browder does not support HTTP Request");
   return;
 }

 var url = templateUrl + "/stats.php?m=sc&fId=" + fixture_id;
 xmlhttp.onreadystatechange = stateChanged;
 xmlhttp.open("GET",url,true);
 xmlhttp.send(null);
}

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl;
    }
    else {
        // Root Url for domain name
        return baseURL;
    }

}

function getReport(post) {
  iBox.showURL(getBaseURL() + "/match-reports/" + post);
}

function getPage(page) {
  iBox.showURL(getBaseURL() + "/" + page);
}

function getFixturesForSeason(templateUrl, season_id, getAll) {
 document.getElementById("cmb_fixtures").length=0;
 p_templateUrl = templateUrl;
 xmlhttp = GetXmlHttpObject();
 if(xmlhttp == null){
   alert("Browder does not support HTTP Request");
   return;
 }

 var url = templateUrl + "/stats.php?m=s&sId=" + season_id+"&all="+getAll;
// alert("Callable URL: " + url);
 xmlhttp.onreadystatechange = stateChanged_getFixturesForSeason;
 xmlhttp.open("GET",url,true);
 xmlhttp.send(null);
}

function stateChanged_getFixturesForSeason(){
	if(xmlhttp.readyState == 4){
		//alert(xmlhttp.responseText);
		var fixDetails = xmlhttp.responseText.split("|");
		//alert(fixDetails.length + ":" + fixDetails[0] + ":" + fixDetails[length-1]);
		for (var i=1;i<fixDetails.length;i++){
			fixInfo = fixDetails[i].split(";");
			//if(fixInfo[0] == "") fixInfo[0] = " ";
			document.getElementById("cmb_fixtures").options[i] =  new Option(fixInfo[1],fixInfo[0]);
		}
		document.getElementById("cmb_fixtures").disabled=false;
	}
}

function sendMail(templateUrl, frmContact){
	var contact_name = frmContact.name.value;
	var email = frmContact.email.value;
	var phone = frmContact.phone.value;
	var message = frmContact.message.value;
	xmlhttp = GetXmlHttpObject();
	 if(xmlhttp == null){
	   alert("Browder does not support HTTP Request");
	   return;
	 }
	 var url = templateUrl + "/SendMail.php?name="+contact_name+"&email="+email+"&phone="+phone+"&message="+message;
	 //alert("Send mail called: " + url);
	 xmlhttp.onreadystatechange = stateChanged_sendMail;
	 xmlhttp.open("GET",url,true);
	 xmlhttp.send(null);
}

function stateChanged_sendMail(){
	if(xmlhttp.readyState == 4){
		alert(xmlhttp.responseText);
	}
}

function getTeamsForFixture(templateUrl, fixture_id) {
 document.getElementById("cmb_toss").length=0;
 p_templateUrl = templateUrl;
 xmlhttp = GetXmlHttpObject();
 if(xmlhttp == null){
   alert("Browder does not support HTTP Request");
   return;
 }

 var url = templateUrl + "/stats.php?m=t&tId=" + fixture_id;
 xmlhttp.onreadystatechange = stateChanged_getTeamsForFixture;
 xmlhttp.open("GET",url,true);
 xmlhttp.send(null);
}

function stateChanged_getTeamsForFixture(){
	if(xmlhttp.readyState == 4){
		//document.getElementById("cmb_toss").length=0;
		var teams = xmlhttp.responseText.split(";");
		document.getElementById("cmb_teams").options[0] = new Option('');
		for(var i=0;i<teams.length;i++){
			document.getElementById("cmb_toss").options[i] = new Option(teams[i]);
			document.getElementById("cmb_teams").options[i+1] = new Option(teams[i]);
		}
		var teams = document.getElementById("cmb_teams").selectedIndex = 0;
	}
}
