var emptyScore = false;

function store(showMessage) {
	var rootElement;
	emptyScore = false;

	//Data verified
	if (document.implementation.createDocument) {
		domDoc = document.implementation.createDocument("","games", null);
		rootElement = domDoc.documentElement;
	} else if ((window.ActiveXObject)) {
		domDoc = new ActiveXObject("Microsoft.XMLDOM");
		rootElement = domDoc.createElement("games");
		domDoc.appendChild(rootElement);
	}

	var realScoreElement = document.getElementById("realScore");
	if (realScoreElement!=null) rootElement.setAttribute('realScore', 'true');

	//Get the data from the table
	var logDiv = document.getElementById('logDiv');

	// Get the groupgames
	var groupPredictionTable = document.getElementById("groupPredictions");
	var groupPredictionRows = groupPredictionTable.rows;
	var groupPredictionsElement = domDoc.createElement("groupPredictions");
	rootElement.appendChild(groupPredictionsElement);
	for (var i=0; i<groupPredictionRows.length; i++) {
		var groupPredictionId = groupPredictionRows[i].id;
		try {
			var groupPrediction = -1;
			if (document.getElementById("prediction_"+groupPredictionId+"_1").checked) groupPrediction=1;
			else if (document.getElementById("prediction_"+groupPredictionId+"_2").checked) groupPrediction=2;
			else if (document.getElementById("prediction_"+groupPredictionId+"_3").checked) groupPrediction=3;
			else emptyScore=true;

			var groupPredictionElement = domDoc.createElement("prediction");
			groupPredictionElement.setAttribute("id", groupPredictionId);
			groupPredictionElement.setAttribute("value", groupPrediction);

			groupPredictionsElement.appendChild(groupPredictionElement);
		} catch (e) {
		}
	}
	
	// Get the Eightfinal games
    var efPredictionTable = document.getElementById("efPredictions");
	efPredictionsElement = getPredictionsElement(efPredictionTable, "efPredictions");
	if (!efPredictionsElement) {
		alert('1/8 Finale: Je hebt dubbele landen ingevuld.\nJe voorspelling is NIET opgeslagen.\nPas je voorspelling aan en sla deze nogmaals op.');
		return;
	}
	rootElement.appendChild(efPredictionsElement);

	// Get the Quarterfinal games
    var qfPredictionTable = document.getElementById("qfPredictions");
	qfPredictionsElement = getPredictionsElement(qfPredictionTable, "qfPredictions");
	if (!qfPredictionsElement) {
		alert('1/4 Finale: Je hebt dubbele landen ingevuld.\nJe voorspelling is NIET opgeslagen.\nPas je voorspelling aan en sla deze nogmaals op.');
		return;
	}
	rootElement.appendChild(qfPredictionsElement);

	// Get the Halffinal games
    var hfPredictionTable = document.getElementById("hfPredictions");
	hfPredictionsElement = getPredictionsElement(hfPredictionTable, "hfPredictions");
	if (!hfPredictionsElement) {
		alert('1/2 finale: Je hebt dubbele landen ingevuld.\nJe voorspelling is NIET opgeslagen.\nPas je voorspelling aan en sla deze nogmaals op.');
		return;
	}
	rootElement.appendChild(hfPredictionsElement);

	// Get the FINAL 
    var fPredictionTable = document.getElementById("fPredictions");
	fPredictionsElement = getPredictionsElement(fPredictionTable, "fPredictions");
	if (!fPredictionsElement) {
		alert('Finale: Je hebt dubbele landen ingevuld.\nJe voorspelling is NIET opgeslagen.\nPas je voorspelling aan en sla deze nogmaals op.');
		return;
	}
	rootElement.appendChild(fPredictionsElement);

	// Get the WINNER 
    var wPredictionTable = document.getElementById("wPredictions");
	wPredictionsElement = getPredictionsElement(wPredictionTable, "wPredictions");
	rootElement.appendChild(wPredictionsElement);
	
	//Get the EXTRA questions
	var extraElement = domDoc.createElement("extra");
	var goalsElement = domDoc.createElement("goals");
	goalsElement.setAttribute("id", document.getElementById("goals_prediction").value);
	goalsElement.setAttribute("val", document.getElementById("goals").value);
	extraElement.appendChild(goalsElement);
	var yellowElement = domDoc.createElement("yellow");
	yellowElement.setAttribute("id", document.getElementById("yellow_prediction").value);
	yellowElement.setAttribute("val", document.getElementById("yellow").value);
	extraElement.appendChild(yellowElement);
	var redElement = domDoc.createElement("red");
	redElement.setAttribute("id", document.getElementById("red_prediction").value);
	redElement.setAttribute("val", document.getElementById("red").value);
	extraElement.appendChild(redElement);
	rootElement.appendChild(extraElement);
	
	
	//Send the data to the server, which will store the object(s)
	var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");

	if ((window.ActiveXObject)) { //IE
		xmlhttp.open("POST", "classes/ajax/storage.php?action=storePredictions", false);
		xmlhttp.send(domDoc);
	} else {
		var serializer = new XMLSerializer();
		var xmlToSent = serializer.serializeToString(domDoc);

		xmlhttp.open("POST", "classes/ajax/storage.php?action=storePredictions&xml="+xmlToSent, false);
		xmlhttp.send(null);
	}

	if (xmlhttp.status == 200) {
		var message = "De scores zijn aangepast.";
		if (emptyScore) {
			message += "\n- LET OP! Je hebt nog niet alle wedstrijden voorspelt, controleer je voorspellingen goed. Voor niet voorspelde wedstrijden worden geen punten gegeven.";
		}
		if (showMessage) alert(message);
	} else if (xmlhttp.status == 500) {
		alert(xmlhttp.responseText);
	}
}

