﻿// JScript 文件
var xmlHttp

function GetXmlHttpObject()
{
    var xmlHttp = null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    
    return xmlHttp;
}

function getCAddress()
{
    var hidTxtCAddress = document.getElementById('ctl00_Content_hidTxtCAddress');
    
    if(document.all.ctl00_Content_ddlCCity.style.visibility == "hidden")
    {
        hidTxtCAddress.value = document.all.ctl00_Content_ddlCProvince.options[document.all.ctl00_Content_ddlCProvince.selectedIndex].text;
    }
    else if(document.all.ctl00_Content_ddlCCity.style.visibility == "visible" && document.all.ctl00_Content_ddlCDistrict.style.visibility == "hidden")
    {
        hidTxtCAddress.value = document.all.ctl00_Content_ddlCProvince.options[document.all.ctl00_Content_ddlCProvince.selectedIndex].text + document.all.ctl00_Content_ddlCCity.options[document.all.ctl00_Content_ddlCCity.selectedIndex].text;
    }
    else
    {
        hidTxtCAddress.value = document.all.ctl00_Content_ddlCProvince.options[document.all.ctl00_Content_ddlCProvince.selectedIndex].text + document.all.ctl00_Content_ddlCCity.options[document.all.ctl00_Content_ddlCCity.selectedIndex].text + document.all.ctl00_Content_ddlCDistrict.options[document.all.ctl00_Content_ddlCDistrict.selectedIndex].text;
    }
}

//获取开通区域列表
function getArea(type,str)
{
    document.getElementById('type').value = type;
    //alert(document.getElementById('type').value);
    if (str.length == 0)
    {
        return;
    }

    xmlHttp=GetXmlHttpObject()

    if (xmlHttp == null)
    {
        alert ("您的浏览器不支持AJAX！");
        return;
    }

    var url = "../get/getAreaList.aspx?txt=" + str +"&rnd=" + generateMixed(5);;
    xmlHttp.onreadystatechange = getAreaStateChanged;
    xmlHttp.open("POST",url,true);
    xmlHttp.send(null);
} 

function getAreaStateChanged() 
{ 
    if (xmlHttp.readyState == 4)
    {
        var optionsText = new Array();
        var optionsValue = new Array();
        var arrTmp = new Array();
        var responseTextTmp = xmlHttp.responseText;
        var responseText = responseTextTmp.split(";");
        xmlHttp = null;
        
        for(var i = 1; i < responseText.length + 1; i++)
        {
            arrTmp = responseText[i - 1].split(",");
            optionsValue[i] = arrTmp[0];
            optionsText[i] = arrTmp[1];
        }
        optionsValue[responseText.length + 1] = "2";
        optionsText[responseText.length + 1] = "其他区域";
        
        var type = document.getElementById('type').value;
        var o,oa;
        
        if(type == 0)
        {
            o = document.getElementById("ctl00_Content_ddlCProvince");
            ro = document.getElementById("ctl00_Content_ddlRProvince");
            
            while(ro.length > 0)
            {
	            ro.remove(ro.options[0]);
            }
            
	        optionsValue[0] = "-3";
            optionsText[0] = "请选择:省/自治区/直辖市";
            
            //从数组中添加内容
            for(var i = 0; i <optionsValue.length; i++)
            {
	            var oo = document.createElement('option');
	            oo.value = optionsValue[i];
	            oo.text = optionsText[i];
	            ro.add(oo);
            }
        }
        else if(type == 1)
        {
            optionsValue[0] = "-3";
            optionsText[0] = "请选择:市/地区/盟";
            o = document.getElementById('ctl00_Content_ddlCCity');
        }
        else if(type == 2)
        {
            optionsValue[0] = "-3";
            optionsText[0] = "请选择:县/县级市/区/旗";
            o = document.getElementById('ctl00_Content_ddlCDistrict');
        }
        else if(type == 3)
        {
            optionsValue[0] = "-3";
            optionsText[0] = "请选择:省/自治区/直辖市";
            o = document.getElementById('ctl00_Content_ddlCProvince');
        }
        else if(type == 4)
        {
            optionsValue[0] = "-3";
            optionsText[0] = "请选择:市/地区/盟";
            o = document.getElementById('ctl00_Content_ddlRCity');
            oa = document.getElementById('ctl00_Content_ddlRProvince').value;
        }
        else if(type == 5)
        {
            optionsValue[0] = "-3";
            optionsText[0] = "请选择:县/县级市/区/旗";
            o = document.getElementById('ctl00_Content_ddlRDistrict');
            oa = document.getElementById('ctl00_Content_ddlRCity').value;
        }
        
        //首先清空options
        while(o.length > 0)
        {
	        o.remove(o.options[0]);
        }
        
        //从数组中添加内容
        for(var i = 0; i <optionsValue.length; i++)
        {
	        var oo = document.createElement('option');
	        oo.value = optionsValue[i];
	        oo.text = optionsText[i];
	        
	        //当没有返回值或返回开通区域为0时,不添加下拉列表.
            if(responseTextTmp.indexOf("-2") != "-1")
            {
                o.style.visibility = "hidden";
                
                if("4,5".indexOf(type) != -1)
                {
                    getAgent(oa);
                }
            }
            else
            {
                o.add(oo);
                o.style.visibility = "visible";
            }
        }
        
        getCAddress();
    }
}

//产生随机数，防止缓存
function generateMixed(n)
{
    var res = "";
    var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

    for(var i = 0; i < n ; i ++)
    {
        var id = Math.ceil(Math.random()*35);
        res += chars[id];
    }

    return res;
}