function changeText(str,strr,el){
	changeText.el=el;
//	$('#' + changeText.el).load('manage_code.php?q=' + str + '&id=' + strr + '&div=' + el);


	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById(changeText.el).innerHTML=xmlhttp.responseText;
			
			}
		}

	xmlhttp.open ("GET", "manage_code.php?q=" + str + "&id=" + strr + "&div=" + el, true);
	xmlhttp.send();
}

function showFormation(str,strr,el){
	showFormation.el=el;
	//alert("haje");
	//$(showFormation.el).load('de_show.php?q=' + str + '&id=' + strr + '&div=' + el);

	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById(showFormation.el).innerHTML=xmlhttp.responseText;
			
			}
		}

	xmlhttp.open ("GET", "de_show.php?q=" + str + "&id=" + strr + "&div=" + el, true);
	xmlhttp.send();
}

function checkForm(form){
	var home_cap = form.home_cap_goals.value;
	var away_cap = form.away_cap_goals.value;
	var home_total = form.home_score.value;
	var away_total = form.away_score.value;
	var home_gt = form.home_gt.value;
	var away_gt = form.away_gt.value;
	if (home_cap > home_total){
		var captain = home_gt;
	}
	if (away_cap > away_total){
		var captain = away_gt;
	}
	if (captain){
		alert("Captain " + captain + " has scored more goals than the total amount of goals? Yeah right.");
	return false;
	}
	else {
		var correct = confirm("You got the goalscorers correct?");
		if (correct){
			return true;
		}
		else {
			return false;
		}
	}
}

function showNav() {
	var varenTwo = document.getElementById("list").getElementsByTagName("ul");
	for (i = 0; i < varenTwo.length; i++){
			varenTwo[i].className = 'hide';
	}
	document.getElementById("hide_list").style.display = "block";
}

function testIng() {
	var varenTwo = document.getElementById("list").getElementsByTagName("li");

	for (i = 0; i < varenTwo.length; i++){
		varenTwo[i].onmouseover = function(){
			rightKey = findNext(this);
			if (rightKey != false){
				this.childNodes[rightKey].className = 'show';
				//this.childNodes[c].style.visibility = "visible";
			}
		}
	}

	for (i = 0; i < varenTwo.length; i++){
		varenTwo[i].onmouseout = function(){
			rightKey = findNext(this);
			if (rightKey != false){
				this.childNodes[rightKey].className = 'hide';
				//this.childNodes[c].style.visibility = "hidden";
			}
	
		}

	}

}
function findNext(abbrev){
	amountNodes = abbrev.childNodes.length;
	for (c = 0; c < amountNodes; c++){
		if (abbrev.childNodes[c].nodeName == 'UL'){
			return c;
			break;
		}
	}
	return false;
}

function checkTen(form){
	var home_team = form.home_team.value;
	var away_team = form.away_team.value;
	if (home_team == away_team){
		alert("Home team and Away team should not be the same.");
		return false;
	}
		return true;
}

function checkTransfer(form){
	var counter = 0;
	var tempValue;
	var onlyZero = true;
	var same = false;
	var uniqueArray = new Array();
	for (i = 0; i < form.elements.length; i ++){
		if (form.elements[i].type == 'select-one'){
			tempValue = form.elements[i].value;
			if (tempValue != 0){
				onlyZero = false;				
				for (j = 0; j < uniqueArray.length; j ++){
					if (uniqueArray[j] == tempValue){
						same = true;
						alert ("Please don't have the same priority for more than one team");
						return false;
						break;
					}
					else {
						same = false;
					}
				}
				if (same == false){
					uniqueArray[counter] = tempValue;
					counter ++;
				}				
			}			
		}
	}
	if (onlyZero == true){
		alert ("Please apply for one of the teams");
		return false;
	}
	return true;
}
function showApplicants(id){

	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById("applicants").innerHTML=xmlhttp.responseText;			
			}
		}

	xmlhttp.open ("GET", "transfer_team_ajax.php?id=" + id, true);
	xmlhttp.send();
}
function showTeams(gt){

	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById("teams").innerHTML=xmlhttp.responseText;			
			}
		}

	xmlhttp.open ("GET", "transfer_gt_ajax.php?gt=" + gt, true);
	xmlhttp.send();
}
var winner;
var team;
var statusInt = 1;
var gTeam_id = 0;
var adm = 0;
var team_id;
var drawStatus;

