	document.domain = "daum.net";

	var chaton = new Array();

	function regChatOn( key , chat, your_cnick, your_peuserid, your_ceuserid, rolecode, host, suss ) {

		var entity = new Array();
		entity["chaton"] = chat;
		entity["your_cnick"] = your_cnick;
		entity["your_peuserid"] = your_peuserid;		
		entity["your_ceuserid"] = your_ceuserid;				
		entity["rolecode"] = rolecode;		
		entity["key"] = key;	
		entity["host"] = host;			
		entity["suss"] = suss;					

		var o = getObject( key );
		if ( o != null ){
			entity["index"] = o["index"];
			var index = o["index"]
			chaton[ index ] = entity;
		}else{
			entity["index"] = chaton.length;
			chaton[ chaton.length ] = entity;			
		}
	}

	function unRegChatOn( key ){
		if ( isExpiredChatOn(key) ){
			return;
		}else{
			var o = getObject( key );
			if ( o != null ){
				var index = o["index"];
				chaton[ index ]["chaton"] = null;
			}
		}
	}

	function getObject( key ){
			for ( var i=0 ; i < chaton.length; i++){
				if ( chaton[ i ]["key"] == key){
					return chaton[ i ];
				}
			}
			return null;
	}

	function getChatOn( key, value ){
		var o = null;
		if ( isExpiredChatOn(key) ){
//			key.close();
			return null;
		}else{
			o = getObject( key )
			if ( o == null ){
				return nul;
			}
		}
		return o [value];
	}

	function isExpiredChatOn(key){
		var o = getObject( key )
		if ( o == null ){
			return true;
		}
		if ( o["chaton"] ==  null ){
				return true;
		}else{
				return false;
		}
	}

	function visitChatOn( yourIrcNickName, cnick,  peid, ceid, node, grpid, cafegrade  ){
		cafeon.MSG( yourIrcNickName, getProtocol("openChatOn", node+msgKey+grpid ));	
		if ( isExpiredChatOn(yourIrcNickName) ){
			openChatOn( "", yourIrcNickName, yourIrcNickName, cnick, cafegrade, ceid, "", peid, "", node, grpid );				
		}else{
/*
			var index = getChatOn( your_inick, "index");		
			if ( !chaton[ index ]["suss"] ){
				printChatOnSysMsg( cnick+"´Ô ´ëÈ­¿¡ Âü¿© ÇÏ¼Ì½À´Ï´Ù",  yourIrcNickName);
			} else {
				chaton[ index ]["chaton"].setEmoticon();
			}
*/
		}
	}

	function chatting( msg, your_inick, cnick ){
		cafeon.MSG( your_inick, getProtocol( "confirmChat",msg) );	
		if ( msg == 'Y' ){
			var index = getChatOn( your_inick, "index");		
			chaton[ index ]["suss"] = true;		
			printChatOnSysMsg( cnick+"´Ô ´ëÈ­¿¡ Âü¿© ÇÏ¼Ì½À´Ï´Ù",  your_inick);
//			alert( chaton[ index ]["suss"] );
		}
	}

	function confirmChat( msg, my_inick, yourIrcNickName, cnick, cafegrade, ceid, aeid, peid, chatgrade, node, grpid ){
		if ( msg == 'Y' ){
			if ( isExpiredChatOn( yourIrcNickName ) ){
				return;
			}
			var index = getChatOn( yourIrcNickName, "index");
			chaton[ index ]["chaton"].chatConfirim = true;
			chaton[ index ]["suss"] = true;
			printChatOnSysMsg( cnick+"´Ô ´ëÈ­¿¡ Âü¿© ÇÏ¼Ì½À´Ï´Ù",  yourIrcNickName);

		}else{
			printChatOnSysMsg( cnick+"´Ô ²²¼­ ´ëÈ­¸¦ °ÅºÎ ÇÏ¼Ì½À´Ï´Ù.",  yourIrcNickName);

		}

	}

	function openChatOn( msg, my_inick, yourIrcNickName, cnick, cafegrade, ceid, aeid, peid, chatgrade, node, grpid, suss ){
		var tokens="";
		var url="";
		if ( msg !="" ){
			tokens = msg.split(msgKey);
			url="http://"+tokens[0]+".daum.net/_c21_/chaton?grpid="+tokens[1]+"&yourIrcNickName="+escape(yourIrcNickName);
		}else{
			url="http://"+node+".daum.net/_c21_/chaton?grpid="+grpid+"&yourIrcNickName="+escape(yourIrcNickName);
		}
		if ( isExpiredChatOn(yourIrcNickName) ){
			var host = false;
			if ( yourIrcNickName == my_inick ){
				host = true
			}
			var suss2 = false;
			try{
				if ( suss == 1 ){
					suss2 = true;
				}
			} catch ( Exception ){
				suss2 = false;
			}
			var chaton = window.open(url, "", 'width=357,height=324,resizable=no,scrollbars=no');
			regChatOn( yourIrcNickName, chaton, cnick,  peid, ceid,cafegrade, host, suss2 );
		}else{
			var index = getChatOn( yourIrcNickName, "index");		
//			alert( index );
//			var b = chaton[ index ]["suss"];
//			alert( b );
//			if ( !chaton[ index ]["suss"] ){
//				printChatOnSysMsg( cnick+"´Ô ´ëÈ­¿¡ Âü¿© ÇÏ¼Ì½À´Ï´Ù",  yourIrcNickName);
//			} else {
//				chaton[ index ]["chaton"].setEmoticon();
//			}		
		}
	}

	function exitChatOn( target, msg ){
		if ( !isExpiredChatOn( target ) ){	
			cafeon.MSG(target, getProtocol("exitYourChatOn", msg ));
		}
		unRegChatOn( target, "chaton" );		
	}

	function exitYourChatOn( msg, my_inick, your_inick, cnick, cafegrade, ceid, aeid, peid, chatgrade){
		if ( !isExpiredChatOn(your_inick) && msg != "" ){		
			printChatOnSysMsg( cnick+msg, your_inick );
			
			alert(cnick+msg);
			var index = getChatOn( your_inick, "index");		
			try{
				chaton[ index ]["chaton"].close();			
			}catch(Exception){
			
			}
		}
	}

	function msgFailed ( data1, data2, data3 ){
		if ( !isExpiredChatOn(your_inick) ){		
			var index = getChatOn( your_inick, "index");
			var cnick = chaton[ index ]["your_cnick"]
			printChatOnSysMsg( cnick+" ´Ô²² ¸Þ½ÃÁö°¡ Àü´ÞµÇÁö ¸øÇÏ¿´½À´Ï´Ù", data1 );		
		}			
	}
	setDID_NOSUCHNICK( msgFailed );

	function printChatOnSysMsg( msg,  your_inick){
		if ( isExpiredChatOn( your_inick ) ){
			return;
		}
		msg ="<br>"+msg+"<br>";

		var index = getChatOn( your_inick, "index");

		var oTable = chaton[ index ]["chaton"].document.getElementById("chatonmsg");

		var oTBody = chaton[ index ]["chaton"].document.createElement("TBODY");
		
		var oTR = chaton[ index ]["chaton"].document.createElement("TR");
		var oTD = chaton[ index ]["chaton"].document.createElement("TD");
		oTD.style.wordBreak ="break-all";
		oTD.colSpan= "3";		
//		oTD.noWrap = true;
		oTD.align='center';
		oTD.valign='top';
		oTD.innerHTML = "<font color='red'>"+msg+"</font>";
		oTR.appendChild( oTD );
		oTBody.appendChild( oTR );

		var oTR2 = chaton[ index ]["chaton"].document.createElement("TR");
		var oTD4 = chaton[ index ]["chaton"].document.createElement("TD");
		oTD4.style.wordBreak ="break-all";		
		oTD4.colSpan= "3";
		oTD4.height='1';
		oTD4.style.padding = '0px';
		oTR2.appendChild( oTD4 );
		oTBody.appendChild( oTR2 );		
		oTable.appendChild( oTBody );
		
		oTable.scrollIntoView(false);

	}
	
	function sendChatOnMsg( color, size, face, bold, msg, your_inick, suss ){
		if ( isProtocol(msg) ){	
			alert("»ç¿ëÇÒ ¼ö ¾ø´Â ´Ü¾îÀÔ´Ï´Ù. ´Ù½Ã ÀÔ·Â ÇÏ¼¼¿ä");		
		}
		var type = color+size+face+suss;
		if ( bold == true ){
			msg = "<B>"+msg+"</B>";
		}
		msg = type+msg;
		cafeon.MSG( your_inick, getProtocol("printChatOnMsg",msg) );
		printChatOnMsg( msg, "", your_inick, cafeon.CNICK, cafeon.CAFEGRADE, "", "", "", "");

		var index = getChatOn( your_inick, "index");		
		chaton[ index ]["chaton"].document.getElementById('got_time_name').innerText	= "¸¶Áö¸· ¸Þ¼¼Áö¸¦ º¸³½ ½Ã°£ : ";
	}

	function printChatOnMsg( msg, my_inick, your_inick, cnick, cafegrade, ceid, aeid, peid, chatgrade){
		if ( isExpiredChatOn( your_inick ) && my_inick !="" ){
			return;
			/*
			try{
				openChatOn( msg, my_inick, your_inick, cnick, cafegrade, ceid, "", peid, "", frames.down.frames.leftmenu.node, frames.down.frames.leftmenu.grpid, msg.substring(10,11) );
			}catch(Exception){
			}
			return;
			*/
		}

		var index = getChatOn( your_inick, "index");

		var color = msg.substring(0,6);
		var size = msg.substring(6,8);
		var face = msg.substring(8,10);
		var suss = msg.substring(10,11);	

		msg = msg.substring(11,msg.length);

		var oTable = chaton[ index ]["chaton"].document.getElementById("chatonmsg");

		var oTBody = chaton[ index ]["chaton"].document.createElement("TBODY");
		
		var oTR = chaton[ index ]["chaton"].document.createElement("TR");

		var oTD = chaton[ index ]["chaton"].document.createElement("TD");
		oTD.style.wordBreak ="break-all";
//		oTD.noWrap = true;	
		oTD.valign='top';
		oTD.width='19';
		oTD.bgColor='#'+color;
		oTD.innerHTML = "<img src='http://cafeimg.hanmail.net/cf_img/top4/bbs/mem_icon_"+cafegrade+".gif' width='13' height='13' border='0' align='absmiddle' vspace='3' hspace='6'>";
		oTR.appendChild( oTD );

		var oTD2 = chaton[ index ]["chaton"].document.createElement("TD");
		oTD2.style.wordBreak ="break-all";		
//		oTD2.noWrap = true;	
		oTD2.valign='top';
		oTD2.bgColor='#'+color;
		oTD2.width='60';
		oTD2.innerHTML = "<font style='color:#"+ "ffffff" +";' face='"+ face+"'> "+cnick+"</font>";
		oTR.appendChild( oTD2 );

		var oTD3 = chaton[ index ]["chaton"].document.createElement("TD");
		oTD3.style.wordBreak ="break-all";
		oTD3.width='233';
		oTD3.innerHTML = "<font style='color:#"+ color +"; font-size:"+size+"pt;' face='"+ face+"'> "+msg+"</font>";		
		oTR.appendChild( oTD3 );
		
		oTBody.appendChild( oTR );

		var oTR2 = chaton[ index ]["chaton"].document.createElement("TR");

		var oTD4 = chaton[ index ]["chaton"].document.createElement("TD");
		oTD4.style.wordBreak ="break-all";
		oTD4.colSpan= "3";	
		oTD4.height='1';
		oTD4.style.padding = '0px';
		oTR2.appendChild( oTD4 );

		oTBody.appendChild( oTR2 );		
		
		oTable.appendChild( oTBody );

		
		oTable.scrollIntoView(false);

		var got_time = new Date();
		var hour = got_time.getHours();
		var min = got_time.getMinutes();
		if (hour > 12){
			hour = '¿ÀÈÄ ' + eval(hour - '12');
		}
		else{
			hour = '¿ÀÀü ' + hour;
		}
		if (min < 10){min = '0' + min;}
		chaton[ index ]["chaton"].document.getElementById('got_time_name').innerText	= "¸¶Áö¸· ¸Þ¼¼Áö¸¦ ¹ÞÀ¸½Å ½Ã°£ : ";		
		chaton[ index ]["chaton"].document.getElementById('got_time').innerText = got_time.getYear() + "-" + eval(got_time.getMonth()+1) + "-" + got_time.getDate() + " " + hour + ":" + min;

	}

	function inviteSendMsg( ircnicks, channel, title, InviteMsg, server_name  ){

		if ( cafeon.MAG !="" ){
			for ( var i=0; i<ircnicks.length;i++ )	{
				cafeon.MSG( ircnicks[i], getProtocol("confirmInviteRoom", channel+msgKey+title+msgKey+InviteMsg+msgKey+server_name+msgKey+cafeon.MAG	+msgKey+cafeon.OAG+msgKey+cafeon.KEY ));
				for(var j=0;j<100;j++);
			}
		}
	}

	function confirmInviteRoom( msg, my_inick, your_inick, cnick, cafegrade, ceid, aeid, peid, chatgrade ){

		var param = "";
		try{
			param = cafeon.ENC( frames.down.frames.leftmenu.getChatParam(), 1);			
		}catch( Exception ){
			try{
			 	param = getChatParam();
			}catch( Exception ){
				return;
			}
		}

		var tokens = msg.split(msgKey);
		var key="";
		if ( typeof(tokens[6]) != "undefined"){
			key=tokens[6];
		}
//		alert(param);
		var URL="http://"+tokens[3]+"/cafechat/main/pop_invite_confirm.do?cnick="+cnick
		+"&channel="+escape(tokens[0])
		+"&title="+tokens[1]
		+"&msg="+tokens[2]
		+"&mgrade="+tokens[4]
		+"&ograde="+tokens[5]
		+"&passwd="+key
		+"&yourinick="+escape(your_inick)
		+"&param="+escape(param);
		name="";
		prop="width=346,height=220,resizable=no,scrollbars=no";
		window.open( URL, name, prop );
	}

	function refusalRoomSendMsg( your_inick, msg ){
		cafeon.MSG( your_inick, getProtocol("refuseInviteRoom",msg));
	}
	
	function refuseInviteRoom( msg, my_inick, your_inick, cnick, cafegrade, ceid, aeid, peid, chatgrade ){
			var URL="/cafechat/chat/pop_invite_refuse.do?msg="+msg+"&cnick="+cnick;
			name="";
			prop="width=346,height=220,resizable=no,scrollbars=no";
			window.open( URL, name, prop );
	}	

	function clearChaton(){
		try{
			while(true){
				o = chaton.pop()
				if ( typeof(o) != "undefined" ){
					if ( o["chaton"] != null ){
						cafeon.MSG( o["key"] , getProtocol("exitYourChatOn", o["your_cnick"]+"´ÔÀº Ä«Æä¿¡¼­ ³ª°¡¼Ì½À´Ï´Ù." ));
						o["chaton"].close();
					}
				}else{
					break;
				}
			}
		} catch ( Exception ){			
		}
	}		
	

