﻿// 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 getAgent(str)
{
    if (str.length == 0)
    {
        return false;
    }
    
    xmlHttp = GetXmlHttpObject()

    if (xmlHttp == null)
    {
        alert ("您的浏览器不支持Ajax！");
        return false;
    }

    var url = "get/getAgentList.aspx?txt=" + str +"&rnd=" + generateMixed(5);;
    xmlHttp.onreadystatechange = getAgentStateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function getAgentStateChanged() 
{ 
    if (xmlHttp.readyState == 4)
    { 
	    var optionsText = new Array();
        var optionsValue = new Array();
        var arrTmp = new Array();
        var o = document.getElementById("ddlAgent");
        var responseText = xmlHttp.responseText.split(";");
        
        xmlHttp = null;
	      optionsValue[0] = "-3";
        optionsText[0] = "请选择代理商";
        //optionsValue[1] = "1";
        //optionsText[1] = "烟台天开通信销售公司";
        
        for(var i = 1; i < responseText.length + 1; i++)
        {
            arrTmp = responseText[i - 1].split(",");
            optionsValue[i] = arrTmp[0];
            optionsText[i] = arrTmp[1];
        }
        
        //首先清空options
        while(o.length > 0)
        {
	        o.remove(o.options[0]);
        }
        
        //从数组中添加内容到代理商下拉列表中
        if(1 == 1)    //document.all.ddlRDistrict.options[document.all.ddlRDistrict.selectedIndex].value != "-2" && document.all.ddlRDistrict.options[document.all.ddlRDistrict.selectedIndex].value != "-3")
        {
            for(var i = 0; i <optionsValue.length; i++) {
	            var oo = document.createElement('option');
	            oo.value = optionsValue[i];
	            oo.text = optionsText[i];
	            o.add(oo);
            }
            
	        document.getElementById("agentList").style.display='block';
        }
        
        //document.getElementById('hidTxtRAddress').value = document.all.ddlRProvince.options[document.all.ddlRProvince.selectedIndex].text + document.all.ddlRCity.options[document.all.ddlRCity.selectedIndex].text + document.all.ddlRDistrict.options[document.all.ddlRDistrict.selectedIndex].text;
    }
}

function getCAddress()
{
    var hidTxtCAddress = document.getElementById('hidTxtCAddress');
    //document.getElementById('hidTxtCAddress').value = document.all.ddlCProvince.options[document.all.ddlCProvince.selectedIndex].text + document.all.ddlCCity.options[document.all.ddlCCity.selectedIndex].text + document.all.ddlCDistrict.options[document.all.ddlCDistrict.selectedIndex].text;
    
    if(document.all.ddlCCity.style.visibility == "hidden")
    {
        hidTxtCAddress.value = document.all.ddlCProvince.options[document.all.ddlCProvince.selectedIndex].text;
    }
    else if(document.all.ddlCCity.style.visibility == "visible" && document.all.ddlCDistrict.style.visibility == "hidden")
    {
        hidTxtCAddress.value = document.all.ddlCProvince.options[document.all.ddlCProvince.selectedIndex].text + document.all.ddlCCity.options[document.all.ddlCCity.selectedIndex].text;
    }
    else
    {
        hidTxtCAddress.value = document.all.ddlCProvince.options[document.all.ddlCProvince.selectedIndex].text + document.all.ddlCCity.options[document.all.ddlCCity.selectedIndex].text + document.all.ddlCDistrict.options[document.all.ddlCDistrict.selectedIndex].text;
    }
}

function getRAddress()
{
    var hidTxtRAddress = document.getElementById('hidTxtRAddress');
    
    if(document.all.ddlRCity.style.visibility == "hidden")
    {
        hidTxtRAddress.value = document.all.ddlRProvince.options[document.all.ddlRProvince.selectedIndex].text;
    }
    else if(document.all.ddlRCity.style.visibility == "visible" && document.all.ddlRDistrict.style.visibility == "hidden")
    {
        hidTxtRAddress.value = document.all.ddlRProvince.options[document.all.ddlRProvince.selectedIndex].text + document.all.ddlRCity.options[document.all.ddlRCity.selectedIndex].text;
    }
    else
    {
        hidTxtRAddress.value = document.all.ddlRProvince.options[document.all.ddlRProvince.selectedIndex].text + document.all.ddlRCity.options[document.all.ddlRCity.selectedIndex].text + document.all.ddlRDistrict.options[document.all.ddlRDistrict.selectedIndex].text;
    }
}

function getAgentID()
{
    document.getElementById('hidTxtAgentID').value = document.all.ddlAgent.options[document.all.ddlAgent.selectedIndex].value;
}

//获取开通区域列表
function getArea(type,str)
{
    document.getElementById('type').value = type;
    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("ddlCProvince");
            ro = document.getElementById("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('ddlCCity');
        }
        else if(type == 2)
        {
            optionsValue[0] = "-3";
            optionsText[0] = "请选择:县/县级市/区/旗";
            o = document.getElementById('ddlCDistrict');
        }
        else if(type == 3)
        {
            optionsValue[0] = "-3";
            optionsText[0] = "请选择:省/自治区/直辖市";
            o = document.getElementById('ddlCProvince');
        }
        else if(type == 4)
        {
            optionsValue[0] = "-3";
            optionsText[0] = "请选择:市/地区/盟";
            o = document.getElementById('ddlRCity');
            oa = document.getElementById('ddlRProvince').value;
        }
        else if(type == 5)
        {
            optionsValue[0] = "-3";
            optionsText[0] = "请选择:县/县级市/区/旗";
            o = document.getElementById('ddlRDistrict');
            oa = document.getElementById('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";
            }
        }
    }
}

//获取代理商代理产品的库存
function getStockPile(str)
{
    var productList = document.getElementsByName('txtHidProductIDList');
    var productAmountList = document.getElementsByName('txtHidProductAmountList');
    var productID = "";
    var productAmount = "";
    for(var i = 0; i < productList.length ; i++)
    {
        productID += productList[i].value + ",";
        productAmount += productAmountList[i].value + ",";
    }
    
    productID = productID.substring(0,productID.length - 1);
    productAmount = productAmount.substring(0,productAmount.length - 1);
    
    str = str + ";" + productID + "|" + productAmount;
    
    if (str.length == 0)
    {
        return false;
    }

    xmlHttp=GetXmlHttpObject()

    if (xmlHttp == null)
    {
        alert ("您的浏览器不支持AJAX！");
        return;
    }

    var url = "get/getStockPile.aspx?txt=" + str +"&rnd=" + generateMixed(5);
    xmlHttp.onreadystatechange = getStockPileStateChanged;
    xmlHttp.open("POST",url,true);
    xmlHttp.send(null);
} 

function getStockPileStateChanged() 
{ 
    if (xmlHttp.readyState == 4)
    {
        var responseText = xmlHttp.responseText;
        xmlHttp = null;
        
        if(responseText.length > 0)
        {
            document.getElementById('stockList').style.display = 'block';
            var type = document.getElementById('stockPile').innerHTML = responseText;
        }
        else
        {
            document.getElementById('stockList').style.display = 'none';
        }
    }
    
    getAgentID();
    getCAddress();
    getRAddress();
}

//产生随机数，防止缓存
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;
}