function urlencode(str)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = str;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}


function print_r(theObj,d){
var ret;
  if(theObj.constructor == Array ||
     theObj.constructor == Object){

    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
	var d = unixtimetodate(p);
        print_r(theObj[p],d);
      } else {
document.getElementById('ch').innerHTML = document.getElementById('ch').innerHTML + "<span class=\"chat_"+p+"\"><span class=\"top\"></span><span class=\"middle\">"+d+"<br>"+theObj[p]+"</span><span class=\"bottom_"+p+"\"></span></span>";
      }
    }
  }
}

	function unixtimetodate(str) {
		var theDate = new Date(str * 1000);
		dateString = theDate.toLocaleTimeString();
		return dateString;
	}

function getmsg(strURL) {
    var e = "e";
    var v = "v";
    var sending = 'getmsg=y';
    var xmlHttpReq = false;
    var self = this;
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.setRequestHeader("Content-length", sending.length);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
	  if (self.xmlHttpReq.responseText != "") {
	  var resp = eval('(' + self.xmlHttpReq.responseText + ')');
		if (self.document.location!='http://rait.no/rait/fjernhjelp/7') {
			alert("test");
      			top.location='http://rait.no/rait/fjernhjelp/7'; 
		} else {
	 		 if (resp) {
			document.getElementById('ch').innerHTML = '';
			print_r(resp,'');
			var n = scrollToBottom('ch');
			}
		  }
	} else {
	if (self.document.location=='http://rait.no/rait/fjernhjelp/7') {
	document.getElementById('ch').innerHTML = "<br>";
	var n = scrollToBottom('ch');
		}
	}
    }}
    self.xmlHttpReq.send(sending);
}

function checknew()
{
getmsg("/rait/chat.php");
t=setTimeout("checknew()",5000);
}

function xmlhttpPost(strURL,verdi,mot) {
    var key = verdi.name;
    var value = urlencode(verdi.value);
    var sending = 'ajax=yes&mottaker='+mot+'&'+key+'='+value;
    var xmlHttpReq = false;
    var self = this;
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.setRequestHeader("Content-length", sending.length);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
		var resp = eval('(' + self.xmlHttpReq.responseText + ')');
	  if (resp) {
		if (resp.resultat.lagre)
		  {
		 //document.getElementById('msg').innerHTML = resp.resultat.lagre;
		 document.getElementById('message').value="";
		getmsg("/rait/chat.php");
		  }
		if (resp.resultat.msg)
		  {
		 document.getElementById(resp.resultat.div).innerHTML = resp.resultat.msg;
		  }
		if (resp.resultat.error)
		 {
		 document.getElementById(resp.resultat.felt).style.border = "2px solid red";
		 load_msg(resp.resultat.error,2);
		 document.getElementById(resp.resultat.felt).select();
		 }
	  }
        }
    }
    self.xmlHttpReq.send(sending);
}