cCountry = '';
cState = '';
cCity = '';

window.onload = function() {
  setCountry();
  //window.location.reload(true);

}

function setZ(country, state, city){

  cCountry = country;
  cState = state;
  cCity = city;

}

function setCountry(){

  var elSel = document.getElementById('country');
  //for (i = elSel.length - 1; i>=0; i--)elSel.remove(i);
  var ix = 0;
  for (var i=0; i < csc.length; i++){
    if(csc[i]=='c'){
      eval("document.getElementById('country').options[ix]=new Option(csc[i+1])");

      //city_consts.js ->cCountry
      if(csc[i+1] == cCountry){
	    eval("document.getElementById('country').options[ix].selected=true");
	  }
      ix++;
    }
  }
  //document.getElementById('state').disabled=true;
  //document.getElementById('city').disabled=true;

  setState();
}

function setState(){
  document.getElementById('state').disabled=false;

  var elSel = document.getElementById('state');
  for (i = elSel.length - 1; i>=0; i--)elSel.remove(i);

  var inx = document.getElementById('country').selectedIndex;
  var cntry = document.getElementById('country').options[inx].text;
 // alert(cntry);
  ix = 0;
  for (var i=0; i < csc.length; i++){
    if(csc[i]=='c' && csc[i+1]==cntry){
      for (var j=i+1; csc[j]!='c' && j < csc.length; j++){
        if(csc[j]=='s'){
          eval("document.getElementById('state').options[ix]=new Option(csc[j+1])");

          //city_consts.js ->cState
	      if(csc[j+1] == cState){
			  
            eval("document.getElementById('state').options[ix].selected=true");
          }

          ix++;
        }
      }
    }
  }

  if(ix==0){
    document.getElementById('state').disabled=true;
    eval("document.getElementById('state').options[0]=new Option('n/a')");
  }
  setCity();
}

function setCity(){

  document.getElementById('city').disabled=false;

  var elSel = document.getElementById('city');
  for (i = elSel.length - 1; i>=0; i--)elSel.remove(i);

  inx = document.getElementById('country').selectedIndex;
  cntry = document.getElementById('country').options[inx].text;

  ix = 0;
  for (var i=0; i < csc.length; i++){
    if(csc[i]=='c' && csc[i+1]==cntry){
      if(csc[i+2]=='s'){

        inx = document.getElementById('state').selectedIndex;
        _state = document.getElementById('state').options[inx].text;

        for (var j=i+2; csc[j]!='c' && j < csc.length; j++){

          if(csc[j]=='s' && csc[j+1]==_state){
            for (var k=j+2; csc[k]!='c' && csc[k]!='s' && k < csc.length; k++){
              eval("document.getElementById('city').options[ix]=new Option(csc[k])");

              //city_consts.js ->cCity
              if(csc[k] == cCity){
                eval("document.getElementById('city').options[ix].selected=true");
              }

              ix++;

            }
          }
        }
      }else if(i+2 < csc.length){
        for (var k=i+2; csc[k]!='c' && k < csc.length; k++){
          eval("document.getElementById('city').options[ix]=new Option(csc[k])");

          //city_consts.js ->cCity
          if(csc[k] == cCity){
            eval("document.getElementById('city').options[ix].selected=true");
          }

          ix++;
        }
      }else{
        i +=2;
      }
    }
  }

  if(ix==0){
    document.getElementById('city').disabled=true;
    eval("document.getElementById('city').options[0]=new Option('n/a')");
  }

}



//NOT USED BUT WORKS
//checks if country, state or city is equal to '--SELECT--' to return false;
/*function checkCounryStateCity(){
  var bRes = true;
  inx = document.getElementById('country').selectedIndex;
  country = document.getElementById('country').options[inx].text;
  if(country=='--SELECT--')
    bRes = false;
  inx = document.getElementById('state').selectedIndex;
  state = document.getElementById('state').options[inx].text;
  if(state=='--SELECT--')
    bRes = false;
  inx = document.getElementById('city').selectedIndex;
  city = document.getElementById('city').options[inx].text;
  if(city=='--SELECT--')
    bRes = false;

  alert(bRes);
  return bRes;
}*/

