var imxmlHttp
var minstat=false;
function send_msg_to_user(str,fromuserid,touserid,flag)
{

	imxmlHttp=GetXmlHttpObject();
	if (imxmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="/im/imsendmessage.php";
	url=url+"?fromuserid="+fromuserid;
	url=url+"&touserid="+touserid;
	url=url+"&msg="+encodeURI(str);
	url=url+"&flag="+flag;
	imxmlHttp.onreadystatechange=stateChanged_send;
	imxmlHttp.open("POST",url,true);
	imxmlHttp.send(null);
} 

function receive_message_usr(fromuserid,flag)
{
	imxmlHttp=GetXmlHttpObject();
	if (imxmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="/im/pingmessage.php";
	url=url+"?fromuserid="+fromuserid;
	//url=url+"&touserid="+touserid;
	url=url+"&flag="+flag;
	imxmlHttp.onreadystatechange=stateChanged_rec;
	imxmlHttp.open("POST",url,true);
	imxmlHttp.send(null);
	
}

function stateChanged_send() 
{ 
	if (imxmlHttp.readyState==4)
	{ 	
		var oldval = document.getElementById("im_window_sub").innerHTML;
		document.getElementById("im_window_sub").innerHTML= oldval + imxmlHttp.responseText;
		document.getElementById("im_window_sub").scrollTop = document.getElementById("im_window_sub").scrollHeight;
	}
}


function stateChanged_rec() 
{ 
	
	if (imxmlHttp.readyState==4)
	{
		
		var msg = imxmlHttp.responseText;
		msg = msg.split(":im:");
		var imtouserid = msg[1];
		var imtousername = msg[2];
		msg = msg[0];
		if(msg)
		{	
			

			var oldval = document.getElementById("im_window_sub").innerHTML;
			document.getElementById("im_window_sub").innerHTML= oldval + msg;
			document.getElementById('im_window').style.visibility="visible";
			document.getElementById('im_window').style.position="absolute";
			document.getElementById('min_win').style.visibility="hidden";
			document.getElementById('min_win').style.position="absolute";
			document.getElementById("imtouserid").value = imtouserid;
			document.getElementById("imtousername").value = imtousername;
	        document.getElementById("im_window_sub").scrollTop = document.getElementById("im_window_sub").scrollHeight;
			document.getElementById('im_title').innerHTML="<font color=\"#FFFFFF\">Chat with "+ imtousername +"</font>";
		}
	}
}

function GetXmlHttpObject()
{
	
	try
  	{
  		// Firefox, Opera 8.0+, Safari
  		imxmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
  		try
    		{
    			imxmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    		}
  		catch (e)
    		{
    			imxmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
  	}
	return imxmlHttp;
}


function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("im_window_sub").innerHTML = document.getElementById("im_window_sub").innerHTML + "<br>" + str;
}

function send_message()
{
	var userid = document.getElementById('imuserid').value;
	var newmsg = document.getElementById('im_type_message').value;
	var oldmsg = document.getElementById('im_window_sub').innerHTML;
	var touserid = document.getElementById('imtouserid').value;
	//newmsg = escape(newmsg);
	send_msg_to_user(newmsg,userid,touserid,'put');

	//document.getElementById('im_window_sub').innerHTML = oldmsg + "<b>me: </b>" +  newmsg + "<br>";
	document.getElementById('im_type_message').value= "";
	
}

function open_im(touserid,toname)
{
	
	document.getElementById('im_window').style.visibility="visible";
	document.getElementById('im_window').style.position="absolute";
	document.getElementById('min_win').style.visibility="hidden";
	document.getElementById('min_win').style.position="absolute";
	document.getElementById('imtouserid').value = touserid;
	document.getElementById('imtousername').value = toname;
	document.getElementById('im_title').innerHTML="<font color=\"#FFFFFF\">Chat with "+ toname +"</font>";
	document.getElementById('minim_title').innerHTML="<font color=\"#FFFFFF\">Chat with "+ toname +"</font>";
}

function receive_message()
{
	var userid = document.getElementById('imuserid').value;
	var newmsg = document.getElementById('im_type_message').value;
	var oldmsg = document.getElementById('im_window_sub').innerHTML;
	var touserid = document.getElementById('imtouserid').value;
	//newmsg = escape(newmsg);
	receive_message_usr(userid,'get');
    
	//document.getElementById('im_window_sub').innerHTML = oldmsg + "<b>me: </b>" +  newmsg + "<br>";

	
}

function timingex( ){

		   /*if(document.getElementById('im_test')==null)
		   {
			   //addDiv('im_window');
		   }*/
		   if(document.all)
  				bodyHeight = document.body.offsetHeight;
  		   else
  				bodyHeight = window.innerHeight;
	
		   var bodyWidth = document.body.offsetWidth;
		   
		   document.getElementById('im_window').style.left = bodyWidth - (248+40);
		   document.getElementById('im_window').style.top = bodyHeight - (260);
		   document.getElementById('min_win').style.left = bodyWidth - (248+40);
		   document.getElementById('min_win').style.top = bodyHeight - 28;
		   
		   setInterval("receive_message()",3000);
}

function hide_div()
{
	document.getElementById('im_window').style.visibility="hidden";
	document.getElementById('im_window').style.position="absolute";
	document.getElementById('min_win').style.visibility="hidden";
	document.getElementById('min_win').style.position="absolute";
	document.getElementById('im_im2').style.display="none";
	document.getElementById('im_im1').style.display="none";
}



function minize()
{
	if(minstat)
	{
		document.getElementById('min_win').style.visibility='hidden';
		document.getElementById('min_win').style.position='absolute';
		//document.getElementById('min_win').style.display='none';
		document.getElementById('im_window').style.visibility='visible';
		document.getElementById('im_window').style.position='absolute';
       	//document.getElementById('im_im2').style.display="none";
	    //document.getElementById('im_im1').style.display="none";

		minstat=false;
	}
	else
	{
		document.getElementById('min_win').style.visibility='visible';
		document.getElementById('min_win').style.position='absolute';
		//document.getElementById('min_win').style.display='block';
		document.getElementById('im_window').style.visibility='hidden';
		document.getElementById('im_window').style.position='absolute';
		//document.getElementById('im_im2').style.display="block";
		//document.getElementById('im_im1').style.display="block";


		minstat=true;
	}
}

function capture_event(keycode)
{
	if(keycode=='13')
	{	
		send_message()
	}
	
}