function do_cmd(cmd,key,mode){
	if(cmd=='logout'){
		if(confirm('This will log you off IM. Continue?')){
			top.checker.close_im();
		}
	}
	else if(mode){
		open_block(cmd, key);
	}
	else{
		parent.fstatic.open_block(cmd, key);
	}
}

function open_block(block,key){
	if(block=='groups'){
		ImOpen(do_link('im','im_groups','pp=up'),'groups','350','350','center','yes');
	}
	else if(block=='friends'){
		ImOpen(do_link('im','im_friends','pp=up'),'friends','350','350','center','yes');
	}
	else if(block=='ignore'){
		ImOpen(do_link('mailbox','myblocks','pp=up'),'ignore','350','350','center','yes');
	}
	else if(block=='options'){
		ImOpen(do_link('im','im_options','pp=up'),'options','350','650','center','yes');
	}
	else if(block=='help'){
		ImOpen(do_link('global','static','pp=up&file=im_help'),'help','350','430','center','yes');
	}
	else if(block=='guidelines'){
		ImOpen(do_link('global','static','pp=up&file=guidelines'),'help','450','530','center','yes');
	}
	else if(block=='chat'){
		ImOpen(do_link('im','request','pp=up&logins_key='+key),'request','600','430','center','yes');
	}
	else if(block=='join'){
		ImOpen(do_link('im','goin','pp=up&cr_key='+key),key,'600','450','center','yes');
	}
	else if(block=='history'){
		ImOpen(do_link('im','history','pp=empty'),key,'600','400','center','yes');
	}	
}

function ImOpen(mypage,myname,w,h,win_position){
        
	alert("here"); return false;
	var win = null;
        if(win_position=="topright"){
           LeftPosition = (screen.width) ? (screen.width-w-12) : 0;
           TopPosition = 20;
        }
        else if(win_position=="center"){
           LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
           TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
        }

        settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,resizable=no';
        
        alert("here");
        
        myname = myusername+myname;
        win = window.open(mypage,myname,settings);
        alert(win);
        if (win == null) {
		alert("Your popup blocker stopped a window from opening\nPlease disable your popup blocker if you wish to see window content!");
	}
}


// --------------------------------------------------------------------------------------------------------------------------------------------



function SetUsrStatus(Id,UsrStatus){
                var img = new Image();
                img.src = "img/im/im_"+UsrStatus+".gif?"+Math.random();
                eval("document.usr_status_"+Id+".src = img.src;");
				var alt = "Offline";
				if(UsrStatus == "busy") alt = "Busy";
				if(UsrStatus == "on") alt = "Available";
				eval("document.usr_status_"+Id+".alt = '" +alt+ "';");
}

function im_close(){
        win = window.open("", "win", "width=1,height=1,screenX=2000,left=2000,screenY=2000,top=2000");
        win.focus();
        win.document.open("text/html");
        win.document.write("<HTML><HEAD><TITLE></TITLE></HEAD><BODY onClose='window.blur();' marginwidth=0 marginheight=0 topmargin=0 leftmargin=0><img src='im_close.php' width=1 height=1><script language='JavaScript'>alert('Your IM session will be closed now.');self.close()</script></BODY></HTML>");
        win.document.close();
}

function ReportAbuse(uid){
	if(confirm("This will report this conversation to the admin.\nPlease note that the entire text will be attached to the email that the admin will receive.\n\nAre you sure you wish to report this discussion as an abuse?")){
		window.open(do_link('global','send_report','pp=up&logins_key='+uid),'sendreport','top=50,left=50,height=400,width=550');
	}
}

function AddFavoriteUser(uid){
	ImOpen(do_link('im','im_friends','pp=up&action=add&logins_username='+uid),'friends','350','350','center','yes');
       
}

function AddBlockUser(uid){
	ImOpen(do_link('mailbox','myblocks','pp=up&username='+uid),'ignore','350','350','center','yes');
       
}

function SaveHistory(uid,chan){
	window.open(do_link('im','history_content','pp=up&mode=save_history&logins_key='+uid+'&chan='+chan),'history','top=50,left=50,height=400,width=550');
	//ImOpen(do_link('im','history_content','pp=up&mode=save_history&logins_key='+uid+'&chan='+chan),'history','350','350','center','yes');
}

function do_alert(howto){
        if(howto>0){
                //ImOpen(do_link('im','im_alerter','pp=up'),'Warning','250','250','center','no');
                window.open(do_link('im','im_alerter','pp=up'),'alerter','top=50,left=50,height=250,width=250');
        }
}

