﻿/**
* @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 = (I18N.lang == 'zh-CN') ? "C" : "E";
        this.attrIndex = (I18N.lang == 'zh-CN') ? 0 : 1;
        this.DefaultCountry = "中国";
        this.DefaultProvince = "北京";
        this.DefaultCity = "北京";
        this.xmlDoc = null;
    },
    loadXML: function(source, country, province, city) {
        //		SelectCity.xmlDoc = SelectCity.xmlDoc || zXmlDom.createDocument();
        //		SelectCity.xmlDoc.load(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._initCityUI();
                    thisObj.CityOnChange(this);
                };
            thisObj._initCountryUI();
            if (I18N.lang == 'en-US') thisObj._sort(thisObj.CountryEl);
            country = country || thisObj.DefaultCountry;
            province = province || thisObj.DefaultProvince;
            city = city || thisObj.DefaultCity;
            if (country && thisObj.CountryEl) {
                thisObj.setSelectedIndex(country, 'country');
                thisObj._initProvinceUI();
                //thisObj.CountryOnChange();
            }
            if (province && thisObj.ProvinceEl) {
                thisObj.setSelectedIndex(province, 'province');
                thisObj._initCityUI();
                //thisObj.ProvinceOnChange();
            }
            if (city && thisObj.CityEl)
                thisObj.setSelectedIndex(city, 'city');
            //thisObj.CityOnChange();
        }
        });
        //		var intervalID = setInterval(function () {
        //			if (SelectCity.xmlDoc.readyState == 4) {
        //			    alert(1);
        //				clearInterval(intervalID);
        //			}
        //		}, 60);
    },
    _initCountryUI: function(value) {
        if (this.CountryEl != null) {
            //	        while (this.CountryEl.options.length > 0) 
            //	            this.CountryEl.options.remove(this.CountryEl.options.length-1);
            this.CountryEl.options.length = 0;
            var countryNodes, Name, Value, newOption;
            if (this.IS_FIREFOX) {
                countryNodes = this._selectNodes("/*")[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 = Value;
                }

            } else {
                countryNodes = this._selectNodes("/*")[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 = Value;
                }
            }
            //this.CountryEl.selectedIndex = CookieItem("travel").getValue("selectCountry1");
        }
        this.CountryOnChange();
        this._initProvinceUI();
    },
    _initProvinceUI: function(value) {
        var countryName;
        if (this.CountryEl && this.CountryEl.options.length > 0)
            countryName = this.CountryEl.options[this.CountryEl.selectedIndex].value;
        else
            countryName = "中国";
        //while (this.ProvinceEl.options.length > 0) 
        //    this.ProvinceEl.options.remove(this.ProvinceEl.options.length-1);
        this.ProvinceEl.options.length = 0;

        var provinceNodes, Name, Value, newOption;
        if (this.IS_FIREFOX) {
            provinceNodes = this._selectNodes("/*/*[@C='" + countryName + "']")[0].getElementsByTagName('P');
            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 = Value;
            }
        } else {
            provinceNodes = this._selectNodes("/*/*[@C='" + countryName + "']")[0].childNodes;
            for (var i = 0; i < provinceNodes.length; i++) {
                Name = provinceNodes[i].getAttribute(this.attrName); //.attributes[this.attrIndex].value;
                Value = provinceNodes[i].getAttribute("C"); //.attributes[0].value;
                newOption = document.createElement("OPTION");
                this.ProvinceEl.options.add(newOption);
                newOption.innerText = Name;
                newOption.value = Value;
            }
        }
        this.ProvinceOnChange();
        this._sort(this.ProvinceEl);
        this._initCityUI();
    },
    _initCityUI: function(value) {
        var cityNodes, Name, Value, newOption, provinceName;
        if (this.IS_FIREFOX) {
            if (this.ProvinceEl && this.ProvinceEl.length > 0)
                provinceName = this.ProvinceEl.options[this.ProvinceEl.selectedIndex].value;
            else
                provinceName = "北京";
            this.CityEl.options.length = 0;

            cityNodes = this._selectNodes("/*/*[@C='" + this.CountryEl.options[this.CountryEl.selectedIndex].value + "']/*[@C='" + provinceName + "']")[0].getElementsByTagName('C');
            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 = Value;
            }
        } else {
            if (this.ProvinceEl && this.ProvinceEl.options.length > 0)
                provinceName = this.ProvinceEl.options[this.ProvinceEl.selectedIndex].value;
            else
                provinceName = "北京";
            while (this.CityEl.options.length > 0)
                this.CityEl.options.remove(this.CityEl.options.length - 1);

            cityNodes = this._selectNodes("/*/*[@C='" + this.CountryEl.options[this.CountryEl.selectedIndex].value + "']/*[@C='" + provinceName + "']")[0].childNodes;
            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 = Value;
            }
        }
        this.CityOnChange();
        this._sort(this.CityEl);
    },
    _selectNodes: function(xPath) {
        var xmlNodes;
        if (zXml.useActiveX)
            xmlNodes = SelectCity.xmlDoc.selectNodes(xPath);
        else {
            var oEvaluator = new XPathEvaluator();
            var oResult = oEvaluator.evaluate(xPath, SelectCity.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 - 1; i++) {
            arrNames[i] = oSel.options[i + 1].innerHTML;
            arrValues[arrNames[i]] = oSel.options[i + 1].value;
        }
        if (I18N.lang == 'zh-CN') {
            //for (var j = 0; j < arrNames.length; j++) {
            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 = 1;
        //        while (ln--) {
        //	        oSel.options[ln] = null;
        //        }
        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(name, country) {
        if (name.length == 0)
            return;
        var thisObj = this;
        var intervalID = setInterval(function() {
            if (SelectCity.xmlDoc != null) {
                clearInterval(intervalID);

                var cityNodes, parentNodes, countryValue, provinceValue, cityValue;

                /// var nodes = zXPath.selectNodes(SelectCity.xmlDoc, "/S[@C='" + country + "']/*/*");
                //var nodes2 = thisObj._selectNodes("//S");
                //debugger;

                if (country == null || country == "")
                    parentNodes = thisObj._selectNodes("//C");
                else
                    parentNodes = thisObj._selectNodes("//S[@C='" + country + "']/*/*");
                if (thisObj.IS_FIREFOX) {
                    //parentNodes = parentNodes[0].getElementsByTagName('C');
                    for (var i = 0; i < parentNodes.length; i++) {
                        cityNodes = parentNodes[i].getElementsByTagName('C');
                        for (var j = 0; j < cityNodes.length; j++) {
                            if (name == cityNodes[j].getAttribute("C")) {
                                countryValue = cityNodes[j].parentNode.parentNode.getAttribute("C");
                                provinceValue = cityNodes[j].parentNode.getAttribute("C");
                                cityValue = name;

                            }
                        }
                    }
                } else {
                    //parentNodes = parentNodes[0].childNodes;
                    for (var i = 0; i < parentNodes.length; i++) {
                        var cityNode = parentNodes[i];
                        if (name == cityNode.attributes[0].value) {
                            countryValue = cityNode.parentNode.parentNode.attributes[0].value;
                            provinceValue = cityNode.parentNode.attributes[0].value;
                            cityValue = name;
                        }
                        //debugger;
                        //cityNodes = parentNodes[i].childNodes;
                        //for (var j = 0; j < cityNodes.length; j++) {
//                        if (name == cityNodes[j].attributes[0].value) {
//                            countryValue = cityNodes[j].parentNode.parentNode.attributes[0].value;
//                            provinceValue = cityNodes[j].parentNode.attributes[0].value;
//                            cityValue = name;
//                        }
                        //}
                    }
                }

                setTimeout(function() {
                    for (var i = 0; i < thisObj.CountryEl.options.length; i++) {
                        if (thisObj.CountryEl.options[i].value == countryValue) {
                            thisObj.CountryEl.options[i].selected = true;
                        }
                    }
                    //thisObj.CountryEl.selectedIndex = countryValue;
                    setTimeout(function() {
                        thisObj.CountryEl.onchange();
                        for (var i = 0; i < thisObj.ProvinceEl.options.length; i++) {
                            if (thisObj.ProvinceEl.options[i].value == provinceValue) {
                                thisObj.ProvinceEl.options[i].selected = true;
                            }
                        }
                        //thisObj.ProvinceEl.selectedIndex = provinceValue;
                        setTimeout(function() {
                            thisObj.ProvinceEl.onchange();
                            for (var i = 0; i < thisObj.CityEl.options.length; i++) {
                                if (thisObj.CityEl.options[i].value == cityValue) {
                                    thisObj.CityEl.options[i].selected = true;
                                }
                            }
                            //thisObj.CityEl.selectedIndex = cityValue;
                        }, 500);
                    }, 500);
                }, 500);
            }
        }, 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) {
                sender = this.ProvinceEl.parentNode.parentNode;
            } else {
                sender = this.ProvinceEl;
            }
            sender.style.display =
	            (this.ProvinceEl.options[this.ProvinceEl.selectedIndex].value == "全部") ? "none" : "";
        } else {
            if (this.ProvinceEl.parentElement.tagName == "TD" && this.ProvinceEl.parentElement.parentElement.tagName == "TR" &&
	              this.ProvinceEl.parentElement.childNodes.length == 1 && this.ProvinceEl.parentElement.lastChild == this.ProvinceEl) {
                sender = this.ProvinceEl.parentElement.parentElement;
            } else {
                sender = this.ProvinceEl;
            }
            sender.style.display =
	            (this.ProvinceEl.options[this.ProvinceEl.selectedIndex].value == "全部") ? "none" : "block";
        }
    }
};

function GetShortCityName(cityName) {
    var shortName, cName;
    for (var i = 0; i < domesticCitys.length; i++) {
        shortName = domesticCitys[i].split("|")[0];
        cName = domesticCitys[i].split(" ")[1];
        if (cityName == cName) {
            return shortName;
        }
    }
    return "NotFound";
}