window.addEvent('domready', function(){
	var mt = window.parent.document.getElementById('cbboxmain').contentWindow.document.getElementById('mt');
	var key = document.cbbox.key;
	var nmei = document.cbbox.nme;
	var emli = document.cbbox.eml;
	var psti = document.cbbox.pst;
	var rf = $('rf');

	function writeCookie(ckName,ckVal){
		var numdays=14;
		var today=new Date();
		var expires=new Date();
		expires.setTime(today.getTime()+(1000*60*60*24*numdays));
		var cookieText=ckName+"="+ckVal+";expires="+expires.toGMTString()+";path=/;";
		document.cookie=cookieText;
		return null;
	}

	function rmGetCookie(Name){
		var search=Name+"=";
		var CookieString=document.cookie;
		var result=null;
		if(CookieString.length>0){
			offset=CookieString.indexOf(search);
			if(offset!=-1){
				offset+=search.length;
				end=CookieString.indexOf(";",offset);
				if(end==-1){
					end=CookieString.length;
				}
				result=unescape(CookieString.substring(offset,end));
			}
		}
		return result;
	}

	function esc(s){
		if(encodeURIComponent==null){
			function encodeURIComponent(s){
				function eC(c){
					function eIB(b){
						return (eB(b,128));
					}
					function eB(b,_13){
						b+=_13;
						return ("%"+b.toString(16).toUpperCase());
					}
					function nB(c){
						if(c<=127){
							return (1);
						}
						if(c<=2047){
							return (2);
						}
						return (3);
					}
					var nb=nB(c);
					var so="";
					for(var i=1;i<nb;i++){
						so=eIB(c&63)+so;
						c=c>>>6;
					}
					var a=new Array(0,192,224);
					so=eB(c,a[nb-1])+so;
					return (so);
				}
				var so="";
				for(var i=0;i<s.length;i++){
					var n=s.charCodeAt(i);
					if(n<128){
						var c=String.fromCharCode(n);
						if("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()".indexOf(c)!=-1){
							so+=c;
						}else{
							so+=eC(n);
						}
					}else{
						so+=eC(n);
					}
				}
				return (so);
			}
		}
		return encodeURIComponent(s);
	}

	rf.addEvents({
		click : function() {
			AjaxRequest.get({
				url:'poll.php',
				onSuccess: function(html) {
					mt.innerHTML=html.responseText;
				}
			});
			window.parent.document.getElementById('cbboxmain').contentWindow.document.getElementById('ab').value=8;
		}
	});

	var pstc = 0;
	psti.onclick = function() {
		if (pstc == 0) {
			psti.value="";
			pstc=1;
			document.cbbox.pst.focus();
		}
	}

	var emlc=0;
	emli.onclick = function() {
		if (emlc == 0) {
			emli.value ='';
			emlc=1;
			document.cbbox.eml.focus();
		}
	}

	var nmec=0;
	nmei.onclick = function() {
		if (nmec == 0) {
			nmei.value ='';
			nmec=1;
			document.cbbox.nme.focus();
		}
	}

	psti.onkeypress = keyPressed;
	function keyPressed(e) {
		var n;
		if(document.all) {
			n = window.event.keyCode;
		}
		else{
			n = e.which? e.which : e.charCode;
		}

		if (n==13) {
			key.value=13;

			var nme = document.cbbox.nme.value;
			var eml = document.cbbox.eml.value;
			var pst = document.cbbox.pst.value;

			var now=new Date((new Date()).getTime()).toGMTString();

			AjaxRequest.get({
				url : 'submit.php?key=13&tme='+now+'&nme='+nme+'&eml='+eml+'&pst='+pst+'',
				onSuccess: function(html) {
				//	alert(html.responseText);
				}
			});

			var pstd = rmGetCookie('mid');
			if (isNaN(pstd)) {
				pstd = 2;
			}

			var mttext = mt.getChildren()[0].getChildren()[0].innerHTML;

			window.parent.document.getElementById('cbboxmain').contentWindow.document.getElementById('ab').value=8;

			if (pstd % 2 == 1) {
				mt.innerHTML = '<table border="0" cellpadding="2" cellspacing="0" width="100%" class="hbtbl"><tr id="'+pstc+'"><td class="stxt2"><div class="dtxt2">'+now+'</div>' + '<a href="'+eml+'" target="_blank"><b class="pn_std">'+nme+'</b></a>: '+pst+'</td></tr>' + mttext + '</table>';
			}
			else {
				mt.innerHTML = '<table border="0" cellpadding="2" cellspacing="0" width="100%" class="hbtbl"><tr id="'+pstc+'"><td class="stxt"><div class="dtxt">'+now+'</div>' + '<a href="'+eml+'" target="_blank"><b class="pn_std">'+nme+'</b></a>: '+pst+'</td></tr>' + mttext + '</table>';
			}

			psti.value="message";
			psti.onclick = function() {
				if (pstc == 0) {
					psti.value="";
					pstc=1;
					document.cbbox.pst.focus();
				}
			}
			pstc=0;

			pstd = parseInt(pstd) + 1;
			writeCookie('mid',pstd);
		}
	}

	rz();

	function rz() {
		if (navigator.appName.match('etscape')) {
			document.cbbox.nme.size = 10;
			document.cbbox.eml.size = 13;
			document.cbbox.pst.size = 31;
		}
		else if (navigator.appName.match('Internet')) {
			document.cbbox.nme.size = 12;
			document.cbbox.eml.size = 13;
			document.cbbox.pst.size = 32;
		}
		else {
			document.cbbox.nme.size = 10;
			document.cbbox.eml.size = 13;
			document.cbbox.pst.size = 31;
		}
	}
});
