// JavaScript Document

	// cmd
	var cmd_industry = 0;
	var cmd_country = 1;
	var cmd_province = 2;
	var cmd_city = 3;
	var cur_cmd = 0;
	
	var req = null;
	var arrCmd = new Array();
	var arrCmdID = new Array();
    function executeURL(cmd, url)
    {
   		if (req != null)
   		{
   			arrCmdID.unshift(cmd);
   			arrCmd.unshift(url);
   			return;
   		}

   		if (window.ActiveXObject) 
   		{ // IE
	    	req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if (window.XMLHttpRequest) 
		{ // Non-IE browsers
			req = new XMLHttpRequest();
		}
		req.onreadystatechange = processStateChange;
		req.open("POST",url,true);
		req.send();

		cur_cmd = cmd; // very important!
    }
	function processStateChange() 
	{
    	try
    	{
			if(req.readyState==4)
			{
				if(req.status==200)
				{ // success!
					//var res=unescape(req.responseText);
					var res=decodeURI(req.responseText);
					analyzeResult(res);
				}
				else
				{ // error!
					alert(req.statusText+"("+req.status+")");
				}
				req=null;
				
				if (arrCmd.length>0)
				{
					executeURL(arrCmdID[0], arrCmd[0]);
					arrCmd.shift();
					arrCmdID.shift();
				}
			}
		}
		catch(e)
		{
			req=null;
		}
   	}
   	
   	var header = "ACTIVEROLDBCALL";
    // analyze message from XMLHTTP
    function analyzeResult(res)
    { // TODO:
    	var flag = res.indexOf(header);
    	if (flag < 0)
    	{
    		alert("error!");
    		return;
    	}
    	var message = res.substr(header.length);
    	// industry
    	if (cur_cmd == cmd_industry)
    	{ // <count>;{<ID|Name>;}
    		var options = document.all("industry").options;
    		options.length = 0;
    		var temp = message.split(";");
    		var count = temp[0];
    		for (var i = 0; i < count; i++)
    		{
    			var temp2 = temp[1+i].split("|");
				var option = document.createElement("OPTION");
				option.value = temp2[0];
				option.text = temp2[1];
				options.add(option);
    		}
    		
    	}
    	
    	// country
    	if (cur_cmd == cmd_country)
    	{ // <count>;{<ID|Name>;}
    		var options = document.all("country").options;
    		options.length = 0;
    		var temp = message.split(";");
    		var count = temp[0];
    		for (var i = 0; i < count; i++)
    		{
    			var temp2 = temp[1+i].split("|");
				var option = document.createElement("OPTION");
				option.value = temp2[0];
				option.text = temp2[1];
				options.add(option);
    		}
		}

    	// province
    	if (cur_cmd == cmd_province)
    	{ // <count>;{<ID|Name>;}
    		var options = document.all("province").options;
    		options.length = 0;
    		var temp = message.split(";");
    		var count = temp[0];
    		for (var i = 0; i < count; i++)
    		{
    			var temp2 = temp[1+i].split("|");
				var option = document.createElement("OPTION");
				option.value = temp2[0];
				option.text = temp2[1];
				options.add(option);
    		}
    	}

    	// city
    	if (cur_cmd == cmd_city)
    	{ // <count>;{<ID|Name>;}
    		var options = document.all("city").options;
    		options.length = 0;
    		var temp = message.split(";");
    		var count = temp[0];
    		for (var i = 0; i < count; i++)
    		{
    			var temp2 = temp[1+i].split("|");
				var option = document.createElement("OPTION");
				option.value = temp2[0];
				option.text = temp2[1];
				options.add(option);
    		}
    		
    	}
    }
    
    function OnSelCountry()
    {
    	var options = document.all("country").options;
    	var option = options[options.selectedIndex];
		executeURL(cmd_province, encodeURI("/proxy/proxy.jsp?cmdid=301&type=province&countryID="+option.value));
    }
    function OnSelProvince()
    {
    	var options = document.all("country").options;
    	var option = options[options.selectedIndex];
    	var countryID = option.value;

    	options = document.all("province").options;
    	option = options[options.selectedIndex];
    	var provinceID = option.value;
		executeURL(cmd_city, encodeURI("/proxy/proxy.jsp?cmdid=301&type=city&countryID="+countryID+"&provinceID="+provinceID));
    }
    
	executeURL(cmd_industry, encodeURI("/proxy/proxy.jsp?cmdid=300"));
	executeURL(cmd_country, encodeURI("/proxy/proxy.jsp?cmdid=301&type=country"));
	executeURL(cmd_province, encodeURI("/proxy/proxy.jsp?cmdid=301&type=province&countryID=1"));
	//executeURL(cmd_city, encodeURI("/roladmin/dbcall/proxy.jsp?cmdid=301&type=city&countryID=1&provinceID=1"));
	executeURL(cmd_city, encodeURI("/proxy/proxy.jsp?cmdid=301&type=city&countryID=1&provinceID=1"));
