document.onkeydown=keydown;
document.onkeyup=keyup;
var mousestate;

function cms_mouse( action )
{
  switch(action){
    case 'up':
      mousestate=0;
      break;
    case 'down':
      mousestate=1;
      break;
  }
}

function cms_dolast(stem,id,menu )
{
var lastsy;
  if ( mousestate ){
    if ( typeof ( document.menuform.__lastsy ) != "undefined" ){
      lastsy=document.menuform.__lastsy.value;
      if ( lastsy=document.menuform.__lastsy.value ){
        document.menuform.__cmscount.value=id;
        do_submit(menu,"APPLAST");
      }
    }
  }
}

function cms_highlight( stem, id )
{
  var cn;
  el = document.getElementById(stem+id);
  cn = el.className;
  if ( /cmshigh\s*$/.test(el.className) ){
    //eval( "el.className=el.className.replace( /^(.*) cmshigh\s*$/, \"$1\")/" );
    el.className=el.className.replace( /^(.*) cmshigh\s*$/, "$1" );
    el.className=el.className.replace( /^cmshigh\s*$/, "" );
  }else{
    el.className=el.className+" cmshigh";
    //el.innerHTML="yes";

  }
}

function toggle(what,hid)
{
  var el;
  var action;
  var dbdataX;
  var dbdataY;
  var tabX;
  var tabY;
  el = document.getElementById(what);
  if ( el.style.display != 'none' ){
    eval( "document.menuform."+hid+".value=0" );
    el.style.display = 'none';
    action=1;
  }else{
    eval( "document.menuform."+hid+".value=1" );
    el.style.display = '';
    action=0;
  }
  switch(what)
  {
    case 'sidemenu':
      if ( action ){
        dbdataX=document.menuform.__dbdataX_noside.value;
        dbdataY=document.menuform.__dbdataY_noside.value;
        tabX=document.menuform.__tabX_noside.value;
        tabY=document.menuform.__tabY_noside.value;
        settop("dbdata", dbdataX, dbdataY );
        settop("tabmenu", tabX, tabY);
      }else{
        dbdataX=document.menuform.__dbdataX_normal.value;
        dbdataY=document.menuform.__dbdataY_normal.value;
        tabX=document.menuform.__tabX_normal.value;
        tabY=document.menuform.__tabY_normal.value;
        settop("dbdata", dbdataX, dbdataY );
        settop("tabmenu", tabX, tabY);
      }
      break;
    default:
      break;
  }
 
}
function togglefullpage()
{
  var el = document.getElementById("sidemenu");
  if ( document.menuform.__fullpage.value == 0  ){
    hideall();
  }else{
    showall();
  }
}

function confirm_submit(menu, faction, mess )
{
  var outmess=Base64.decode(mess);
  if( confirm(outmess) ){
    do_submit(menu, faction);
  }
}

function do_submit(menu, faction, sec)
{
 
  with(document.menuform)
  {
    action="/";
    __menu.value=menu;
    __formaction.value=faction;
    if ( typeof(sec) != 'undefined' ){
      __sec.value=sec;
    }
    submit();
  }
}



function changepagecontrol( menu, action, mydesc )
{
  tbs=parseInt( eval( "document.menuform._TABPAGESTART_"+mydesc+".value" ) );
  tbe=parseInt( eval( "document.menuform._TABPAGEEND_"+mydesc+".value" ) );
  tbcs=parseInt( eval( "document.menuform._TABCONTROLSTART_"+mydesc+".value" ) );
  ponc=parseInt( eval( "document.menuform._TABPAGESONCONTROL_"+mydesc+".value" ) );
  maxp=parseInt( eval( "document.menuform._TABMAXPAGES_"+mydesc+".value" ) );
  switch( action ){
    case "INC":
      if ( tbcs+1 > maxp - ponc + 1  ){
        return;
      }
      new_tbcs=tbcs+1;
      break;
    case "DEC":
      if ( tbcs == 1 ){
        return;
      }
      new_tbcs=tbcs-1;
      break;
    case "NEXT":
      if ( tbcs+ponc > maxp - ponc + 1  ){
        new_tbcs=maxp-ponc+1;
      }else{
        new_tbcs=tbcs+ponc;
      }
      break;
    case "PREV":
      if ( tbcs-ponc < 1 ){
        new_tbcs=1;
      }else{
        new_tbcs=tbcs-ponc;
      }
      break;
    case "START":
      new_tbcs=1;
      break;
    case "END":
      new_tbcs=maxp-ponc+1;
      break;
    default:
      /* treat action as a number */
      actnum=parseInt(action);
      if ( isNaN(actnum) ){
        return;
      }
      if ( actnum < 1 ){
        return;
      }
      if ( actnum > maxp-ponc+1 ){
        new_tbcs=maxp-ponc+1;
      }else{
        new_tbcs=actnum;
      }
  }
  new_tbcs=Math.max(1,new_tbcs);
  if ( new_tbcs == tbcs ){
    return;
  }
  /* 
   * at this point we know that we have to do something 
   * loop through the all the rendered pages and change the
   * page control
   * */

  for(i=tbs;i<=tbe;i++){
      pagesel=document.getElementById( "_PAGESEL_"+mydesc+"_"+i );
      newhtml="<ul>";
      for( lp=new_tbcs;lp<Math.min(new_tbcs+ponc,maxp+1);lp++ ){
        if ( lp > tbe || lp < tbs ){
          cache="tablecontrolnotcached";
        }else{
          cache="";
        }
        /* should we highligh */
        if ( lp == i ){
          sel="tablecontrolselected";
        }else{
          sel="";
        }
        newhtml=newhtml+"<li class=\"tablecontrol "+cache+" "+sel+"\"><a href=\"javascript:newgotopage("+menu+","+lp+",'"+mydesc+"')\">"+lp+"</a></li>";
      }
      newhtml=newhtml+"</ul>";
      pagesel.innerHTML=newhtml;
  }
  /* update the hidden form input */
  eval( "document.menuform._TABCONTROLSTART_"+mydesc+".value="+new_tbcs );
  return;
}

