//<script>

// Variables
var timeoutsList = {};
var timeoutLengt = 250;
var timeoutsListSub = {};
var active_menu = null;
var active_sub = null;
var preload_imgs = [
  "/pics/infobar_sluiten_over.gif",
  "/pics/infobar_openen_over.gif",
  "/pics/logo_over.jpg",
  "/pics/topbar_home_over.gif",
  "/pics/topbar_printversie_over.gif",
  "/pics/topbar_sitemap_over.gif",
  "/pics/btn_stuur_email_over.gif",
  "/pics/btn_inschrijven_over.gif",
];
var loaded_imgs = [];

var fade_steps = 8;
var fade_step_time = 12;
var fade_step = 100 / fade_steps;
var fade_type = null;

// Functions

function Obj (elId) {
  return document.getElementById(elId);
}

function SetImg (elId,imgUrl) {
  Obj(elId).src = imgUrl;
}

var debug_counter = 0;
function debug (func,id,oldact1,oldact2,img,parent) {
  return;
  Obj('debug').style.display = "block";
  debug_counter++;
  Obj('debug').innerHTML = debug_counter + ": " + func + " " + id + "," + oldact1 + "," + oldact2 + "," + img + "," + parent + "<br>" + Obj('debug').innerHTML
}

var SubMenuOpen = null;
var SubMenuClosing = false;
function ActivateMenu (elId,img) {
  if (timeoutsList[elId]) self.clearTimeout(timeoutsList[elId]);
  if (active_menu && active_menu[0] == elId) return;
  // One at a time
  debug(elId + " test " + SubMenuOpen);
  //mnu1 test 2_1
  if (SubMenuOpen && elId != "mnu" + SubMenuOpen.substr(0,1)) {
    SubMenuOpen = null;
    DeActivateAnySubMenu();
    }
  DeActivateAnyMenu();
  active_menu = [elId,img];
  //Obj("div_mnu_" + elId).style.visibility = "visible";
  SetImg("img_mnu_" + elId,"/pics/menu_" + img + "_over.gif");
  StartFade("div_mnu_" + elId,true);
}

function DeActivateMenu (elId,img,timed) {
  //alert(elId + " == mnu" + SubMenuOpen.substr(0,1));
  if (timeoutsList[elId]) self.clearTimeout(timeoutsList[elId]);
  if (timed) {
    debug("Block " + elId + " by submenu " + SubMenuOpen + "?");
    if (SubMenuOpen && elId == "mnu" + SubMenuOpen.substr(0,1)) {
      debug("Yes locked by submenu " + SubMenuOpen);
      return;
      }
    active_menu = null;
	//Obj("div_mnu_" + elId).style.visibility = "hidden";
    SetImg("img_mnu_" + elId,"/pics/menu_" + img + ".gif");
    StartFade("div_mnu_" + elId,false);
	}
  else {
    timeoutsList[elId] = self.setTimeout("DeActivateMenu('" + elId + "','" + img + "',true);",timeoutLengt);
	}
}

function DeActivateAnyMenu () {
  if (active_menu) DeActivateMenu (active_menu[0],active_menu[1],true);
  active_menu = null;
}

function ActivateSubMenu (id,img,parent) {
  var elId = "sub" + id;
  if (timeoutsListSub[elId]) self.clearTimeout(timeoutsListSub[elId]);
  if (active_sub && active_sub[0] == elId) return;
  // One at a time


  Obj("submnu_div_" + id).className = 'MenuItem NormalOver';
  Obj("submnu_a_" + id).style.color = "white";

  
  SubMenuClosing = false;
  SubMenuOpen = id;
  DeActivateAnySubMenu();
  active_sub = [elId,img];
  //Obj("div_mnu_" + elId).style.visibility = "visible";
  if (img) SetImg("img_mnu_" + elId,"/pics/menu_" + img + "_over.gif");
  // find X coord
  
  var parObj = Obj("submnu_div_" + id);
  var x = findX(parObj);
  var y = findY(parObj);
  x += parObj.offsetWidth;
  Obj("div_mnu_" + elId).style.left = x + "px";
  Obj("div_mnu_" + elId).style.top = y + "px";
  
  StartFade("div_mnu_" + elId,true);
}

