// constants
var noValue = '-99'

// globals
var curOption = new Array();
var isLoaded = new Array();
var msg_espera = 'Aguarde, Carregando Dados';

function body_onLoad(){

  // initialize lists
  emptyList( 'selNeg' );
  emptyList( 'selCat');
  emptyList( 'selTipo' );
  emptyList( 'selImovUF');
  emptyList( 'selImovCidade');
  emptyList( 'selImovBairro' );
  jsrsExecute( 'remote_script/select_rs.asp', cbFillImovUF, 'UFList' );    
  jsrsExecute( 'remote_script/select_rs.asp', cbFillNeg, 'NegocioList' );
}


function selImovUF_onChange(){
  var val = this.options[this.selectedIndex].value;
  if(val == noValue){
    // don't allow novalue selection - revert to current
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    // init dependent lists
    emptyList( 'selImovCidade');
    emptyList( 'selImovBairro' );
    window.status = msg_espera;
    jsrsExecute( 'remote_script/select_rs.asp', cbFillImovCidade, 'CidadeList', val );
  }  
}

function selImovUF_mudar(val){
	curOption['selImovUF'] = val;
    // init dependent lists
    emptyList( 'selImovCidade');
    emptyList( 'selImovBairro' );
    window.status = msg_espera;
    jsrsExecute( 'remote_script/select_rs.asp', cbFillImovCidade, 'CidadeList', val );
}

function selImovCidade_onChange(){
  var val = this.options[this.selectedIndex].value;
  if(val == noValue){
    // don't allow novalue selection - revert to current
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    // init dependent lists
    emptyList( 'selImovBairro' );
    window.status = msg_espera;
    jsrsExecute( 'remote_script/select_rs.asp', cbFillImovBairro, 'BairroList', val );
  }  
}

function selImovBairro_onChange(){
  var val = this.options[this.selectedIndex].value;
  if(val == noValue){
    // don't allow novalue selection - revert to current
    selectOption( this.name, curOption[this.name] )
  }  
}

function selNeg_onChange(){
  var val = this.options[this.selectedIndex].value;
  if(val == noValue){
    // don't allow novalue selection - revert to current
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    // init dependent lists
    emptyList( 'selCat');
    emptyList( 'selTipo' );
    window.status = msg_espera;
    jsrsExecute( 'remote_script/select_rs.asp', cbFillCat, 'CategoriaList', val );
  }  
}


function selCat_onChange(){
  var val = new Array() ;
  val[0] = QForm.selNeg.options[QForm.selNeg.selectedIndex].value;
  val[1] = this.options[this.selectedIndex].value;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    emptyList( 'selTipo');
    window.status = msg_espera;
    jsrsExecute( 'remote_script/select_rs.asp', cbFillTipo, 'TipoList', val );
	if (QForm.selCat.options[QForm.selCat.selectedIndex].value ==11) 
	{
		QForm.selDormitorio.disabled=false;
		QForm.selSuite.disabled=false;
	}
	else
	{
		QForm.selDormitorio.selectedIndex=0;
		QForm.selDormitorio.disabled=true;
		QForm.selSuite.selectedIndex=0;
		QForm.selSuite.disabled=true;
	}

  }  
}


function selTipo_onChange(){
  var val = this.options[this.selectedIndex].value;
  if(val == noValue){
    // don't allow novalue selection - revert to current
    selectOption( this.name, curOption[this.name] )
  }
}

function cbFillImovUF ( strUF ){ 
  window.status = '';
  fillList( 'selImovUF',  strUF ); 
}

function cbFillImovCidade ( strCidade ){ 
  window.status = '';
  fillList( 'selImovCidade',  strCidade ); 
}

function cbFillImovBairro ( strBairro ){ 
  window.status = '';
  fillList( 'selImovBairro',  strBairro ); 
}

function cbFillNeg ( strNeg ){ 
  window.status = '';
  fillList( 'selNeg',  strNeg ); 
}

function cbFillCat ( strCat ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'selCat',  strCat ); 
}

function cbFillTipo( strTipo ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'selTipo', strTipo ); 
}

function fillList( listName, strOptions ){
  // fill any list with options
  emptyList( listName );
  
  // always insert selection prompt
  var lst = document.forms['QForm'][listName];
  lst.disabled = true;
  lst.options[0] = new Option('(Selecione)', noValue);
  
  // options in form "value~displaytext|value~displaytext|..."
  var aOptionPairs = strOptions.split('|');
  for( var i = 0; i < aOptionPairs.length; i++ ){
    if (aOptionPairs[i].indexOf('~') != -1) {
      var aOptions = aOptionPairs[i].split('~');
      lst.options[i + 1] = new Option(aOptions[1], aOptions[0]);
    }  
  }
  
  // init to no value
  selectOption( listName, noValue );
  lst.onchange = eval( listName + "_onChange" );
  isLoaded[listName] = true;
  lst.disabled = false;
}

function emptyList( listName ){
  var lst = document.forms['QForm'][listName];
  lst.options.length = 0;
  lst.onchange = null;
  isLoaded[listName] = false;
  curOption[listName] = noValue;
  lst.disabled = true;
}

function selectOption( listName, optionVal ){
  // set list selection to option based on value
  var lst = document.forms['QForm'][listName];
  for( var i = 0; i< lst.options.length; i++ ){
    if( lst.options[i].value == optionVal ){
      lst.selectedIndex = i;
      curOption[listName] = optionVal;
      return;
    }  
  }
}