function ShowNextOnline(){
        ImOpen(do_link('im','im_nextonline','pp=up'),'NextTimesOnline','350','250','center','yes');
}

function ImOpen(mypage,myname,w,h,win_position,Scrool){
        var win = null;
        if(win_position=="topright"){
           LeftPosition = (screen.width) ? (screen.width-w-12) : 0;
           TopPosition = 20;
        }
        else if(win_position=="center"){
           LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
           TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
        }

        settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+Scrool+',resizable=no'
        win = window.open(mypage,myname,settings)
}

function SetUsrStatus(Id,UsrStatus){
                var img = new Image();
                img.src = "img/im/im_"+UsrStatus+".gif?"+Math.random();
                eval("document.usr_status_"+Id+".src = img.src;");
				var alt = "Offline";
				if(UsrStatus == "busy") alt = "Busy";
				if(UsrStatus == "on") alt = "Available";
				eval("document.usr_status_"+Id+".alt = '" +alt+ "';");
}

function im_close(){
        win = window.open("", "win", "width=1,height=1,screenX=2000,left=2000,screenY=2000,top=2000");
        win.focus();
        win.document.open("text/html");
        win.document.write("<HTML><HEAD><TITLE></TITLE></HEAD><BODY onClose='window.blur();' marginwidth=0 marginheight=0 topmargin=0 leftmargin=0><img src='im_close.php' width=1 height=1><script language='JavaScript'>alert('Your IM session will be closed now.');self.close()</script></BODY></HTML>");
        win.document.close();
}














/**
* Encodes a string according to "URL encoding" rules whereby every
* character is converted to the %xx equivelent. This is similar
* the JavaScript 'escape()' function, except that it encodes all
* the characters, not just the special ones like "-/?&...". For example,
* the string "foo@example.com" will be encoded into
* "%66%6F%6F%40%65%78%61%6D%70%6C%65%2E%63%6F%6D".
*@see myUrlDecode
*/
function myUrlEncode(str, urlField, urlLink) {
        var result = "";
        var i = 0;

        for (i=0; i < str.length; i++) {
                result = result + "%";
                result = result + "0123456789ABCDEF".charAt((str.charCodeAt(i)/16)&0x0F);
                result = result + "0123456789ABCDEF".charAt((str.charCodeAt(i)/1)&0x0F);
        }

        urlField.value = result;   /*Set the form-field with this value*/
        urlLink.href = "mailto:" + result; /*Set the test hyperlink with this value*/
}

function hexstr(c) {
        var result = "";

        result = result + "0123456789ABCDEF".charAt((c/16)&0x0F);
        result = result + "0123456789ABCDEF".charAt((c/1)&0x0F);

        return result;
}

function myUtf8Encode(str, urlField, urlLink) {
        var result = "";
        var i = 0;

        for (i=0; i < str.length; i++) {
                var c = str.charCodeAt(i);

                result = result + hexstr(0xC0 + ((c >> 6) & 0x03)) + " ";
                result = result + hexstr(0x3F & c) + " ";
        }

        urlField.value = result;   /*Set the form-field with this value*/
        urlLink.href = "mailto:" + result; /*Set the test hyperlink with this value*/
}

/**
* Encodes a string according to "character entity" symbol rules in HTML.
*@see myEntDecode
*/
function myEntEncode(str) {
        var result = "";
        var i = 0;

        for (i=0; i < str.length; i++) {
        var c = str.charCodeAt(i);
        var tmp = "";

        while (c >= 1) {
                tmp = "0123456789".charAt(c % 10) + tmp;
                c = c / 10;
        }

        if (tmp == "")
                tmp = "0";

                tmp = "#" + tmp;
                tmp = "&" + tmp;
                tmp = tmp + ";";

                result += tmp;
        }

        return result;
}



