//--------------------------------------------------------------
function htmlspecialchars(s) {
	var tmp=''
	for(var i=0;i<s.length;i++)
	{
		if(s.charAt(i) == '&') tmp += '&amp;'; else tmp += s.charAt(i);
	}
	while(tmp.indexOf('<')>-1) tmp = tmp.replace('<', '&lt;');
	while(tmp.indexOf('>')>-1) tmp = tmp.replace('>', '&gt;');
	return tmp;
}
//--------------------------------------------------------------
function nl2br(s) {
	while(s.indexOf('\r\n')>-1) s = s.replace('\r\n', '\n');
	var tmp=''
	for(var i=0;i<s.length;i++)
	{
		if(s.charAt(i) == '\r' || s.charAt(i) == '\n') tmp += '<br>'; else tmp += s.charAt(i);
	}
	return tmp;
}
//--------------------------------------------------------------
var resize_count=0;
function safe_window_resize(w,h){

	if (resize_count>5) {resize_count=0;return false;}
	var x=Math.round((screen.width-w)/2);
	var y=Math.round((screen.height-h)/2);
	try {
		resizeTo(w,h);
		moveTo(x,y);
	}
	catch(err) {
		resize_count++;
		return safe_window_resize(w,h);
	}
	finally {
		return true;
	}
	return false;
}
//--------------------------------------------------------------
function radio_selectedIndex(radio){
	for (var i=0;i<radio.length;i++) {
		if (radio[i].checked) return (i+1);
	}
	return 0;
}
//--------------------------------------------------------------
function popup(action,id){
	var url='?action='+action;
	if (id) url+='&id='+id;
	var newwin = window.open(url, action+id, 'scrollbars=1,fullscreen=0,toolbar=0,menubar=0,status=1,resizable=1,location=0,directories=0,width=420,height=130');
	newwin.focus();
}
//--------------------------------------------------------------
function ipopup(action,id){
	var url='./?action='+action;
	if (id) url+='&id='+id;
	var newwin = window.open(url, action+id, 'scrollbars=1,fullscreen=0,toolbar=0,menubar=0,status=1,resizable=1,location=0,directories=0,width=420,height=130');
	newwin.focus();
}
//--------------------------------------------------------------
function urlpopup(url){
	// var url='./?action='+action;
	// if (id) url+='&id='+id;
	var newwin = window.open(url, "Download", 'scrollbars=1,fullscreen=1,toolbar=0,menubar=0,status=1,resizable=1,location=0,directories=0,width=420,height=130');
	newwin.focus();
}
//--------------------------------------------------------------
function xpopup(action,id,w,h){
	var url='?action='+action;
	if (id) url+='&id='+id;
	if (!w) w=720;
	if (!h) h=520;
	var newwin = window.open(url, action+id, 'scrollbars=1,fullscreen=0,toolbar=0,menubar=0,status=1,resizable=1,location=0,directories=0,width='+w+',height='+h);
	newwin.focus();
}
//--------------------------------------------------------------
function trim(value) {
	var temp = value;
	var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
	if (obj.test(temp)) {
		temp = temp.replace(obj, '$2');
	}
	return temp;
}
//--------------------------------------------------------------
function parseResponseHeader(key, header) {

	var lines = header.split("\n");
	var re = new RegExp("^" + key + ":\\s");

	for (var i in lines) {
		if (re.exec(lines[i])) {
			var returnValue = trim(RegExp.rightContext);
			return returnValue;
		}
	}
	return "";
}
//--------------------------------------------------------------
function on_key_press(key_press)
{
	if(is_ok){
		var kC;
		var cK;
		var kM;
		if(window.event){kC=window.
		event.keyCode;cK=window.event.ctrlKey;kM=2;}
		else{kC=key_press.keyCode;cK=key_press.ctrlKey;
		if(key_press.modifiers!=undefined){kM=key_press.modifiers;cK=2;kC=key_press.which;}
		else kM=2;
		}
		typing();
		if(cK&&kM==2&&(kC==13||kC==10)) sendmessage()
	}
}
is_ok = true;
//--------------------------------------------------------------
