window.onload = function()
{
	votingMouseRollOver();
	votingInitForm();
}

var votingResult=false; // Popup-Fenster

// Mouseover für Bilder
function votingMouseRollOver()
{
	$$('.rollOver').each(function (i)
	{
		i.observe('mouseover', function (e)
		{
			index = i.src.lastIndexOf('.');
			if (index > -1) {i.src = i.src.substring(0, index)+'.mo'+i.src.substring(index);}
		})

		i.observe('mouseout', function (e)
		{
			i.src = i.src.replace('.mo.', '.');
		})
	});
}

// Formularkontrolle nach dem Laden
function votingInitForm() {
	$$('.votingForm').each(function (i)
	{
		var t = i.readAttribute('id');
		var id = t.substring(t.indexOf("_")+1);
		var c = votingGetCookie("votingID_" + id);
		if(c != null) { // Cookie gesetzt
			var obj = document.getElementById("votingForm_" + id);
			votingFormDisable( obj );
			obj.elements["answer"][0].checked=true; // Default-Eintrag, um die Abfrage zu umgehen
		}
	});
}

// Formularkontrolle vor dem Versenden
function votingCheckForm(obj) {
	var l = obj.elements["answer"].length;
	var index = -1;
	for(var i=0; i<l; i++) { if(obj.elements["answer"][i].checked) index=i; }
	if(index==-1) {
		if(votingResult) votingResult.close();
		alert("Bitte wählen Sie eine Antwort aus!");
		return false;
	}
	
	votingFormDisable( obj );
	
	var t = $(obj).readAttribute('id'); 
	var id = t.substring(t.indexOf("_")+1);
	votingSetCookie("votingID_" + id , id);
	
	return true;
}

// Abstimmen ausblenden, Ergebnis einblenden und Radios unsichtbar machen
function votingFormDisable(obj) {
	var l = obj.elements["answer"].length;
	// Problem: desable löscht das Feld -> die Antwort fehlt im post-Array
	//for(var i=0; i<l; i++) { obj.elements["answer"][i].disabled=true; }
	for(var i=0; i<l; i++) { obj.elements["answer"][i].style.visibility="hidden"; }
	
	$(obj).down(".votingButtonAbstimmen").hide();
	$(obj).down(".votingButtonErgebnis").setStyle({display:"block"});
}


// ++++++++++++++++++++++++++++++++ COOKIES ++++++++++++++++++++++++++++++++++++++++++
function votingSetCookie(name,val) {
	var date = new Date();
	date.setTime(date.getTime() + (300*24*60*60*1000)); // 300 Tage
	var exp = "; expires=" + date.toGMTString();
	document.cookie = name + "=" + val + exp + "; path=/";
}

function votingGetCookie(name) {
	var name2 = name + "=";
	var arr = document.cookie.split(';');
	for(var i=0;i < arr.length;i++) {
		var c = arr[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(name2) == 0) return c.substring(name2.length,c.length);
	}
	return null;
}
