/**
 * @name		jQuery Cascdejs plugin
 * @author		zdy
 * @version 	1.0 
 */

//首先需要初始化
var xmlDoc;
var TopnodeList;
var CityList;
var citys;
var countyNodes;
var nodeindex = 0;
var childnodeindex = 0;
var new_area = 0;
var open_street = 0;

//获取xml文件
function cascdeInit(na,os,v1,v2,v3,v4,v5) {
    id1= "sel-provance";
    id2= "sel-city";
    id3= "sel-area";
    id4= "sel-street";

    new_area = na;
    open_street = os;

    if (v5) {
        id1 += v5;
        id2 += v5;
        id3 += v5;
        id4 += v5;
    }

    //打开xlmdocm文档
    if (new_area == 1) {
        var xmlfile = '/app/admin/static/components/area/AreaNew.xml?v=5';
    } else {
        var xmlfile = '/app/admin/static/components/area/Area.xml?v=4';
    }

    xmlDoc = loadXmlFile(xmlfile);
    var dropElement1 = document.getElementById(id1);
    var dropElement2 = document.getElementById(id2);
    var dropElement3 = document.getElementById(id3);
    var dropElement4 = document.getElementById(id4);
    RemoveDropDownList(dropElement1);
    RemoveDropDownList(dropElement2);
    RemoveDropDownList(dropElement3);
    RemoveDropDownList(dropElement4);
    if (window.ActiveXObject) {
        TopnodeList = xmlDoc.selectSingleNode("address").childNodes;
    }
    else {
        TopnodeList = xmlDoc.childNodes[0].getElementsByTagName("province");      
    }
    if (TopnodeList.length > 0) {
        //省份列表
        var county;
        var province;
        var city;
        for (var i = 0; i < TopnodeList.length; i++) {
            //添加列表项目
            county = TopnodeList[i];          
            var option = document.createElement("option");
            option.value = county.getAttribute("name");
            option.text = county.getAttribute("name");
            if (county.getAttribute("code")) {
                option.setAttribute('code', county.getAttribute("code"));
            }
            if (v1 == option.value) {
                option.selected = true;
                nodeindex = i;
            }
            dropElement1.add(option);
        }
        if (TopnodeList.length > 0) {
            //城市列表
            citys = TopnodeList[nodeindex].getElementsByTagName("city")
            for (var i = 0; i < citys.length; i++) {
                var id = dropElement1.options[nodeindex].value;
                //默认为第一个省份的城市
                province = TopnodeList[nodeindex].getElementsByTagName("city");
                var option = document.createElement("option");
                option.value = province[i] .getAttribute("name");
                option.text = province[i].getAttribute("name");
                if (province[i].getAttribute("code")) {
                    option.setAttribute('code', province[i].getAttribute("code"));
                }
                if (v2 == option.value) {
                    option.selected = true;
                    childnodeindex = i;
                }
                dropElement2.add(option);
            }
            selectcounty(v3,v4,v5);
        }
    }
}