function startDraw(form){
	if (form.team_id){
		team_id = form.team_id.value;
		if (gTeam_id == 0){
			gTeam_id = team_id;
			drawStatus = team_id;
		}
	}
	else {
		//var first_letter = form.charAt(0);
		drawStatus = form;
		var winnerCall = true;
	}

	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open ("GET", "countdown_ajax.php?id=" + drawStatus + "&adm=" + adm, false);
	xmlhttp.send(null);

	//return xmlhttp.responseText;
	if (winnerCall){
		winner = xmlhttp.responseText;
	}
}
var mainCounter = 11;

function checkStart(admStatus, index){
	if (index != undefined){
		team_id = index;
	}

	if (mainCounter < 1){
		adm = admStatus;
		startDraw('W' + drawStatus);
		document.getElementById("countdown").innerHTML = winner;
		clearTimeout(timerID);
	}
	else {
		drawStatus = checkDatabase();
		if (drawStatus != 0){
			document.getElementById("countdown").innerHTML = mainCounter;
			mainCounter --;
		}
		else {
			if (statusInt % 2 == 1){
				document.getElementById("status").innerHTML = "If this text are blinking, you are present at the Live Draw.";
			}
			else {
				document.getElementById("status").innerHTML = "";
			}
			if (statusInt % 10 == 0){
				liveDrawsApp(team_id);
			}
			statusInt ++;
		}

		timerID = setTimeout("checkStart(" + admStatus + ")", 1000);
	}
}

function checkDatabase(){
	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open ("GET", "check_database.php", false);
	xmlhttp.send(null);
	return xmlhttp.responseText;
}

function checkSwap(form){
	var list1 = form.gt_2;
	var list2 = form.gt_3;
	var gt_2 = list1.options[list1.selectedIndex].value;
	var gt_3 = list2.options[list2.selectedIndex].value;
	if (gt_2 == gt_3){
		alert("Gamertag 2 and 3 can't be the same. Try again.");
		return false;
	}
	else {
		return true;
	}
}

function youSure(){
	if (confirm("Are you sure?")){
		return true;
	}
	else {
		alert("ASDF");
		return false;
	}
}

function drawOngoing(){
	window.stop();
	alert ("A draw is going on, please wait around 10 seconds and try again.");
	window.location.reload();
}

function liveDrawsApp(id){

	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById("live_information").innerHTML=xmlhttp.responseText;			
			}
		}

	xmlhttp.open ("GET", "live_draws_ajax.php?id=" + id, true);
	xmlhttp.send();
}

function checkBr (){
	var br = navigator.appName;
	if (br == "Microsoft Internet Explorer"){
		window.stop();
		alert ("Live Draws are unfortunately not for IE-users right now. Please use Firefox, thanks.");
		window.location="http://www.mozilla.org/";
	}
}

function checkMom(form){
	var momChecked_h;
	var momChecked_a;
	for (var i = 0; i < form.home_mom.length; i ++){
		if (form.home_mom[i].checked){
			momChecked_h = true;
			break;
		}
	}
	for (var j = 0; j < form.away_mom.length; j ++){
		if (form.away_mom[j].checked){
			momChecked_a = true;
			break;
		}
	}
	if (momChecked_h == true && momChecked_a == true){
		alert ("Only a total of 1 Man of the Match please.");
		for (var k = 0; k < form.home_mom.length; k ++){
			form.home_mom[k].checked = false;
			form.away_mom[k].checked = false;
		}
		return false;
	}
	else if (momChecked_h == true || momChecked_a == true){
		return true;
	}
	else {
		alert ("Please fill in Man of the Match, thanks.")
		return false;
	}
}

