﻿// JavaScript 文档
// Code By 董帅(dsaimh@gmail.com) 2007-6-2

// 三级地区下拉列表绑定类
//objCountry    国家下拉列表ID
//objProvince   属于objCountry的省列表ID
//objCity       属于objProvince的市区列表
//PleaseChose   是否自动添加'请选择'项

 
 function AreaSelector(objCountry, objProvince, objCity, PleaseChose)
 {
      //国家选择框
      this.Country = document.getElementById(objCountry);       
      //省选择框
      this.Province = document.getElementById(objProvince);     
      //市选择框
      this.City = document.getElementById(objCity);             
      //是否自动添加'请选择'项
      this.PleaseChose = PleaseChose;                           
      //在省选择框中添加'请选择'项
      this.Province.PleaseChose = this.PleaseChose;             
      //在市选择框中添加'请选择'项
      this.City.PleaseChose = this.PleaseChose;                
      //设定国家的下级下拉框为省
      this.Country.associateObject = this.Province;             
      //设定省的下级下拉框为市
      this.Province.associateObject = this.City;                
      //初始化绑定国家列表
	  this.init = SetCountryList;  
	  //绑定国家下拉框的onchange事件
	  this.Country.onchange = SetProvinceList;                  
	   //绑定省下拉框的onchange事件
	  this.Province.onchange = SetCityList; 
	  
	  //设定各列表初始选项
	  this.SetDefaultItem = function(strValue)
	  {
	   if(strValue != "" && !isNaN(strValue))
	    {
	      var SelectedCountry,SelectedProvince,SelectedCity;
	      if(strValue < 10000)
	       {
	        SelectedCity = ""
	        SelectedProvince = "";
	        SelectedCountry = strValue;
	       }
	      else if(strValue >= 10000 && strValue < 20000)
	       {
	        SelectedCity = ""
	        SelectedProvince = strValue;
	        SelectedCountry = GetParentValue(1,SelectedProvince);
	       }
	      else if(strValue >= 20000 && strValue <30000) 
	       {
	        SelectedCity = strValue
	        SelectedProvince = GetParentValue(2,SelectedCity);
	        SelectedCountry = GetParentValue(1,SelectedProvince);
	       }
	      else
	       return;
	       
	       this.SetSeletedValues(SelectedCountry,SelectedProvince,SelectedCity);
	    }
	  }
	  
	 this.SetSeletedValues = function(SelectedCountry,SelectedProvince,SelectedCity)
	  {

           for(var i = 0; i < this.Country.options.length; i++)
            {
             if(this.Country.options[i].value == SelectedCountry)
               {
                 try
                 {
                  this.Country.options[i].selected = true;
                  this.Country.onchange();
                 }
                 catch(e)
                  {
                    try
                     {
                      this.Country.selectedIndex = i;
                      this.Country.onchange();
                     }catch(ex){throw ex}
                  }
                }
            }

            for(i = 0; i < this.Province.options.length; i++)
             {
              if(this.Province.options[i].value == SelectedProvince)
               {
                 try
                 {
                  this.Province.options[i].selected = true;
                  this.Province.onchange();
                 }
                 catch(e)
                  {
                    try
                    { 
                      this.Province.selectedIndex = i;
                      this.Province.onchange();
                     }catch(ex){throw ex}
                  }
               }
             }

           for(i = 0; i < this.City.options.length; i++)
            {
              if(this.City.options[i].value == SelectedCity)
                try
                {
                  this.City.options[i].selected = true;
                }
                catch(e)
                {
                  try
                   {
                     this.City.selectedIndex = i;
                   }catch(ex){throw ex}
                }
            }
       }

 }
 
  function GetParentValue(strIndex,strValue)
   {
    var arrSearch = "";
    var strReturn = "";
    switch(strIndex)
     {
      case 1:
       arrSearch = ArrProvince;
       break;
      case 2:
       arrSearch = ArrCity;
       break;
       default:
       break;
     }
     
     if(arrSearch != "")
      {
       for(var i = 0; i < arrSearch.length; i++)
        {
         if(arrSearch[i][1] == strValue)
          strReturn = arrSearch[i][0];
        }
      }
     return strReturn;
   }
 
  //绑定国家下拉框
	 function SetCountryList() 
	  {
	     //清除选项
	    this.Country.options.length = 0;                           
        //添加'请选择'项
	    if(this.PleaseChose) AddOptions(this.Country,"请选择国家","");  
	    
        //添加国家列表
        for(var i = 0; i < ArrCountry.length; i++)
	     {
	       AddOptions(this.Country,ArrCountry[i][1],ArrCountry[i][0])
	     }
	     
	  }
	  
	 //绑定省级下拉框
 	function SetProvinceList()
	 {
	       //取得省级下拉框对象
	       TagObj = this.associateObject; 
           //清除现有选项
	       TagObj.options.length = 0;                                       
    	   //添加'请选择'项
	       if(TagObj.PleaseChose) AddOptions(TagObj,"请选择省","");           
    	   
    	   //循环添加省份选项
	       for(var i = 0; i < ArrProvince.length; i++)
	       {
	         if(ArrProvince[i][0] == this.value)
	           AddOptions(TagObj,ArrProvince[i][2],ArrProvince[i][1]);
	       }
	      //在市级列表中显示该省下的市
	      TagObj.onchange();                    
	 }
	 
   //绑定市级下拉框
   function SetCityList()
   {
           //取得市级下拉框对象
           TagObj = this.associateObject; 
           //清除现有选项
	       TagObj.options.length = 0;                                       
    	   //添加'请选择'项
	       if(TagObj.PleaseChose) AddOptions(TagObj,"请选择市","");           
    	   
    	   //循环添加地市选项
	       for(var i = 0; i < ArrCity.length; i++)
	       {
	         if(ArrCity[i][0] == this.value)
	           AddOptions(TagObj,ArrCity[i][2],ArrCity[i][1]);
	       }
   }


  function SingleCountryList(obj,str)
   {
    obj = document.getElementById(obj);
    obj.options.length = 0;
    for(var i = 0; i < ArrCountry.length; i++)
     {
      AddOptions(obj,ArrCountry[i][1],ArrCountry[i][0]);
      if(ArrCountry[i][0] == str)
       {
         obj.options[i].selected = true;
        }
     }
   }
   
   function GetAreaNameByID(strID)
    {
     var arr = "",strTmp = "";
     var strCountry = "",strProvince = "",strCity = ""; 
//      if(strID != "" && strID < 10000)
//       {
//        strCountry = strID;
//       }
//      else if(strID >= 10000 && strID < 20000)
//       {
//        strCountry = GetParentValue(1,strID);
//        strProvince = strID;
//       }
//      else if(strID >= 20000 && strID < 30000)
//       {
//        strCity = strID;
//        strProvince = GetParentValue(2,strCity);
//        strCountry = GetParentValue(1,strProvince);
//       }
        if(strID != "")
        {
            //取得省市的编号
            strProvince = strID.substring(0,5)+"00";
            strCity = strID;
        }
       if(strCountry != "")
        {
         for(var i = 0; i < ArrCountry.length; i++)
          {
           if(ArrCountry[i][0] == strCountry)
            {
             strTmp += ArrCountry[i][1];
            }
          }
        }
       if(strProvince != "")
        {
         for(i = 0; i < ArrProvince.length; i ++)
          {
            if(ArrProvince[i][1] == strProvince)
             {
              strTmp += ArrProvince[i][2];
             }
          }
        }
        
        //省市编号不等取市名（相等时应该是直辖市就不在取市名）
       if(strProvince != strCity)
       {
            if(strCity != "")
            {
                for(i = 0; i < ArrCity.length; i ++)
                {
                    if(ArrCity[i][1] == strCity)
                    {
                        strTmp += "-"+ArrCity[i][2];
                    }
                }
            }
       }
   
        if(strTmp == "")
        {
            strTmp = "";
        }
        return strTmp;
    }
    
    function ShowAreaNameMult(str)
     {
      var a = "";
      var ar = str.split(",");
      for(var i = 0; i < ar.length; i++)
       {
         if(a == "")
         {
          a += GetAreaNameByID(ar[i]);
         }
         else
         {
          a += "<br />" + GetAreaNameByID(ar[i]);
         }
       }
       document.write(a);
     }
     
     function setAreaListMult(strobj,str)
      {
        var obj = GetObjectById(strobj);
        var ar = str.split(",");
        for(var i = 0; i < ar.length; i++)
         {
          AddOptions(obj,GetAreaNameByID(ar[i]),ar[i]);
          obj.options[i].selected = true;
         }
      }
      
      function AddToMultiple(obj1,obj2,obj3,objt,max)
      {
       var objCountry = document.getElementById(obj1);
       var objProvince = document.getElementById(obj2);
       var objCity = document.getElementById(obj3);
       var objtarget = document.getElementById(objt);
       var strValue = objCity.value == "" ? (objProvince.value == "" ? objCountry.value : objProvince.value) : objCity.value;
       if(objtarget.options.length < max)
        {
          if(NotExsitOption(objtarget,strValue))
          {
           AddOptions(objtarget,GetAreaNameByID(strValue),strValue);
           objtarget.options[objtarget.options.length - 1].selected = true;
          }
          else
           alert("该项您已经选择过了!");
        }
       else
        {
          alert("对不起,您最多只能选择"+max+"项!");
        }
      }
      
      function delFromMutiple(obj)
      {
       var objtarget = document.getElementById(obj);
        for(var i = 0; i < objtarget.length; i++)
        {
         if(objtarget.options[i].selected)
          {
           objtarget.remove(i);
          }
        }
      }
      
      function NotExsitOption(obj,str)
      {
       for(var i = 0; i < obj.options.length; i++)
        {
         if(obj.options[i].value == str)
         {
          return false;
          }
        }
        return true;
}
// JScript File