function findX (obj) {
  var x = obj.offsetLeft;
  while (obj = obj.offsetParent) x += obj.offsetLeft;
  return x;
}

function findY (obj) {
  var x = obj.offsetTop;
  while (obj = obj.offsetParent) x += obj.offsetTop;
  return x;
}


function DeActivateSubMenu (elId,indirect,timed) {
  //var elId = "sub" + id;
  if (timeoutsListSub[elId]) self.clearTimeout(timeoutsListSub[elId]);
  if (timed) {
    if (!indirect) DeActivateAnyMenu();
    if (SubMenuClosing) {
      debug("SubMenuOpen wordt null want SubMenuClosing=" + SubMenuClosing);
      SubMenuOpen = null;
      }
    //if (elId == "mnu" + SubMenuOpen) SubMenuOpen = null;
    if (active_sub && active_sub[0]) {
      var xid = active_sub[0].substr(3);
      Obj("submnu_div_" + xid).className = 'MenuItem Normal';
      Obj("submnu_a_" + xid).style.color = "";
      }

    active_sub = null;
	//Obj("div_mnu_" + elId).style.visibility = "hidden";
    //if (img) SetImg("img_mnu_" + elId,"/pics/menu_" + img + ".gif");
    StartFade("div_mnu_" + elId,false);
	}
  else {
    SubMenuClosing = true;
    timeoutsListSub[elId] = self.setTimeout("DeActivateSubMenu('" + elId + "'," + (indirect ? "true" : "false") + ",true);",timeoutLengt);
	}
}

function DeActivateAnySubMenu () {
  if (active_sub) DeActivateSubMenu (active_sub[0],active_sub[1],true);
  active_sub = null;
}

