

 var gIMUser
 function ShowIMUserName()
 {
    var askIMUserNameHTML = "<div id='divIMUser'><br><br><center><table width=100% border=0>"
    askIMUserNameHTML += "<tr><td align=right>Your name:</td><td align=left><input type=text style='border:1px solid gray;' onkeyup='StartByKey(event);' id='txtIMUser' size=30 maxlength=20></td></tr>"
    askIMUserNameHTML += "<tr><td align=right>&nbsp;</td><td align=left><input id=btnStartIM type=button onclick='StartIM()' class=button value='Start IM'><input type=text style='visibility:hidden'></td></tr></table></center></div>"
    if (document.getElementById('divIMWnd'))
        document.getElementById('divIMWnd').innerHTML = askIMUserNameHTML
    if (document.getElementById('btnStartIM'))
        document.getElementById('btnStartIM').disabled = true
 }
 
 function StartByKey(event)
 {
   event.returnValue = false;
   event.cancel = true;
   if (document.getElementById('txtIMUser'))
    {
        if (document.getElementById('txtIMUser').value == "")
        {
            document.getElementById('btnStartIM').disabled = true
            return
        }
        else
             document.getElementById('btnStartIM').disabled = false
    }
   try{
    if (event.which == 13) //VK_ENTER
         StartIM();
   }catch(e){}
   try{
    if (event.keyCode == 13) //VK_ENTER
         StartIM();
   }catch(e){}
       

 }

 function ValidateUser()
 {
    if (document.getElementById('txtIMUser'))
    {
        var usr = document.getElementById('txtIMUser').value
	    usr = trimAll(usr,' ')
	    usr = trimAll(usr,'\n')
	    usr = trimAll(usr,'\r')
	    usr = trimAll(usr,'\t')
    	if (usr == "")
        {
            document.getElementById('txtIMUser').value = ""
	        document.getElementById('btnStartIM').disabled = true
            return false
        }
        else
            return true
    }
    else 
        return false
    return true
 }

 function StartIM()
 {
    if (ValidateUser() == false)
        return
        
    gIMUser = document.getElementById('txtIMUser').value
    var divIMHTML = "<div id='divIM'><center><table cellpadding=0 cellspacing=0 width=100% border=0>"
    divIMHTML += "<tr valign=top><td colspan=2 width=96%><div class=divIM id='divIMs' style='border:1px solid gray; height:300px; padding:3px;  overflow:auto'></div></td></tr>"
    divIMHTML += "<tr valign=middle><td align=left><textarea style='border:1px solid gray; width: 370px; height: 42px;' onkeyup='SendByKey(event);' id='txtIMMsg'></textarea></td>"
    divIMHTML += "<td align=left><input id=btnSendIM type=button onclick='SendIM()' style='cursor:pointer;border:1px solid gray; height: 42px;' class=button value='Send'></td></tr></table></center></div>"
    if (document.getElementById('divIMWnd'))
	    document.getElementById('divIMWnd').innerHTML = divIMHTML
    if(document.getElementById('btnSendIM'))
        document.getElementById('btnSendIM').disabled = true
    setInterval('GetIMs()',1500)
    ResizeIMDiv()
 }
//    
 var reqIM
 function SendIM()
 {
    if (ValidateMsg() == false)
        return

	var url = "im.aspx?cmd=AddIM&arg1=" + _id + "&arg2=" + gIMUser + "&arg3=" + document.getElementById('txtIMMsg').value
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		reqIM = new XMLHttpRequest();
		reqIM.onreadystatechange = ShowIMs;
		reqIM.open("GET", url, true);
		reqIM.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		reqIM = new ActiveXObject("Microsoft.XMLHTTP");
		if (reqIM) {
			reqIM.onreadystatechange = ShowIMs;
		// document.getElementById('status').innerHTML = url
			reqIM.open("GET", url, true);
			reqIM.send();
		}
	}
    if (document.getElementById('txtIMMsg'))
        document.getElementById('txtIMMsg').value = ""
    if(document.getElementById('btnSendIM'))
        document.getElementById('btnSendIM').disabled = true
 }
 
 function ValidateMsg()
 {
    if (document.getElementById('txtIMMsg'))
    {
        var msg = document.getElementById('txtIMMsg').value
	    msg = trimAll(msg,' ')
	    msg = trimAll(msg,'\n')
	    msg = trimAll(msg,'\r')
	    msg = trimAll(msg,'\t')
    	if (msg == "")
        {
            document.getElementById('txtIMMsg').value = ""
	        document.getElementById('btnSendIM').disabled = true
            return false
        }
        else
            return true
    }
    else 
        return false
    return true
 }
 
 function SendByKey(event)
 {
    event.returnValue = false;
    event.cancel = true;
    var msg = document.getElementById('txtIMMsg').value
	
    if (msg == "")
        {
            document.getElementById('btnSendIM').disabled = true
            return
        }
        else
            document.getElementById('btnSendIM').disabled = false
   try{
    if (event.which == 13) //VK_ENTER
         SendIM()
   }catch(e){}
   try{
    if (event.keyCode == 13) //VK_ENTER
         SendIM()
   }catch(e){}
   
}


 function GetIMs()
 {
	var url = "im.aspx?cmd=GetIMs&arg1=" + _id 
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		reqIM = new XMLHttpRequest();
		reqIM.onreadystatechange = ShowIMs;
		reqIM.open("GET", url, true);
		reqIM.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		reqIM = new ActiveXObject("Microsoft.XMLHTTP");
		if (reqIM) {
			reqIM.onreadystatechange = ShowIMs;
		// document.getElementById('status').innerHTML = url
			reqIM.open("GET", url, true);
			reqIM.send();
		}
	}
 }
 
  function ShowIMs()
  {
	if (reqIM.readyState == 4) {
		// only if "OK"
		if (reqIM.status == 200) {
		    var strIMs = reqIM.responseText.replace("^|^", "<br>")+ "<br>"
	        var divIMs = document.getElementById('divIMs')
	        if (strIMs != "" && strIMs!="<br>")
	            divIMs.innerHTML = strIMs 
	        //document.getElementById('divIMs').s
	          try { divIMs.scrollTop = 99999; } catch(e) { } 
              try { divIMs.scrollY = 99999; } catch(e) { }
              try { divIMs.scrollTo(0,99999); } catch(e) { }

	    }
	}
 }
 function ResizeIMDiv()
 {
	var divIM = document.getElementById('divIM')
	if (divIM)
	{
	    divIM.style.height = _firefox ? _clientHeight-mpHeight-20 :_clientHeight-mpHeight-10
	    divIM.style.width = _firefox ? mpWidth-16 : mpWidth-10
	    document.getElementById('txtIMMsg').style.width = _firefox ? 340 : 376
    }
	var divIMsObj = document.getElementById('divIMs')
	if (divIMsObj)
	{
	    divIMsObj.style.height = _firefox ? _clientHeight-mpHeight-42 - 20  : _clientHeight-mpHeight-42 - 15 
	    divIMsObj.style.width = _firefox ? mpWidth-10 : mpWidth-2
	}
 }
 
 function trimAll(sString, whiteSpc) 
{
	while (sString.substring(0,1) == whiteSpc)
	{
		sString = sString.substring(1, sString.length)
	}
	while (sString.substring(sString.length-1, sString.length) == whiteSpc)
	{
		sString = sString.substring(0,sString.length-1)
	}
	return sString
}