function quickgotopage(menu, mydesc, page )
{
  el = document.getElementById( "__"+mydesc+"_goto_"+page );
  /*el = eval( "document.getElementById( '__"+mydesc+"_goto')"); */
  /*changepagecontrol( menu, el.value, mydesc );*/
  newgotopage( menu, el.value, mydesc );
  return;
}

function guardgotopage( menu, mydesc )
{
  num_rows=parseInt( eval( "document.menuform._TABNUMROWS_"+mydesc+".value" ) );
  if ( isNaN(num_rows) ){
    return;
  }
  if( num_rows > 1000 ){
    alert( "There are "+num_rows+" in this table. "+
           "Displaying more than 1000 will probably cause a problem in" +
           "your browser, please use the CSV download button to "+
           "download the whole table instead of trying to display it." );

    return;
  }
  if ( num_rows > 500 ){
    r=confirm( "There are "+num_rows+" rows in this table. "+
           "Are sure you want to display them all ?" );
    if( r == false ){
      return;
    }
  }
  gotopage( menu, 'ALL', mydesc );
}

function pagecontroltocurrent( menu,mydesc )
{
  curp=parseInt( eval( "document.menuform._TABCURRENTPAGE_"+mydesc+".value" ) );
  changepagecontrol(menu,curp,mydesc );
}

function newgotopage( menu, page, mydesc )
{
  tbs=parseInt( eval( "document.menuform._TABPAGESTART_"+mydesc+".value" ) );
  tbe=parseInt( eval( "document.menuform._TABPAGEEND_"+mydesc+".value" ) );
  tbcs=parseInt( eval( "document.menuform._TABCONTROLSTART_"+mydesc+".value" ) );
  ponc=parseInt( eval( "document.menuform._TABPAGESONCONTROL_"+mydesc+".value" ) );
  maxp=parseInt( eval( "document.menuform._TABMAXPAGES_"+mydesc+".value" ) );
  curp=parseInt( eval( "document.menuform._TABCURRENTPAGE_"+mydesc+".value" ) );
  apage=page;
  page=parseInt(page);

  switch( apage ){
    case "NEXT":
      page=curp+1;
      break; 
    case "PREV":
      page=curp-1;
      break;
  }

  if( isNaN(page) ){
    return;
  }
  if ( page > maxp ){
    return;
  }
  if ( page < 1 ){
    return;
  }

  el = eval( "document.getElementById( '__"+mydesc+"_goto')");
  with(document.menuform)
  {
    __pagereq.value=page;
    __passdesc.value=mydesc;
  }
   
  if ( page > tbe || page < tbs ){
    do_submit( menu, '__GOTOPAGE' );
    return;
  }
  eval( "document.menuform._TABCURRENTPAGE_"+mydesc+".value=page" );

  for(i=tbs;i<=tbe;i++){
    table=document.getElementById( "_TAB_"+mydesc+"_page_"+i );
    if ( i == page ){
      table.style.display='';
    }else{
      table.style.display='none';
    }
  }
}

function gotopagenum(menu, mydesc )
{
  el = eval( "document.getElementById( '__"+mydesc+"_goto')");
  with(document.menuform)
  {
    __pagereq.value=el.value;
    __passdesc.value=mydesc;
  }
  do_submit( menu, "__GOTOPAGE" );
}
function gotopage(menu, page, mydesc )
{
  with(document.menuform)
  {
    __pagereq.value=page;
    __passdesc.value=mydesc;
  }
  do_submit( menu, "__GOTOPAGE" );
}

function bodyload()
{
  var x=document.menuform.__xscroll.value;
  var y=document.menuform.__yscroll.value;
  var el;
  window.scrollTo( x, y );
  if ( el=document.getElementById("messout") ){
    el.scrollTop = 10000;
  }
}

