// JavaScript Document
var chat = Array();
var pm=0;
function check_pm(myid){
  if(myid){
	if(!pm){
	pm=setInterval('load_pm(\''+myid+'\')',10000);
	}
  }
}

function load_pm(myid){
	var pms=Ajax_load_value('reader.php?id='+myid+'&action=pm');
	var pm=pms.split(",");
		if(pm[1]){
		creatediv(pm[0],pm[2],pm[1],pm[4],pm[3],'');
		}
	}

function pause_chat(winid){
	clearInterval(chat[winid]);
	chat[winid]=0;
}

function close_chat(id){
	document.getElementById("handle"+id).style.visibility="hidden";
	document.getElementById(id).style.width="1px";
	pause_chat(id);
	}	
function start_chat(winid){
	if(!chat[winid]){
	chat[winid]=setInterval('load_message(\''+winid+'\')',1000);
	}
}

function send(objid){
var message=document.getElementById('message'+objid)
/*   if(message.value)
	Ajax_load_page('writer.php?message='+message.value, 'writer');
*/
	message.value='';
}

var chat_window = Array();

function load_message(winid){
   var sbar=document.getElementById("chat_window"+winid)
   chat_window[winid]=document.getElementById("chat_window"+winid)

   message=Ajax_load_value('reader.php?id='+winid);
   if(!message){
   message='';
   }
   chat_window[winid].innerHTML=message;
   sbar.scrollTop=sbar.scrollTop+1000;
}


//	FUNCTION TO GET COOKIE
function Get_Cookie( name ) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

function creatediv(id,toid,chatid,touser,fromuser,e) {
if(!id){
	window.location="login.php";
	} else {	
	//	CHECK IF DIV EXIST
  if (document.getElementById("handle"+chatid) != null)
  {
	  document.getElementById(chatid).style.width="300px";
	  document.getElementById("handle"+chatid).style.visibility="visible";
	  start_chat(chatid);
	  } else {
   var newdiv = document.createElement('div');
   newdiv.setAttribute('id', chatid);
  

if (e){
  if (IE) { // grab the x-y pos.s if browser is IE
    tempY = event.clientY+document.body.scrollTop;
	tempX = event.clientX+document.body.scrollLeft;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }
  tempX=tempX - 300;
}

if(!tempX&&!tempY){
	  tempX =Math.floor(Math.random()*500);
	  tempY =Math.floor(Math.random()*300);
	  }
 
       newdiv.style.width = 300+"px";
       newdiv.style.height = 300+"px";
       newdiv.style.position = "absolute";
       newdiv.style.left = tempX;
       newdiv.style.top = tempY;
   /*
   newdiv.style.background = "#00C";*/
   newdiv.style.color = "#000000";
   

   //newdiv.innerHTML = '<form action="javascript:get(document.getElementById(\'chatform'+chatid+'\'),\''+chatid+'\');" name="chatform'+chatid+'" id="chatform'+chatid+'"><div style="width:300px; z-index:1; position:absolute;font-family:Arial, Helvetica, sans-serif;" id="handle'+chatid+'"><div style="float:right;width:25px;cursor:pointer;" align="center" onclick="close_chat(\''+chatid+'\')">X</div><div class="bar" style="width:270px;" onmousedown="dragStart(event, \'handle'+chatid+'\')">&nbsp;&nbsp;Instant Message - '+touser+'</div><div  id="chat_window'+chatid+'" style="width:300px; height:200px; background-color:#CCCCCC; border:1px;word-wrap: break-word;padding:5px; overflow-y:scroll; z-index:100;" onMouseDown="pause_chat(\''+chatid+'\')" onMouseUp="start_chat(\''+chatid+'\');" onMouseOut="start_chat(\''+chatid+'\');"></div><div style="width:300px; height:20px; z-index:100;"><input name="message'+chatid+'" type="text" id="message'+chatid+'" style="width:80%;" onclick="start_chat(\''+chatid+'\');"><input name="toid'+chatid+'" type="hidden" id="toid'+chatid+'" value="'+toid+'"><input name="touser'+chatid+'" type="hidden" id="touser'+chatid+'" value="'+touser+'"><input name="fromuser'+chatid+'" type="hidden" id="fromuser'+chatid+'" value="'+fromuser+'"><input name="fromid'+chatid+'" type="hidden" id="fromid'+chatid+'" value="'+id+'"><input name="chatid'+chatid+'" type="hidden" id="chatid'+chatid+'" value="'+chatid+'"><input type="button" name="button" value="Send" style="width:20%;"onclick="javascript:get(this.parentNode,\''+chatid+'\');start_chat(\''+chatid+'\');"></div></div></form>';
   newdiv.innerHTML='<form action="javascript:get(document.getElementById(\'chatform'+chatid+'\'),\''+chatid+'\');" name="chatform'+chatid+'" id="chatform'+chatid+'"><div class="Table_01" style="z-index:1; position:absolute;" id="handle'+chatid+'"><div class="frametop1"></div><div class="top2" onMouseDown="dragStart(event, \'handle'+chatid+'\')"><a href="http://www.yehey.com/tol/" target="_blank"><img src="images/TOLicon.gif" align="absmiddle" border="0"><a/> IM - '+touser+'</div><div class="closebutton" style="cursor:pointer;" onClick="close_chat(\''+chatid+'\')"><img src="images/closebutton.gif"></div><div class="frametop3"></div><div class="left"></div><div  class="pinoyhenyo2-05" id="chat_window'+chatid+'" style="overflow-y:scroll; z-index:100;" onMouseDown="pause_chat(\''+chatid+'\')" onMouseUp="start_chat(\''+chatid+'\');" onMouseOut="start_chat(\''+chatid+'\');"></div><div class="right"></div><div class="framebottom1"></div><div style="z-index:100;" class="inputbox"><input name="message'+chatid+'" type="text" id="message'+chatid+'" style="width:80%;height:23px;border="0"" onClick="start_chat(\''+chatid+'\');"><input name="toid'+chatid+'" type="hidden" id="toid'+chatid+'" value="'+toid+'"><input name="touser'+chatid+'" type="hidden" id="touser'+chatid+'" value="'+touser+'"><input name="fromuser'+chatid+'" type="hidden" id="fromuser'+chatid+'" value="'+fromuser+'"><input name="fromid'+chatid+'" type="hidden" id="fromid'+chatid+'" value="'+id+'"><input name="chatid'+chatid+'" type="hidden" id="chatid'+chatid+'" value="'+chatid+'"><input type="button" name="button" value="Send" style="width:20%; height:24px;color:#ef5701;"onclick="javascript:get(this.parentNode,\''+chatid+'\');start_chat(\''+chatid+'\');"></div><div class="bottom2" ></div><div class="framebottom3"></div></div></form>';

   start_chat(chatid);
   document.body.appendChild(newdiv);
  }
}
} 
