function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

 
// PARTITESTEN-SPESIFIC 

function candidateChangeAll() { 

	data = xmlHttp.responseText.split("~");

	if (xmlHttp.readyState == 4) {
	 fillInCandidate(true,data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10],data[11]);
	}
}


function candidateChange() { 

	data = xmlHttp.responseText.split("~");

	if (xmlHttp.readyState == 4) {
	 fillInCandidate(false,data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],"","","","");
	}
}

function fillInCandidate(all,name,slogan,logo,program,url,about,leaderName,leaderImg,intro,outro,feed,color){

	if (all) {

	 document.getElementById("slagord").innerHTML 	 = slogan;
	 document.getElementById("navn").innerHTML 	 = "<span style=\"color:"+color+";\">"+name+"</span>";
	 document.getElementById("om").innerHTML 	 = "<strong style=\"color:"+color+";\">Identitet</strong><br />"+about;
	 document.getElementById("feed").innerHTML 	 = feed;
	 document.getElementById("intro").innerHTML 	 = "<strong style=\"color:"+color+";\">Historie</strong><br />"+intro;
	 document.getElementById("outro").innerHTML 	 = "<strong style=\"color:"+color+";\">Hovedsaker</strong><br />"+outro;
	 document.getElementById("kilde").innerHTML	 = "<em>Tekst og foto er helt eller delvis hentet fra "+url+".</em>";

	} else {

	 document.getElementById("slagord").innerHTML 	 = slogan;
	 document.getElementById("navn").innerHTML 	 = name;
	 document.getElementById("om").innerHTML 	 = about;
	 document.getElementById("logo").src 		 = "bilder/logoer/" + logo;
	 document.getElementById("program").href 	 = program;
	 document.getElementById("url").href	 	 = url;
	 document.getElementById("leaderName").innerHTML = leaderName;
	 document.getElementById("leaderImg").alt 	 = leaderName;
	 document.getElementById("leaderImg").src	 = "bilder/ledere/" + leaderImg;

	 var l = logo.split(".");
	 document.getElementById("readmore").href	 = l[0];

	}
}

function showCandidate(id,showall) {

	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	} 

	var url="scripts/getCandidate.php?id="+id+"&sid="+Math.random();

	if (showall) {
			xmlHttp.onreadystatechange=candidateChangeAll;
	} else {
			xmlHttp.onreadystatechange=candidateChange;
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function showCandidateHtml(id) {

	var html  = '<h1 id="navn" style="text-transform:uppercase;"></h1>';
	    html += '<p class="info"><strong id="slagord"></strong></p>';
	    html += '<span class="info" id="feed"></span>';
	    html += "<div style=\"width:440px;\">";
	    html += '<p class="info" id="intro"></p>';
	    html += '<p class="info" id="om"></p>';
	    html += '<p class="info" id="outro"></p>';
	    html += '<p class="info" id="kilde" style="font-size:10px;"></p></div>';

	document.getElementById("content").innerHTML = html;
	showCandidate(id,true);
}

function opinion(id,opinion) {

	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	} 

	var url="scripts/opinion.php?id="+id+"&o="+opinion+"&sid="+Math.random();
	xmlHttp.onreadystatechange=displaySatisfaction;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function displaySatisfaction() {

	if (xmlHttp.readyState == 4) {

	data = xmlHttp.responseText.split("~");
	stats = document.getElementById("stat_"+data[0]).src;

	newImg = "graphs/satisfaction.php?a="+data[1]+"&g="+data[2];
	document.getElementById("stat_"+data[0]).src = newImg;

	total = Math.round(data[1]) + Math.round(data[2]);

	button1 = document.getElementById("p1_"+data[0]);
	value = button1.innerHTML;
	satisfied = "Fornøyd: " + Math.round(data[1]*100/total) + " %";
	button1.innerHTML = satisfied;

	button2 = document.getElementById("p2_"+data[0]);
	goal = button2.innerHTML;
	unsatisfied = "Misfornøyd: " + Math.round(data[2]*100/total) + " %";
	button2.innerHTML = unsatisfied;

	var link1 = "<div onclick=\"showOpinions("+data[0]+",'"+newImg+"','"+satisfied+"','"+unsatisfied+"');\">Vis meninger</div>";
	document.getElementById("b1_"+data[0]).innerHTML = link1;

	var link2 = "<div onclick=\"showGraph("+data[0]+",'"+stats+"','"+value+"','"+goal+"');\">Vis graf</div>";
	document.getElementById("b0_"+data[0]).innerHTML = link2;

	document.getElementById("b1_"+data[0]).style.color = "#d9cea6";
	}
}

function DS() {

	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	} 

	var url="scripts/destroySession.php?&sid="+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function showGraph(id,src,value,goal) {
	document.getElementById("stat_"+id).src = src;
	document.getElementById("p1_"+id).innerHTML = value;
	document.getElementById("p2_"+id).innerHTML = goal;
	document.getElementById("b0_"+id).style.color = "#d9cea6";
	document.getElementById("b1_"+id).style.color = "#f2e7c2";
}

function showOpinions(id,src,satisfied,unsatisfied) {
	document.getElementById("stat_"+id).src = src;
	document.getElementById("p1_"+id).innerHTML = satisfied;
	document.getElementById("p2_"+id).innerHTML = unsatisfied;
	document.getElementById("b1_"+id).style.color = "#d9cea6";
	document.getElementById("b0_"+id).style.color = "#f2e7c2";
}