var y_pos = 200;
var sec = 4;
var opa = 1;
var newsAmount;
var curNews = 0;
var speedGlobal = 250;
var wait_type = 1;
var waitID;

function startNews(amount){
	newsAmount = amount;
	changeNews();
	//newsScroll();
}
function newsScroll(){	
	document.getElementById("s_news").style.top = y_pos + "px";
	y_pos --;
	if (y_pos < 15){
		/*clearTimeout(timerID);*/
		clearInterval(timerID);
		wait();
	}
	else {
		/*timerID = setTimeout("newsScroll()", 50);*/
	}
}

function wait(){
	if (sec <= 0){
		clearTimeout(waitID);
		sec = 4;
		fadeOut();
	}
	else {
	waitID = setTimeout("wait()", 1000);
	sec --;
	}
}

function wait_two(){
	if (wait_type == 0){
		getResults();
		clearTimeout(wait_twoID);
	}
	else {
	wait_twoID = setTimeout("wait_two()", 2500);
	wait_type = 0;
	}
}

function fadeOut(){
	if (opa < 0.2){
		clearTimeout(opaID);
		y_pos = 200;
		opa = 1;
		document.getElementById("s_news").style.opacity = 1;
		document.getElementById("s_news").style.filter = 'alpha(opacity = 100)';

		document.getElementById("s_news").style.top = y_pos + "px";
		changeNews();
	}
	else {
		opaID = setTimeout("fadeOut()", speedGlobal);
		opa = opa - 0.1;
		ieopa = opa * 100;
		document.getElementById("s_news").style.opacity = opa;
		document.getElementById("s_news").style.filter = 'alpha(opacity = ' + ieopa + ')';	
	}
}

function skip(){
	speedGlobal = 100;
	clearTimeout(waitID);
	clearInterval(timerID);
	fadeOut();
	/*clearTimeout(waitID);*/
	sec = 2;
}

function changeNews(){
	if (curNews == newsAmount){
		curNews = 0;
	}
	$('#s_news').load('change_news_ajax.php?no=' + newsAmount + '&cur=' + curNews);

	curNews ++;
	timerID = setInterval("newsScroll()", 50);
	speedGlobal = 250;
}

var result_no = 1;
var container_x = 882;
var x_pos = container_x;
var horiID;
var result_width = 0;

function horiScroll() {
	if (x_pos < result_width){
		clearInterval (horiID);
		x_pos = container_x;
		if (result_no == 3)
			result_no = 1;
		else
			result_no ++;
		//alert("x_pos = " + x_pos + ", width = " + result_width);
		getResults();
	}
	else {
		document.getElementById("latest").style.left = x_pos + "px";
		x_pos --;
		if (x_pos == 800 || x_pos == 799){
			result_width = document.getElementById("latest").offsetWidth;
			result_width = - result_width;
		}
	}
}

function getResults() {
	$('#latest').load('get_results_ajax.php?no=' + result_no);
	horiID = setInterval("horiScroll()", 20);
}

