function xhanch_get_obj(object_name){
	return document.getElementById(object_name);
}

function xhanch_catch_error(str){
	if(str != "")
		throw new Error(str);
}

function xhanch_get_error(e){
	return e.message
}

var xhanch_ajax_prg_flag = new Array();
var xhanch_ajax_sct = new Array();
var xhanch_base_url = "";

function xhanch_in_array(hook, stack){
	for(tmp in stack){
		if(tmp == hook)
			return true;
	}
	return false;
}

function xhanch_ajax_sct_reg(xhanch_nm){
	if(!xhanch_in_array(xhanch_nm, xhanch_ajax_sct))
		xhanch_ajax_sct[xhanch_ajax_sct.length] = xhanch_nm;
}

function xhanch_ajax_hnd_err(xhanch_sct, e){
	xhanch_ajax_disp_msg(xhanch_sct, "error", xhanch_get_error(e));
	xhanch_ajax_disp_prg(xhanch_sct, false);
}

function xhanch_ajax_disp_msg(xhanch_sct, xhanch_type, xhanch_msg){
	xhanch_ajax_hide_all_msg();
	var xhanch_wrn = xhanch_get_obj("sct_ajax_" + xhanch_sct + "_msg");
	if(xhanch_wrn == null)
		return;
	xhanch_wrn.className = "ajax_message " + xhanch_type;
	xhanch_wrn.style.display = "";
	xhanch_wrn.innerHTML = xhanch_msg;
}

function xhanch_ajax_hide_msg(xhanch_sct){
	var xhanch_msg = xhanch_get_obj("sct_ajax_" + xhanch_sct + "_msg");	
	if(xhanch_msg == null)
		return;
	xhanch_msg.style.display = "none";
	xhanch_msg.innerHTML = xhanch_msg;
}

function xhanch_ajax_hide_all_msg(){
	for(xhanch_sct in xhanch_ajax_sct){
		xhanch_ajax_hide_msg(xhanch_ajax_sct[xhanch_sct]);
	}
}

function xhanch_ajax_disp_prg(xhanch_sct, xhanch_prg){
	if(typeof xhanch_prg == "undefined")
		xhanch_prg = true;
	if(typeof xhanch_ajax_prg_flag[xhanch_sct] == "undefined")
		xhanch_ajax_prg_flag[xhanch_sct] = false;
	var xhanch_frm = xhanch_get_obj("sct_ajax_" + xhanch_sct);
	var xhanch_trn = xhanch_get_obj("sct_ajax_" + xhanch_sct + "_prg");
	
	if(xhanch_prg){
		if(xhanch_ajax_prg_flag[xhanch_sct])
			return;		
			
		xhanch_ajax_hide_msg(xhanch_sct);
		
		if(xhanch_to_int(xhanch_frm.offsetHeight) <= 20)
			xhanch_trn.style.height = "20px";
		else
			xhanch_trn.style.height = xhanch_frm.offsetHeight + "px";
		
		xhanch_trn.style.width = xhanch_frm.offsetWidth + "px";
		
		xhanch_frm.style.display = "none";
		xhanch_trn.style.display = "";
		
		xhanch_ajax_prg_flag[xhanch_sct] = true;
	}else{
		if(!xhanch_ajax_prg_flag[xhanch_sct])
			return;	
			
		xhanch_frm.style.display = "";
		xhanch_trn.style.display = "none";
		xhanch_ajax_prg_flag[xhanch_sct] = false;
	}
}
	
function xhanch_create_http_req() {
	var xhanch_request;
	if(window.ActiveXObject){
		try{
			xhanch_request = new ActiveXObject("Microsoft.XMLHTTP");
		}catch (e){
			xhanch_request = false;
		}
	}else{
		try{
			xhanch_request = new XMLHttpRequest();
		}catch (e){
			xhanch_request = false;
		}
	}
	if (!xhanch_request)
		xhanch_catch_error("Your browser does not support AJAX!");
	else
		return xhanch_request;
}

