
/* Internal Variables & Stuff */
	var waittime = 1000;
        var xmlhttp = false;
        var xmlhttp2 = false;
	var intUpdate = false;
	var timeoutfinal;
function closediv(divbox){
	document.getElementById(divbox).style.display='none';
}


function hidediv(id){
 ID = document.getElementById(id);
     if(ID.style.display == "")
          ID.style.display = "none";
     else
          ID.style.display = "";
}


function ajax_read(url, qstring, arrayval, shutterbox) {
        if(window.XMLHttpRequest){
                xmlhttp=new XMLHttpRequest();
                if(xmlhttp.overrideMimeType){
                        xmlhttp.overrideMimeType('text/xml');
                }
        } else if(window.ActiveXObject){
                try{
                        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
                } catch(e) {
                        try{
                                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                        } catch(e){
                        }
                }
        }

        if(!xmlhttp) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
        }
	return xmlhttp;
}

function ajax_get_chat(url, qstring, shutterbox, waittime){
	xmlhttpreq = ajax_read();
	 xmlhttpreq.onreadystatechange = function() {
        if (xmlhttpreq.readyState==4) {
                document.getElementById(shutterbox).innerHTML = xmlhttpreq.responseText;
                      chatboxall = setTimeout("ajax_get_chat('"+ url + "', '" + qstring + "', '" + shutterbox + "', '" + waittime + "')", waittime);
                }
        }
        xmlhttpreq.open('GET',url + qstring, true);
        xmlhttpreq.send(null);
}

function shutterbox_count_friend(url, qstring, shutterbox, waittime){
		xmlhttprecf = ajax_read();
        xmlhttprecf.onreadystatechange = function() {
        if (xmlhttprecf.readyState==4) {
			document.getElementById(shutterbox).innerHTML = xmlhttprecf.responseText;
        		countfriend = setTimeout("shutterbox_count_friend('"+ url + "', '" + qstring + "', '" + shutterbox + "', '" + waittime + "')", waittime);
                }
        }
        xmlhttprecf.open('GET',url  + qstring,true);
        xmlhttprecf.send(null);

	
}
function shutterbox_count_messages(url, qstring, shutterbox, waittime){
	xmlhttpreqb = ajax_read();
        xmlhttpreqb.onreadystatechange = function() {
        if (xmlhttpreqb.readyState==4) {
			document.getElementById(shutterbox).innerHTML = xmlhttpreqb.responseText;
        		countmessages = setTimeout("shutterbox_count_messages('"+ url + "', '" + qstring + "', '" + shutterbox + "', '" + waittime + "')", waittime);
                }
        }
        xmlhttpreqb.open('GET',url  + qstring,true);
        xmlhttpreqb.send(null);
}
function shutterbox_show_users(url, qstring, shutterbox, waittime){
        xmlhttpreqc = ajax_read();
        xmlhttpreqc.onreadystatechange = function() {
	        if (xmlhttpreqc.readyState==4) {
        	      document.getElementById(shutterbox).innerHTML = xmlhttpreqc.responseText;
                      countmessages = setTimeout("shutterbox_show_users('"+ url + "', '" + qstring + "', '" + shutterbox + "', '" + waittime + "')", waittime);
                }
        }
        xmlhttpreqc.open('GET',url  + qstring,true);
        xmlhttpreqc.send(null);
}


function shutterbox_show_online(url, qstring, shutterbox, waittime){
        xmlhttpreol = ajax_read();
        xmlhttpreol.onreadystatechange = function() {
	        if (xmlhttpreol.readyState==4) {
        	      document.getElementById(shutterbox).innerHTML = xmlhttpreol.responseText;
                      timeoutfinal = setTimeout("shutterbox_show_online('"+ url + "', '" + qstring + "', '" + shutterbox + "', '" + waittime + "')", waittime);
                }
        }
        xmlhttpreol.open('GET',url  + qstring,true);
        xmlhttpreol.send(null);
}
function shutterbox_chat(url, qstring, shutterbox, waittime){
        xmlhttpreqd = ajax_read();
        xmlhttpreqd.onreadystatechange = function() {
	        if (xmlhttpreqd.readyState==4) {
        	      document.getElementById(shutterbox).innerHTML = xmlhttpreqd.responseText;
                      timeoutfinal = setTimeout("shutterbox_chat('"+ url + "', '" + qstring + "', '" + shutterbox + "', '" + waittime + "')", waittime);
                }
        }
        xmlhttpreqd.open('GET',url  + qstring,true);
        xmlhttpreqd.send(null);
		var objDiv = document.getElementById(shutterbox);
		objDiv.scrollTop = objDiv.scrollHeight;

		//location.href='#chat';
}
function stoptimer(){
	clearTimeout(timeoutfinal);
}
/* Request for Writing the Message */
function ajax_write(url){
		//url = urlEncode(url);
		//alert(url);
        if(window.XMLHttpRequest){
                xmlhttp2=new XMLHttpRequest();
                if(xmlhttp2.overrideMimeType){
                        xmlhttp2.overrideMimeType('text/xml');
                }
        } else if(window.ActiveXObject){
                try{
                        xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP");
                } catch(e) {
                        try{
                                xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
                        } catch(e){
                        }
                }
        }

        if(!xmlhttp2) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
        }

        xmlhttp2.open('GET',url,true);
        xmlhttp2.send(null);
}

/* Submit the Message */

function submit_msg(nickname, message){
        nick = document.getElementById(nickname).value;
        msg = document.getElementById(message).value;
		//document.write(msg);
        if (nick == "") { 
                check = prompt("please enter username:"); 
                if (check === null) return 0; 
                if (check == "") check = "anonymous"; 
                document.getElementById(nickname).value = check;
                nick = check;
        } 
		if(msg == ""){
				alert("please enter msg");
				return 0;
		}

        document.getElementById(message).value = "";
	if (message == 'shutter_message'){
	ajax_write("shutterbox.jsp?pagetype=savemsg&msg=" + urlEncode(msg) + "&sender_id=" + document.getElementById('owner_id').value +  "&recipient_id="+ nick );
	}	
	else{
        ajax_write("w.php?m=" + msg + "&n=" + nick);
	}
}

/* Check if Enter is pressed */
function keyup(arg1, recid, mesg) { 
        if (arg1 == 13) submit_msg(recid, mesg); 
}



function urlEncode(inputString, encodeAllCharacter){
       var outputString = '';
       if (inputString != null){
         for (var i = 0; i < inputString.length; i++ ){
            var charCode = inputString.charCodeAt(i);
            var tempText = "";
            if (charCode < 128) {
                if (encodeAllCharacter)
                {
                  var hexVal = charCode.toString(16);
                  outputString += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();  
                } else {
                  outputString += String.fromCharCode(charCode);
                }
                            
            } else if((charCode > 127) && (charCode < 2048)) {
                tempText += String.fromCharCode((charCode >> 6) | 192);
                tempText += String.fromCharCode((charCode & 63) | 128);
                outputString += escape(tempText);
            } else {
                tempText += String.fromCharCode((charCode >> 12) | 224);
                tempText += String.fromCharCode(((charCode >> 6) & 63) | 128);
                tempText += String.fromCharCode((charCode & 63) | 128);
                outputString += escape(tempText);
            }
         }
       }
       return outputString;
    }