function docsv( menu, mydesc )
{
  document.menuform.__passdesc.value=mydesc;
  document.menuform.__CSVALL.value=0;
  do_submit( menu, '__CSV' ); 
}
function docsv_all( menu, mydesc )
{
  document.menuform.__passdesc.value=mydesc;
  document.menuform.__CSVALL.value=1;
  do_submit( menu, '__CSV' ); 
}
function rmfilter( menu, filter, mydesc )
{
  menuform.__passdesc.value=mydesc;
  menuform.__filter.value=filter;
  menuform.__pagereq.value="rmfilter";
  do_submit( menu, "__GOTOPAGE" );
}
function dofilter( menu, filter, mydesc )
{
  menuform.__passdesc.value=mydesc;
  menuform.__filter.value=filter;
  menuform.__pagereq.value="filter";
  do_submit( menu, "__GOTOPAGE" );
}
function doorderby(menu, orderby, ascdsc, faction, mydesc )
{
  with(document.menuform)
  {
    __orderby.value=orderby;
    __ascdsc.value=ascdsc;
    menuform.__passdesc.value=mydesc;
  }
  do_submit(menu, faction );
}
function do_showhide(menu, divname)
{
  document.menuform.__showhide.value=divname;
  do_submit(menu, "SHOWHIDE" );
}

function do_AZ(menu, azaction, let)
{
  document.menuform.__menu.value=menu;
  document.menuform.__formaction.value=azaction;
  document.menuform.__AZsearch.value=let;
  do_submit(menu, azaction); 
}

function do_tmode( menu, mode, mydesc )
{
  document.menuform.__menu.value=menu;
  document.menuform.__tmode.value=mode;
  document.menuform.__passdesc.value=mydesc;
  document.menuform.__pagereq.value="MODE";
  do_submit(menu, "__GOTOPAGE" );
}

function do_colvis(menu, col, faction, mydesc )
{
  document.menuform.__menu.value=menu;
  document.menuform.__passdesc.value=mydesc;
  document.menuform.__showhide.value=col;
  do_submit(menu, faction );
}

function do_cal( ts )
{
  document.calform.__sec.value=ts;
  document.calform.submit();
}

function noenter(e)
{
  var key = window.event?e.keyCode : e.which;
  if ( key == 13 ){
    return false;
  } else {
    return true;
  };
}


var browserType;

