var posx=0;
var posy=0;
var option_posx = (window.screen.availWidth-580)/2;

document.onmousemove = mouseMove;
function mouseMove(ev)
{
	ev  = ev || window.event;
	var mousePos = mouseCoords(ev);
	posx = mousePos.x;
	posy = mousePos.y + 5 ;
}

function mouseCoords(ev)
{
	if(ev.pageX || ev.pageY)
	{
		return {x:ev.pageX, y:ev.pageY};
	}
	return  {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		};
}


var oTimeout;
var boolerr=false;
var boolreq=false;
var a = navigator.userAgent.toLowerCase();
if (typeof(HTMLElement)!="undefined") {
      //HTMLElement.prototype.__defineGetter__("currentStyle", function() { return document.defaultView.getComputedStyle(this, null); });
}
var option_flag=false; //option_flag=true为单选，选择完毕就关闭窗口
var option_obj=null;
var obj_value=null;
var max_selected = 3;
var error_message="选择的个数超过了规定的个数了！";

function activateKey(cat,o_text,o_value,maxselect,errormsg,f)
{
	//x=encodeURI(obj_text);
	//x=URLEncoding(obj_text);
	//x=encodeURIComponent(o_text);
	//x=escape(o_text);
	option_obj=o_text;
	obj_value=o_value;
	if (maxselect !=null)max_selected=maxselect;
	if (errormsg !=null)error_message=errormsg;
	option_flag=f;
	if (option_flag)max_selected=1;
	if(boolreq) return false;
	if (!document.getElementById) return true;
	RemoveOpenResults();
	yqFetchResults(cat,o_text);
	//return false;
}
function doit(cat)
{
	var cbo = new CallBackObject();			
	cbo.OnComplete = Cbo_Complete;	
	var url = "/net/info/callback.aspx?cat=" + cat; // +"&arg=" + x ;
	cbo.DoCallBack(url);
}
function Cbo_Complete(responseText, responseXML)
{
	if (responseText !=null && responseText.indexOf('<script>alert')!=-1)
	{
		document.write(responseText);
		return;
	}
	removeLoading();
	var oResults = document.getElementById('Xmrcresults');
	if (oResults  && !boolerr) 
	{ 
   		var oResultsFrame = document.createElement('div');
       	 	oResultsFrame.id = 'topmenu'; //'Xmrcframe';
		if (responseXML !=null)
		{
			var ds = responseXML.getElementsByTagName("info");
			if (ds.length==0)
			{
				oResultsFrame.innerHTML="很抱歉，没有找到!!";			
			}
			else
			{
				var id="";
				var name="";
				for (var i=0; i < ds.length; i++ )
				{
					info=ds[i];
					id = info.getElementsByTagName("ID")[0].firstChild.nodeValue;	
					name = info.getElementsByTagName("NAME_CN")[0].firstChild.nodeValue;

					var li = document.createElement("li");
					li.id = "li_" + id;

					var c = document.createElement("input");
					c.id = "check" + id;
					c.setAttribute("type","checkbox");
					c.setAttribute("value",name);
					//c.disabled=true;
					c.onclick=function(){checkSelected0(this.id,this.value);};
					//c.setAttribute("onclick","checkSelected\('" + id + "','" + name + "'\)");
					li.appendChild(c);

					var a = document.createElement("a");
					a.href = "javascript:checkSelected2('" + id + "','" + name + "');";
					a.innerHTML = name;
					li.appendChild(a);
					oResultsFrame.appendChild(li);
				}
			}
		}
		else
		{
			oResultsFrame.innerHTML="很抱歉，取数据出错了，没有返回结果!!";
		}
        	oResults.appendChild(oResultsFrame);
		setSelectedChkbox();
		boolreq = !boolreq;
	}	
}
function ResetObj()
{
	if (option_obj!=null)document.getElementById(option_obj).value="";
	if (obj_value!=null)document.getElementById(obj_value).value="";
	clearSelectedChkbox();
}
function removeLoading()
{
	var oLoading = document.getElementById('Xmrcloading');
	if (oLoading  && !boolerr)
	{
    		var oLoadingParent = oLoading.parentNode;
    		oLoadingParent.removeChild(oLoading);
	}
}
function yqFetchResults(cat) 
{
	var oResult		= document.createElement('div');
	oResult.id		= 'Xmrcresult';
	oResult.className	= 'Xmrcresult';
	oResult.className +=' Xmrcresultinl';
	oResult.style.left= option_posx ;
	oResult.style.top=  posy+10 ;
	oResult.style.visibility="visible";


	var allSelect = document.getElementsByTagName('select');
	for(var i = 0; i< allSelect.length; i++)
        	allSelect[i].style.visibility = 'hidden';

	
	document.getElementsByTagName("body").item(0).appendChild(oResult);
	var oResultIn		= document.createElement('div');
	oResultIn.id		= 'Xmrcresultin';
	oResultIn.className	= 'Xmrcresultin';

	var sA		= '<img src="" width="30" height="0"><a href="" onclick="RemoveOpenResults(); boolreq=false;  return false" class=a2><b>选择完成后请关闭窗口</b></a>';
	var sC		= '<a href="" onclick="RemoveOpenResults(); boolreq=false;  return false" class=but><img src="/net/js/images/closewin.gif" height=14 width=14 border=0 alt="关闭窗口"></a>';
	var sT		= '<h3 id=XmrcQueryHead class=drag>' + sC + ' 请选择 <img src="" height=0 width=50><a href="javascript:ResetObj()"><u>清空选择的数据</u></a><font color=#ff0000><b>(可选择<font color=#0000ff>' + max_selected +'</font>项)</b></font>' + sA +'</h3>\n';
	var sCBtn	= '<div id=Xmrcresultclose><a href=# class=help>？</a> ' + sC + '</div>\n';
	var sL		= '<div id=Xmrcloading class=Xmrcloading><p>请稍侯，正在读取数据中 . . .</p></div>\n';
	var sRs		= '<div id=Xmrcresults class=Xmrcresults></div>\n';	
	if(a.indexOf('safari')=='-1' || 1==1){
		oResultIn.innerHTML	= sT + sCBtn + sL + sRs ;
		oResult.appendChild(oResultIn);
		if (boolreq) return false;
		doit(cat);
		boolreq = !boolreq;
	} 
	oTimeout = setTimeout('CheckForResults()',20000);
	return false;
}
function RemoveOpenResults() 
{
	var oResults = document.getElementById('Xmrcresult');
	if (oResults) {
		var oOpenForm = oResults.parentNode;
		oOpenForm.removeChild(oResults);
		window.clearTimeout(oTimeout);		
	}
	var allSelect = document.getElementsByTagName('select');
	for(var i = 0; i< allSelect.length; i++)
        	allSelect[i].style.visibility = 'visible';
}
function CheckForResults() 
{
	var oLoading = document.getElementById('Xmrcloading');
	if (oLoading)
	{
		boolerr=true;
		boolreq = !boolreq;
		oLoading.innerHTML = '<p class=error>连接超时, 请<a href="#" onclick="RemoveOpenResults(); boolreq=false; return false;" >关闭窗口</a>再试一次.</p>';
	}
}	
function clearSelectedChkbox()
{
	var oResults = document.getElementById('Xmrcresults');
	if (oResults !=null)
	{ 
        	var all = document.getElementById('Xmrcresults').getElementsByTagName("input");
        	for(var i=0; i<all.length; i++)
		{
            		var chk = all[i];
            		if (chk.type == 'checkbox' && chk.checked)
                		chk.checked = false;
		}
	}
}
function setSelectedChkbox()
{
	var oResults = document.getElementById('Xmrcresults');
	if (oResults !=null)
	{ 
		var v=document.getElementById(option_obj).value;
        	var all = document.getElementById('Xmrcresults').getElementsByTagName("input");
        	for(var i=0; i<all.length; i++)
		{
            		var chk = all[i];
            		if (chk.type == 'checkbox' && v.indexOf(chk.value)!=-1)
                		chk.checked = true;
		}
	}	
}
function checkSelected0(id,Name)
{
	if (id==null)return;
	var ID = id.substr(5);
	checkSelected(ID,Name);
}
function checkSelected(ID,Name)
{
	if (option_flag)
	{
		document.getElementById("check" + ID).checked=true;
		if (option_obj!=null)document.getElementById(option_obj).value=Name;
		if (obj_value!=null)document.getElementById(obj_value).value=ID;
		RemoveOpenResults();
		return;
	}
	readdata();
	if (option_obj==null)return;
	var o=document.getElementById(option_obj);
	var v=o.value;
	var selectedIDs = v.split(',');
	var chk = document.getElementById("check" + ID);
	if (selectedIDs.length > max_selected )
	{
		chk.checked=false;
		readdata();
		alert(error_message + "\n\n如果要重新选择，请先清空所选择的数据再重新选择");
		return;
	}
}
function checkSelected2(ID,Name)
{
	if (option_flag)
	{
		document.getElementById("check" + ID).checked=true;
		if (option_obj!=null)document.getElementById(option_obj).value=Name;
		if (obj_value!=null)document.getElementById(obj_value).value=ID;
		RemoveOpenResults();
		return;
	}
	if (option_obj==null)return;
	var o=document.getElementById(option_obj);
	var v=o.value;
	var selectedIDs = v.split(',');
	var chk = document.getElementById("check" + ID);
	if (selectedIDs.length >= max_selected)
	{
		if (v.indexOf(Name)!=-1)
		{
			chk.checked=false;
			readdata();
		}
		else
		{
			alert(error_message + "\n\n如果要重新选择，请先清空所选择的数据再重新选择");
		}
		return;
	}
	if (v.indexOf(Name)==-1)
		chk.checked=true;
	else
		chk.checked=false;

	readdata();
}
function readdata()
{
	var s="";
	var v="";
        var all = document.getElementById('Xmrcresults').getElementsByTagName("input");
        for(var i=0; i<all.length; i++)
	{
            	var chk = all[i];
            	if (chk.type == 'checkbox' && chk.checked)
                {
			if (s !="")s +=",";
			s +=chk.value;
			if (v !="")v +=",";
			v +=chk.id.substr(5);
		}
	}
	if (option_obj!=null)document.getElementById(option_obj).value = s;	
	if (obj_value!=null)document.getElementById(obj_value).value = v;
}
var b="<link rel=STYLESHEET type=text/css href='/net/js/option.css'>\n";
document.write(b);

