


function Dict_Select(comb_id,dict_id,mode)
{
	//alert("0");
	var comb;
	var show_mode;
	var div_id = "dict_" + comb_id + "_div";

	/*
	var ctrls_div_id = document.getElementsByName(div_id);
	if(ctrls_div_id == null)
	{
		alert('cant get by name ' + div_id);
		return;
	};
	//var ctrl = document.getElementById(ctrl_id);
	var div = ctrls_div_id[0];
	if(div == null)
	{
		 //alert('cant get ' + div_id);
		 //return;
	};
	*/

	var div = document.getElementById(div_id);
	var body = document.body;
	var iframe_tmp_dict_select = document.getElementById("iframe_tmp_dict_select");
	var dg_id = "db_" + dict_id;
	var dg_html;
	
	show_mode = mode;
	
		
	//alert('show_mode is '+show_mode);
			
	/*
	var ctrls = document.getElementsByName(comb_id);
	if(ctrls == null)
	{
		alert('cant get by name ' + comb_id);
		return;
	};
	*/
	//var ctrl = document.getElementById(ctrl_id);
	//var comb = ctrls[0];
	/*
	var comb = document.getElementById(ctrl_id);
	if(comb == null)
	{
		alert('cant get ' + ctrl_id);
		 return;
	};
*/
	
	comb = document.getElementById(comb_id);
	if(comb == null) return 0;
	
	if(iframe_tmp_dict_select == null)
	{
		iframe_tmp_dict_select = document.createElement("iframe");
  	iframe_tmp_dict_select.id = "iframe_tmp_dict_select";
    iframe_tmp_dict_select.style.position = "absolute";
    iframe_tmp_dict_select.style.display = "block";
    iframe_tmp_dict_select.frameBorder = 0;
    iframe_tmp_dict_select.style.backgroundcolor="white";
    //iframe_tmp_dict_select.src = "about:";
  	body.appendChild(iframe_tmp_dict_select);
	}

	if(div != null)
	{
		iframe_tmp_dict_select.style.left = div.style.left;
  	iframe_tmp_dict_select.style.top = div.style.top;
  	iframe_tmp_dict_select.style.height = div.style.height;
  	iframe_tmp_dict_select.style.width = div.style.width;

		div.style.display = "block";
		iframe_tmp_dict_select.style.display = "block";

		return 1;
	}

	div = document.createElement("div");
	div.style.display = "block";
	div.style.border = "outset 2px";

  availWidth = parseInt(document.body.clientWidth);
  availHeight = parseInt(document.body.clientHeight);

  var w=200,h=360;
	if(show_mode == 1 || show_mode == 2)
		w=300;
	else
		w=500;

  div.style.width = w + "px";
	div.style.height = h + "px";

	div.style.left = (availWidth - w)/2 + "px";
	div.style.top  =  (availHeight-h)/2 + "px";

	div.style.background = "#DDD";
	div.style.overflow = "auto";

	div.setAttribute("id",div_id);
	

	
	div.style.position="absolute";

	iframe_tmp_dict_select.style.left = div.style.left;
  iframe_tmp_dict_select.style.top = div.style.top;
  iframe_tmp_dict_select.style.height = div.style.height;
  iframe_tmp_dict_select.style.width = div.style.width;
  iframe_tmp_dict_select.style.display = "block";
  	
	if(show_mode == 1)
	{
	
		var dbio = new Xml_Mgr("get_dict_xml?dict_id="+dict_id);
		var dbio_row;
    dg_html  = "";
    dg_html += "<div  style=' left: 180; top: 3; width: 100; height: 24; font-family: Arial; font-size: 13px; color:#3f1fee;font-weight:bolder; text-decoration: none;  overflow: hidden; position: absolute;  ' >";
    dg_html += "<input type='button' name='' value='\u5173\u95ed\u7a97\u53e3' style='font-family: Arial; font-size: 12px; color:0;text-decoration: none; ' onclick='Close_Select(";
    dg_html += "\"" + div_id + "\")'>";
    dg_html += "</div>";

		//关闭窗口\u5173\u95ed\u7a97\u53e3
		//编号\u7f16\u53f7
		//名称\u540d\u79f0
		
    dg_html += "<div  style=' left: 2; top: 30; width: 275; height: 300; font-family: Arial; font-size: 13px; color:#3f1fee;font-weight:bolder; text-decoration: none;  position: absolute;  ' >";

    dg_html += "<table id='" + dg_id + "' cellspacing='1' style='font-size:9pt;table-layout: fixed;'>" +
	 		"<tr height='26pt'>" +
	 		"<td width=90 bgcolor='#5a75ce' style='text-align:center;font-family:Arial;font-size:10pt;color:#ffffff;'>\u7f16\u53f7</td>" +
			"<td bgcolor='#5a75ce' style='text-align:center;font-family:Arial;font-size:10pt;color:#ffffff;'>\u540d\u79f0</td>" +
			"</tr>";

		var row_list = dbio.getElementsByTagName("row");
		
		var code_id,code_name;
		
		//		alert('d' + row_list.length);
		
		for (var id = 0; id < row_list.length; id++)
		{
			code_id = row_list[id].getElementsByTagName("code_id")[0].childNodes[0].nodeValue;
			code_name = row_list[id].getElementsByTagName("code_name")[0].childNodes[0].nodeValue;
	
			dg_html += "<tr height='22' ondblclick=\"Set_Comb_Val('" + code_id 	+ "','" + comb_id + "','" + div_id + "')\" bgColor='";
			if(id%2 == 0)
				dg_html += "#f7f8f9'>";
			else
				dg_html += "#ffffff'>";


			dg_html += "<td>" + code_id + "</td>";
			dg_html += "<td>" + code_name + "</td>";
			
			dg_html += "</tr>";

		}
		dg_html += "</table>";
    dg_html += "</div>";

		div.innerHTML = dg_html;
    body.appendChild(div);

		return 1;
  }

	if(show_mode == 2)
	{
    var tree = new Tree(dict_id,0,0,160,360,comb_id,div_id,dg_id,show_mode);
		var xml = new Xml_Mgr("get_tree_dict_xml?dict_id="+dict_id);
		var child_list = xml.getElementsByTagName("tree");

		tree.Load_List(null,child_list[0].childNodes);

    dg_html  = "";
    dg_html += "<div  style=' left: 180; top: 3; width: 100; height: 24; font-family: Arial; font-size: 13px; color:#3f1fee;font-weight:bolder; text-decoration: none;  overflow: hidden; position: absolute;  ' >";
    dg_html += "<input type='button' name='' value='\u5173\u95ed\u7a97\u53e3' style='font-family: Arial; font-size: 12px; color:0;text-decoration: none; ' onclick='Close_Select(";
    dg_html += "\"" + div_id + "\")'>";
    dg_html += "</div>";

		//关闭窗口\u5173\u95ed\u7a97\u53e3

    dg_html += "<div  style=' left: 2; top: 30; width: 275; height: 300; font-family: Arial; font-size: 13px; color:#3f1fee;font-weight:bolder; text-decoration: none;  position: absolute;  ' >";
    dg_html += tree.To_Str();
    dg_html += "</div>";

    div.innerHTML = dg_html;

    body.appendChild(div);
    
    return 1;
  }
  
  if(show_mode == 3)
	{
		var tree = new Tree(dict_id,0,0,200,360,comb_id,div_id,dg_id,show_mode);
		var xml = new Xml_Mgr("get_dict_idx_tree_xml?dict_id="+dict_id);
		var child_list = xml.getElementsByTagName("tree");
		
		tree.Load_List(null,child_list[0].childNodes);

    dg_html  = "";
    dg_html += "<div  style=' left: 180; top: 3; width: 100; height: 24; font-family: Arial; font-size: 13px; color:#3f1fee;font-weight:bolder; text-decoration: none;  overflow: hidden; position: absolute;  ' >";
    dg_html += "<input type='button' name='' value='\u5173\u95ed\u7a97\u53e3' style='font-family: Arial; font-size: 12px; color:0;text-decoration: none; ' onclick='Close_Select(";
		//关闭窗口\u5173\u95ed\u7a97\u53e3
		//编号\u7f16\u53f7
		//名称\u540d\u79f0

    dg_html += "\"" + div_id + "\")'>";
    dg_html += "</div>";
    dg_html += "<div  style=' left: 2; top: 30; width: 460; height: 400; font-family: Arial; font-size: 13px; color:#3f1fee;font-weight:bolder; text-decoration: none;  position: absolute;  ' >";

    dg_html += tree.To_Str();
    dg_html += "<div id='" + dg_id + "' style='left:160;top:0;width:320;height:360; position:absolute'><table cellspacing='1' style='font-size:9pt;table-layout: fixed;'>" +
	 		"<tr height='26pt'>" +
	 		"<td width=90 bgcolor='#5a75ce' style='text-align:center;font-family:Arial;font-size:10pt;color:#ffffff;'>\u7f16\u53f7</td>" +
			"<td width=200 bgcolor='#5a75ce' style='text-align:center;font-family:Arial;font-size:10pt;color:#ffffff;'>\u540d\u79f0</td>" +
			"</tr></table></div>";
    dg_html += "</div>";
			
		div.innerHTML = dg_html;

    body.appendChild(div);

		return 1;
  }
  
  return 1;
}

