
function Dict_Select(comb_id,dict_id,mode)
{
	//alert("0");
	var comb;
	var show_mode;
	var div_id = "dict_" + comb_id + "_div";
	var div = document.getElementById(div_id);
	var body = document.body;
	var iframe_tmp = document.getElementById("iframe_tmp");
	var dg_id = "db_" + dict_id;
	var dg_html;
	
	show_mode = mode;
	
	comb = document.getElementById(comb_id);
	if(comb == null) return 0;
	
	if(iframe_tmp == null)
	{
		iframe_tmp = document.createElement("iframe");
  	iframe_tmp.id = "iframe_tmp";
    iframe_tmp.style.position = "absolute";
    iframe_tmp.style.display = "block";
    iframe_tmp.frameBorder = 0;
    iframe_tmp.style.backgroundcolor="white";
    iframe_tmp.src = "about:";
  	body.appendChild(iframe_tmp);
	}

	if(div != null)
	{
		div.style.display = "block";
		iframe_tmp.style.display = "block";

		iframe_tmp.style.left = div.style.left;
  	iframe_tmp.style.top = div.style.top;
  	iframe_tmp.style.height = div.style.height;
  	iframe_tmp.style.width = div.style.width;

		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.style.left = div.style.left;
  iframe_tmp.style.top = div.style.top;
  iframe_tmp.style.height = div.style.height;
  iframe_tmp.style.width = div.style.width;
  iframe_tmp.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='关闭窗口' style='font-family: Arial; font-size: 12px; color:0;text-decoration: none; ' onclick='Close_Select(";
    dg_html += "\"" + div_id + "\")'>";
    dg_html += "</div>";

    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;'>编号</td>" +
			"<td bgcolor='#5a75ce' style='text-align:center;font-family:Arial;font-size:10pt;color:#ffffff;'>名称</td>" +
			"</tr>";
			
		var row_list = dbio.getElementsByTagName("row");
		for (var id = 0; id < row_list.length; id++)
		{
			dbio_row = row_list[id].childNodes;

			dg_html += "<tr height='22' ondblclick=\"Set_Comb_Val('" + dbio_row[0].childNodes[0].nodeValue 
				+ "','" + comb_id + "','" + div_id + "')\" bgColor='";
			if(id%2 == 0)
				dg_html += "#f7f8f9'>";
			else
				dg_html += "#ffffff'>";

			dg_html += "<td>" + dbio_row[0].childNodes[0].nodeValue + "</td>";
			dg_html += "<td>" + dbio_row[1].childNodes[0].nodeValue + "</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='关闭窗口' style='font-family: Arial; font-size: 12px; color:0;text-decoration: none; ' onclick='Close_Select(";
    dg_html += "\"" + div_id + "\")'>";
    dg_html += "</div>";

    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='关闭窗口' style='font-family: Arial; font-size: 12px; color:0;text-decoration: none; ' onclick='Close_Select(";
    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;'>编号</td>" +
			"<td width=200 bgcolor='#5a75ce' style='text-align:center;font-family:Arial;font-size:10pt;color:#ffffff;'>名称</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 = document.getElementById("iframe_tmp");
	
	if(div == null || comb == null || iframe_tmp == 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.style.display = "none";
}

function Close_Select(div_id)
{
	var iframe_tmp = document.getElementById("iframe_tmp");
	var div = document.getElementById(div_id);

  //alert(div_id);
	if(iframe_tmp == null) return;
	if(div == null) return;
  //alert("cccc");

	div.style.display = "none";
	iframe_tmp.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");
	for (var id = 0; id < row_list.length; id++)
	{
		dbio_row = row_list[id].childNodes;
		
		dg_html += "<tr height='22' ondblclick=\"Set_Comb_Val('" + dbio_row[0].childNodes[0].nodeValue 
			+ "','" + comb_id + "','" + div_id + "')\" bgColor='";
		if(id%2 == 0)
			dg_html += "#f7f8f9'>";
		else
			dg_html += "#ffffff'>";
				
		dg_html += "<td>" + dbio_row[0].childNodes[0].nodeValue + "</td>";
		dg_html += "<td>" + dbio_row[1].childNodes[0].nodeValue + "</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 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);
*/
	
	if (window.xml_httpRequest)
  {
  	xml_http = new xml_httpRequest();
  }
	else if (window.ActiveXObject)
  {
  	xml_http = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
	{
	 alert('你的浏览器不支持创建xmlHttp');
	}

	if (xml_http == null) 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(xml_http.responseText);
  //alert(xml_http.getAllResponseHeaders());
  //alert(xml_http.responseXML.xml);
  xml_doc = xml_http.responseXML;
	
	return xml_doc;
}
