// constants
var noValue = ''

// globals
about='0~For rent|1~For sale|2~Vacation rental|3~B&B|4~Rented|';
tipo='0~Apartment|1~Ground Floor Prop. and Offic.|2~Villas and Lands|';
dove='0~Ortigia|1~Siracusa|';
stanze='2~2|3~3|4~4|5~5|6~6|7~7|8~8|9~9|10~10|11~11|12~12|13~>12|'
metri='30~30|40~40|50~50|60~60|70~70|80~80|90~90|100~100|110~110|120~120|130~130|140~140|150~>140|'
nposti='1~1|2~2|3~3|4~4|5~5|6~6|7~7|8~8|9~9|10~10|11~>10|'

var curOption = new Array();
var isLoaded = new Array();
var prima= new Array (4);
prima[0]="affitto";
prima[1]="vendita"
prima[2]="case_vacanza"
prima[3]="bed_and_breakfast"
prima[4]="locato"



function canc(){
document.forms['QForm']['formprezzo'].value="€";
}


function body_onLoad(){

  // initialize lists
  //emptyList( 'lstMake' );
  //emptyList( 'lstModel');
  //emptyList( 'lstOptions' );
document.forms['QForm']['B2'].disabled = true


  //jsrsExecute( 'select_rs.asp', cbFillMake, 'makeList' );
//document.forms['QForm']['lstModel'].options[0] = new Option('- KIND -', noValue);
//document.forms['QForm']['lstOptions'].options[0] = new Option('- AREA -', noValue);
//document.forms['QForm']['vani'].options[0] = new Option('- VANI MAX -', noValue);

document.forms['QForm']['lstModel'].options[0] = new Option('--------', noValue);
document.forms['QForm']['lstOptions'].options[0] = new Option('--------', noValue);
document.forms['QForm']['vani'].options[0] = new Option('--------', noValue);
document.forms['QForm']['MQ'].options[0] = new Option('--------', noValue);
document.forms['QForm']['posti'].options[0] = new Option('--------', noValue);

cbFillMake(about);

}

function lstMake_onChange(){
  var val = this.options[this.selectedIndex].value;

document.forms['QForm']['tabella'].value=prima[val];
  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( 'lstModel' );
    //------emptyList( 'lstOptions');
    window.status = 'Loading Model Selections...';

    //jsrsExecute( 'select_rs.asp', cbFillModel, 'modelList', val );
if (val<2 ){
cbFillModel(tipo);
cbFillOptions('');
cbFillVani('');
cbFillMQ('');
cbFillposti('');
}
if(val==2){
document.forms['QForm']['B2'].disabled = false
cbFillModel('');
cbFillOptions('');
cbFillVani(stanze);
cbFillMQ(metri);
cbFillposti(nposti);
}


if(val==3 || val==4) {
document.forms['QForm']['B2'].disabled = false
cbFillModel('');
cbFillOptions('');
cbFillVani('');
cbFillMQ('');
cbFillposti('');}
  }  
}

function lstModel_onChange(){
  var val = this.options[this.selectedIndex].value;

  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    emptyList( 'lstOptions');
    window.status = 'Loading Options Selections...';
    //jsrsExecute( 'select_rs.asp', cbFillOptions, 'optionsList', val );

if (val<2){
cbFillOptions(dove);
cbFillVani('');
cbFillMQ('');
cbFillposti('');
}
else if (val=2){ 
document.forms['QForm']['B2'].disabled = false
cbFillOptions('');
}
else{
cbFillOptions('');}
  }  
}

function lstOptions_onChange(){
  var val = this.options[this.selectedIndex].value;

  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    emptyList( 'vani');
    window.status = 'Loading Options Selections...';
    //jsrsExecute( 'select_rs.asp', cbFillOptions, 'optionsList', val );
if (val){
cbFillVani(stanze);
cbFillMQ(metri);
document.forms['QForm']['B2'].disabled = false
}
else{
//cbFillOptions('');
document.forms['QForm']['B2'].disabled = true
}
  }
}


function vani_onChange(){
  var val = this.options[this.selectedIndex].value;

  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    var msg = "You have selected: \n\n";
    msg += this.form.lstMake.options[this.form.lstMake.selectedIndex].value + "\n";
    msg += this.form.lstModel.options[this.form.lstModel.selectedIndex].value + "\n";
    msg += this.options[this.selectedIndex].value + "\n";
    //alert (msg);
  }
}

function MQ_onChange(){
  var val = this.options[this.selectedIndex].value;

  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    var msg = "You have selected: \n\n";
    msg += this.form.lstMake.options[this.form.lstMake.selectedIndex].value + "\n";
    msg += this.form.lstModel.options[this.form.lstModel.selectedIndex].value + "\n";
    msg += this.options[this.selectedIndex].value + "\n";
    //alert (msg);
  }
}
function posti_onChange(){
  var val = this.options[this.selectedIndex].value;

  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    var msg = "You have selected: \n\n";
    msg += this.form.lstMake.options[this.form.lstMake.selectedIndex].value + "\n";
    msg += this.form.lstModel.options[this.form.lstModel.selectedIndex].value + "\n";
    msg += this.options[this.selectedIndex].value + "\n";
    //alert (msg);
  }
}



function cbFillMake ( strMakes ){ 

  window.status = '';
  fillList( 'lstMake',  strMakes ); 
}

function cbFillModel ( strModels ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstModel',  strModels ); 
}

function cbFillOptions( strOptions ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstOptions', strOptions ); 
}
function cbFillVani( strOptions ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'vani', strOptions ); 
}
function cbFillMQ( strOptions ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'MQ', strOptions ); 
}
function cbFillposti( strOptions ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'posti', strOptions ); 
}


function fillList( listName, strOptions ){

  // fill any list with options
  //-----emptyList( listName );
  
  // always insert selection prompt
  var lst = document.forms['QForm'][listName];
  lst.disabled = true;

  
  // 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]);
    }  
  }

if (strOptions!='') {

  if (listName=='lstMake'){
  lst.options[0] = new Option('- ABOUT -', noValue);
  document.forms['QForm']['lstModel'].options[0] = new Option('--------------------------------', noValue);
  document.forms['QForm']['lstOptions'].options[0] = new Option('-------', noValue);
  document.forms['QForm']['vani'].options[0] = new Option('-------', noValue);
                          }


  if (listName=='lstModel'){
  lst.options[0] = new Option('- KIND -', noValue);
 
  document.forms['QForm']['lstOptions'].options[0] = new Option('-------', noValue);
  document.forms['QForm']['vani'].options[0] = new Option('-------', noValue);
  }
  if (listName=='lstOptions'){
  lst.options[0] = new Option('- AREA -', noValue); 
  document.forms['QForm']['vani'].options[0] = new Option('-------', noValue);
  }
  if (listName=='vani'){
  lst.options[0] = new Option('- ROOMS MAX -', noValue); 
  document.forms['QForm']['MQ'].options[0] = new Option('- MQ MAX -', noValue);
  }
   if (listName=='posti' ) {

  lst.options[0] = new Option('- n. BEDS -', noValue);
  }

}
else{

  lst.options[0] = new Option('-------', noValue);
      }
        if (document.forms['QForm']['lstMake'].options[document.QForm.lstMake.selectedIndex].value==2 ) {


        document.forms['QForm']['posti'].options[0] = new Option('- n. BEDS -', noValue); 
         }
 
  // 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;

}

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;
    }  
  }
}


