document.onkeydown=keydown;
document.onkeyup=keyup;
//document.onkeypress=keypress;
/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

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;
    __sec.value=sec;
    submit();
  }
}

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;
  window.scrollTo( x, y );
  var 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 expandmdiv()
{
  var mdiv=document.getElementById("messout");
  if ( mdiv.style.height == '50px' ){
     mdiv.style.height='200px';
  }else{
     mdiv.style.height='50px';
  }
}


function hide_dbdata(nm)
{
  var dbdivider=document.getElementById( "__div_norm_"+nm );
  var dbgray=document.getElementById( "__div_gray_"+nm );
  if ( dbdivider.style.display == 'none' ){
     dbdivider.style.display='';
     dbgray.style.display='none';
     eval( "document.menuform.__div_"+nm+".value=1" );
  }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);

   m=sfx;
   b=sfy;
   t=sfy;

    var el = document.getElementById("cal");
    el.style.right=m+'px';
    var el = document.getElementById("topright");
    el.style.right=m+'px';
    
    n=m;
    var 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 )
{
 var cms;
 if ( cms=document.getElementById( "cmseditor" ) ){
   show2("cmseditor");
 }
 var entry;
 eval( "entry=document.menuform.__"+ent+num+".value" );
 var cmsvalue;
 cmsvalue=document.getElementById("cms_value");
 cmsvalue.value=Base64.decode(entry);
 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" );
}

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";
}

/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* start */
var bustcachevar=0 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
  var page_request = false
  if (window.XMLHttpRequest) // if Mozilla, Safari etc
    page_request = new XMLHttpRequest()
  else if (window.ActiveXObject){ // if IE
    try {
     page_request = new ActiveXObject("Msxml2.XMLHTTP")
    } 
    catch (e){
      try{
        page_request = new ActiveXObject("Microsoft.XMLHTTP")
      }
      catch (e){}
    }
  } else
   return false
  page_request.onreadystatechange=function(){
    loadpage(page_request, containerid)
  }
  if (bustcachevar) //if bust caching of external page
    bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
    page_request.open('GET', url+bustcacheparameter, true)
    page_request.send(null)
}

function loadpage(page_request, containerid)
{
  if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
    document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
  if (!document.getElementById)
  return
  for (i=0; i<arguments.length; i++){
    var file=arguments[i]
    var fileref=""
    if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
      if (file.indexOf(".js")!=-1){ //If object is a js file
        fileref=document.createElement('script')
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", file);
      }else if (file.indexOf(".css")!=-1){ //If object is a css file
        fileref=document.createElement("link")
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", file);
      }
    }
    if (fileref!=""){
      document.getElementsByTagName("head").item(0).appendChild(fileref)
      loadedobjects+=file+" " //Remember this object as being already added to page
    }
  }
}
/* end */

/*
 *  md5.jvs 1.0b 27/06/96
 *
 * Javascript implementation of the RSA Data Security, Inc. MD5
 * Message-Digest Algorithm.
 *
 * Copyright (c) 1996 Henri Torgemane. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. 
 *
 * Of course, this soft is provided "as is" without express or implied
 * warranty of any kind.
 *
 * $Id: md5.js,v 1.1 2003/07/18 08:53:41 neg Exp $
 *
 */



function array(n) {
  for(i=0;i<n;i++) this[i]=0;
  this.length=n;
}

/* Some basic logical functions had to be rewritten because of a bug in
 * Javascript.. Just try to compute 0xffffffff >> 4 with it..
 * Of course, these functions are slower than the original would be, but
 * at least, they work!
 */

function integer(n) { return n%(0xffffffff+1); }

function shr(a,b) {
  a=integer(a);
  b=integer(b);
  if (a-0x80000000>=0) {
    a=a%0x80000000;
    a>>=b;
    a+=0x40000000>>(b-1);
  } else
    a>>=b;
  return a;
}

function shl1(a) {
  a=a%0x80000000;
  if (a&0x40000000==0x40000000)
  {
    a-=0x40000000;  
    a*=2;
    a+=0x80000000;
  } else
    a*=2;
  return a;
}

function shl(a,b) {
  a=integer(a);
  b=integer(b);
  for (var i=0;i<b;i++) a=shl1(a);
  return a;
}

function and(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0) 
    if (t2>=0) 
      return ((t1&t2)+0x80000000);
    else
      return (t1&b);
  else
    if (t2>=0)
      return (a&t2);
    else
      return (a&b);  
}

function or(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0) 
    if (t2>=0) 
      return ((t1|t2)+0x80000000);
    else
      return ((t1|b)+0x80000000);
  else
    if (t2>=0)
      return ((a|t2)+0x80000000);
    else
      return (a|b);  
}

function xor(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0) 
    if (t2>=0) 
      return (t1^t2);
    else
      return ((t1^b)+0x80000000);
  else
    if (t2>=0)
      return ((a^t2)+0x80000000);
    else
      return (a^b);  
}

function not(a) {
  a=integer(a);
  return (0xffffffff-a);
}