/*
//依据省设置城市，县
*/
function selectCity(v5) {

    id1= "sel-provance";
    id2= "sel-city";
    id3= "sel-area";
    id4= "sel-street";

    if (v5) {
        id1 += v5;
        id2 += v5;
        id3 += v5;
        id4 += v5;
    }
    var dropElement1 = document.getElementById(id1);
    var name = dropElement1.options[dropElement1.selectedIndex].value;
    var code = dropElement1.options[dropElement1.selectedIndex].getAttribute('code');
    if (code) {
        $("#chose_province_code").val(code);
    }


    countyNodes = TopnodeList[dropElement1.selectedIndex];      
    var province = document.getElementById(id2);
    var city = document.getElementById(id3);
    RemoveDropDownList(province);
    RemoveDropDownList(city);
    var citynodes;
    var countycodes;

    if (window.ActiveXObject) {
        citynodes = xmlDoc.selectSingleNode('//address/province [@name="' + name + '"]').childNodes;
    } else {
        citynodes = countyNodes.getElementsByTagName("city")
    }
    if (window.ActiveXObject) {
        countycodes = citynodes[0].childNodes;
    } else {
        countycodes = citynodes[0].getElementsByTagName("county")
    }
  
    if (citynodes.length > 0) {
        //城市
        for (var i = 0; i < citynodes.length; i++) {
            var provinceNode = citynodes[i];
            var option = document.createElement("option");
            option.value = provinceNode.getAttribute("name");
            option.text = provinceNode.getAttribute("name");
            if (provinceNode.getAttribute("code")) {
                option.setAttribute('code', provinceNode.getAttribute("code"));
            }
            province.add(option);
        }
        if (countycodes.length > 0) {
            //填充选择省份的第一个城市的县列表
            for (var i = 0; i < countycodes.length; i++) {
                var dropElement2 = document.getElementById(id2);
                var dropElement3 = document.getElementById(id3);
                //取当天省份下第一个城市列表

                var option = document.createElement("option");
                option.value = countycodes[i].getAttribute("name");
                option.text = countycodes[i].getAttribute("name");
                if (countycodes[i].getAttribute("code")) {
                    option.setAttribute('code', countycodes[i].getAttribute("code"));
                }
                dropElement3.add(option);
            }
        }
    selectcounty(0,0,v5);
    }
}
/*
//设置县,区
*/
function selectcounty(v3,v4,v5) {
    id1= "sel-provance";
    id2= "sel-city";
    id3= "sel-area";
    id4= "sel-street";

    if (v5) {
        id1 += v5;
        id2 += v5;
        id3 += v5;
        id4 += v5;
    }

    var dropElement1 = document.getElementById(id1);
    var dropElement2 = document.getElementById(id2);
    var name = dropElement2.options[dropElement2.selectedIndex].value;
    var code = dropElement2.options[dropElement2.selectedIndex].getAttribute('code');
    if (code) {
        $("#chose_city_code").val(code);
    }


    var dropElement3 = document.getElementById(id3);
    var countys = TopnodeList[dropElement1.selectedIndex].getElementsByTagName("city")[dropElement2.selectedIndex].getElementsByTagName("county");

    if (new_area == 1 && open_street == 1) {
        var city_code = TopnodeList[dropElement1.selectedIndex].getElementsByTagName("city")[dropElement2.selectedIndex].getAttribute("code");
        if (city_code) {
            var left = city_code.substring(0,2);
            var xmlUrl = '../addons/ewei_shopv2/static/js/dist/area/list/'+left+'/'+city_code+'.xml';
            xmlCityDoc = loadXmlFile(xmlUrl);

            if (window.ActiveXObject) {
                CityList = xmlCityDoc.selectSingleNode("address").childNodes.childNodes;
            } else {
                CityList = xmlCityDoc.childNodes[0].getElementsByTagName("county");
            }
        }
    }

    RemoveDropDownList(dropElement3);
    if (countys.length > 0) {
        for (var i = 0; i < countys.length; i++) {
            var countyNode = countys[i];
            var option = document.createElement("option");
            option.value = countyNode.getAttribute("name");
            option.text = countyNode.getAttribute("name");
            if (countyNode.getAttribute("code")) {
                option.setAttribute('code', countyNode.getAttribute("code"));
            }

            if(v3==option.value){
                option.selected=true;
            }
            dropElement3.add(option);
        }
        if (new_area == 1 && open_street == 1) {
            selectstreet(v4,v5);
        }
    }

}

function selectstreet(v4,v5) {
    id1= "sel-provance";
    id2= "sel-city";
    id3= "sel-area";
    id4= "sel-street";

    if (v5) {
        id1 += v5;
        id2 += v5;
        id3 += v5;
        id4 += v5;
    }

    var dropElement1 = document.getElementById(id1);
    var dropElement2 = document.getElementById(id2);
    var name = dropElement2.options[dropElement2.selectedIndex].value;

    var dropElement3 = document.getElementById(id3);
    var dropElement4 = document.getElementById(id4);

    var area = dropElement3.options[dropElement3.selectedIndex].value;
    var code = dropElement3.options[dropElement3.selectedIndex].getAttribute('code');
    if (code) {
        $("#chose_area_code").val(code);
    }

    var area_code = TopnodeList[dropElement1.selectedIndex].getElementsByTagName("city")[dropElement2.selectedIndex].getElementsByTagName("county")[dropElement3.selectedIndex].getAttribute("code");
    RemoveDropDownList(dropElement4);

    if(CityList && CityList.length>0) {
        for (var i = 0; i < CityList.length; i++) {
            var county = CityList[i];
            var county_code = county.getAttribute("code");

            if(county_code == area_code){
                var streetlist = county.getElementsByTagName("street");
                for (var m = 0; m < streetlist.length; m++) {
                    var street = streetlist[m];
                    var option = document.createElement("option");
                    option.value = street.getAttribute("name");
                    option.text = street.getAttribute("name");
                    if (street.getAttribute("code")) {
                        option.setAttribute('code', street.getAttribute("code"));
                    }
                    if (v4 == option.value) {
                        option.selected = true;
                        nodeindex = m;
                    }
                    dropElement4.add(option);
                }
            }
        }
    }
}

function RemoveDropDownList(obj) {
    if (obj) {
        var len = obj.options.length;
        if (len > 0) {  
            for (var i = len; i >= 0; i--) {
                obj.remove(i);
            }
        }
    }
}
/*
//读取xml文件
*/
function loadXmlFile(xmlFile) {
    var xmlDom = null;
    if (window.ActiveXObject) {
        xmlDom = new ActiveXObject("Microsoft.XMLDOM");
        xmlDom.async = false;
        xmlDom.load(xmlFile) || xmlDom.loadXML(xmlFile);//如果用的是XML字符串//如果用的是xml文件  
    } else if (document.implementation && document.implementation.createDocument) {
        var xmlhttp = new window.XMLHttpRequest();
        xmlhttp.open("GET", xmlFile, false);
        xmlhttp.send(null);
        xmlDom = xmlhttp.responseXML;
    } else {
        xmlDom = null;
    }
    return xmlDom;
}