function Set_Comb_Val(code_id,comb_id,div_id)
{
	//alert(code_id);
	//alert(comb_id);
	//alert(div_id);
	var div = document.getElementById(div_id);
	comb = document.getElementById(comb_id);
	var iframe_tmp_dict_select = document.getElementById("iframe_tmp_dict_select");
	
	if(div == null || comb == null || iframe_tmp_dict_select == null) return;
	
	for (var id = 0; id < comb.options.length; id++)
	{
		if(comb.options[id].value == code_id)
		{
			comb.options[id].selected = true;
			break;
		}
	}
	
	div.style.display = "none";
	iframe_tmp_dict_select.style.display = "none";
}

function Close_Select(div_id)
{
	//alert(div_id);
	var iframe_tmp_dict_select = document.getElementById("iframe_tmp_dict_select");
	var div = document.getElementById(div_id);

	if(iframe_tmp_dict_select == null)
	{
		alert('iframe_tmp_dict_select is null')
		return;
	}
	if(div == null)
	{
		alert(div_id + ' control is null')
		return;
  }
  //alert("cccc");

	div.style.display = "none";
	iframe_tmp_dict_select.style.display = "none";
}

function Node_Chg(dict_id,code_id,comb_id,div_id,dg_id)
{
	var dg = document.getElementById(dg_id);

  var dbio = new Xml_Mgr("get_dict_filter_xml?dict_id="+dict_id+"&idx_val="+code_id);
	//var dbio = new Xml_Mgr("emp_" + code_id + ".xml");
	var dg_html;
		
	//alert("1");
	if(dg == null) return 0;
	//alert("2");
	//alert(dg.innerHTML);
	
	dg_html = "<table cellspacing='1' style='font-size:9pt;table-layout: fixed;'><tr height='26pt'>" +
		"<td width=90 bgcolor='#5a75ce' style='text-align:center;font-family:Arial;font-size:10pt;color:#ffffff;'>编号</td>" +
		"<td width=300 bgcolor='#5a75ce' style='text-align:center;font-family:Arial;font-size:10pt;color:#ffffff;'>名称</td>" +
		"</tr>";

  //alert("3");
	var row_list = dbio.getElementsByTagName("row");
	var code_id,code_name;

	for (var id = 0; id < row_list.length; id++)
	{
		code_id = row_list[id].getElementsByTagName("code_id")[0].childNodes[0].nodeValue;
		code_name = row_list[id].getElementsByTagName("code_name")[0].childNodes[0].nodeValue;

		//dbio_row = row_list[id].childNodes;
		
		dg_html += "<tr height='22' ondblclick=\"Set_Comb_Val('" + code_id 
			+ "','" + comb_id + "','" + div_id + "')\" bgColor='";
		if(id%2 == 0)
			dg_html += "#f7f8f9'>";
		else
			dg_html += "#ffffff'>";
				
		dg_html += "<td>" + code_id + "</td>";
		dg_html += "<td>" + code_name + "</td>";
		//dg_html += "<td>" + dbio_row[2].childNodes[0].nodeValue + "</td>";
			
		dg_html += "</tr>";
	}
	dg_html += "<table>";
	//alert("4");

	//alert(dg_html);
	dg.innerHTML = dg_html;
	
	return 1;
}