if (document.layers) {browserType = "nn4"}
if (document.all) {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {
 browserType= "gecko"
}

function hideall() 
{
   var dbdataX = document.menuform.__dbdataX_full.value;
   var dbdataY = document.menuform.__dbdataY_full.value;
   hide2("tabmenu");
   hide2("cal");
   hide2("topleft");
   hide2("sidemenu");
   hide2("messout");
   hide2("footer" );
   document.menuform.__fullpage.value=1;
   settop("dbdata", dbdataX,dbdataY );
}

function calon()
{
  var el;
  if (browserType == "gecko" )
     el = document.getElementById("cal");
  else if (browserType == "ie")
     el = document.getElementById("cal");
  else
     el = document.layers["cal"];

  if ( el.style.display == 'none' ){
    document.menuform.__calendar.value=1;
    el.style.display = '';
  }


}


function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

//function diary_mode()
//{
//  var mode_el = new Array();
//  var modeclass;
//  modeclass=document.menuform.__modeclass.value;
//  mode_el=getElementsByClassName(document.getElementById("dbdata"), "input", modeclass  );
//  if ( mode_el[0].checked ){
//    return( 1 );
//  }else{
//    return( 0 );
//  }
//}
//function change_diary_mode( mode )
//{
//  var mode_el = new Array();
//  var modeclass;
//  modeclass=document.menuform.__modeclass.value;
//  mode_el=getElementsByClassName(document.getElementById("dbdata"), "input", modeclass  );
//  switch ( mode ){
//    case 1:
//      if ( mode_el[0].checked == 1 ){
//        return;
//      }
//      mode_el[0].checked=1;
//      ctl_move(1);
//      document.menuform.__app_id.value=-1;
//      break;
//    case 0:
//      if ( mode_el[1].checked == 1 ){
//        return;
//      }
//      mode_el[1].checked=1;
//      ctl_move(0);
//      break;
//  }
//}
function confirm_delete( )
{
  var app_id;
  var answer;
  var bookdesc;
  app_id=document.menuform.__app_id.value;
  var menu=document.menuform.__menu.value;
  if ( app_id > -1 ){
    eval( "bookdesc=document.menuform.__app_"+app_id+".value" );
    answer=confirm( "Ok to delete booking `"+bookdesc+"`?" );
    if ( answer ){
      do_submit(menu,"DELETEBOOK");
    }
  }
}


function inverseHEX(HEXcolour)
{
 if(  HEXcolour.length <6 || HEXcolour.length>6 ){
   return false;
 }
 a=HEXcolour.slice(0,2);
 b=HEXcolour.slice(2,4);
 c=HEXcolour.slice(4,6);
 a1=16*getHex(a.slice(0,1));
 a2=getHex(a.slice(1,2));
 a=a1+a2;
 b1=16*getHex(b.slice(0,1));
 b2=getHex(b.slice(1,2));
 b=b1+b2;
 c1=16*getHex(c.slice(0,1));
 c2=getHex(c.slice(1,2));
 c=c1+c2;
 newColor=DecToHex(255-a)+""+DecToHex(255-b)+""+DecToHex(255-c)
 return( newColor );
}
var hexbase="0123456789ABCDEF";
function DecToHex(number) {
 return hexbase.charAt((number>> 4)& 0xf)+ hexbase.charAt(number& 0xf);
 }
function getHex(s){
 s=s.toUpperCase();
 return parseInt(s,16);
}
 
function get_colour_inverse( candidate )
{
  if ( /^rgb/.test(candidate) ){
    var match=/rgb\(\s*([0-9]+),\s*([0-9]+),\s*([0-9]+)\s*\)/i.exec( candidate );
    var red=255-match[1];
    var green=255-match[2];
    var blue=255-match[3];
    return( "rgb( "+red+", "+green+" ,"+blue+" )");
  }
  if ( /^#[0-9a-fA-F]{6}/.test( candidate ) ){
    var match=/^#([0-9a-fA-F]{6})/.exec( candidate );
    return( inverseHEX( match[1] ) );
  }
}

var high_app_id=-1;
var save_bg_color="";
var save_fg_color="";
function  highlight_app ( app_id, action )
{
  var ss=eval( "document.menuform.__app_"+app_id+"_startslot.value" );
  var es=eval( "document.menuform.__app_"+app_id+"_endslot.value" );
  var c=eval( "document.menuform.__app_"+app_id+"_col.value" );
  var highlight=document.menuform.__highlight.value;
  var i;
  switch( action ){
    case "on":
      for(i=parseInt(ss);i<=parseInt(es);i++){
        bk=document.getElementById( c+"_"+i );
        save_bg_color=bk.style.backgroundColor; 
        save_fg_color=bk.style.color;
        bk.style.backgroundColor=get_colour_inverse(save_bg_color );
        bk.style.color=get_colour_inverse(save_fg_color);
      }
      high_app_id=app_id;
      break;
    case "off":
      for(i=parseInt(ss);i<=parseInt(es);i++){
        bk=document.getElementById( c+"_"+i );
        if ( eval( "/ "+highlight+"\s*$/.test(bk.className)") ){
          eval( "bk.className=bk.className.replace( /^(.*) "+highlight+"\s*$/, \"$1\" )" );
        }else if ( eval( "/^"+highlight+" /.test(bk.className)") ){
          eval( "bk.className=bk.className.replace( /^"+highlight+" (.*)$/, \"$1\" )" );
        }else if (eval( "/ "+highlight+" /.test(bk.className)") ){
          eval( "bk.className=bk.className.replace( /^(.*) "+highlight+" (.*)$/, \"$1 $2\" )" );
        }
        bk.style.backgroundColor=save_bg_color;
        bk.style.color=save_fg_color;
      }
      high_app_id=-1;
      break;
  }
}
function mod_booking( app_id, resid )
{
  //change_diary_mode(0);
  dateclass=document.menuform.__dateclass.value;

  if ( high_app_id != -1 ){
    highlight_app( high_app_id, "off" );
  }
  highlight_app( app_id, "on"  );

  document.getElementById(document.menuform.__bstart.value).value=
         hdate(eval( "document.menuform.__app_"+app_id+"_start.value" ));
  document.getElementById(document.menuform.__bend.value).value=
         hdate(eval( "document.menuform.__app_"+app_id+"_end.value" ));
  document.getElementById(document.menuform.__resinp.value).value=resid;
  document.getElementById(document.menuform.__description.value).value=
         eval( "document.menuform.__app_"+app_id+".value" );
  document.menuform.__app_id.value=app_id;
  document.getElementById(document.menuform.__noteinp.value).value=
         eval( "Base64.decode(document.menuform.__app_"+app_id+"_note.value)" );

  //document.body.style.cursor='move';
}

function diary_update()
{
  if ( document.menuform.__app_id.value == -1 ){
    alert( "Please select an appointement to modify!" );
    return;
  }
  with(document.menuform)
  {
    do_submit( __menu.value, "MODIFYBOOK" );
  }
}
function diary_clear()
{
  if ( high_app_id != -1 ){
    highlight_app( high_app_id, "off" );
  }
  /*document.getElementById(document.menuform.__bstart.value).value="NOT SET";
  document.getElementById(document.menuform.__bend.value).value="NOT SET";
  document.getElementById(document.menuform.__resinp.value).value="-1";*/
  document.getElementById(document.menuform.__description.value).value="";
  document.body.style.cursor='auto';
  document.menuform.__app_id.value=-1;
  document.getElementById(document.menuform.__noteinp.value).value="";
}

function ctl_move( state )
{
  var mb;
  var movebook;
  movebook=document.menuform.__movebook.value;
  mb=document.getElementById(movebook);
  mb.disabled=state;
  //if ( state == 1 ){
    diary_clear();
  //}
}

//function do_booking_booked( startdate, enddate, resid, app_id )
function do_booking_booked( day, slot, resid, appid )
{
  if ( diary_mode() == 0 && document.menuform.__app_id.value == -1 ){
    mod_booking( app_id, resid );
  }else{
    do_booking_slot( day, slot, resid ); 
  }
}

function do_booking_slot( day, slot, res )
{
  var ddate;
  var starttime;
  var endtime;
  ddate=eval( "document.menuform.__diary_day_"+day+".value" );
  starttime=eval( "document.menuform.__slot_"+slot+"_start.value" );
  endtime=eval( "document.menuform.__slot_"+slot+"_end.value" );
  do_booking( ddate+" "+starttime, ddate+" "+endtime, res );
}
//function do_booking( startdate, enddate, resid )
//{
//  //alert( startsql+" "+resid );
//  var diaryel = new Array();
//  var diaryeldesc = new Array();
//  var bookmess;
//  var hdate;
//  var dateclass;
//  var resclass;
//  var moveid;
//  var movebook;
//  var side;
//  var tab;
//  var resel = new Array(); 
//  var i = -1;
// 
//  if ( diary_mode() == 0 && document.menuform.__app_id.value == -1 ){
//    return;
//  }
//
//  dateclass=document.menuform.__dateclass.value;
//  resclass=document.menuform.__resclass.value;
//  markerclass=document.menuform.__markerclass.value;
//  
//  marker=getElementsByClassName(document.getElementById("dbdata"), "input", markerclass  );
//  resel=getElementsByClassName(document.getElementById("dbdata"), "select", resclass  );
//  diaryel=getElementsByClassName(document.getElementById("dbdata"), "input", dateclass  );
//  
//  moveid=document.menuform.__movebook.value;
//  movebook=document.getElementById( moveid );
//
//  if ( movebook.checked ){
//    marker[0].checked=1;
//  }
//  if ( marker[0].checked ){
//    i=0;
//  }
//  if ( marker[1].checked ){
//    i=1;
//  }
//  if ( diary_mode() == 1 &&  i==0  ){
//    resel[0].value=resid;
//  }
//  switch (i){
//    case 0:
//      //document.menuform.__bookstart.value=startdate;
//      hdate=startdate;
//      marker[1].checked=true;
//      break;
//    case 1:
//      //document.menuform.__bookend.value=enddate;
//      hdate=enddate;
//      marker[0].checked=true;
//      break;
//  }
//  //Convert to human date format
//  hdate=hdate.replace( /^(....)-(..)-(..) (..):(..):(..)$/, "$3/$2/$1 $4:$5" );
//  diaryel[i].value=hdate;
//  //diaryeldesc[i].innerHTML=bookmess+hdate;
//  if( movebook.checked ){
//    side=document.menuform.__side.value;
//    tab =document.menuform.__tab.value; 
//    do_submit( side,tab,"MOVEBOOK")
//  }
//}

function move_book(ts,res,stay)
{
  if ( highlight ){
    return;
  }
  if ( document.menuform.__app_id.value == -1 ){
    highlight=true;
    alert( "Please select an appointement to move!" );
    return;
  }
  if ( res > -1 ){
    document.getElementById(document.menuform.__resinp.value).value=res;
  }
  with(document.menuform)
  {
    __moveto_stay.value=stay;
    __moveto.value=ts;
    do_submit( __menu.value, "MOVEBOOK" );
  }
}
function cellbook( startslot, endslot , res, action )
{
   var aslot;
   var td;
   var dynabook="dynabook";
   for( aslot=startslot;aslot<=endslot;aslot++){
     td=document.getElementById( res+"_"+aslot );
     switch ( action ){
       case 'on':
           td.className=td.className+" "+dynabook;
         break;
       case 'off':
           eval( "td.className=td.className.replace( /^(.*) "+dynabook+"\s*$/, \"$1\" )");
           eval( "td.className=td.className.replace( /^"+dynabook+"\s*$/, \"$1\" )");
         break;
     }
   }
}
function keypress(evt)
{
   //alert( "in here" );
   //alert( "keycode is "+evt.keyCode );
  if ( typeof( document.menuform ) != "undefined" && typeof( document.menuform.__copyswitch ) != "undefined" && indiary ){
    evt = (evt) ? evt : ((event) ? event : null);
    if ( evt ){
      if ( evt.keyCode == 46 ){
        confirm_delete();
      }
    }
  }

}
function keydown(evt)
{
  if ( typeof( document.menuform ) != "undefined" && 
       typeof( document.menuform.__copyswitch ) != "undefined" ){
    if  ( indiary ){
      evt = (evt) ? evt : ((event) ? event : null);
      if ( evt ){
        if ( evt.keyCode == 46 ){
          confirm_delete();
          return;
        }
      }
    }
    document.menuform.__copyswitch.value=1;
  }
}
function keyup()
{
  if ( typeof( document.menuform ) != "undefined" &&
       typeof( document.menuform.__copyswitch ) != "undefined" ){
    document.menuform.__copyswitch.value=-1;
  }

}

var highlight = true;
var bss;
var bss_endtime;
var bsr;
var ess=-1;
var selection_vis=false;
var indiary=false;
function drag_book( state, day, slot, column_id, res  )
{
  var ddate;
  var starttime;
  var endtime;
  var numslots=document.menuform.__num_slots.value;
  var slotnum;
  var s_input=document.getElementById(document.menuform.__bstart.value);
  var e_input=document.getElementById(document.menuform.__bend.value);
  var sdis=false;
  var edis=false;
  if ( s_input.disabled ) sdis=true;
  if ( e_input.disabled ) edis=true; 
  ddate=eval( "document.menuform.__diary_day_"+day+".value" );
  starttime=eval( "document.menuform.__slot_"+slot+"_start.value" );
  endtime=eval( "document.menuform.__slot_"+slot+"_end.value" );
  slotnum=(day*numslots)+slot;
  if ( state ){
    //start of booking
    document.getElementById(document.menuform.__resinp.value).value=res;
    diary_celllight( column_id+res, slotnum, 0, true  );
    if ( ess != -1 ){
      cellbook( bss, ess, bsr, "off" ); 
      selection_vis=false;
    }
    if ( sdis ) s_input.disabled=false;
    s_input.value=hdate(ddate+" "+starttime);
    if ( sdis ) s_input.disabled=true;
    bss=slotnum;
    bss_endtime=hdate(ddate+" "+endtime);
    bsr=res;
    cellbook(bss, bss, bsr, "on" );
  }else{
    //end of booking
    ess=slotnum;
    if ( bss > ess ){
      var latch=bss;
      bss=ess;
      ess=latch;
      if ( edis ) e_input.disabled=false;
      e_input.value=bss_endtime;
      if ( sdis ) s_input.disabled=false;
      s_input.value=hdate(ddate+" "+starttime);
      if ( sdis ) s_input.disabled=true;
      if ( edis ) e_input.disabled=true;
    }else{
      if ( edis ) e_input.disabled=false;
      e_input.value=hdate(ddate+" "+endtime);
      if ( edis ) e_input.disabled=true;
    }
  }
  highlight=(! state) ;
  return false;
}
function hdate( d )
{
    return( d.replace( /^(....)-(..)-(..) (..):(..):(..)$/, "$3/$2/$1 $4:$5" ));
}


function diary_flyin( day, slot, column_id, res )
{
   var startslot;
   var endslot;
   var slotnum;
   var numslots=document.menuform.__num_slots.value;
   indiary=true;
   slotnum=(day*numslots)+slot;
   diary_celllight( column_id+res, slotnum, 1, false );
   if ( ! highlight ){
     if ( bss >= slotnum ){
       startslot=slotnum;
       endslot=bss;
     }else{
       startslot=bss;
       endslot=slotnum;
     }
     //alert( "calling cellbook with "+startslot+", "+endslot+", "+bsr+", on" );
     cellbook( startslot, endslot, bsr, "on" );  
     selection_vis=true;
   }
}
function diary_flyout( day, slot, column_id, res )
{
   var startslot;
   var endslot;
   var slotnum;
   var numslots=document.menuform.__num_slots.value;
   indiary=false;
   slotnum=(day*numslots)+slot;
   diary_celllight( column_id+res, slotnum, 0, false );
   if ( ! highlight ){
     if ( bss >= slotnum ){
       startslot=slotnum;
       endslot=bss;
     }else{
       startslot=bss;
       endslot=slotnum;
     }
     cellbook( startslot, endslot, bsr, "off" );  
     selection_vis=false;
   }
}

function diary_celllight( res, counter , action, skiphead )
{
  var td;
  var id;
  var flyover=document.menuform.__flyover.value;
  var timeflyover=document.menuform.__timeflyover.value
  var numslots=document.menuform.__num_slots.value;
  var slotnum;
  var daynum;
  slotnum=counter%numslots;
  //slotnum=counter;
  daynum=Math.floor(counter/numslots);
  id=res+"_"+counter;
  td=document.getElementById(id);
  tme=document.getElementById("time_"+slotnum);
  day=document.getElementById("date_"+daynum);
  //alert( "daynum is "+daynum );
  
  switch(action){
    case 0:
      if ( eval( "/"+flyover+"\s*$/.test(td.className)") && highlight ){
        eval( "td.className=td.className.replace( /^(.*) "+flyover+"\s*$/, \"$1\" )");
      }
      if ( ! skiphead ){
        if ( eval( "/"+flyover+"\s*$/.test(tme.className)") ){
          eval( "tme.className=tme.className.replace( /^(.*) "+timeflyover+"\s*$/, \"$1\" )");
        }
        if ( eval( "/"+flyover+"\s*$/.test(day.className)") ){
          eval( "day.className=day.className.replace( /^(.*) "+flyover+"\s*$/, \"$1\" )");
        }
      }
    break;
    case 1:
      if ( ! eval( "/"+flyover+"\s*$/.test(td.className)") && highlight ){
        td.className=td.className+" "+flyover;
      }
      if ( ! skiphead ){
        if ( ! eval( "/"+flyover+"\s*$/.test(tme.className)") ){
          tme.className=tme.className+" "+timeflyover;
        }
        if ( ! eval( "/"+flyover+"\s*$/.test(day.className)") ){
          day.className=day.className+" "+flyover;
        }
      }
    break;
  }
  //alert ( "classname is "+td.className );
}


function getElementsByClass(searchClass,tag) {
  var classElements = new Array();
  //if ( tag == null )
  // rather that searching the whole document
  var els = document.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
 }
  return classElements;
}


