/*
 * 
 * Copyright © 2006 Ole Markus With
 * 
 */


box = document.getElementById("poll");

req = new XMLHttpRequest();
req.open("GET", "poll/data.php?mode=info", false);
req.send(null);

var resp = req.responseXML;
var div;
if (resp && typeof resp.getElementsByTagNameNS !=
'undefined' && (div =
resp.getElementsByTagNameNS('http://www.w3.org/1999/xhtml',
'div')[0])) {
	var clonedDiv = document.importNode(div, true);
	document.getElementById("pollContent").appendChild(clonedDiv);
}

var expectedHash = "";

window.setInterval("checkHash()", 1000);

checkHash();

function checkHash() {
	hash = getHash();
	if (expectedHash != hash) {
		// Do some parsing of the hash to set up the poll
//		alert("OMG");
		i = 1;
		printError("Got: " +  hash + ". Expected: " + expectedHash );
		
		// iterate through all members of the pollGiven class
		

		numPoints = 0;
	
		
		given = document.getElementsByTagName("td");
		for (i = 0; i < given.length; i++) {
			if (given[i].className == "pollGiven") {
				given[i].firstChild.nodeValue=0;
				numPoints++;
			} else {
					if (given[i].className.length > 0) {
						printError("class: " + given[i].className);
						
					}
				}
		}
	

		
		t = "Useful info: \n";
		i = 1;
		numGiven = 0;
		while(i < hash.length) {
			numGiven++;
			part = hash.substring(i, i+ 6);
			aid = part.substring(0,3) *1;
			points = part.substring(3, 5) * 1;
			t = "got point " + points;
			i = i + 5;
			pgt = document.getElementById("pollGiven" + aid);
			if (pgt == null) printError("No element by id " + aid);
			else {
				pgt.firstChild.nodeValue= points;
				//pgt.style.textDecoration = "line-through";
			}
		}
		printError("Found " + numPoints + " rows to give points to and  and " + numGiven + " points given ");
		document.getElementById("pollPoints").firstChild.nodeValue = numPoints - numGiven;
		
		//alert(t);
		expectedHash = hash;
	}
}


//to remove the # since Konqueror feels so bad about it
function getHash() {
	hash = window.location.hash.substring(1);
	if (hash.length == 0 || hash.substring(0,4) == "poll")
		return hash.substring(4);
	else {
		printError("Another program got hash: " + hash + " with id " + hash.substring(0,4));
		clearField(true);
		return false;
	}
}

function pollAnswer(aid) {
	//alert("you clicked on id " + aid);
	hash = getHash();
	printError("has at start: " + hash);
	if (hash.length == 0) hash = "1";
	
	pointTag = document.getElementById("pollPoints");
	points = pointTag.firstChild.nodeValue;
	
	pgTag = document.getElementById("pollGiven" + aid);
	if (pgTag.firstChild.nodeValue == 0) {
		pgTag.firstChild.nodeValue= points;
		while (points.length != 2) {
			points = '0' + points;
		}
		while (aid.length != 3) {
			aid = '0' + aid;
		}
		hash = hash + aid + points;
		recordHash(hash);
		pointTag.firstChild.nodeValue = points - 1;

			
		
		
	}
	
	
	
	return false; // prevent redirect
	
}



function recordHash(hash) {
	printError("Set hash: " + hash);
	expectedHash = hash;
	window.location.hash = "poll" + hash;
}


function pollSubmit() {
	if (hash.length>0) {
		clearField(false);
		y = new XMLHttpRequest();
		hash = getHash();
		printError("Sending hash: " + hash);
		y.open("GET", "poll/data.php?mode=result&hash=" + getHash(), false);
		y.send(null);
		var resp = y.responseXML;
		if (resp && typeof resp.getElementsByTagNameNS !=
		'undefined' && (div =
		resp.getElementsByTagNameNS('http://www.w3.org/1999/xhtml',
		'div')[0])) {
			var clonedDiv = document.importNode(div, true);
			pc = document.getElementById("pollContent");
			pc.appendChild(clonedDiv);
		}
	}
	
	return false;
	
}

function clearField(clearAll) {
	if (clearAll) {
		h = document.getElementById("pollContent");
		if (h) 	document.getElementById("poll").removeChild(h);
	} else {
		h = document.getElementById("fromPoll");
		if (h) 	document.getElementById("pollContent").removeChild(h);
	}
	

}

function printError(error) {
	if (typeof(opera) != "undefined")	{
		opera.postError(error);
	}
}

function getResult() {

	clearField();
	
	y = new XMLHttpRequest();
	y.open("GET", "poll/data.php?mode=result", false);
	y.send(null);
	var resp = y.responseXML;	
	if (resp && typeof resp.getElementsByTagNameNS !=
	'undefined' && (div =
	resp.getElementsByTagNameNS('http://www.w3.org/1999/xhtml',
	'div')[0])) {
		var clonedDiv = document.importNode(div, true);
		document.getElementById("pollContent").appendChild(clonedDiv);
	}
	return false;

}
