// JavaScript Document
var xmlhttp=false; 
var iRefreshCount=0;
var bCallInProgress=false;
/*@cc_on @*/ 
/*@if (@_jscript_version >= 5) 
// JScript gives us Conditional compilation, we can cope with old IE versions. 
// and security blocked creation of the objects. 
  try { 
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (e) { 
   try { 
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
   } catch (E) { 
    xmlhttp = false; 
   } 
  } 
@end @*/ 
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{ 
	xmlhttp = new XMLHttpRequest(); 
} 
/*
function loadFragmentInToElement(fragment_url, element_id)
{
	var element = document.getElementById(element_id); 
	element.innerHTML = 'Loading ...'; 
	xmlhttp.open("GET", fragment_url); //true/false==asynchronous/s, GET for (primarly) retrievel, POST for primarly sending to server
	xmlhttp.onreadystatechange = function()//Event handler for an event that fires at every state change
	{ 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{//4==finished downloading; 200==file found and loaded, 404 file not found
    		element.innerHTML = xmlhttp.responseText; 
		}
	}
    xmlhttp.send(null);//execute xmlhttp request
}
*/
function Call_ChangeEl(sId, sAction, sValue)
{
	var sUrl;
	if (bCallInProgress == false)
	{
		bCallInProgress=true;
		iRefreshCount++;
		sUrl='http://www.tv-guide.se/tvse_server.php?action='+sAction+'&value='+sValue+'&c='+iRefreshCount;
		xmlhttp.open("GET", sUrl); //true/false==asynchronous/s, GET for (primarly) retrievel, POST for primarly sending to server
		xmlhttp.onreadystatechange = function()//Event handler for an event that fires at every state change
		{ 
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{//4==finished downloading; 200==file found and loaded, 404 file not found
			//responseText lägger till space+space+newline?, fast bara 2 till chars!
    			var s1=xmlhttp.responseText;
				document.getElementById(sId).innerHTML=s1;//alert(s1); 
				bCallInProgress=false;//lägg in en onError callinprogress=false;
			}
		}
		xmlhttp.send(null);//execute xmlhttp request
	}
	else if (bCallInProgress == true)
	{
		setTimeout("Call_ChangeEl('" + sId + "', '" + sAction + "', '" + sValue + "')", 1000);
	}
	
}
function Call_CreateDOM(sEl, sId, oParent, oBeforeMe, sClass, sInnerHTML)//action inbakat?
{//först skapa en class, sedan en div!innerHTML på class?
	var newEl=document.createElement(sEl);
	newEl.setAttribute("id", sId);
	newEl.className = sClass;
	if (oBeforeMe != "") oParent.insertBefore(newEl, oBeforeMe);
	else oParent.insertBefore(newEl);//==appendChild
	if (sInnerHTML.substr(0, 4) != "http") newEl.innerHTML=sInnerHTML;
	else
	{
		var sUrl;
		iRefreshCount++;
		sUrl=sInnerHTML+'&c='+iRefreshCount;
		xmlhttp.open("GET", sUrl); //true/false==asynchronous/s, GET for (primarly) retrievel, POST for primarly sending to server
		xmlhttp.onreadystatechange = function()//Event handler for an event that fires at every state change
		{ 
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{//4==finished downloading; 200==file found and loaded, 404 file not found
			//responseText lägger till space+space+newline?, fast bara 2 till chars!
    			newEl.innerHTML=xmlhttp.responseText;//bara newEl???
			}
		}
		xmlhttp.send(null);//execute xmlhttp request	
	}
}

function CheckLogin()
{
	var sUrl, sLoginName, sPassword, sRadio1, sRadio2;
	sLoginName=document.getElementById("LoginName").value;
	sPassword=document.getElementById("Password").value;
	sRadio1=document.getElementById("Radio1").checked;
	sRadio2=document.getElementById("Radio2").checked;
	
	iRefreshCount++;
	sUrl='http://www.tv-guide.se/tvse_server.php?loginname='+sLoginName+'&password='+sPassword+'&radio1='+sRadio1+'&radio2='+sRadio2+'&c='+iRefreshCount;//en för login en för pass!!
	xmlhttp.open("GET", sUrl); //true/false==asynchronous/s, GET for (primarly) retrievel, POST for primarly sending to server
	xmlhttp.onreadystatechange = function()//Event handler for an event that fires at every state change
	{ 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{//4==finished downloading; 200==file found and loaded, 404 file not found
		//responseText lägger till space+space+newline?, fast bara 2 till chars!
    		var s1=xmlhttp.responseText;
			if (s1.charAt(0) == '1') document.getElementById('divLogin').innerHTML="Välkommen "+sLoginName;
			else alert('Error! Kunde inte logga in!'); 
			//document.getElementById("divLogin").style.visibility="hidden";
		}
	}
	xmlhttp.send(null);//execute xmlhttp request
	//document.getElementById('divLogin').innerHTML=sRadio2;
}
function GetUser()
{
	var sUrl, sGetUser;
	sGetUser="1";
	iRefreshCount++;
	sUrl='http://www.tv-guide.se/tvse_server.php?getuser='+sGetUser+'&c='+iRefreshCount;
	xmlhttp.open("GET", sUrl); //true/false==asynchronous/s, GET for (primarly) retrievel, POST for primarly sending to server
	xmlhttp.onreadystatechange = function()//Event handler for an event that fires at every state change
	{ 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{//4==finished downloading; 200==file found and loaded, 404 file not found
		//responseText lägger till space+space+newline?, fast bara 2 till chars!
    		var s1=xmlhttp.responseText;
			document.getElementById('divLogin').innerHTML=s1;
		}
	}
	xmlhttp.send(null);//execute xmlhttp request
}

var cookiestr="";//global!
function SaveCookies()
{
	
	var sSelectedIndex, sKanalNr;
	var dec, hex;
	var i, d, m;
	var convHexL=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
	var convKanal=new Array("255","0","1","2","3","4","5", "25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41", "50","51","52","53","54","55","56","57","58", "75","76", "100","101","102","103","104","105","106","107","108","109","110","111", "120","121","122","123","124","125", "140", "150","151", "160", "170", "200","201","202");
	
	cookiestr="";//reset
	
	for (i=0; i<35; i++)
	{
		sSelectedIndex=eval('document.Form'+i+'.Select'+i+'.selectedIndex');
		sKanalNr=convKanal[sSelectedIndex];
		dec=parseInt(sKanalNr);
		m=dec%16;//modulus
		d=parseInt(dec/16);//div
		hex=convHexL[d]+convHexL[m];
		cookiestr=cookiestr+hex;
	}
	
	Call_ChangeEl("pMsg", "ACTION_SaveCookies", cookiestr);//sSelectedIndex+"-"+sKanalNr+"-"+dec+"-"+d);
}
function ResetCookies()
{
	Call_ChangeEl("divMsg", "ACTION_SaveCookies", "reset");
}
function ExportCookie()
{
	window.location = "http://www.tv-guide.nu/setup.php?co="+cookiestr;
}
