var xmlHttp;
var refreshtimeout;
if (window.ActiveXObject){
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttp = new XMLHttpRequest();
}

function sendChat(){
    try{
        var message=document.getElementById("message").value;
        var name=document.getElementById("name").value;
        if( name=="Your Name" ){
            document.getElementById("status").innerHTML="Please type your name.";
            document.getElementById("name").focus();
            document.getElementById("name").select();
            return false;
        }
        document.getElementById("status").innerHTML="";
        if ( message == "" )
            return false;
        var id=document.getElementById("chatId").value;
        clearTimeout(refreshtimeout);
        if ( id == "" ){
            getID();
            return false;
        }else{
            xmlHttp.open("GET", "chatAction.jsp?action=receiveMessage&id="+id+"&message="+message+"&name="+name, true);
            xmlHttp.onreadystatechange = chatReturn;
            xmlHttp.send(null);
            document.getElementById("message").value = "";
            document.getElementById("message").focus();
        }
        return false;
    }catch( e ){
        document.getElementById("status").innerHTML=e;
    }
}

function getID(){
    var id=document.getElementById("chatId").value;
    var name=document.getElementById("name").value;
    if ( id == "" ){
        xmlHttp.open("GET", "chatAction.jsp?action=getId&name="+name, true);
        xmlHttp.onreadystatechange = receiveId;
        xmlHttp.send(null);
    }
}

function unregister(){
    alert( "Unregister" );
    var id=document.getElementById("chatId").value;
    if ( id != "" ){
        xmlHttp.open("GET", "chatAction.jsp?action=unregister&id="+id, true);
        xmlHttp.onreadystatechange = receiveId;
        xmlHttp.send(null);
    }
}

function chatReturn(){
    if (xmlHttp.readyState == 4) {
        text = xmlHttp.responseText;
        chatDiv = document.getElementById("chat");
        if ( text.length != 0 ){
            document.getElementById("chat").innerHTML += text;
            chatDiv.scrollTop = chatDiv.scrollHeight;
        }
        scheduleRefresh();
        document.getElementById("message").focus();
    }
}

function scheduleRefresh(){    
    refreshtimeout = setTimeout("refreshChat()",2000);
}

function refreshChat(){
    clearTimeout(refreshtimeout);
    id=document.getElementById("chatId").value;
    xmlHttp.open("GET", "chatAction.jsp?action=refresh&id="+id+"&"+refreshtimeout, true);
    xmlHttp.onreadystatechange = chatReturn;
    xmlHttp.send(null);
}

function receiveId(){
    if (xmlHttp.readyState == 4) {
        text = xmlHttp.responseText;
        document.getElementById("chatId").value = text;
        sendChat();
    }
}

function openChat( id ){
    if ( id == "" ){
        window.open("../Chat/chat.jsp","NEWCHAT",'status=0,left=20,top=20,width=300,height=400,toolbar=0,resizable=no');
    }else{
        window.open("../Chat/chat.jsp?chatId="+id,"CHAT"+id,'status=0,left=20,top=20,width=300,height=400,toolbar=0,resizable=no');
    }
}

function scheduleReload(){    
    setTimeout("location.reload()",2000);
}
