﻿/**
 * @author prcvictim
 * Import: zXml.js
 */
SelectCity = function () {this.initialize.apply(this, arguments);};
SelectCity.prototype = {
	initialize: function (arg) {
	    this.IS_FIREFOX = (typeof ActiveXObject == "undefined");
	    this.CountryEl  = document.getElementById(arg.CountryEl);
	    this.ProvinceEl = document.getElementById(arg.ProvinceEl);
	    this.CityEl     = document.getElementById(arg.CityEl);
	    this.CountryOnChange = arg.CountryOnChange || new Function();
	    this.ProvinceOnChange = arg.ProvinceOnChange || this.provinceOnChange;
	    this.CityOnChange = arg.CityOnChange || new Function();
	    this.attrName   = "N";
	    this.attrIndex   = 1;
	    this.DefaultCountry = "CN";
	    this.DefaultProvince = "北京";
	    this.DefaultCity = "PEK";
	    this.CityDocList = {};
	    this.xmlDoc     = null;
	    this.type     = arg.type || "";
	    this.loadXML(BASE_PATH + "/common/XmlCountryList.aspx?lang=" + I18N.langCode + "&type=" + this.type);
	    
	},
	loadXML: function (source) {
		var thisObj = this;
		new zXmlHttp.Request(source, {onSuccess: function (transport) {
		    SelectCity.xmlDoc = transport.responseXML;
			if (thisObj.CountryEl) 
			    thisObj.CountryEl.onchange = function () { 
			        thisObj._initProvinceUI();
			        thisObj.CountryOnChange(this);
			    };
			if (thisObj.ProvinceEl) 
			    thisObj.ProvinceEl.onchange = function () {
			        thisObj._initCityUI();
			        thisObj.ProvinceOnChange(this);
			    };
			if (thisObj.CityEl) 
			    thisObj.CityEl.onchange = function () {
			        thisObj.CityOnChange(this);
			    };
			thisObj._initCountryUI(thisObj.DefaultCountry);
			if (thisObj.CountryEl) {
			    thisObj.setSelectedIndex(thisObj.DefaultCountry, 'country');
			    thisObj._initProvinceUI();
			}
			if (thisObj.ProvinceEl) {
			    thisObj.setSelectedIndex(thisObj.DefaultProvince, 'province');
			    thisObj._initCityUI();
			}
			if (thisObj.CityEl)
			    thisObj.setSelectedIndex(thisObj.DefaultCity, 'city');
		} });
		
	},
	_initCountryUI: function () {
	    if (this.CountryEl != null) {
	        this.CountryEl.options.length = 0;
	        var countryNodes, Name, Value, newOption;
	        if(this.IS_FIREFOX) {	  
	            countryNodes = this._selectNodes(SelectCity.xmlDoc, "/*")[0].getElementsByTagName('S');      
	            for (var i = 0; i < countryNodes.length; i++) {
			        Name = countryNodes[i].getAttribute(this.attrName);
			        Value = countryNodes[i].getAttribute("C");
			        newOption = document.createElement("OPTION");
                    this.CountryEl.options.add(newOption);
                    newOption.textContent = Name;
                    newOption.value = escapeHTML(Value);
	            }
    	            
		    } else {
	            countryNodes = this._selectNodes(SelectCity.xmlDoc, "/*")[0].childNodes;
	            for (var i = 0; i < countryNodes.length; i++) {
			        Name = countryNodes[i].attributes[this.attrIndex].value;
			        Value = countryNodes[i].attributes[0].value;
			        newOption = document.createElement("OPTION");
                    this.CountryEl.options.add(newOption);
                    newOption.innerText = Name;
                    newOption.value = escapeHTML(Value);
	            }
			}
	    }
        this._sort(this.CountryEl);
		this.setSelectedIndex(this.DefaultCountry, 'country');
		this.CountryOnChange();
	    this._initProvinceUI(this.DefaultProvince);
	},
	_initProvinceUI: function () {
	    var countryCode;
        if (this.CountryEl && this.CountryEl.options.length > 0)
            countryCode = this.CountryEl.options[this.CountryEl.selectedIndex].value;
        else
            countryCode = this.DefaultCountry;
	    
	    this.ProvinceEl.options.length = 0;
	    if (this.CityDocList[countryCode] == null) {
	        var thisObj = this;
		    new zXmlHttp.Request(BASE_PATH + "/common/XmlCityList.aspx?lang=" + I18N.langCode + "&Code=" + countryCode + "&type=" + thisObj.type, {onSuccess: function (transport) {
	            thisObj.CityDocList[countryCode] = transport.responseXML;
	            thisObj._provinceSelectFill(countryCode);
	        } });
	    } else {
	        this._provinceSelectFill(countryCode);
	    }
	},
	_provinceSelectFill: function (countryCode) {
	    var xmlDoc = this.CityDocList[countryCode];
        var provinceNodes, Name, Value, newOption;
        provinceNodes = zXPath.selectNodes(xmlDoc, "/*/*");
//        if (provinceNodes.length > 0) {
	        this.ProvinceEl.options.length = 0;
            if (this.IS_FIREFOX) {
                for (var i = 0; i < provinceNodes.length; i++) {
	                Name = provinceNodes[i].getAttribute(this.attrName);
	                Value = provinceNodes[i].getAttribute("C");
	                newOption = document.createElement("OPTION");
                    this.ProvinceEl.options.add(newOption);
                    newOption.textContent = Name;
                    newOption.value = escapeHTML(Value);
                }
            } else {
                for (var i = 0; i < provinceNodes.length; i++) {
	                Name = provinceNodes[i].getAttribute(this.attrName);
	                Value = provinceNodes[i].getAttribute("C");
	                newOption = document.createElement("OPTION");
                    this.ProvinceEl.options.add(newOption);
                    newOption.innerText = Name;
                    newOption.value = escapeHTML(Value);
                }
            }
		    this.setSelectedIndex(this.DefaultProvince, 'province');

            this.ProvinceOnChange();
            this._initCityUI();
//        } else {
//            this.CountryEl.options.remove(this.CountryEl.selectedIndex);
//        }
	},
	_initCityUI: function () {
	    var xmlDoc = this.CityDocList[this.CountryEl.options[this.CountryEl.selectedIndex].value];
	    var Name, Value, newOption, provinceName;
        if (this.ProvinceEl && this.ProvinceEl.options.length > 0)
            provinceName = this.ProvinceEl.options[this.ProvinceEl.selectedIndex].value;
        else
            provinceName = "北京";
	    var cityNodes = zXPath.selectNodes(xmlDoc, "/*/*[@C='" + provinceName + "']/*");
//        if (cityNodes.length > 0) {
	        if (this.IS_FIREFOX) {
	            this.CityEl.options.length = 0;
	            for (var i = 0; i < cityNodes.length; i++) {
			        Name = cityNodes[i].getAttribute(this.attrName);
			        Value = cityNodes[i].getAttribute("C");
			        newOption = document.createElement("OPTION");
                    this.CityEl.options.add(newOption);
                    newOption.textContent = Name;
                    newOption.value = escapeHTML(Value);
	            }
	        } else {
	            while (this.CityEl.options.length > 0) 
	                this.CityEl.options.remove(this.CityEl.options.length-1);
    	            
	            for (var i = 0; i < cityNodes.length; i++) {
			        Name = cityNodes[i].attributes[this.attrIndex].value;
			        Value = cityNodes[i].attributes[0].value;
			        newOption = document.createElement("OPTION");
                    this.CityEl.options.add(newOption);
                    newOption.innerText = Name;
                    newOption.value = escapeHTML(Value);
	            }
	        }
    	    
	        this._sort(this.CityEl);
		    this.setSelectedIndex(this.DefaultCity, 'city');
		    this.CityOnChange();
//        } else {
//            if (provinceNodes.length > 0) {
//                this.ProvinceEl.options.remove(this.ProvinceEl.selectedIndex);
//                this._initCityUI();
//            } else {
//                this.CountryEl.options.remove(this.CountryEl.selectedIndex);
//                this._initProvinceUI();
//            }
//        }
	},
	_selectNodes: function (xmlDoc, xPath) {
		var xmlNodes;
		if (zXml.useActiveX)
				xmlNodes = xmlDoc.selectNodes(xPath);
		else{
			var oEvaluator = new XPathEvaluator();
			var oResult = oEvaluator.evaluate(xPath, xmlDoc.documentElement, null,
				XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
			var oElement;
			xmlNodes = [];
			while (oElement = oResult.iterateNext()) {
				  xmlNodes.push(oElement);
			}
		}
		
		return xmlNodes;
	},
    _sort: function (oSel) {
	    var ln = oSel.options.length;
        var arrNames = [];
        var arrValues = {};
        for (var i = 0; i < ln; i++) {
	        arrNames[i] = oSel.options[i].innerHTML;
	        arrValues[arrNames[i]] = oSel.options[i].value;
        }
	    if (I18N.lang == 'zh-CN') {
	            arrNames.sort(function(a,b){return a.localeCompare(b)});
	    } else {
	        arrNames.sort();
	    }
        while (oSel.options.length > 1) 
            oSel.remove(oSel.options.length-1);
        oSel.options.length = 0;
        for (i = 0; i < arrNames.length; i++) {
	        if (this.IS_FIREFOX) {
	            oSel.options.add(this.createOption(arrNames[i], arrValues[arrNames[i]]));
	        } else {
                oSel.add(new Option(arrNames[i], arrValues[arrNames[i]]));
	        }
        }
    },
	setSelectedIndex: function (value, type) {
	    var select;
	    if (type == "country")
	        select = this.CountryEl;
	    else if (type == "province")
	        select = this.ProvinceEl;
	    else if (type == "city")
	        select = this.CityEl;
	    else
	        return;
	        
	    var items = select.options;
	    for (var i = 0; i < items.length; i++) {
	        if (items[i].value.indexOf(value) != -1 || value.indexOf(items[i].value) != -1) {
	            select.selectedIndex = i;
	            return;
	        }
	    }
	},
	createOption: function (name, value) {
	    var option = document.createElement("OPTION");
        option.value = value;
	    if (this.IS_FIREFOX) {
            option.textContent = name;
	    } else {
            option.innerText = name;
	    }
	    return option;
	},
	selectRegion: function (cityCode) {
	    if (cityCode.length == 0)
	        return;
	    var thisObj = this;
		var intervalID = setInterval(function () {
			if (SelectCity.xmlDoc != null) {
				clearInterval(intervalID);
				
		        new zXmlHttp.Request(BASE_PATH + "/common/XmlQueryCity.aspx?lang=" + I18N.langCode + "&Code=" + cityCode, {onSuccess: function (transport) {
	                var doc = transport.responseXML;
	                var provinceName;
	                
//		            if (doc.selectNodes("/*/*/*")[0].attributes[0].value.indexOf("上海") != -1) {
//		                debugger;
//		            }
		
	                thisObj.DefaultCity = cityCode;
	                if (thisObj.IS_FIREFOX) 
	                    thisObj.DefaultProvince = doc.selectNodes("/*/*/*")[0].getAttribute("C");
	                else
	                    thisObj.DefaultProvince = doc.selectNodes("/*/*/*")[0].attributes[0].value;
	                    
	                var countryCode;
	                if (thisObj.IS_FIREFOX) 
	                    thisObj.DefaultCountry = doc.selectNodes("/*/*")[0].getAttribute("C");
	                else
	                    thisObj.DefaultCountry = doc.selectNodes("/*/*")[0].attributes[0].value;
	                if (thisObj.DefaultCountry != "" && thisObj.DefaultCity != "")
			            thisObj._initCountryUI();
	            } });
			}
		}, 60);
	},
	provinceOnChange: function () {
	    var sender = null;
	    if (this.IS_FIREFOX) {
	        if ((this.ProvinceEl.parentNode.tagName == "TD" && this.ProvinceEl.parentNode.parentNode.tagName == "TR" &&
	              this.ProvinceEl.parentNode.childNodes.length == 1 && this.ProvinceEl.parentNode.lastChild == this.ProvinceEl) || 
	              (this.ProvinceEl.parentNode.className == "inputBox")) {
	            sender = this.ProvinceEl.parentNode.parentNode;
	        } else {
	            sender = this.ProvinceEl;
	        }
	        sender.style.display = 
	            (this.ProvinceEl.options[this.ProvinceEl.selectedIndex].value == "null") ? "none" : "";
	    } else {
	        if ((this.ProvinceEl.parentNode.tagName == "TD" && this.ProvinceEl.parentNode.parentNode.tagName == "TR" &&
	              this.ProvinceEl.parentNode.childNodes.length == 1 && this.ProvinceEl.parentNode.lastChild == this.ProvinceEl) || 
	              (this.ProvinceEl.parentNode.className == "inputBox")) {
	            sender = this.ProvinceEl.parentNode.parentNode;
	        } else {
	            sender = this.ProvinceEl;
	        }
	        sender.style.display = 
	            (this.ProvinceEl.options[this.ProvinceEl.selectedIndex].value == "null") ? "none" : "block";
	    }
	}
};