/**
* A helper function for converting a hex digit into the corresponding
* integer value. I really don't know much about JavaScript, so the
* the conversion is a little laborious. There probably is an easier
* way (including a built-in function), but I don't know it.
*/
function hexval(c) {
        if (String('0').charCodeAt(0) <= c && c <= String('9').charCodeAt(0))
                return c - String('0').charCodeAt(0);
        if (String('A').charCodeAt(0) <= c && c <= String('F').charCodeAt(0))
                return c - String('A').charCodeAt(0) + 10;
        if (String('a').charCodeAt(0) <= c && c <= String('f').charCodeAt(0))
                return c - String('a').charCodeAt(0) + 10;
        return 0;
}
function hexval2(c) {
        if (c == '0') return 0;
        if (c == '1') return 1;
        if (c == '2') return 2;
        if (c == '3') return 3;
        if (c == '4') return 4;
        if (c == '5') return 5;
        if (c == '6') return 6;
        if (c == '7') return 7;
        if (c == '8') return 8;
        if (c == '9') return 9;

        if (c == 'a') return 10;
        if (c == 'b') return 11;
        if (c == 'c') return 12;
        if (c == 'd') return 13;
        if (c == 'e') return 14;
        if (c == 'f') return 15;

        if (c == 'A') return 10;
        if (c == 'B') return 11;
        if (c == 'C') return 12;
        if (c == 'D') return 13;
        if (c == 'E') return 14;
        if (c == 'F') return 15;

        return 16;
}
function isxdigit(c) {
        return hexval2(c) < 16;
}
function decval(c) {
        if (c == '0') return 0;
        if (c == '1') return 1;
        if (c == '2') return 2;
        if (c == '3') return 3;
        if (c == '4') return 4;
        if (c == '5') return 5;
        if (c == '6') return 6;
        if (c == '7') return 7;
        if (c == '8') return 8;
        if (c == '9') return 9;
        return 10;
}
function isdigit(c) {
        return decval(c) < 10;
}

/**
* Decodes a string that has been previously encoded according to the
* myUrlDecode() function.
*@see myUrlEncode
*/
function myUrlDecode(str, fld, lnk) {
        var result = new String("");
        var i=0;

        for (i=0; i < str.length; i++) {
                if (str.charAt(i) == '%' && i+3 <= str.length) {
                        var foo = 0;
                        i++;
                        result = result +
                        String.fromCharCode(
                                hexval(str.charCodeAt(i)) * 16 +
                                hexval(str.charCodeAt(i+1))
                        );
                        i++;

                } else
                        result  = result + str.charAt(i);
        }
        fld.value = result;
        lnk.href = "mailto:" + result;
}


function myEntDecode(str) {
        var result = new String("");
        var i=0;

        while (i < str.length) {
                while (str.charAt(i) != '&' && i < str.length)
                        result = result + str.charAt(i++);

                while (str.charAt(i) == '&' && i < str.length) {
                        if (i+1 >= str.length || str.charAt(i+1) != '#') {
                                result = result + str.charAt(i++);
                                continue;
                        }
                        i += 2; /*skip "?" */

                        if (i >= str.length) {
                                result += "?";
                                continue;
                        }

                        if (str.charAt(i) == 'x' || str.charAt(i) == 'X') {
                                var c = 0;

                                i++;
                                if (i >= str.length || !isxdigit(str.charAt(i)))
                                        c = str.charAt(i-1);
                                else
                                        while (isxdigit(str.charAt(i)) && i < str.length)
                                                c = c * 16 + hexval2(str.charAt(i++));

                                result = result + String.fromCharCode(c);
                                if (i < str.length && str.charAt(i) == ';')
                                        i++;
                        } else if (isdigit(str.charAt(i))) {
                                var c = 0;
                                while (i < str.length && isdigit(str.charAt(i)))
                                        c = c * 10 + decval(str.charAt(i++));
                                result = result + String.fromCharCode(c);
                                if (i < str.length && str.charAt(i) == ';')
                                        i++;
                        }
                }
        }


       return result;
}

function check_existance(status,msg){
	if(status>0){
		// OK
	}
	else{
		sendText(msg);
		//document.inp.userinput.disabled=true;
		//document.inp.sender.disabled=true;
		form_is_disabled=1;
	}
}

function sendTyping(){
	typo=":#TYPO#:"+MY_USERNAME;
        
        if (!just_typed) {		
	        just_typed = 1;
        	setTimeout("send_j_type(typo);", 1500);
	}
	
}

function send_j_type(typo){
	document.applets[0].sendLine(typo);
	just_typed = 0;
}

function show(){
	if (document.getElementById){
		document.getElementById("typing").style.display="block";
	}
}

function hide(){
	if (document.getElementById){
		document.getElementById("typing").style.display="none";
	}
}


function hide_type(){
	if(!just_show_typed){
		hide();
	}
	setTimeout("hide_type()",3000);
}

function reset_timer(){
	just_show_typed=0;
}