﻿// JScript 文件
//Function:     Ajax Chat
//Author:       Siser(Siser0409@163.com)
//Modify Date:  2007-10-1
var Request=new function()
{
  
    /**
     * 创建新的通信对象
     * @return 一个新的通信对象
     */
    this.createInstance=function()
    {
        var instance=null;
        if (window.XMLHttpRequest)
        {
            //mozilla
            instance=new XMLHttpRequest();
            //有些版本的Mozilla浏览器处理服务器返回的未包含XML mime-type头部信息的内容时会出错。因此，要确保返回的内容包含text/xml信息
            if (instance.overrideMimeType)
            {
                instance.overrideMimeType="text/xml";
            }
        }
        else if (window.ActiveXObject)
        {
            //IE
            var MSXML = ['MSXML2.XMLHTTP.5.0', 'Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
            for(var i = 0; i < MSXML.length; i++)
            {
                try
                {
                    instance = new ActiveXObject(MSXML[i]);
                    break;
                }
                catch(e)
                {                    
                }
            }
        }
        return instance;
    }
    /////////////////////////////////
    ////////////////检查  
   this.GetCountry = function()
   {
        var type = "country";
        var _url = "/Controls/AjaxMethod.aspx?type="+type+"&t="+Math.random();
        
        this.Chat(_url,type);
      
   }
   
   //--------------------------------------------------------
   this.GetProvince = function(countrycode)
   {
        var type = "province";
        var _url = "/Controls/AjaxMethod.aspx?type="+type+"&code="+countrycode+"&t="+Math.random();
        this.Chat(_url,type);
      
   }
   
    //--------------------------------------------------------
   
   this.GetCity = function(provinceID)
   {
        var type = "city";
        var _url = "/Controls/AjaxMethod.aspx?type="+type+"&code="+provinceID+"&t="+Math.random();
        this.Chat(_url,type);
      
   }    
   
   //--------------------------------------------------------
   
   this.GetCounty = function(cityID)
   {
        var type = "county";
        var _url = "/Controls/AjaxMethod.aspx?type="+type+"&code="+cityID+"&t="+Math.random();
        this.Chat(_url,type);
      
   }
   

 

   
   ////////////////////////////////////
 
  
   ///////////////////////////////
   ///////////////////////////////
   
   this.Chat = function(url,type)
   {
        var _url = url; 
        var _asynchronous = true;
        var instance = this.createInstance();
   
        instance.open("GET",_url,_asynchronous);
        instance.send(null);
        
      instance.onreadystatechange=function()
        {
            if (instance.readyState == 4) // 判断对象状态            {
                if (instance.status == 200) // 信息已经成功返回，开始处理信息                {
                    if(typeof(type) != 'undefined')
                    { 
                        type = type.toLowerCase();
                        if(type == "country")
                        {  
                           DisplayCountry(instance.responseText);
                        }
                        
                        if(type == "province")
                        {
                            DisplayProvince(instance.responseText);
                        }
                    
                        if(type == "city")
                        {
                            DisplayCity(instance.responseText);
                        }
                        
                        if(type == "county")
                        {
                            DisplayCounty(instance.responseText);
                        }
                        
                        
                     }
                   
                }
            
            }
        }
     }
 
}


function DisplayCountry(inner)
{
   if(typeof($("country")) != "undefined")
             AppendOption($("country"),inner);
  
}

function DisplayProvince(inner)
{
    if(inner.length > 0)
    {
        AppendOption($("province"),inner);
        $("province").style.display  ="inline";
        $("city").style.display  ="inline";
        $("county").style.display  ="inline";
     }
    else
    {
         $("province").style.display  ="none";
         $("city").style.display  ="none";
         $("county").style.display  ="none";
         
    }
}

function DisplayCity(inner)
{
    if(typeof($("city")) != "undefined")
        AppendOption($("city"),inner);

}

function DisplayCounty(inner)
{
    if(typeof($("county")) != "undefined")
        AppendOption($("county"),inner);

}

function AppendOption(selectCTL,arrOption)
{
    var arrOptions = arrOption.split("|");
    for(i=0; i < arrOptions.length; i++)
    {
        op = arrOptions[i].split(",");
        if(typeof(op[1]) != "undefined")
            selectCTL.options.add(new Option(op[1],op[0]));
    }
}

function $(id)
{
    return document.getElementById(id);
}