function hide_show(hide,show)
{
  var dbdivider=document.getElementById( "__div_norm_"+hide );
  var dbgray=document.getElementById( "__div_gray_"+hide );
  if ( dbdivider.style.display != 'none' ){
     dbdivider.style.display='none';
     dbgray.style.display='';
     eval( "document.menuform.__div_"+hide+".value=0" );
  }
  var dbdivider=document.getElementById( "__div_norm_"+show );
  var dbgray=document.getElementById( "__div_gray_"+show );
  if ( dbdivider.style.display == 'none' ){
     dbdivider.style.display='';
     dbgray.style.display='none';
     eval( "document.menuform.__div_"+show+".value=1" );
  }
}

function hide_dbdata(nm,m_id)
{
  var dbdivider=document.getElementById( "__div_norm_"+nm );
  var dbgray=document.getElementById( "__div_gray_"+nm );
  var isloaded;
  if ( dbdivider.style.display == 'none' ){
    
    eval( "isloaded=document.menuform.__div_loaded_"+nm+".value" );
    /* here if its not loaded we need to do a submit */
      eval( "document.menuform.__div_"+nm+".value=1" );
      if ( isloaded == '1'  ){
       dbdivider.style.display='';
       dbgray.style.display='none';
     }else{
       do_submit(m_id)
    }
  }else{
     dbdivider.style.display='none';
     dbgray.style.display='';
     eval( "document.menuform.__div_"+nm+".value=0" );
  }
}
/*
function hide_dbdata(num)
{
  var dbdivider=getElementsByClass("dbdivider","div");
  var dbgray=getElementsByClass("dbdivider-gray","div");
  if ( dbdivider[num].style.display == 'none' ){
     dbdivider[num].style.display='';
     dbgray[num].style.display='none';
     eval( "document.menuform.__div"+num+".value=1" );
  }else{
     dbdivider[num].style.display='none';
     dbgray[num].style.display='';
     eval( "document.menuform.__div"+num+".value=0" );
  }
}
*/

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
var btl_IntervalId=0;
var btl_divisor=1.75;
var sfx=0;
var sfy=0;
var tfx=0;
var tfy=0;
var dir_y=0;