function getPredictionsElement(predictionTable, elementName) {
	var ids = new Array();

	var debug = "";
	var predictionRows = predictionTable.rows;
	var predictionsElement = domDoc.createElement(elementName);
	for (var i=0; i<predictionRows.length; i++) {
		var id = predictionRows[i].id;
		var countryNumber = id.substr(0, id.lastIndexOf("_"));
		var predictionId = id.substr(id.lastIndexOf("_")+1);
		debug += id + " - " + countryNumber + " - " + predictionId + "\n";
		try {
			var prediction = -1;
			var countryId = document.getElementById(countryNumber+"Country_"+predictionId).value;
			if (!ids.indexOf) {
				for (var index=0; index<ids.length; index++) {
					if (countryId!='' && ids[index] == countryId) return false;
				}
			} else if (countryId!='' && ids.indexOf(countryId)>0) return false;
			if (countryId=='') emptyScore = true;
			ids[i] = countryId;

			var predictionElement = domDoc.createElement("prediction");
			predictionElement.setAttribute("id", predictionId);
			predictionElement.setAttribute("countryId", countryId);
			predictionElement.setAttribute("countryNumber", countryNumber);

			predictionsElement.appendChild(predictionElement);
		} catch (e) {
		}
	}
	
	return predictionsElement;
}

function storeAccount(createNew, id) {
	var firstName = document.getElementById('firstName');
	var infix = document.getElementById('infix');
	var lastName = document.getElementById('lastName');
	var nickName = document.getElementById('nickName');
	var email = document.getElementById('email');
	var password = document.getElementById('password');
	var rePassword = document.getElementById('rePassword');
	var mobile = document.getElementById('mobile');
	var bankAccount = document.getElementById('bankAccount');

	//Data verified
	if (document.implementation.createDocument) {
		domDoc = document.implementation.createDocument("","account", null);
		rootElement = domDoc.documentElement;
	} else if ((window.ActiveXObject)) {
		domDoc = new ActiveXObject("Microsoft.XMLDOM");
		rootElement = domDoc.createElement("account");
		domDoc.appendChild(rootElement);
	}
	rootElement.setAttribute('id', id);

	var firstNameElement = domDoc.createElement('firstName');
	firstNameElement.setAttribute('required', 'true');
	firstNameElement.appendChild(domDoc.createTextNode(firstName.value));
	rootElement.appendChild(firstNameElement);
	var lastNameElement = domDoc.createElement('lastName');
	lastNameElement.setAttribute('required', 'true');
	lastNameElement.appendChild(domDoc.createTextNode(lastName.value));
	rootElement.appendChild(lastNameElement);
	var infixElement = domDoc.createElement('infix');
	infixElement.setAttribute('required', 'false');
	infixElement.appendChild(domDoc.createTextNode(infix.value));
	rootElement.appendChild(infixElement);
	var nickNameElement = domDoc.createElement('nickName');
	nickNameElement.setAttribute('required', 'false');
	nickNameElement.appendChild(domDoc.createTextNode(nickName.value));
	rootElement.appendChild(nickNameElement);
	var emailElement = domDoc.createElement('email');
	emailElement.setAttribute('required', 'true');
	emailElement.appendChild(domDoc.createTextNode(email.value));
	rootElement.appendChild(emailElement);
	var passwordElement = domDoc.createElement('password');
	passwordElement.setAttribute('required', 'true');
	passwordElement.appendChild(domDoc.createTextNode(password.value));
	rootElement.appendChild(passwordElement);
	var rePasswordElement = domDoc.createElement('rePassword');
	rePasswordElement.setAttribute('required', 'false');
	rePasswordElement.appendChild(domDoc.createTextNode(rePassword.value));
	rootElement.appendChild(rePasswordElement);
	var mobileElement = domDoc.createElement('mobile');
	mobileElement.setAttribute('required', 'false');
	mobileElement.appendChild(domDoc.createTextNode(mobile.value));
	rootElement.appendChild(mobileElement);
	var bankAccountElement = domDoc.createElement('bankAccount');
	bankAccountElement.setAttribute('required', 'false');
	bankAccountElement.appendChild(domDoc.createTextNode(bankAccount.value));
	rootElement.appendChild(bankAccountElement);

	var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	if ((window.ActiveXObject)) { //IE
		xmlhttp.open("POST", "classes/ajax/storage.php?action=storeAccount", false);
		xmlhttp.send(domDoc);
	} else {
		var serializer = new XMLSerializer();
		var xmlToSent = serializer.serializeToString(domDoc);
		xmlhttp.open("POST", "classes/ajax/storage.php?action=storeAccount&xml="+xmlToSent, false);
		xmlhttp.send(null);
	}

	if (xmlhttp.status == 200) {
		var text = xmlhttp.responseText;
		if (text.indexOf('notstored:')>-1) {
			alert(text.substring(10));
		}  else {
			alert(text);
			window.location="?module=showLogin";
		}
	}
}

function createElement() {
	
}

function printXML(element, toPrint, level) {
	//Start element
	for (var i=0; i<level; i++) {
		toPrint+="\t";
	}
	toPrint += "<"+element.tagName;
	if (element.attributes) {
		for (var i=0; i<element.attributes.length; i++) {
			toPrint+=" " + element.attributes.item(i).nodeName + "=" + element.attributes.item(i).nodeValue;
		}
	}
	toPrint+=">\n";
	level++;

	var nodes = element.childNodes;
	for (var i=0; i<nodes.length; i++) {
		nextElement = nodes[i];
		toPrint = printXML(nextElement, toPrint, level);
	}

	//Close element
	for (var i=0; i<(level-1); i++) {
		toPrint+="\t";
	}
	toPrint += "</"+element.tagName+">\n";

	return toPrint;
}

	
var domDoc = null;

