﻿var countries = new Array();
var regions = new Array();
var countryText;
var regionText;
var cityText;
var countryLbl = document.getElementById("lblCountry");
var regionLbl = document.getElementById("lblRegion");
var cityLbl = document.getElementById("lblCity");

function init(co,re,ci) {
    var p = document.getElementById("country");
    var e = " ";
    if ( p && e ) {
       for( var i = 0; i < countries.length; i++ ) {
        e = document.createElement( "option" );
        e.setAttribute( "value", i);
        e.innerHTML = countries[i][0];
        p.appendChild(e);
       }
    }
    countryText = document.getElementById(co);
    regionText = document.getElementById(re);
    cityText = document.getElementById(ci);

    countryText.style.display='';
    regionText.style.display = '';
    cityText.style.display = '';
}

function selectCountry() {
    var p = document.getElementById("country");
    var idx = p.options[p.selectedIndex].value;
    if ( idx != "-1"  ) {
       countryText.value=p.options[p.selectedIndex].text;
    }else
        countryText.value="";
        
    showRegion(idx);
    IsShowText(p.selectedIndex,countryText,countryLbl);
    IsShowText(0,regionText,regionLbl);
    IsShowText(0,cityText,cityLbl);
}

function selectRegion(){
	var p = document.getElementById("region");
	var idx = p.options[p.selectedIndex].value;
	if ( idx != "-1") {
	   
	   regionText.value = p.options[p.selectedIndex].text;
	}else
	   regionText.value = "";
	   
	showCity(idx);
	IsShowText(p.selectedIndex,regionText,regionLbl);
	IsShowText(0,cityText,cityLbl);
}

function selectCity(){
    var p = document.getElementById("city");
    var idx = p.options[p.selectedIndex].value;
    if(idx != "-1")
    {
        cityText.value = p.options[p.selectedIndex].text;
    }else
        cityText.value = "";
        
    IsShowText(p.selectedIndex,cityText,cityLbl);
    
}

function IsShowText(selectIndex,txtControl,lblControl){
    if(selectIndex != 0)
    {
        lblControl.style.display = 'none';
        txtControl.style.display = 'none';
    }
    else
    {
        lblControl.style.display = '';
        txtControl.style.display = '';
        txtControl.value = "";
    }
}

function showRegion(idx) {
    var c = document.getElementById("region");
    if ( c ) {
       while( c.hasChildNodes() ) {
   	    if(c.lastChild.value != "-1")
    	    c.removeChild(c.lastChild);
	    else
	    {
		    showCity("-1");
		    break;
	    }
       }
    }
    if ( idx != "-1" ) {
       var region = countries[idx][1].split( "|" ); 
       if(region == "")
            return false;
       for( var i = 0; i < region.length; i++ ) {
        e = document.createElement( "option" );
        e.setAttribute( "value", countries[idx][0]);
        e.innerHTML = region[i];
        c.appendChild(e);
       }
    }
}
function showCity(idx) {
    var c = document.getElementById("city");
    var $region = document.getElementById("region");
    if ( c ) {
       while( c.hasChildNodes() ) {
        if(c.lastChild.value != "-1")
    	    c.removeChild(c.lastChild);
	    else
		    break;
       }
    }
    if ( idx != "-1" ) {
       var citys = 0;
       for(var i=0; i < regions.length; i++)
       {
            if(regions[i][0] == idx && regions[i][1] == $region.options[$region.selectedIndex].text)
            {
                citys = regions[i][2].split( "|" );
                break;
            }
       }
       if(citys == 0)
            return false;
       for( var i = 0; i < citys.length; i++ ) {
        e = document.createElement( "option" );
        e.setAttribute( "value", i );
        e.innerHTML = citys[i];
        c.appendChild(e);
       }
    }
}
