﻿var city;
var province;
var country;
/**
 *请求国家表中的数据
 */
function getCountry(){
	url="/area.do";
	xhrSend(url,createCountry);
}
/**
 *按国家ID请求省份表中的数据
 */
function getProvince(id1,defValue){
    url="/area.do?id="+id1;
   	xhrSend(url,createProvince,defValue);
}
/**
 *按国家ID,省份ID请求城市表中的数据
 */
function getCity(id1,id2,defValue){
    url="/area.do?id="+id1+"&id="+id2;
	xhrSend(url,createCity,defValue);
}
/**
 *创建国家下拉框
 */
function createCountry(myreq){
   //解析数据
   var restr=myreq.responseText;
   var items=new Array();
   items=restr.split(",");
   
   country=document.getElementById("countryid");
   //初始化下拉框
   country.length=0;
   
   country.options[0]=new Option("请选择国家","0");
   for(i=0;i< items.length-2 ;i=i+2)
       country.options[country.length]=new Option(items[i+1],items[i]);
}    
/**
 *生成省份列表
 */
function createProvince(myreq,curr_value){
   //解析数据
   var restr=myreq.responseText;
   var items=new Array();
   items=restr.split(",");
   
   province=document.getElementById("provinceid");
   //初始化下拉框
   province.length=0;
   if(items[0]==""||items[0]>0)
   	   province.options[0]=new Option("请选择省份","0");
   for(i=0;i< items.length-2 ;i=i+2){
       province.options[province.length]=new Option(items[i+1],items[i]);
   }
   if(typeof(curr_value)=="number"){
   	province.value=curr_value;
   }
}    
/**
 *生成城市列表
 */
function createCity(myreq,curr_value){
   //解析数据
   var restr=myreq.responseText;
   var items=new Array();
   items=restr.split(",");
   city=document.getElementById("cityid");
   //初始化下拉框
   city.length=0;
   if(items[0]==""||items[0]>0)
  	  city.options[0]=new Option("请选择城市","0");
   for(i=0;i< items.length-2 ;i=i+2){
      city.options[city.length]=new Option(items[i+1],items[i]);
   }   
   if(typeof(curr_value)=="number"){
   	city.value=curr_value;
   }
}   
/**
 *国家改变
 */
function  countryChange(cid,defValue){
   getProvince(cid,defValue);
   //清空城市
   city=document.getElementById("cityid");
   city.length=0;  
   city.options[0]=new Option("请选择城市","0");
}
/**
 *省份改变
 */
function  provinceChange(pid,defValue){
   country=document.getElementById("countryid");
   cid=country.value;
   getCity(cid,pid,defValue);
}

function newXMLHttpRequest() {
  var xmlreq = false;
  if (window.XMLHttpRequest) {
    xmlreq = new XMLHttpRequest();
  }else if (window.ActiveXObject) {
    try {
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      try {
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        xmlreq = false;
      }
    }
  }
  return xmlreq;
}
function xhrSend(url,processMethod,defValue){
	var req = newXMLHttpRequest();
    req.onreadystatechange = function(){
     if (req.readyState == 4) {
    	 if (req.status == 200) {
     	  processMethod(req,defValue);
    	 } else {
      	 alert("HTTP error "+req.status+": "+req.statusText);
    	 }
 	  }
    }
    req.open("GET",url, true);
    req.send(null);
}