/*
     $Revision$
     $Id$
*/
var cur_c = 0;

function inc_cc(){
        cur_c++;
        if(parent.window) parent.window.document.body.style.cursor='wait';
        window.document.body.style.cursor='wait';

}

function dec_cc(){
if (1) return reset_cc();
        cur_c--;
        if(cur_c==0){
          if(parent.window) parent.window.document.body.style.cursor='auto';
          if(parent.parent.window) parent.parent.window.document.body.style.cursor='auto';
          window.document.body.style.cursor='auto';
        }
}

function reset_cc(){
  cur_c=0;
  if(parent.parent.window) parent.parent.window.document.body.style.cursor='auto';
  if(parent.window) parent.window.document.body.style.cursor='auto';
  window.document.body.style.cursor='auto';
}

function makeC(a,b,c,d) {
  this.fkey = a;
  this.pkey = b;
  this.name = c;
  this.binding = d; // Optionele param voor ins_srtdeel.ins_srtdeel_binding of voor bezetting van ruimte bij toevoegen van ruimte aan organisatie
}

function makeD(a,b,c,d) {
  this.fkey = a;
  this.pkey = b;
  this.name = c;
  this.account = d;
}

function makeE(a,b,c,d,e) {
  this.fkey = a;
  this.pkey = b;
  this.name = c;
  this.account = d;
  this.ordernr = e;
}

function makeG(a,b,c,d,e,f,g) {
  this.fkey = a;
  this.pkey = b;
  this.name = c;
  this.account = d;
  this.acc_text = e;
  this.ordernr = f;
  this.module = g;
}

function insOpt(name,pkey,value2,so,sel) {
  var oOption = document.createElement("OPTION");
  oOption.text = name;
  oOption.value = pkey;
  oOption.value2 = value2
  so.options.add(oOption);
  oOption.selected = (sel==true);
}

function SelChange(
  fctrl,  // Master control         Example : ('disc', 'stdm',CC, true, m_stdm)
  pctrl,  // Depending (changing) control
  arr,    // Array with values
  emp,    // Add empty field
  deflt   // Default value
 ) {

  if( document.getElementById(fctrl) == null ) return;

//  var tt = document.getElementById(fctrl).options[document.getElementById(fctrl).selectedIndex].value;
  var tt = parseInt( document.getElementById(fctrl).value, 10 );

  //if( pctrl=='stdm' ) deflt=m_stdm;

  // Delete old items
  var itm=document.getElementById(pctrl).options;
  for(var i=itm.length-1; i>=0; i-- ) {
    itm.remove(i);
  }
  // Insert new items
  var j=0; if( emp ) { insOpt( '', -1, -1, document.getElementById(pctrl) ); j++; }
  var was0 = false, was1 = false, i0 = -1;
  var ival = 0;

  for( i=0; i<arr.length; i++ ) {

    if( arr[i].fkey == tt || (tt == 0 && !emp) || arr[i].fkey == 0 ) {

      if( arr[i].pkey == 0 ) {
        was0 = true; i0=i;
      } else {
        if( was0 && was1 ) {
          insOpt( arr[i0].name, arr[i0].pkey, arr[i0].binding, document.getElementById(pctrl), null );
          j++;
        }
        was1 = true; was0 = false;
        if( arr[i].pkey == deflt ) ival = j;

        insOpt( arr[i].name, arr[i].pkey, arr[i].binding, document.getElementById(pctrl),
          pctrl=='r_objs' ? room_obj_selected[arr[i].pkey] : false );
        j++;
      }
    }
  }

  if(pctrl!='r_objs')document.getElementById(pctrl).selectedIndex = ival;
  dec_cc();
// =================================
//  alert(pctrl + '   length = '  + arr.length + ' tt=' + tt);
// =================================
}


// Autoselect eerste waarde in listbox als de huidige waarde '0' is
function SelCheck0( sl ) {
  if(sl.options) {

    if( sl.options.length < 1) return;

    if( sl.options[ sl.selectedIndex ].value == 0 ) {
      sl.selectedIndex++;
    }

  }
}

var rooms_loaded = new Array();

function updateRooms(room_key, add_occupation) {
  // room_key is the default value instead of my_room
  // add_occupation adds the department occupation behind room description
  if( document.u2 && document.u2.rooms ) {
    var flr = parseInt( document.u2['floor'].value, 10 );

    if ( document.u2['userkey'])
      var userkey = parseInt( document.u2['userkey'].value, 10 );

    if ( document.u2['autfunction'] )
      var autfunction= (document.u2['autfunction'].value);

    var readmode = 0;
    if (document.u2['readmode'] )
      readmode = (document.u2['readmode'].value);

//  alert('read : '+readmode);

    inc_cc();

    if( rooms_loaded[flr] || flr==0 ) {
      SelChange('floor','rooms',Rooms,true, (!isNaN(room_key) && room_key > 0?room_key:my_room));
      updateWorkpl();
    } else {
       var frm = document.getElementById("hidFrame");
       var l_param_no_room_res = ''; // default
       if ((typeof (no_room_res) != 'undefined')) {
          if (no_room_res) {
            var l_param_no_room_res = '&no_room_res=1';
          }
       }
       if ((typeof (userkey) != 'undefined') && ( typeof(autfunction) != 'undefined'))
         frm.src = '../Shared/load_rooms.asp?floor='+flr+'&user_key='+userkey+'&autfunction='+autfunction+'&readmode='+readmode+'&room_key='+(!isNaN(room_key) && room_key > 0?room_key:my_room)+l_param_no_room_res + (add_occupation?'&add_occupation=1':'');
       else
         frm.src = '../Shared/load_rooms.asp?floor='+flr+'&room_key='+(!isNaN(room_key) && room_key > 0?room_key:my_room)+l_param_no_room_res + (add_occupation?'&add_occupation=1':'');
    }
  }
}

var workps_loaded = new Array();

function updateWorkpl() {

  if( document.u2.workpl ) {
    var room = parseInt( document.u2['rooms'].value, 10 );
    inc_cc();
    if( workps_loaded[room] || room==0 ) {
      SelChange('rooms','workpl',Workps,true,my_workpl);
    } else {
      window.document.getElementById("hidFrame").src = '../Shared/load_workpl.asp?room='+room;
    }
  }
}


CntCPs_loaded = new Array();
function updateCntCPs()
{
  if( document.u2 && document.u2.cnt_cont_person && document.u2.cnt_company ) {
    var company = parseInt( document.u2['cnt_company'].value, 10 );
    inc_cc();

    if( CntCPs_loaded[company] || company ==0 ) {
      SelChange('cnt_company','cnt_cont_person', cntCPs, false, this.my_cnt_cont_person ? my_cnt_cont_person : null );
    } else {
       window.document.getElementById("hidFrame").src = '../Shared/load_cntcps.asp?company='+company;
    }
  }
}