function checkAvailable(form){
	var hour_s = parseInt(form.hour_one_s.options[form.hour_one_s.selectedIndex].value);
	var min_s = parseInt(form.min_one_s.options[form.min_one_s.selectedIndex].value);
	var hour_e = parseInt(form.hour_one_e.options[form.hour_one_e.selectedIndex].value);
	var min_e = parseInt(form.min_one_e.options[form.min_one_e.selectedIndex].value);
	var h1 = validTimes(hour_s, hour_e, min_s, min_e, 1);
	if (h1 != true){
		alert (h1);
		return false;
	}

	hour_s = parseInt(form.hour_one_s.options[form.hour_two_s.selectedIndex].value);
	min_s = parseInt(form.min_one_s.options[form.min_two_s.selectedIndex].value);
	hour_e = parseInt(form.hour_one_e.options[form.hour_two_e.selectedIndex].value);
	min_e = parseInt(form.min_one_e.options[form.min_two_e.selectedIndex].value);
	if (hour_s == hour_e && min_s == min_e){
		return true;
	}
	var h2 = validTimes(hour_s, hour_e, min_s, min_e, 2);
	if (h2 != true){
		alert (h2);
		return false;
	}

	hour_s = parseInt(form.hour_one_s.options[form.hour_three_s.selectedIndex].value);
	min_s = parseInt(form.min_one_s.options[form.min_three_s.selectedIndex].value);
	hour_e = parseInt(form.hour_one_e.options[form.hour_three_e.selectedIndex].value);
	min_e = parseInt(form.min_one_e.options[form.min_three_e.selectedIndex].value);
	if (hour_s == hour_e && min_s == min_e){
		return true;
	}
	var h3 = validTimes(hour_s, hour_e, min_s, min_e, 3);
	if (h3 != true){
		alert (h3);
		return false;
	}
	return true;
}

function validTimes(h1_s, h1_e, m1_s, m1_e, index){
	if (h1_s <= h1_e){
		if (h1_s == h1_e && (m1_e - m1_s) >= 30){
			h1 = true;
		}
		else if ((h1_e - h1_s) == 1 && m1_e + (60 - m1_s) >= 30){
			h1 = true;
		}
		else if ((h1_e - h1_s) > 1){
			h1 = true;
		}
		else {
			h1 = "Please submit a timespace with at least 30 minutes (Match #" + index + ").";
		}
	}
	else if ((h1_e + 24 - h1_s) == 1 && m1_e + (60 - m1_s) < 30){
		h1 = "Please submit a timespace with at least 30 minutes (Match #" + index + ").";
	}
	else if (h1_e == 0 && m1_e == 0) {
		h1 = true;
	}
	else {
		h1 = "No time spans over midnight, divide your sessions please (Match #" + index + ").";
	}
	return h1;
}

function toggleMiddle(){
	middleID = setInterval("changeMiddle()", 5000);
}
var middleStatus = 0;

function updateAvailable(gt){
	$('#avail_container').load('update_opp_status.php?gt=' + gt);
}
var gt;
function star(amo, gamertag, avail_status){
	gt = gamertag;
	if (avail_status != 1){
		setInterval(function(){
		updateAvailable(gt)}, 10000);
		//availID = setInterval("updateAvailable(gt)", 10000);
	}
	newsAmount = amo;
	changeNews();
	wait_two();
	
	//toggleMiddle();
}
function showTopLeft(){
	document.getElementById("top_left_news").style.display="none";
	document.getElementById("top_left").style.display="inline";
}

/*function justDet(one, two, three){
	$(document).ready(function(){
		star(one, two, three);
		calender(0);
	});
}*/
var calenderVar = 0;

function calender(data, info){
	calenderVar = calenderVar + data;
	if (info == 'undefined'){
		info = 0;
	}
	$('#calender_container').load('change_calender_ajax.php?c=' + calenderVar + '&i=' + info);
}

function changeGraph(gt, type){
	$('#rank_type').load('graph_type.php?gt=' + gt + '&t=' + type);
}

function answerPage(division){
	//var form = document.forms[0];
	//var list = document.forms[0].choices;
	//alert (division);
	if (!division){
		var div = $("#div").val();
	}
	else {
		var div = division;
	}
	//alert (div);
	var answer = $("#answer").val();
	//var div = form.div.options[form.div.selectedIndex].value;
	//var answer = form.answer.options[form.answer.selectedIndex].value;
	$('#answer_pic').load('questions_ajax.php?d=' + div + '&id=' + answer);
}

//$(document).ready(function(){
	//alert ("Hej");
//}

function y_nav(){
	$("#tabs li").click(function(){
		var index = $("#tabs li").index(this);
		alert ("test = " + index);
		$('#yp_content').load('yp_ajax.php?i=' + index);
	});
};
		