/* Here begin the real algorithm */

    var state = new array(4); 
    var count = new array(2);
	count[0] = 0;
	count[1] = 0;                     
    var buffer = new array(64); 
    var transformBuffer = new array(16); 
    var digestBits = new array(16);

    var S11 = 7;
    var S12 = 12;
    var S13 = 17;
    var S14 = 22;
    var S21 = 5;
    var S22 = 9;
    var S23 = 14;
    var S24 = 20;
    var S31 = 4;
    var S32 = 11;
    var S33 = 16;
    var S34 = 23;
    var S41 = 6;
    var S42 = 10;
    var S43 = 15;
    var S44 = 21;

    function F(x,y,z) {
	return or(and(x,y),and(not(x),z));
    }

    function G(x,y,z) {
	return or(and(x,z),and(y,not(z)));
    }

    function H(x,y,z) {
	return xor(xor(x,y),z);
    }

    function I(x,y,z) {
	return xor(y ,or(x , not(z)));
    }

    function rotateLeft(a,n) {
	return or(shl(a, n),(shr(a,(32 - n))));
    }

    function FF(a,b,c,d,x,s,ac) {
        a = a+F(b, c, d) + x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function GG(a,b,c,d,x,s,ac) {
	a = a+G(b, c, d) +x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function HH(a,b,c,d,x,s,ac) {
	a = a+H(b, c, d) + x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function II(a,b,c,d,x,s,ac) {
	a = a+I(b, c, d) + x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function transform(buf,offset) { 
	var a=0, b=0, c=0, d=0; 
	var x = transformBuffer;
	
	a = state[0];
	b = state[1];
	c = state[2];
	d = state[3];
	
	for (i = 0; i < 16; i++) {
	    x[i] = and(buf[i*4+offset],0xff);
	    for (j = 1; j < 4; j++) {
		x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8);
	    }
	}

	/* Round 1 */
	a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
	d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
	c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
	b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
	a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
	d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
	c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
	b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
	a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
	d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
	c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
	b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
	a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
	d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
	c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
	b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */

	/* Round 2 */
	a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
	d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
	c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
	b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
	a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
	d = GG ( d, a, b, c, x[10], S22,  0x2441453); /* 22 */
	c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
	b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
	a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
	d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
	c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
	b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
	a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
	d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
	c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
	b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */

	/* Round 3 */
	a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
	d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
	c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
	b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
	a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
	d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
	c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
	b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
	a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
	d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
	c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
	b = HH ( b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
	a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
	d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
	c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
	b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */

	/* Round 4 */
	a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
	d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
	c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
	b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
	a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
	d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
	c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
	b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
	a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
	d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
	c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
	b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
	a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
	d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
	c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
	b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */

	state[0] +=a;
	state[1] +=b;
	state[2] +=c;
	state[3] +=d;

    }

    function init() {
	count[0]=count[1] = 0;
	state[0] = 0x67452301;
	state[1] = 0xefcdab89;
	state[2] = 0x98badcfe;
	state[3] = 0x10325476;
	for (i = 0; i < digestBits.length; i++)
	    digestBits[i] = 0;
    }

    function update(b) { 
	var index,i;
	
	index = and(shr(count[0],3) , 0x3f);
	if (count[0]<0xffffffff-7) 
	  count[0] += 8;
        else {
	  count[1]++;
	  count[0]-=0xffffffff+1;
          count[0]+=8;
        }
	buffer[index] = and(b,0xff);
	if (index  >= 63) {
	    transform(buffer, 0);
	}
    }

    function finish() {
	var bits = new array(8);
	var	padding; 
	var	i=0, index=0, padLen=0;

	for (i = 0; i < 4; i++) {
	    bits[i] = and(shr(count[0],(i * 8)), 0xff);
	}
        for (i = 0; i < 4; i++) {
	    bits[i+4]=and(shr(count[1],(i * 8)), 0xff);
	}
	index = and(shr(count[0], 3) ,0x3f);
	padLen = (index < 56) ? (56 - index) : (120 - index);
	padding = new array(64); 
	padding[0] = 0x80;
        for (i=0;i<padLen;i++)
	  update(padding[i]);
        for (i=0;i<8;i++) 
	  update(bits[i]);

	for (i = 0; i < 4; i++) {
	    for (j = 0; j < 4; j++) {
		digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff);
	    }
	} 
    }

/* End of the MD5 algorithm */

function hexa(n) {
 var hexa_h = "0123456789abcdef";
 var hexa_c=""; 
 var hexa_m=n;
 for (hexa_i=0;hexa_i<8;hexa_i++) {
   hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
   hexa_m=Math.floor(hexa_m/16);
 }
 return hexa_c;
}


var ascii="01234567890123456789012345678901" +
          " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
          "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

function MD5(entree) 
{
 var l,s,k,ka,kb,kc,kd;

 init();
 for (k=0;k<entree.length;k++) {
   l=entree.charAt(k);
   update(ascii.lastIndexOf(l));
 }
 finish();
 ka=kb=kc=kd=0;
 for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8));
 for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8));
 for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8));
 for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8));
 s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka);
 alert( s );
 return s; 
}