function Tree_Node(dict_id,code_id,code_name,comb_id,div_id,dg_id,show_mode)
{
  this.Dict_Id = dict_id;
	this.mCode_Id = code_id;
	this.mCode_Name = code_name;
	this.mChilds = new Array();
	this.mComb_Id = comb_id;
	this.mDiv_Id = div_id;
	this.mDg_Id = dg_id;
	this.mShow_Mode = show_mode;
	
	this.Add_Child = function(tree_node)
	{
		this.mChilds[this.mChilds.length] = tree_node;
	}

	this.To_Str = function(left)
	{
		var id;
		var html_str = "";

		html_str += "<div id='div_" + this.Dict_Id + "_" + this.mCode_Id + "' style=' display:block; '>";
		html_str += "<table width='100%' height='22' border='0' cellpadding='0' cellspacing='0'>";
    html_str += "<tr style='cursor: hand;'>";
    html_str += "<td width='" + left + "'>";
    for(id = 0; id < left; id++)
    	html_str += "&nbsp;";
    html_str += "</td>";
    html_str += "<td width='12' align='center' id='td_" + this.mCode_Id + "' onclick=Show_Chg('" + this.Dict_Id + "_" + this.mCode_Id + "') >";
    if(this.mChilds.length > 0)
    	html_str += "+";
   	else
   		html_str += "&nbsp;";
    html_str += "</td>";
    html_str += "<td>";
    
   	html_str += "<input type='button' id='node_" + this.mCode_Id + "'";
   	
   	if(this.mShow_Mode == 2)
   		html_str += "  ondblclick=\"Set_Comb_Val('" + this.mCode_Id + "','" + this.mComb_Id + "','" + this.mDiv_Id;
   	else
   		html_str += "  onclick=\"Node_Chg('" + this.Dict_Id + "','" + this.mCode_Id + "','" + this.mComb_Id + "','" + this.mDiv_Id + "','" + this.mDg_Id;
   	html_str += "')\" value='&nbsp;" + this.mCode_Name + "' style=' font-family: Arial; font-size: 13px; color:#1f7f9f;"
   		+ "text-decoration: none; height:-10007px;text-align:left; border:none; background:transparent;margin-left: 2; '>";

  	html_str += "</td>";
  	html_str += "</tr>";
  	html_str += "</table>";
  	html_str += "</div>";
		
		if(this.mChilds.length == 0) return html_str;
		html_str += "<div id='div_chd_" + this.Dict_Id + "_" + this.mCode_Id + "' style=' display:none; '>";
		for (id = 0; id < this.mChilds.length; id++)
			html_str += this.mChilds[id].To_Str(left + 5);
		html_str += "</div>";

		return html_str;
	}
}

