var http = createRequestObject();

function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
  catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
    catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
  	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}

function history(load_roomId,load){

	var r = Math.round((99977562 - 10016486) * Math.random() + 1) + 22423;

    http.open("GET","./history.php?roomId="+load_roomId+"&now="+r,true);
    http.onreadystatechange = function(){

  	if((http.readyState == 4)&&(http.status == 200)){
  		var response = http.responseText;
  		
 			var results = response.split("*");

 			if(load_roomId != null){
 				document.getElementById("room_"+load_roomId).innerHTML = "";
 			}
		
			
			for (i=0; i < results.length; i++) {
 
  			 	var fields = results[i].split('|');
  			 	
 			 	var roomId = parseInt(fields[0]);
 			 	var writer = fields[1];
 			 	var message = fields[2];
			 	var time = fields[3];
				
				// last line without border
   				if(i < 1){
					lastline = "style=\"border-top:0px;\"";
				}
				else{
					lastline = "";
				}
				
					if(document.getElementById("room_"+roomId) != null){
					
						document.getElementById("typemess_"+roomId).style.display = "none";
						document.getElementById("type_"+roomId).src = "chat/onlineuser.gif";
						//document.getElementById("type_"+roomId).style.display = "none";
						
					if(time != "0" && writer != "0" && time != "01" && writer != "01"){
						document.getElementById("room_"+roomId).innerHTML += "\
						<div class=\"message_username\" "+lastline+">\
						<div class=\"message_title\" style=\"margin:0px;\">"+writer+"</div>\
						<div class=\"message_title_time\" style=\"margin:0px;\">"+time+"</div>\
						<div class=\"text_normal\" style=\"float:right;margin:0px;line-height:110%;\">"+message+"</div></div>";
					}
					else if(time == "01" && writer == "01"){
						document.getElementById("type_"+roomId).src = "chat/offlineuser.gif";
						//document.getElementById("type_"+roomId).style.display = "";
						document.getElementById("room_"+roomId).innerHTML += "\
						<div style=\"width:95%;float:right;color:#3B5998;border-top:0px;margin:3px 5px 2px 0px;\">\
						<div class=\"text_normal\" style=\"text-align:center;height:20px;width:150px;color:#3B5998;font-weight:bold;\">"+message+"</div></div>";
						
					}
					else{
						document.getElementById("room_"+roomId).innerHTML += "\
						<div style=\"width:87%;float:right;color:#3B5998;border-top:0px;margin:3px 5px 2px 0px;\">\
						<div class=\"text_normal\" style=\"float:right;margin:0px;line-height:110%;\">"+message+"</div></div>";
					}	
   						if(i > (results.length-3)){
						document.getElementById("room_"+roomId).scrollTop = 5500;
						}
					}
			 }
			 
			/////////
    	}
    }
	http.send(null);
	pause(400);
	
}

var Sound = new Object();
Sound.play = function Sound_play(src) {
if (!src) return false;
this.stop();
var elm;
if (typeof document.all != "undefined") {
elm = document.createElement("bgsound");
elm.src = src;
}
else {
elm = document.createElement("object");
elm.setAttribute("data",src);
elm.setAttribute("type","audio/x-wav");
elm.setAttribute("controller","true");
}
document.body.appendChild(elm);
this.elm = elm;
return true;
};

Sound.stop = function Sound_stop() {
if (this.elm) {
this.elm.parentNode.removeChild(this.elm);
this.elm = null;
}
};




function pause(numberMillis)
{
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true)
{
now = new Date();
if (now.getTime() > exitTime)
return;
}
} 