function backtoleft()
{
  if ( btl_IntervalId != 0 ){
    clearInterval( btl_IntervalId );
  }
  var coord=getScrollXY();
  tfx=coord[0]*-1;
  tfy=coord[1]*-1;
  document.menuform.__xscroll.value=coord[0];
  document.menuform.__yscroll.value=coord[1];
  btl_IntervalId = setInterval( "backtoleft_interval()", 100 );
  dir_y=0;
  if ( sfy >= tfy ){
    dir_y=1;
  }


}

function backtoleft_interval()
{
   // document.body.scrollTop http://codepunk.hardwar.og.uk/ajs02.htm
   distx=((tfx-sfx)/btl_divisor);
   sfx=sfx+((tfx-sfx)/btl_divisor);
   sfy=sfy+((tfy-sfy)/btl_divisor);

   var el;

   m=sfx;
   b=sfy;
   t=sfy;

    if ( el = document.getElementById("cal") ){
      el.style.right=m+'px';
    }
    if ( el = document.getElementById("topright") ){
      el.style.right=m+'px';
    }
    
    n=m;
    if ( el = document.getElementById("messout") ){
      el.style.right=n+'px';
    }
   
    if ( document.menuform.__scroll_sidemenu.value == 1 ){
      var el = document.getElementById("sidemenu" );
      t=100-t;
      el.style.top=t+'px';
    }


    //if ( typeof docu
    if ( el=document.getElementById("cmseditor") ){
      cmsy=10-sfy;
      cmsx=10-sfx;
      el.style.top=cmsy+'px';
      el.style.left=cmsx+'px';
    }

    if( document.menuform.__browser.value == 'moz' ){
      if ( dir_y  ){
        b=b+17;
      }else{
        b=b+18;
      }
    }else{
      b=b+0;
    }
    //var el = document.getElementById("footer");
    //el.style.bottom=b+'px';
    //el.style.right=m+'px';
    //if ( Math.abs(sfx-tfx) <= 1 && Math.abs( sfy-tfy ) <= 1){
    //  clearInterval( btl_IntervalId );
    //}
}

