//ajax begin
var iRefreshCount=0;
var xmlhttp=false;
var bCallInProgress=false;
var timeStart=null;
//ajax end
/*@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 Call_ChangeEl(sId, sAction, sValue, sTimeProc)
{
	var sUrl;
	if (bCallInProgress == false)
	{
		timeStart = new Date();
		
		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;
				if (s1.charAt(0) == 'z')
				{
					setTimeout("Call_ChangeEl('" + sId + "', '" + sAction + "', '" + sValue + "')", 100);
					s1=s1.substring(1, s1.length);
				}
				document.getElementById(sId).innerHTML=s1;//alert(s1); 
				bCallInProgress=false;//lägg in en onError callinprogress=false;
				
				var timeNow = new Date();
				var timeDiff=timeNow.getTime()-timeStart.getTime();//timeNow.setTime(timeDiff);//InfoTime(timeDiff);
				eval(sTimeProc);
			}
		}
		xmlhttp.send(null);//execute xmlhttp request
	}
	else if (bCallInProgress == true)
	{
		setTimeout("Call_ChangeEl('" + sId + "', '" + sAction + "', '" + sValue + "')", 1000);
	}
}

function Call_PostMsg(sAction, sValue)
{
	//Fix CR/LF in forms
	var iii;
	for (iii=0; iii < sValue.length; iii++)
	{
		if (sValue.substr(iii, 1) == "&")
		{
			sValue=sValue.replace(/&/, "¤");//#1/2 &amp; should work
		}
		else if (sValue.charAt(iii) == '\r')
		{
			sValue=sValue.replace(/\r/, "~");//replace CR with tilde
			sValue=sValue.replace(/\n/, " ");//replace next LF with space
		}
		else if (sValue.charAt(iii) == '\n')
		{
			sValue=sValue.replace(/\n/, "~");//replace LF with tilde
		}
	}
	var sUrl;
	sUrl='http://www.tv-guide.se/tvse_server.php?action='+sAction+'&value='+sValue+'&c='+iRefreshCount;
	iRefreshCount++;
	xmlhttp.open("GET", sUrl);
	xmlhttp.onreadystatechange = function()
	{ 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
   			var s1=xmlhttp.responseText;
			//document.getElementById("textareaMsg").value=s1;
			document.getElementById("divMiddle").style.display="block";
			document.getElementById("divDlgNewThreadMsg").style.display="none";
			if (s1 == "0") window.location.reload();
			else window.location=s1;
			////eval(s1);
		}
	}
	xmlhttp.send(null);//execute xmlhttp request	
}

function Call_ChangeProperty(sId, sAction, sProperty, sValue1, sValue2)
{
	var sUrl;//sProperty, sValue1 and sValue2 is sent but not always processed!
	sUrl='http://www.tv-guide.se/tvse_server.php?action='+sAction+'&value='+sProperty+';'+sValue1+';'+sValue2+'&c='+iRefreshCount;
	iRefreshCount++;
	
	xmlhttp.open("GET", sUrl);
	xmlhttp.onreadystatechange = function()
	{ 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
   			var s1=xmlhttp.responseText;
			if (s1 == "1") eval('document.getElementById(sId).style.'+sProperty+'="'+sValue1+'"');
			else if (s1 == "0") eval('document.getElementById(sId).style.'+sProperty+'="'+sValue2+'"');
		}
	}
	xmlhttp.send(null);//execute xmlhttp request	
}

function getKeyNum(e)
{//if (getKeyNum(event) == 13)
	var keynum;
	if (window.event) // IE
	{
		keynum=e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum=e.which;
	}
	return keynum
}