function xhanch_ajax_query(prm_cmd, prm_xhanch_dat_post, prm_xhanch_misc){
	var xhanch_xml = xhanch_create_http_req();
	if(typeof prm_dat_get == "undefined")
		var prm_dat_get = {};
	if(typeof prm_xhanch_dat_post == "undefined")
		var prm_xhanch_dat_post = {};
	if(typeof prm_xhanch_misc == "undefined")
		var prm_xhanch_misc = {};
		
	function xhanch_send_query(){
		if(prm_cmd == ""){
			alert("No act to be send!");
			return false;
		}
		
		if (xhanch_xml.readyState == 4 || xhanch_xml.readyState == 0){ 
			req_url = xhanch_url(prm_cmd);	
			dat_str = "";
			for(tmp in prm_xhanch_dat_post){
				if(dat_str != "")
				   	dat_str += "&";
				dat_str += tmp + "=" + prm_xhanch_dat_post[tmp];
			}
			if(dat_str == ""){
				xhanch_xml.open("GET", req_url, true); 			
				xhanch_xml.onreadystatechange = xhanch_response;
				xhanch_xml.setRequestHeader("Accept-Charset","UTF-8");
				xhanch_xml.send(null);
			}else{
				xhanch_xml.open("POST", req_url, true); 			
				xhanch_xml.onreadystatechange = xhanch_response;
				xhanch_xml.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xhanch_xml.setRequestHeader("Accept-Charset","UTF-8");
				xhanch_xml.send(dat_str); 
			}			
			return true;
		}		
		return false;
	}
	
	function xhanch_response(){
		try{
								
			if (xhanch_xml.readyState == 4) { 
				if (xhanch_xml.status == 200) {
					try{
						xhanch_res = xhanch_xml.responseXML.documentElement;	
						xhanch_handle_response(xhanch_res);
						if(typeof prm_xhanch_misc.exec_cpl != "undefined")
							eval(prm_xhanch_misc.exec_cpl);
						if(typeof prm_xhanch_misc.exec_done != "undefined")
							eval(prm_xhanch_misc.exec_done);
					}catch(err){}
				}else{
					xhanch_catch_error(xhanch_xml.responseText); 
				}
			}
		}catch (e){
			if(typeof prm_xhanch_misc.exec_fail != "undefined")
				eval(prm_xhanch_misc.exec_fail);
			if(typeof prm_xhanch_misc.exec_done != "undefined")
				eval(prm_xhanch_misc.exec_done);
			xhanch_catch_error(e);
		}
	}
	
	function xhanch_handle_response(xhanch_reply){	
		try{
			if(typeof prm_xhanch_misc.exec != "undefined")
				eval(prm_xhanch_misc.exec + "(xhanch_reply, prm_xhanch_misc);");
		}catch (e){
			xhanch_catch_error(e);
		}
	}
	
	return xhanch_send_query();
}

function xhanch_str_replace(str,sr,val){
	return str.replace(sr, val, "g");	
}

function xhanch_parse_html(html){
	var xhanch_res = html;
	
	xhanch_res = xhanch_str_replace(xhanch_res,"&gt;",">");	
	xhanch_res = xhanch_str_replace(xhanch_res,"&lt;","<");	
	xhanch_res = xhanch_str_replace(xhanch_res,"&quot;","\"");
	xhanch_res = xhanch_str_replace(xhanch_res,"&brvbar;","|");
	xhanch_res = xhanch_str_replace(xhanch_res,"&amp;","&");
	return xhanch_res;
}

function xhanch_to_int(str){
	xhanch_res = parseInt(xhanch_to_num(str));
	if(isNaN(xhanch_res))
		xhanch_res = 0;
	return xhanch_res;
}

function xhanch_to_num(str){
	xhanch_res = parseFloat(str);
	if(isNaN(xhanch_res))
		xhanch_res = 0;
	return xhanch_res;
}

function xhanch_is_ie(){
	if(navigator.appName == "Microsoft Internet Explorer")
		return true;
	else
		return false;
}

function xhanch_is_opera(){
	if(navigator.appName == "Opera")
		return true;
	else
		return false;
}

/* URL */

function xhanch_url(xhanch_nm){		
	return "php/" + xhanch_nm;
}

/* End of URL */

/* xhanch_xml */

function xhanch_xml_nd_val(xhanch_nd){
	try{
		return xhanch_parse_html(xhanch_nd.firstChild.data);
	}catch(e){
		return "";
	}
}

function xhanch_xml_nds_by_tag(xhanch_xml, xhanch_tag){
	xhanch_res = new Array();
	xhanch_nds = xhanch_xml.getElementsByTagName(xhanch_tag);
	xhanch_len = xhanch_nds.length;
	for(xhanch_ai=0;xhanch_ai<xhanch_len;xhanch_ai++)
		xhanch_res[xhanch_ai] = xhanch_nds[xhanch_ai];
	return xhanch_res;
}

function xhanch_xml_nd_chd(xhanch_nd){
	xhanch_res = new Array();
	xhanch_nds = xhanch_nd.childNodes;
	xhanch_len = xhanch_nds.length;	
	for(xhanch_bi=0;xhanch_bi<xhanch_len;xhanch_bi++)
		xhanch_res[xhanch_bi] = xhanch_nds[xhanch_bi];
	return xhanch_res;
}

function xhanch_xml_msg(xhanch_xml, xhanch_tag){
	if(typeof xhanch_tag == "undefined")
		var xhanch_tag = "error";
	
	lst_msg = new Array();
	nds_msg = xhanch_xml_nds_by_tag(xhanch_xml, xhanch_tag);
	if(nds_msg.length == 0)
		return "";
	for(nd_msg in nds_msg)
		lst_msg[lst_msg.length] = xhanch_xml_nd_val(nds_msg[nd_msg]);	
	return lst_msg.join("<br/>");
}

/* End of xhanch_xml*/