function cms_off()
{
  var cms;
  if ( cms=document.getElementById( "cmseditor" ) ){
    hide2("cmseditor");
  }

}

function cms_insert_var(desc)
{
  var variable=document.getElementById(desc);
  if ( variable.value != -1  ){
    cms_insert( variable.value );
  }
}
function cms_insert( intext ) {
  //IE support
  var cmsvalue;
  cmsvalue=document.getElementById("cms_value");
  if (document.selection) {
    cmsvalue.focus();
    sel = document.selection.createRange();
    sel.text = intext ;
  } else if (cmsvalue.selectionStart || cmsvalue.selectionStart == '0') {
    //Mozilla/Firefox/Netscape 7+ support
    var startPos = cmsvalue.selectionStart;
    var endPos = cmsvalue.selectionEnd;
    var startstring=cmsvalue.value.substring( 0, startPos );
    var endstring=cmsvalue.value.substring( endPos, cmsvalue.value.length );
    cmsvalue.value=startstring+intext+endstring;
  } else {
    cmsvalue.value += cmsvalue.value;
  }
} 

function cms_clear( )
{
 var cmsvalue;
 cmsvalue=document.getElementById("cms_value");
 cmsvalue.value="";
}
function cms_editor( ent, num, wp )
{
 var cms;
 if ( cms=document.getElementById( "cmseditor" ) ){
   show2("cmseditor");
 }else{
   alert( "Cannot find the cmseditor, please check the layout carefully, a missing cmseditor layout directive, or a badly constructed comment can cause this problem." );
   exit;
 }
 var entry;
 eval( "entry=document.menuform.__"+ent+num+".value" );
 var cmsvalue;
 cmsvalue=document.getElementById("cms_value");
 var cmsval;
 cmsval=Base64.decode(entry);
 cmsvalue.value=cmsval;
 var cmsidvalue;
 var cmsid; 
 cmsidvalue=eval( "document.menuform.__sty"+num+".value" );
 cmsid=document.getElementById("cms_id");
 cmsid.value=cmsidvalue;
 var cmsid;
 cmsid=document.menuform.__cmsid.value;
 document.menuform.__cmsid.value=eval( "document.menuform.__hml"+num+".value" );
 document.menuform.__page.value=wp;
}

