var isIE = true;
switch (navigator.appName) {
	case ("Microsoft Internet Explorer"):
		isIE=true;
		break;
	case ("Netscape"):
		isIE=false;
		break;
	default:
		isIE=false;
		break;
}
function Load_JS(scriptID,file){
	var scriptTag = document.getElementById(scriptID);
	var head = document.getElementsByTagName('head').item(0)
	if (scriptTag) {
		scriptTag.parentNode.removeChild(scriptTag);
	}
	var script = document.createElement('script');
	script.src = file+"&random="+Math.random();
	script.type = 'text/javascript';
	script.charset = "UTF-8";
	script.id = scriptID;
	head.appendChild(script);
}
var glb_switch = true;

function SelectCity(obj,pSelectProvinceObj,pSelectCityObj,pSelectDepareaObj,pSelectSpotAreaObj,pSelectCityMsgObj) {
	this.DefaultStr = "请选择..."; //默认文字"不限"或者"请选择"
	this.StartDeep = 1; //从哪个开始
	this.EndDeep = 4; //到哪个结束
	this.isView = true; //是否一开始就全部显示出来,还是逐个显示出来
	this.isPrivilege = false; //当“省”选择直辖市时是否显示区
	
	this.url = "http://menu.jinti.com";
	
	this.obj = obj;
	this.SelectProvinceObj = pSelectProvinceObj;
	this.SelectCityObj = pSelectCityObj;
	this.SelectDepareaObj = pSelectDepareaObj;
	this.SelectSpotAreaObj = pSelectSpotAreaObj;
	this.SelectCityMsg = pSelectCityMsgObj;
	
	this.DefaultCountry = 1;
	this.DefaultProvince = "";
	this.DefaultCity = "";
	this.DefaultDeparea = "";
	this.DefaultSpotArea = "";
	
	this.data = new Array();
	this.value = new Array();
	this.text = new Array();
	
	this.Initialize = function() {
		this.StartDeep = parseInt(this.StartDeep);
		this.EndDeep = parseInt(this.EndDeep);
		if (this.StartDeep < 1) this.StartDeep = 1;
		if (this.StartDeep > 4) this.StartDeep = 4;
		if (this.EndDeep < 1) this.EndDeep = 1;
		if (this.EndDeep > 4) this.EndDeep = 4;
		if (this.EndDeep < this.StartDeep) {
			var tmp = this.StartDeep;
			this.StartDeep = this.EndDeep;
			this.EndDeep = tmp;
		}
	}
	
	this.ClearDefault = function() {
		this.DefaultProvince = "";
		this.DefaultCity = "";
		this.DefaultDeparea = "";
		this.DefaultSpotArea = "";
		this.SetValue();
		if (document.getElementById(this.SelectCityMsg)) {
			document.getElementById(this.SelectCityMsg).innerHTML = "";
		}		
	}
	
	this.SetValue = function() {
		this.value = new Array();
		this.text = new Array();		
		if (document.getElementById("Select_Province")) {
			this.value.push(document.getElementById("Select_Province").value);
			this.text.push(document.getElementById("Select_Province").options[document.getElementById("Select_Province").selectedIndex].text);
		}
		else {
			this.value.push(0);
			this.text.push(this.DefaultStr);
		}
		if (document.getElementById("Select_City")) {
			this.value.push(document.getElementById("Select_City").value);
			this.text.push(document.getElementById("Select_City").options[document.getElementById("Select_City").selectedIndex].text);
		}
		else {
			this.value.push(0);
			this.text.push(this.DefaultStr);
		}
		if (document.getElementById("Select_Deparea")) {
			this.value.push(document.getElementById("Select_Deparea").value);
			this.text.push(document.getElementById("Select_Deparea").options[document.getElementById("Select_Deparea").selectedIndex].text);
		}
		else {
			this.value.push(0);
			this.text.push(this.DefaultStr);
		}
		if (document.getElementById("Select_SpotArea")) {
			this.value.push(document.getElementById("Select_SpotArea").value);
			this.text.push(document.getElementById("Select_SpotArea").options[document.getElementById("Select_SpotArea").selectedIndex].text);
		}
		else {
			this.value.push(0);
			this.text.push(this.DefaultStr);
		}
	}
	
	this.List = function(DefaultProvince,DefaultCity,DefaultDeparea,DefaultSpotArea) {
		this.Initialize();
		if (this.StartDeep == 1) this.ListProvince(DefaultProvince,DefaultCity,DefaultDeparea,DefaultSpotArea);
		if (this.StartDeep == 2) {
			if (DefaultCity) this.DefaultCity = DefaultCity;
			if (DefaultDeparea) this.DefaultDeparea = DefaultDeparea;
			if (DefaultSpotArea) this.DefaultSpotArea = DefaultSpotArea;
			this.ListCity(-1);
		}
		if (this.StartDeep == 3) {
			if (DefaultDeparea) this.DefaultDeparea = DefaultDeparea;
			if (DefaultSpotArea) this.DefaultSpotArea = DefaultSpotArea;
			this.ListDeparea(-1);
		}
		if (this.StartDeep == 4) {
			this.ListSpotArea(-1);
			if (DefaultSpotArea) this.DefaultSpotArea = DefaultSpotArea;
		}
	}
	
	this.ListProvince = function(DefaultProvince,DefaultCity,DefaultDeparea,DefaultSpotArea) {
		this.Initialize();
		if (this.StartDeep > 1) return;
		if (DefaultProvince) this.DefaultProvince = DefaultProvince;
		if (DefaultCity) this.DefaultCity = DefaultCity;
		if (DefaultDeparea) this.DefaultDeparea = DefaultDeparea;
		if (DefaultSpotArea) this.DefaultSpotArea = DefaultSpotArea;
		if (this.isView) {
			if (this.EndDeep > 0) document.getElementById(this.SelectProvinceObj).innerHTML = '<select id="'+this.obj+'_Select_Province" name="Select_Province"><option value="0">'+this.DefaultStr+'</option></select>';
			if (this.EndDeep > 1) document.getElementById(this.SelectCityObj).innerHTML = '<select id="'+this.obj+'_Select_City" name="Select_City"><option value="0">'+this.DefaultStr+'</option></select>';
			if (this.EndDeep > 2) document.getElementById(this.SelectDepareaObj).innerHTML = '<select id="'+this.obj+'_Select_Deparea" name="Select_Deparea"><option value="0">'+this.DefaultStr+'</option></select>';
			if (this.EndDeep > 3) document.getElementById(this.SelectSpotAreaObj).innerHTML = '<select id="'+this.obj+'_Select_SpotArea" name="Select_SpotArea"><option value="0">'+this.DefaultStr+'</option></select>';
		}
		if (document.getElementById(this.SelectCityMsg)) {
			document.getElementById(this.SelectCityMsg).innerHTML = "正在获取省市列表...";
		}
		Load_JS(this.obj+"_JS_SelectProvince", this.url + "/SelectCity/SelectProvince.aspx?obj="+this.obj+"&countryid="+this.DefaultCountry);
	}
	this.CallBack_ListProvince = function() {
		var onchange = "";
		if (this.EndDeep > 1) onchange = 'onchange="' + this.obj + '.ListCity(this.value);"';
		else onchange = 'onchange="' + this.obj + '.SetValue();"';
		var str = '<select id="'+this.obj+'_Select_Province" name="Select_Province" ' + onchange + '><option value="0">'+this.DefaultStr+'</option>';
		for (var i=0; i < this.data.length; i++) {
			str += '<option value="' + this.data[i][0] + '">' + this.data[i][1] + '</option>';
		}
		str += '</select>';
		document.getElementById(this.SelectProvinceObj).innerHTML = str;
		if (this.DefaultProvince != "") {
			var tmpObj = document.getElementById(this.obj+"_Select_Province");
			if ((typeof this.DefaultProvince) == "number") {
				tmpObj.value = this.DefaultProvince;
				if (tmpObj.selectedIndex==-1) tmpObj.value=0; 
			}
			else {
				for (var i=0; i < tmpObj.options.length; i++) {
					if (tmpObj.options[i].text==this.DefaultProvince) {
						tmpObj.value = tmpObj.options[i].value;
					}
				}
			}
			if (tmpObj.value != 0 && this.EndDeep > 1) {
				this.ListCity(tmpObj.value);
			}
			else {
				this.ClearDefault();
			}
		}
		else {
			this.ClearDefault();
		}
	}
	
	this.ListCity = function(provinceid) {
		this.Initialize();
		if (this.StartDeep > 2) return;
		if (this.isView) {
			if (this.EndDeep > 2) document.getElementById(this.SelectDepareaObj).innerHTML = '<select id="'+this.obj+'_Select_Deparea" name="Select_Deparea"><option value="0">'+this.DefaultStr+'</option></select>';
			if (this.EndDeep > 3) document.getElementById(this.SelectSpotAreaObj).innerHTML = '<select id="'+this.obj+'_Select_SpotArea" name="Select_SpotArea"><option value="0">'+this.DefaultStr+'</option></select>';
		}
		if (document.getElementById(this.SelectCityMsg)) {
			document.getElementById(this.SelectCityMsg).innerHTML = "正在获取城市列表...";
		}
		var strPrivilege = "";
		if (this.isPrivilege) strPrivilege="&Province_Is_Privilege=1";
		Load_JS(this.obj+"_JS_SelectCity_"+parseInt(provinceid), this.url + "/SelectCity/SelectCity.aspx?obj="+this.obj+"&provinceid="+parseInt(provinceid)+"&countryid="+this.DefaultCountry+strPrivilege);
	}
	this.CallBack_ListCity = function() {
		var onchange = "";
		if (this.EndDeep > 2) onchange = 'onchange="' + this.obj + '.ListDeparea(this.value);"';
		else onchange = 'onchange="' + this.obj + '.SetValue();"';
		var str = '<select id="'+this.obj+'_Select_City" name="Select_City" ' + onchange + '><option value="0">'+this.DefaultStr+'</option>';
		for (var i=0; i < this.data.length; i++) {
			str += '<option value="' + this.data[i][0] + '">' + this.data[i][1] + '</option>';
		}
		str += '</select>';
		document.getElementById(this.SelectCityObj).innerHTML = str;
		if (this.DefaultCity != "") {
			var tmpObj = document.getElementById(this.obj+"_Select_City");
			if ((typeof this.DefaultCity) == "number") {
				tmpObj.value = this.DefaultCity;
				if (tmpObj.selectedIndex==-1) tmpObj.value=0;
			}
			else {
				for (var i=0; i < tmpObj.options.length; i++) {
					if (tmpObj.options[i].text==this.DefaultCity) {
						tmpObj.value = tmpObj.options[i].value;
					}
				}
			}
			if (tmpObj.value != 0 && this.EndDeep > 2) {
				this.ListDeparea(tmpObj.value);
			}
			else {
				this.ClearDefault();
			}
		}
		else {
			this.ClearDefault();
		}		
	}
	
	this.ListDeparea = function(cityid) {
		this.Initialize();
		if (this.StartDeep > 3) return;
		if (this.isView) {
			if (this.EndDeep > 3) document.getElementById(this.SelectSpotAreaObj).innerHTML = '<select id="'+this.obj+'_Select_SpotArea" name="Select_SpotArea"><option value="0">'+this.DefaultStr+'</option></select>';
		}
		if (document.getElementById(this.SelectCityMsg)) {
			document.getElementById(this.SelectCityMsg).innerHTML = "正在获取区县列表...";
		}
		Load_JS(this.obj+"_JS_SelectDeparea_"+parseInt(cityid), this.url + "/SelectCity/SelectDeparea.aspx?obj="+this.obj+"&cityid="+parseInt(cityid));
	}
	this.CallBack_ListDeparea = function() {
		var onchange = "";
		if (this.EndDeep > 3) onchange = 'onchange="' + this.obj + '.ListSpotArea(this.value);"';
		else onchange = 'onchange="' + this.obj + '.SetValue();"';
		var str = '<select id="'+this.obj+'_Select_Deparea" name="Select_Deparea" ' + onchange + '><option value="0">'+this.DefaultStr+'</option>';
		for (var i=0; i < this.data.length; i++) {
			str += '<option value="' + this.data[i][0] + '">' + this.data[i][1] + '</option>';
		}
		str += '</select>';
		document.getElementById(this.SelectDepareaObj).innerHTML = str;
		if (this.DefaultDeparea != "") {
			var tmpObj = document.getElementById(this.obj+"_Select_Deparea");
			if ((typeof this.DefaultDeparea) == "number") {
				tmpObj.value = this.DefaultDeparea;
				if (tmpObj.selectedIndex==-1) tmpObj.value=0;
			}
			else {
				for (var i=0; i < tmpObj.options.length; i++) {
					if (tmpObj.options[i].text==this.DefaultDeparea) {
						tmpObj.value = tmpObj.options[i].value;
					}
				}
			}
			if (tmpObj.value != 0 && this.EndDeep > 3) {
				this.ListSpotArea(tmpObj.value);
			}
			else {
				this.ClearDefault();
			}
		}
		else {
			this.ClearDefault();
		}		
	}
	
	this.ListSpotArea = function(depareaid) {
		this.Initialize();
		if (document.getElementById(this.SelectCityMsg)) {
			document.getElementById(this.SelectCityMsg).innerHTML = "正在获取地段列表...";
		}		
		Load_JS(this.obj+"_JS_SelectSpotArea_"+parseInt(depareaid), this.url + "/SelectCity/SelectSpotArea.aspx?obj="+this.obj+"&depareaid="+parseInt(depareaid));
	}
	this.CallBack_ListSpotArea = function() {
		var str = '<select id="'+this.obj+'_Select_SpotArea" name="Select_SpotArea" onchange="' + this.obj + '.SetValue();"><option value="0">'+this.DefaultStr+'</option>';
		for (var i=0; i < this.data.length; i++) {
			str += '<option value="' + this.data[i][0] + '">' + this.data[i][1] + '</option>';
		}
		str += '</select>';
		document.getElementById(this.SelectSpotAreaObj).innerHTML = str;
		if (this.DefaultSpotArea != "") {
			var tmpObj = document.getElementById(this.obj+"_Select_SpotArea");
			if ((typeof this.DefaultSpotArea) == "number") {
				tmpObj.value = this.DefaultSpotArea;
				if (tmpObj.selectedIndex==-1) tmpObj.value=0;
			}
			else {
				for (var i=0; i < tmpObj.options.length; i++) {
					if (tmpObj.options[i].text==this.DefaultSpotArea) {
						tmpObj.value = tmpObj.options[i].value;
					}
				}
			}
		}
		this.ClearDefault();
	}
}