/*
function ActivateMenu (elId,img,parent) {
  if (timeoutsList[elId]) self.clearTimeout(timeoutsList[elId]);
  debug("ActivateMenu", elId, (active_menu ? active_menu[0] : ""), (active_sub ? active_sub[0] : ""), img, parent);
  if (parent) {
    if (active_sub && active_sub[0] == elId) return;
    active_sub = [elId,img];
    }
  else {
    if (active_menu && active_menu[0] == elId) return;
    active_menu = [elId,img];
    //if (active_menu[0]) DeActivateAnyMenu();
    }
  // One at a time
  //if (!parent) DeActivateAnyMenu();
  //Obj("div_mnu_" + elId).style.visibility = "visible";
  if (img) SetImg("img_mnu_" + elId,"/pics/menu_" + img + "_over.gif");
  StartFade("div_mnu_" + elId,true);
}

function DeActivateMenu (elId,img,timed) {
  if (timeoutsList[elId]) self.clearTimeout(timeoutsList[elId]);
  if (timed) {
    active_menu = null;
	//Obj("div_mnu_" + elId).style.visibility = "hidden";
    if (img) SetImg("img_mnu_" + elId,"/pics/menu_" + img + ".gif");
    debug("DeActivateMenu timed", elId, "", img);
    StartFade("div_mnu_" + elId,false);
	}
  else {
    debug("DeActivateMenu 1", elId, "", img);
    timeoutsList[elId] = self.setTimeout("DeActivateMenu('" + elId + "','" + img + "',true);",timeoutLengt);
	}
}

function DeActivateAnyMenu () {
  if (active_menu) DeActivateMenu (active_menu[0],active_menu[1],true);
  active_menu = null;
}
*/
// Fading effects
function StartFade (elId,fadeIn) {
  // Is fading supported?
  if (!fade_type) {
    var obj = Obj(elId);
	if (typeof obj.style.MozOpacity != 'undefined') {		fade_type = 'moz';   	}
	else if (typeof obj.style.opacity != 'undefined') {				fade_type = 'w3c';  	}
	else if (typeof obj.style.KhtmlOpacity != 'undefined') {	fade_type = 'khtml';  	}
	else if (typeof obj.filters == 'object') {
	  //weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
	  //then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
	  //then the returned value type, which should be a number, but in mac/ie5 is an empty string
	  fade_type = (obj.filters.length > 0 && typeof obj.filters.alpha == 'object' && typeof obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
	  }
	else {
	  fade_type = "none";
	  }
  //fade_type = "none";
	//alert(fade_type);
	}
	
  if (fade_type == "none") {
    if (fadeIn) {
      Obj(elId).style.visibility = "visible";
      return true;
	  }
    else {
      Obj(elId).style.visibility = "hidden";
      return true;
	  }
    }

  var opactity = 100;
  var direction = (fade_step * -1);
  if (fadeIn) {
    opactity = 0;
	direction = fade_step;
    }
  Fader(elId,opactity,direction)
}

function Fader (elId,opacity,direction) {
  var toOpacity = opacity + direction;
  if (toOpacity <= 0) {
    toOpacity = 0;
    }
  else if (toOpacity >= 100) {
    toOpacity = 100;
	}
  var obj = Obj(elId);//.filters.alpha.opacity = toOpacity;
  var small = parseInt(toOpacity / 10) / 10;
  //restrict max opacity to prevent a visual popping effect in firefox
  if (small == 1) { small = 0.999999; }
  //alert(toOpacity + " = " + small);
  //Obj("DEBUG").value += "" + toOpacity + " = " + small + "\n";
  switch (fade_type)	{
		case 'ie' :
			try { obj.filters.alpha.opacity = toOpacity; }
      catch (err){ /* Ignore because this is just a visual effect */ };
			break;
			
		case 'khtml' :
			obj.style.KhtmlOpacity = toOpacity / 100;
			break;
			
		case 'moz' : 
			obj.style.MozOpacity = small;
			break;
			
		default : 
			obj.style.opacity = small;
	}  
  
  // Make visible or invisible at opacity 1 or 0
  if (toOpacity <= 0) {
    obj.style.visibility = "hidden";
    return true;
	}
  else {
    obj.style.visibility = "visible";
    if (toOpacity >= 100) {
      //if (fade_type == "ie") {
	  //  obj.style.filter = "";
	  //  }
	  return true;
      }
	}
  self.setTimeout("Fader('" + elId + "'," + toOpacity + "," + direction + ");",fade_step_time);
}

function MouseOverSubMenuItem (id,clss) {
  Obj("submnu_div_" + id).className = 'MenuItem ' + clss + 'Over';
  Obj("submnu_a_" + id).style.color = "white";
}
function MouseOutSubMenuItem (id,clss) {
  if (SubMenuOpen != id) {
    Obj("submnu_div_" + id).className = 'MenuItem ' + clss;
    Obj("submnu_a_" + id).style.color = "";
    }
}

function PreLoadImgs () { // preload images
  for (var i = 0; i < preload_imgs.length; i++) {
    //alert("preload '" + preload_imgs[i] + "'");
    if (preload_imgs[i]) {
      loaded_imgs[i] = new Image();
	    loaded_imgs[i].src = preload_imgs[i];
      }
	  }
}

var MO = (document.getElementById && !document.all) ? true : false;
    
function SetHtml (id,html) {
  if (MO) {
    document.getElementById(id).innerHTML = html;
    }
  else {
    document.all[id].innerHTML = html;
    }
  }

function InfobarInklappen (elId) {
  var obj = Obj(elId);
  obj.style.overflow = "hidden";
  for (var i = 122; i > 24; i -= 5) {
    self.setTimeout("Obj('" + elId + "').style.height='" + i + "px';",2 * (122 - i));
	}
  StartFade(elId,false);
}	  
       
     
// Bestel spul
// LET OP: (todo) Script vervange door server-side, cookie script werkt niet meer

function GetBestelLink (id,title,prijs,type,fast,remove) {
  title = title.replace(/'/,"\\'");
  return "<a href=\"" + remove +"\" onClick=\"Bestellen(" + id + ",'" + title + "'," + 
         prijs + ",'" + type + "'," + fast + ")\">";
}

function GetBestelHtml (id,title,prijs,type,fast,remove) {
  if (!fast) fast = false;
  if (prijs == "") prijs = "0";
  if (!remove) remove = "#Bestellen";
  var a = GetBestelLink(id,title,prijs,type,fast,remove);
  var html = "";
  if (bestellijst[id]) {
    if (fast) {
      html = a + "verwijderen van bestellijst.</a>";
      }
    else {
      html = a + "<img src=\"/pics/basket.gif\" border=0 align=left>" +
             "Deze publicatie staat op de bestellijst. " +
             "Klik om het van de bestellijst te verwijderen.</a><br><br>" +
             "Verstuur uw bestelling via de " +
             "<a href=\"/index.pl/nl/list/135\">Bestellijst pagina.</a>";
      }
    }
  else {
    if (fast) {
      html = a + "direct bestellen</a>";
      }
    else {
      html = a + "<img src=\"/pics/books.gif\" border=0 align=absmiddle>" +
             "Online bestellen.</a>";
      }
    }
  return html;
}
       
function Bestellen (id,title,prijs,type,fast) {
  var c = "";
  if (bestellijst[id]) {
    bestellijst[id] = false;
    c = title + " is verwijderd van de bestellijst.";
    }
  else {
    bestellijst[id] = [title,prijs,type];
    c = title + " is toegevoegd aan de bestellijst.";
    }
  var text = GetBestelHtml(id,title,prijs,type,fast);
  if (MO) {
    if (document.getElementById("Bestellen" + id))
      document.getElementById("Bestellen" + id).innerHTML = text;
    }
  else {
    if (document.all["Bestellen" + id])
      document.all["Bestellen" + id].innerHTML = text;
    }
  ShowBestellijst();
  SaveBestellijst();
  alert(c); 
}      

function ShowBestellijst () {
  //var cols = 24;
  //var sp = "                        ";
  var string = "";
  //var t = 0;
  for (id in bestellijst) {
    if (bestellijst[id]) {
      //var p = (bestellijst[id][1] + "").replace(/(\d\d)$/,",$1");
      //var l = cols - p.length;
      string += bestellijst[id][0] + "\n";
      //t +=  parseInt(bestellijst[id][1]);
      }
    }
  /*
  string += "------------------------\n";
  var p = (t + "").replace(/(\d\d)$/,",$1");
  var l = cols - p.length;
  string += ("Subtotaal:" + sp).substr(0,l - 1) + " " + p + "\n";
  var b = parseInt((t * 0.6) * 100) / 100;
  var btw = "+ 6% BTW (" + b + ")";
  string += sp.substr(0,cols - btw.length) + btw + "\n";
  var adm = "Administratiekosten: 3,00";
  string += sp.substr(0,cols - adm.length) + adm + "\n";
  string += "------------------------\n";
  t += btw + 300;
  p = (t + "").replace(/(\d\d)$/,",$1");
  l = cols - p.length;
  string += ("Totaal:" + sp).substr(0,l - 1) + " " + p + "\n";
 */
  if (string) {
    if (MO) {
      //document.getElementById("BestelLijst").style.visibility = "visible";
      document.getElementById("BestelLijst").style.display = "";
      }
    else {
      //document.all.BestelLijst.style.visibility = "visible";
      document.all.BestelLijst.style.display = "";
      }
    document.forms.BestelLijstForm.BestelLijstText.value = string;
    }
  else {
    if (MO) {
      //document.getElementById("BestelLijst").style.visibility = "hidden";
      document.getElementById("BestelLijst").style.display = "none";
      }
    else {
      //document.all.BestelLijst.style.visibility = "hidden";
      document.all.BestelLijst.style.display = "none";
      }
    }
}

function SaveBestellijst () {
  var string = "";
  for (id in bestellijst) {
    if (bestellijst[id]) {
      string += id + "|" + bestellijst[id][0] + "|" + 
                bestellijst[id][1] + "|" + bestellijst[id][2] + ";;";
      } 
    }
  string = string.replace(/;;$/,"");
  var now = new Date();
  now.setYear(now.getYear() + 1);
  setCookie("bestellijst",string,now,"/");
}

// Cookie script
// -------------
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

// Bestellijst inlezen
var bestellijst = new Object();
var bList = getCookie("bestellijst");
if (bList) {
  bList = bList.split(/;;/);
  for (var i = 0; i < bList.length; i++) {
    var l = bList[i].split(/\|/);
    bestellijst[l[0]] = [l[1],l[2],l[3]];
    }
  }

function checkForm () {
  var req = document.form.required.value.split(/,/);
  var msg = "";
  for (var i = 0; i < req.length; i++) {
    // check field
    if (document.form[req[i]].value == "") {
      msg += "- " + document.form[req[i]].title + "\n";
      }
    }
  if (msg != "") {
    alert("Wilt u de volgende velden nog invullen?\n\n" + msg);
    return false;
    }
  else {
    return true;
    }
}