function showall()
{
   var dbdataX = document.menuform.__dbdataX_normal.value;
   var dbdataY = document.menuform.__dbdataY_normal.value;
   show2("tabmenu");
   if (  document.menuform.__calendar.value==1 ){
     show2("cal");
   }
   show2("topleft");
   show2("sidemenu");
   show2("messout");
   document.menuform.__fullpage.value=0;
   settop("dbdata", dbdataX, dbdataY  );
   show2("footer"); 
   show2("dbdata" );
   hide2("terms");
}
function settop(what, x, y)
{
  if (browserType == "gecko" )
     document.poppedLayer = 
         eval('document.getElementById("'+what+'")');
  else if (browserType == "ie")
     document.poppedLayer = 
        eval('document.getElementById("'+what+'")');
  else
     document.poppedLayer =   
        eval('document.layers["'+what+'"]');
  document.poppedLayer.style.top = y+"px";
  document.poppedLayer.style.left = x+"px";
}
function hide2(what) {

  if (browserType == "gecko" )
     document.poppedLayer = 
         eval('document.getElementById("'+what+'")');
  else if (browserType == "ie")
     document.poppedLayer = 
        eval('document.getElementById("'+what+'")');
  else
     document.poppedLayer =   
        eval('document.layers["'+what+'"]');
  document.poppedLayer.style.display = "none";
}

function show2(what) {
  if (browserType == "gecko" )
     document.poppedLayer = 
         eval('document.getElementById("'+what+'")');
  else if (browserType == "ie")
     document.poppedLayer = 
        eval('document.getElementById("'+what+'")');
  else
     document.poppedLayer = 
         eval('document.layers["'+what+'"]');
  document.poppedLayer.style.display = "inline";
  document.poppedLayer.style.display = "inline";
}


var messoutfirst=1;
function expandmdiv()
{
  var mdiv=document.getElementById("messout");
  var smallsize=document.menuform.__messoutsmallsize.value;
  var largesize=document.menuform.__messoutlargesize.value;
 
  if ( messoutfirst ){
    mdiv.style.height=largesize;
    messoutfirst=0;
  }else{
    mdiv.style.height=smallsize;
    messoutfirst=1;
  }
}