function AreaSelector2(objProvince,objCity,PleaseChoose)
{
    
    //省选择框
    this.Province=document.getElementById(objProvince);

    //市选择框
    this.City=document.getElementById(objCity); 
    
    //在省下拉框添加“请选择”项
    this.PleaseChoose=PleaseChoose;    
    this.Province.PleaseChoose=this.PleaseChoose;
    
    //在市下拉框添加“请选择”项
    this.City.PleaseChoose=this.PleaseChoose;
    
   //设定省的下拉框为市
   this.Province.associateObject=this.City;
   
   //初始化省下拉框
   this.init=SetProvinceList;
   
   //绑定省下拉框的onchange事件
   this.Province.onchange=SetCityList;
   
   //设置选择项
   //strFirst 一级选择项值
  //strSecond 二级选择项值
//  //设定各列表初始选项
//	  this.SetDefaultItem = function(strValue)
//	  {
//	   if(strValue != "" && !isNaN(strValue))
//	    {

//	       if(strValue >= 10000 && strValue < 20000)
//	       {
//	        SelectedCity = ""
//	        SelectedProvince = strValue;
//	
//	       }

//	      else
//	       return;
//	       
//	       this.SetSeletedValues(SelectedProvince,SelectedCity);
//	    }
//	  }
  this.SetDefaultItem = function(strValue1,strValue2)
	  {
		  if(strValue1 != "" && !isNaN(strValue1) && strValue2 != "" && !isNaN(strValue2) )
			{
				 this.SetSeletedValues(strValue1,strValue2);
			}
	  
	  }
	  
	 this.SetSeletedValues = function(SelectedProvince,SelectedCity)
	  {

           for(var i = 0; i < this.Province.options.length; i++)
            {
             if(this.Province.options[i].value == SelectedProvince)
               {
                 try
                 {
                  this.Province.options[i].selected = true;
                  this.Province.onchange();
                 }
                 catch(e)
                  {
                    try
                     {
                      this.Province.selectedIndex = i;
                      this.Province.onchange();
                     }catch(ex){throw ex}
                  }
                }
            }



           for(i = 0; i < this.City.options.length; i++)
            {
              if(this.City.options[i].value == SelectedCity)
                try
                {
                  this.City.options[i].selected = true;
                }
                catch(e)
                {
                  try
                   {
                     this.City.selectedIndex = i;
                   }catch(ex){throw ex}
                }
            }
       }

 }


 

