﻿// JavaScript Document
var tdid="zone";
var currProvince="0";//当前省份
//var arrayOptions=new Array();//options的所有选项
var valueEqtext=false;
var provinceName="p";
var styleCss="";
var cityName="c";
var opHasBlank=true;
var opBlankText="-不限-";
var opBlankValue="0";
var selectedProvinceValue;
var selectedCityValue;
/**
*provinceElementName 省份的的表单名
*cityElementName 城市的表单名
*valueEtest 列表框的value是否等于text
*css 样式
*hasBlank 是滞有空白选项
*blankText 空白选项
*blankValue 空白选项的值
**/
function createLocalZone(id,provinceElementName,cityElementName,valueEtext,css,hasBlank,blankText,blankValue,provinceValue,cityValue)
{
//以下设置全局变量
valueEqtext=valueEtext;
provinceName=provinceElementName;
//cityName=cityElementName;
styleCss=css;
tdid=id;
opHasBlank=hasBlank;
opBlankText=blankText;
opBlankValue=blankValue;
selectedProvinceValue=provinceValue;
selectedCityValue=cityValue;
//以上设置全局变量
//createTable(id);
//sendRequest(0);//发送请求，读取xml文件
var xmlDoc=importXML("/xml/zoneLayer_zh-CN.xml");
//alert(xmlDoc);
readProvinceOptions(xmlDoc,cityElementName,id);
resetZone(xmlDoc,cityElementName,id,provinceName,opHasBlank,opBlankText,opBlankValue,valueEtext);
//alert(tdid);
}
/**
*列出省份
**/
function readProvinceOptions(xmlDoc,cityFormName,id)
{
var parent=xmlDoc.getElementsByTagName("Parent")[0];
document.write("<select name=\""+provinceName+"\" id=\""+provinceName+"\" onChange=\"javascript:sendRequest(\'"+cityFormName+"\',\'"+id+"\',\'"+provinceName+"\',"+opHasBlank+",\'"+opBlankText+"\',\'"+opBlankValue+"\',"+valueEqtext+");\" class=\""+styleCss+"\">");
document.write("</select>");
//var province=document.createElement("SELECT");
var province=document.getElementById(provinceName);
//province.setAttribute("name",provinceName);
//province.setAttribute("id",provinceName);
//province.setAttribute("onChange","javascript:sendRequest(\'"+cityFormName+"\',\'"+id+"\',\'"+provinceName+"\',"+opHasBlank+",\'"+opBlankText+"\',\'"+opBlankValue+"\');");
//province.onchange=function(){sendRequest(cityFormName,id,provinceName,opHasBlank,opBlankText,opBlankValue);};
//province.className=styleCss;
var o;
var arrayOptions=parent.getElementsByTagName("L1");
//alert(opHasBlank);
if(opHasBlank)
{
	o=document.createElement("option");
	o.setAttribute("value",opBlankValue);
	o.appendChild(document.createTextNode(opBlankText));
	province.appendChild(o);
}
for(i=0;i<arrayOptions.length;i++)
{
	o=document.createElement("option");
	if(valueEqtext)
		{
			o.setAttribute("value",arrayOptions[i].getAttribute("value"));
		}
	else
		{
			o.setAttribute("value",arrayOptions[i].getAttribute("id"));
		}
	o.appendChild(document.createTextNode(arrayOptions[i].getAttribute("value")));
	if(selectedProvinceValue==o.getAttribute("value"))
	{
		//o.selected="true";
		o.setAttribute("selected","true");
	}
	province.appendChild(o);
}
document.getElementById(tdid).appendChild(province);
}
/**
*列出地级市
**/
function readCityOptions()
{
if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
			{
				var xml=xmlHttp.responseXML;
				listCitys(xml);
    	   }
    }
}

function sendRequest(cityFormName,id,provinceName,hasBlank,blankText,blankValue,valueEtext)
{
	createXmlHttp();
	currProvince=document.getElementById(provinceName).options[document.getElementById(provinceName).selectedIndex].value;
	//alert(currProvince);
	cityName=cityFormName;
	tdid=id;
	opHasBlank=hasBlank;
	opBlankText=blankText;
	opBlankValue=blankValue;
	valueEqtext=valueEtext;
	xmlHttp.onreadystatechange = readCityOptions;
    xmlHttp.open("GET", "/xml/zoneLayer_zh-CN.xml?ran="+Math.random(), true);
    xmlHttp.send(null);
}

function importXML(fileName)
{	
	var xmlDoc;
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
	}
	else if (window.ActiveXObject)
	{
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return null;
	}
	xmlDoc.async = false;
	xmlDoc.resolveExternals = false;
	xmlDoc.validateOnParse = false;
	xmlDoc.preserveWhiteSpace = true;
	xmlDoc.load(fileName);
	return xmlDoc;
}
function listCitys(xml)
{
				var arrayOptions=xml.getElementsByTagName("L1");
				//alert(arrayOptions.length);
				var city;
				if(document.getElementById(cityName)==null)
				{
					city=document.createElement("select");
					city.setAttribute("name",cityName);
					city.setAttribute("id",cityName);
					city.className=styleCss;
				}
				else 
				{
					var city=document.getElementById(cityName);
					while(city.hasChildNodes())
					{
						city.remove(0);
					}
				}
				var o;
				if(opHasBlank)
				{
					o=document.createElement("option");
					o.setAttribute("value",opBlankValue);
					o.appendChild(document.createTextNode(opBlankText));
					city.appendChild(o);
				}
				//alert(valueEqtext);
				if(valueEqtext)
				{
					//alert(arrayOptions.length);
					for(i=0;i<arrayOptions.length;i++)
					{
						if(arrayOptions[i].getAttribute("value")==currProvince)
						{
							var citys=arrayOptions[i].getElementsByTagName("L2");
							for(j=0;j<citys.length;j++)
							{
								o=document.createElement("option");
								o.setAttribute("value",citys[j].getAttribute("value"));
								o.appendChild(document.createTextNode(citys[j].getAttribute("value")));
								if(selectedCityValue==o.getAttribute("value"))
								{
									o.setAttribute("selected","true");
								}
								city.appendChild(o);
							}
							break;
						}
					}
				}
				else 
				{
					//alert(arrayOptions.length);
					for(i=0;i<arrayOptions.length;i++)
					{
						if(arrayOptions[i].getAttribute("id")==currProvince)
						{
							var citys=arrayOptions[i].getElementsByTagName("L2");
							for(j=0;j<citys.length;j++)
							{
								o=document.createElement("option");
								o.setAttribute("value",citys[j].getAttribute("id"));
								o.appendChild(document.createTextNode(citys[j].getAttribute("value")));
								//alert(selectedCityValue+":"+o.getAttribute("value"));
								if(selectedCityValue==o.getAttribute("value"))
								{
									o.setAttribute("selected","true");
								}
								city.appendChild(o);
							}
							break;
						}
					}
				}
				document.getElementById(tdid).appendChild(city);
}
/**
*重置地区值
**/
function resetZone(xml,cityFormName,id,provinceName,hasBlank,blankText,blankValue,valueEtext)
{
if(document.getElementById(provinceName).type=="select-one"&&selectedProvinceValue!=null&&selectedCityValue!=null)
{
	currProvince=selectedProvinceValue;
	cityName=cityFormName;
	tdid=id;
	opHasBlank=hasBlank;
	opBlankText=blankText;
	opBlankValue=blankValue;
	valueEqtext=valueEtext;
	listCitys(xml)
	//sendRequest(cityFormName,id,provinceName,hasBlank,blankText,blankValue);
}
}