function Tree(dict_id,x,y,width,height,comb_id,div_id,dg_id,show_mode)
{
  this.Dict_Id = dict_id;
	this.X = x;
	this.Y = y;
	this.mWidth = width;
	this.mHeight = height;
	this.mNodes = new Array();
	this.mComb_Id = comb_id;
	this.mDiv_Id = div_id;
	this.mDg_Id = dg_id;
	this.mShow_Mode = show_mode;

	this.Add_Node = function(parent_node,code_id,code_name)
	{
		var node = new Tree_Node(this.Dict_Id,code_id,code_name,this.mComb_Id,this.mDiv_Id,this.mDg_Id,this.mShow_Mode);
		if(node == null) return null;

		if(parent_node == null)
			this.mNodes[this.mNodes.length] = node;
		else
			parent_node.Add_Child(node);

		return node;
	}

	this.Load_List = function(parent_node,xml_list)
	{
		var id;
		var node;
		var child_list;
		//alert(xml_list.length);
		for (id = 0; id < xml_list.length; id++)
		{
			if(xml_list[id].nodeName != "node") continue;

			node = this.Add_Node(parent_node,xml_list[id].getElementsByTagName("code_id")[0].childNodes[0].nodeValue,
				xml_list[id].getElementsByTagName("code_name")[0].childNodes[0].nodeValue);
			if(node == null) return 0;

			child_list = xml_list[id].childNodes;
			if(child_list.length > 2)
				this.Load_List(node,child_list);
			
		}
		
		return 1;
	}
	
	this.Write = function()
	{
		window.document.write(this.To_Str());
	}
	
	this.To_Str = function()
	{
		var html_str = "";
		
		html_str += "<div  style='  left: " + this.X + "; top: " + this.Y + "; width: " +
			this.mWidth + "; height: " + this.mHeight + 
			"; font-family: Arial; font-size: 13px; color:#1f7f9f;text-decoration: none; overflow:auto; zoom:1; position:absolute; ' >";
		
		for (var i = 0; i < this.mNodes.length; i++)
			html_str += this.mNodes[i].To_Str(0);
		html_str += "</div>";
		//alert(html_str);
		return html_str;
	}
}

function Show_Chg(code_id)
{
	//alert("Show_Chg");
	//alert(code_id);

	var div = eval("div_chd_" + code_id);

	if(div == null) return;
	if (div.style.display == "none")
		div.style.display = "block";
	else
		div.style.display = "none";

	var td = eval("td_" + code_id);
	
	if(td.innerText == "+")
		td.innerText = "-";
	else if(td.innerText == "-")
		td.innerText = "+";
}

function m_new_xmlhttp(){
var xmlHttp;
try { // Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e) {
try {// Internet Explorer
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
}
}
} 


return xmlHttp;
}