//绑定省份列表 
function SetProvinceList()
    {
        //清除 选项
        this.Province.options.length=0;
        //添加列表选项
        if(this.PleaseChoose)
        {
            AddOptions(this.Province,"- 请选择省份 -","0");
        }
        //添加列表选项
         for(var i = 0; i < ArrProvince.length; i++)
	     {
	         if(ArrProvince[i][0]=="1" && ArrProvince[i][2] != "不限")
	         {
	            AddOptions(this.Province,ArrProvince[i][2],ArrProvince[i][1]);
	         }
	     }
	     this.Province.onchange();        
    }
    //绑定城市列表
    function SetCityList()
    {
        //取得省份下拉矿框对象
        TagObj=this.associateObject;
        
        //清除现有选项
        TagObj.options.length=0;
        
        //添加'请选择'项
	    if(TagObj.PleaseChoose) AddOptions(TagObj,"- 请选择市 -","0"); 
	    
	     //循环添加二级选项
	       for(var i = 0; i < ArrCity.length; i++)
	       {
	         if(ArrCity[i][0] == this.value)
	           AddOptions(TagObj,ArrCity[i][2],ArrCity[i][1]);
	       }  
    }
    
     function AddOptions(obj,strName,strValue)
	 {
       obj.options.add(new Option(strName,strValue));
	 }
	 
	 //设置选中项
     function SetProvince(obj,strSelected)
	 {
	    var ObjTag = document.getElementById(obj);
     
         if(ObjTag)
          {
              for(var i = 0; i < ObjTag.options.length; i++)
              {
                
                 if(ObjTag.options[i].value == strSelected)
                  {
                     ObjTag.options[i].selected = true;
                  }
              }
          }
}