function Xml_Mgr(xml_file)
{
	var xml_http=null;
	var xml_doc=null;

	/*
	try
  {
  	xml_doc=new ActiveXObject("Microsoft.XMLDOM");
  }
	catch(e)
  {
  	try //Firefox, Mozilla, Opera, etc.
    {
    	xml_doc=document.implementation.createDocument("","",null);
    }
  	catch(e)
    {
    	alert(e.message);
    	return;
    }
  }
	xml_doc.async=false;
	xml_doc.load(xml_file);
*/

	xml_http = m_new_xmlhttp();

	//alert("a");
	
/*
	//alert("a");
	
	if (window.XMLHTTPRequest)  //window.xml_httpRequest)
  {
  	//xml_http = new xml_httpRequest();
  	xml_http = new XMLHTTPRequest();
  }
	else if (window.ActiveXObject)
  {
  	xml_http = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
	{
	 alert('你的浏览器不支持创建xmlHttp');
	}
*/

	if (xml_http == null) 
	{
		alert('创建xmlHttp失败');
		return null;
	};
	
  xml_http.open("POST", xml_file, false);
  //xml_http.setRequestHeader("Content-Type","text/xml");
  xml_http.send();
  if(xml_http.status != 200) return null;
	//alert("e");
	  //alert(xml_http.responseText);
  //alert(xml_http.getAllResponseHeaders());
  //alert(xml_http.responseXML.xml);
  xml_doc = xml_http.responseXML;
	
	//alert(xml_http.responseText);
	return xml_doc;
}



function Hint_Get_Position(ctrl, top_parent)
{
	var _top_parent=(top_parent)?top_parent:window.document.body;
	var x = ctrl.offsetLeft;
	var y = ctrl.offsetTop;
	var tmp_ctrl = ctrl.offsetParent;
	while ((tmp_ctrl!=_top_parent) && tmp_ctrl)
	{
		x += tmp_ctrl.offsetLeft + tmp_ctrl.clientLeft - tmp_ctrl.scrollLeft;
		y += tmp_ctrl.offsetTop + tmp_ctrl.clientTop - tmp_ctrl.scrollTop;
		tmp_ctrl = tmp_ctrl.offsetParent;
	}
	return ([x, y]);
}

function Hint_Show(ctrl_id,request_id)
{
	var ctrl = document.getElementById(ctrl_id);
	if(ctrl == null) return;
	var div_date_grid = null;
	var iframe_date_grid = null;
	var body = document.body;
	var html_str;
	var date_select_index = -1;
	var id,sid;
	var year;
	var month;
	var day;

	
	div_date_grid = document.getElementById("div_hint_" + ctrl_id);
	iframe_date_grid = document.getElementById("iframe_hint_" + ctrl_id);

	if(div_date_grid != null && iframe_date_grid != null && div_date_grid.style.display == "block")
	{
		div_date_grid.style.display = "none";
		iframe_date_grid.style.display = "none";
		return;
	}

	if(iframe_date_grid == null)
	{
		iframe_date_grid = document.createElement("iframe");
  	iframe_date_grid.id = "iframe_hint_" + ctrl_id;
    iframe_date_grid.style.position = "absolute";
    iframe_date_grid.style.display = "block";
    iframe_date_grid.frameBorder = 0;
    iframe_date_grid.style.backgroundcolor="white";
    iframe_date_grid.src = "about:";
  	body.appendChild(iframe_date_grid);
	}

	if(div_date_grid == null)
	{
		div_date_grid = document.createElement("div");

		div_date_grid.style.background = "#e0ecf9";
		div_date_grid.style.width = "300px";
		div_date_grid.style.height = "260px";
		div_date_grid.setAttribute("id","div_hint_" + ctrl_id);
		div_date_grid.style.position="absolute";

		//alert("1");

		var ccf = new Xml_Mgr(request_id);
		var hint = ccf.getElementsByTagName("hint");
		
		//alert(ccf);
		//alert(hint[0].childNodes[0].nodeValue);
		
		html_str = hint[0].childNodes[0].nodeValue;

		div_date_grid.innerHTML = html_str;
    body.appendChild(div_date_grid);
	}

	var pos = Hint_Get_Position(ctrl);
	
	div_date_grid.style.left = pos[0] + "px";
	div_date_grid.style.top = pos[1] + ctrl.offsetHeight + "px";
	iframe_date_grid.style.left = div_date_grid.style.left;
  iframe_date_grid.style.top = div_date_grid.style.top;
  iframe_date_grid.style.height = div_date_grid.style.height;
  iframe_date_grid.style.width = div_date_grid.style.width;

	div_date_grid.style.display = "block";
	iframe_date_grid.style.display = "block";

}
