/*
** $Revision: 8 $
*/

// `detect' browser
var b = new Object;
b.w3c = !!(document.getElementById);
b.ns4 = !!(document.layers);
b.ie4 = !!(document.all && !b.w3c);
b.ie5 = !!(document.all && b.w3c);
b.ns6 = !!(b.w3c && navigator.userAgent.indexOf("Gecko/") >= 0);
b.safari = !!(b.w3c && navigator.userAgent.indexOf("Safari") >= 0);
b.mac = !!(navigator.platform.indexOf('Mac') == 0);
b.iemac = b.mac && (b.ie4 || b.ie5);

// create a click method for Gecko
if (b.ns6)
{
	try {
		// create a element so that HTMLAnchorElement is accessible
		document.createElement('a');
		HTMLElement.prototype.click = function () {
			if (typeof(this.onclick) == 'function') {
				var res = this.onclick({type: 'click'});
				if ((res == null || res) && this.href) 
				{
					window.open(this.href, this.target ? this.target : '_self');
				}
			}
			else if (this.href)
				window.open(this.href, this.target ? this.target : '_self');
		};
	}
	catch (e) {
		alert('click method for HTMLAnchorElement couldn\'t be added')
	}
}


function hl(obj, img)
{
	obj.src = img;
}

function unhl(obj)
{
	obj.src = 'images/nixel.gif';
}

var input_enabled = false;

var boxheight = 0;
var boxwidth = 0;
var speed = 20;
var pixelstep = 8;
var godown = true;
var timerID = null;
var curTop = null;
var curCat = null;

var outer,inner,elementheight,ref;
function element(ID)
{
	return b.ie4? (document.all[ID]): (document.getElementById(ID));
}

function elements(tagName)
{
	return b.ie4? (document.all) : (document.getElementsByTagName(tagName));
}

function elementsByName(name)
{
	if (b.ie4)
		return (document.all);
		
	alert("retreiving elements with name " + name);
	return document.getElementsByName(name);
}

function show_list(listName)
{
	var els = elements('select');
	var s = "";
	for (var i = 0; i < els.length; i++)
	{
		var id = els.item(i).id;
		if (id.indexOf('sel_') == 0)
		{
			els[id].style.display = (id == listName)? 'inline': 'none';
		}
	}
}

function change_prod_alt(objId, cat, img, sect, type, prodnr)
{
	update_inputs(cat, prodnr);
	element('prodcont').src = "prod.php?sect=" + sect + "&cat=" + cat + "&type=" + type + "&prodnr=" + prodnr;

	if ((typeof(img) != 'string') || (img.length == 0))
		img = 'images/nixel.gif';

	var prodimg = element('prodimg');
	if (prodimg)
	{
		prodimg.src = img;
	}
	outer.style.top = getPageTop(ref);
	outer.style.left = getPageLeft(ref);
}


function change_prod(objId, cat, img)
{
	update_inputs(cat, false);
	var objs = elements('a');
	for (var i = 0; i < objs.length; i++)
	{
		if (objs[i].id.indexOf('prod_' + cat) == 0)
		{
			if (objs[i].oldValue)
			{
				if (b.iemac)
					objs[i].style.color = objs[i].oldValue;
				else
					objs[i].className = objs[i].oldValue;

				objs[i].oldValue = null;
			}
		}
	}
	var obj = element(objId);
	if (b.iemac)
	{
		var temp = element('ref');
		obj.oldValue = obj.currentStyle.color;
		obj.style.color = temp.currentStyle.color;
	}
	else
	{
		obj.oldValue = obj.className;
		obj.className = 'activeprod';
	}
	if ((typeof(img) != 'string') || (img.length == 0))
		img = 'images/nixel.gif';

	var prodimg = element('prodimg');
	if (prodimg)
	{
		prodimg.src = img;
	}
}

var boxHeight = 0;
var boxScroll = 0;
var boxPos    = 0;
var boxTop    = 0;

var box_name  = '';
var box_outer = null;
var box_inner = null;
var box_ref   = null;

var box_timer = null;

function display_object(id, obj)
{
  var str = "";
	var lines = 0;

	for (i in obj)
	{
		str += id + "." + i + ": ";
		str += (typeof(obj[i]) == 'function')? 'function': obj[i];
		str += "\n";
		lines++;
		if (lines > 30)
		{
			alert(str);
			str = "";
			lines = 0;
		}
	}
	if (lines) alert(str);
}

function start_scrollbox(boxName)
{
	boxPos = 0;
	check_scrollbox(boxName);
	scroll_box(boxName, 0)
}

function init_scrollbox_vars(boxName)
{
	box_outer = element(boxName + "_outer");
	if (box_outer == null)
	{
		box_outer = element("outer");
		if (box_outer == null)
			return;
	}
	
	box_inner = element(boxName + "_inner");
	if (box_inner == null)
		return;

	box_name = boxName;
}

function check_scrollbox(boxName)
{
	init_scrollbox_vars(boxName);

	// alert('aap = ' + box_inner.innerHeight);
	// display_object('outer.style', box_outer.style);
	// display_object('i', box_inner.style);
	check_box_scrollers(boxName);
}

function start_scroll(boxName, dir)
{
	box_timer = setInterval('scroll_box("' + boxName + '", ' + dir + ');', 100);
}

function start_scroll2(dir)
{
	box_timer = setInterval('scroll_box("' + curCat + '", ' + dir + ');', 100);
}

function stop_scroll(boxName)
{
	if (box_timer)
	{
		clearInterval(box_timer);
		box_timer = null;
	}
}

function scroll_box(boxName, dir)
{
	init_scrollbox_vars(boxName);
	if (box_inner)
	{
		dir = dir * 5;
		if (dir < 0)
		{
			if ((boxPos + dir) > 0)
				boxPos += dir;
			else
			{
				boxPos = 0;
				stop_scroll(box_name);
			}
		}
		else
		{
			if ((boxPos + dir) < (boxScroll - boxHeight))
				boxPos += dir;
			else
			{
				boxPos = (boxScroll - boxHeight);
				stop_scroll(box_name);
			}
		}
		box_inner.style.top = (-boxPos) + 'px';
		check_box_scrollers(boxName);
	}
}

function check_box_scrollers(boxName)
{
	init_scrollbox_vars(boxName);
	boxHeight = parseInt(box_outer.style.height);
	boxScroll = parseInt(box_inner.offsetHeight);
	if (boxName == "car" || boxName == "moped" || boxName == "bike" || boxName == "truck")
		boxTop = -parseInt(box_inner.style.top);
	else
		boxTop = boxPos;

	var img_name = box_name;
	if (boxName == "car" || boxName == "moped" || boxName == "bike" || boxName == "truck")
		img_name = "box2";
	element(img_name + '_up').style.visibility = (boxTop > 0)? 'visible': 'hidden';
	element(img_name + '_down').style.visibility = (boxTop < (boxScroll - boxHeight))? 'visible': 'hidden';

	//var str = box_name;
	//str = str + " boxTop=" + boxTop;
	//str = str + " boxScroll=" + boxScroll;
	//str = str + " boxHeight=" + boxHeight;
	//str = str + " " + img_name + '_up='+ ((boxTop > 0)? 'visible': 'hidden');
	//str = str + ' ' + img_name + '_down=' + ((boxTop < (boxScroll - boxHeight))? 'visible': 'hidden');
	//if (box_name != "_box") alert(str);
}

function bar_hl(sect, catName)
{
	obj = element(catName);
	if (obj)
	{
		obj.src = "images/bar/" + sect + "_" + catName + "_hl.gif";
	}
}

function bar_unhl(sect, catName)
{
	if (catName == curCat)
		return;

	obj = element(catName);
	if (obj)
	{
		obj.src = "images/bar/" + sect + "_" + catName + "_on.gif";
	}
}


function change_cat(sect, catName)
{
	change_product(sect, catName, false);
}


function update_inputs(cat, prodnr)
{
	element('cat').value = cat;
	element('prodnr').value = prodnr;
}

var nextAction = null;

function change_product(sect, catName, prodNr)
{
	update_inputs(catName, prodNr);

	var wantFirst = true;
	if (prodNr)
	{
		wantFirst = false;
	}
	if (curCat != catName)
	{
		nextAction = "scroll_in('" + sect + "', '" + catName + "', " + wantFirst + ")";
		if (curCat)
		{
			var curAction = 'scroll_layer(false, "ref", "outer", "' + curCat + '_inner");';
			eval(curAction);
			curCat = catName;
			for (i in barCat)
			{
				bar_unhl(sect, barCat[i]);
			}
		}
		else
		{
			var temp = nextAction;
			nextAction = null;
			eval(temp);
		}
	}

	if (prodNr)
	{
		var el = element('prod_' + catName + '_' + prodNr);
		if (el.tagName == "OPTION")
		{
			eval(el.value);
		}
		else
		{
			el.click();
		}
	}
}


function scroll_in(sect, catName, selectFirst)
{
	//check_scrollbox(catName);
	
	curCat = catName;
	scroll_layer(true, "ref", "outer", curCat + "_inner");
	bar_hl(sect, curCat);
	var temp = element('catimg');
	if (temp)
	{
		temp.style.backgroundImage = "url(images/cat/" + catName + ".jpg)";
	}
	temp = element(catName + "_inner");
	if (selectFirst && temp && temp.firstChild)
	{
		if (temp.firstChild.tagName == "SELECT")
		{
			eval(temp.firstChild.value);
			/*
			temp = elements('option');
			for (var i = 0; i < temp.length; i++)
			{
				if (temp[i].id.indexOf('prod_' + catName) == 0)
				{
					eval(temp[i].value);
					break;
				}
			}
			*/
		}
		else
		{
			temp.firstChild.click();
		}
	}
}

function scroll_layer(openIt, refID, outerID, innerID)
{
	if (b.ns4)
	{
		window.status = "Upgrade your browser to Mozilla or Netscape 6+";
		return;
	}

	outer = element(outerID);
	if (outer == null)
		return;

	inner = element(innerID);
	if (inner == null)
		return;

	ref = element(refID);
	if (ref == null)
		return;

	boxwidth = parseInt(outer.style.width);
	boxheight = parseInt(outer.style.height);
	godown = openIt;

	scroll_it();
}

function getElHeight(el)
{
	if (el)
	{
		if (b.ie4 || b.ie5)
			return (el.style.height)? parseInt(el.style.height): parseInt(el.clientHeight);
		else 
			return (el.style.height)? parseInt(el.style.height): parseInt(el.offsetHeight);
	}
}

function getPageLeft(el)
{
	var x;
	if (b.ie4 || b.w3c)
	{
		x = 0;
		while(el.offsetParent != null)
		{
			x += el.offsetLeft;
			el = el.offsetParent;
		}
		x += el.offsetLeft;
		return x;
	}
}

function getPageTop(el)
{
	var y;
	var s = "";
	if (b.ie4 || b.w3c)
	{
		y = 0;
		while(el.offsetParent != null)
		{
			s += el.tagName + ':' + el.offsetTop + ';';
			y += el.offsetTop;
			el = el.offsetParent;
		}
		s += el.tagName + ':' + el.offsetTop + ';';
		//window.status = s;
		y += el.offsetTop;
		return y;
	}
}

function scrollbox()
{
	
	if (curTop == null)
		return;

	if (godown && (curTop >= 0))
	{
		clearInterval(timerID);
		timerID = null;
		curTop = null;
		if (nextAction != null)
		{
			setTimeout(nextAction, 10);
			nextAction = null;
		}
		else
		{
			check_scrollbox(curCat);
		}
	}
	else if ((!godown) && (curTop < -elementheight))
	{
		clearInterval(timerID);
		timerID = null;
		curTop = null;
		inner.style.visibility = 'hidden';
		outer.style.visibility = 'hidden';
		if (nextAction != null)
		{
			setTimeout(nextAction, 10);
			nextAction = null;
		}
		else
		{
			check_scrollbox(curCat);
		}
	}
	else
	{
		curTop += godown? pixelstep: -pixelstep;
		if (curTop > 0) curTop = 0;
		outer.style.top = getPageTop(ref);
		outer.style.left = getPageLeft(ref);
		inner.style.top = curTop + 'px';
	}
}

function scroll_it()
{
	if (timerID)
	{
		clearInterval(timerID);
		timerID = null;
	}

	elementheight = getElHeight(inner);
	
	if (((parseInt(inner.style.top) >= 0) && godown && (inner.style.visibility != 'hidden')) || 
	    ((parseInt(inner.style.top) <= -elementheight) && !godown))
	{
		return;
	}

	if (!curTop)
		curTop = (godown)? -elementheight: 0;

	outer.style.left = getPageLeft(ref) + 'px';
	outer.style.top = getPageTop(ref) + 'px';
	inner.style.top = curTop + 'px';
	inner.style.left = '0px';
	// inner.style.clip = 'rect(0px, ' + (boxwidth) + 'px, ' + (elementheight) + 'px, 0px)';
	outer.style.visibility = "visible";
	inner.style.visibility = "visible";

	if (elementheight < boxheight)
		inner.style.height = boxheight + 'px';


	timerID = setInterval('scrollbox()', speed);
}

function ref_resize()
{
	if (outer)
	{
		ref.style.visibility = 'hidden';
		ref.style.visibility = 'visible';
		outer.style.left = getPageLeft(ref);
		outer.style.top = getPageTop(ref);
	}
	if (box_outer)
	{
		/*
		box_outer.style.left = getPageLeft(box_ref) + 'px';
		box_outer.style.top = getPageTop(box_ref) + 'px';
		*/
	}
}

function walk_children(obj, res)
{
	for (var i = 0; i < obj.childNodes.length; i++)
	{
		res[res.length] = obj.childNodes[i];
		if (obj.childNodes[i].hasChildNodes())
		{
			res = walk_children(obj.childNodes[i], res);
		}
	}

	return res;
}

function children(obj)
{
	var res = new Array();

	res = walk_children(obj, res);

	return res;
}

function expand(id)
{
	var obj = element('carret_' + id);
	if (obj == null)
		return;

	var state = obj.state;
	if (state == 'expanded')
	{
		var tbl = element(id + '_table');
		if (tbl) tbl.className = 'hidden';
		if (1)
		{
			var tmp = children(tbl);
			for (var i = 0; i < tmp.length; i++)
			{
				if (tmp[i].tagName == 'INPUT' && tmp[i].className)
				{
					tmp[i].prevClass = tmp[i].className;
					tmp[i].className = 'blue';
				}
			}
		}
		obj.src = 'images/carret_right.gif';
		obj.state = 'imploded';
		obj.alt = 'uitklappen';
	}
	else
	{
		var tbl = element(id + '_table');
		if (tbl) tbl.className = 'visible';
		var tmp = children(tbl);
		for (var i in tmp)
		{
			if (tmp[i].prevClass)
			{
				tmp[i].className = tmp[i].prevClass;
				tmp[i].prevClass = null;
			}
		}
		obj.src = 'images/carret_down.gif';
		obj.state = 'expanded';
		obj.alt = 'inklappen';
	}
}

function setClasses(aIC)
{
	var obj = null;
	var i,id;
	for (i = 0; i < aIC.length; i += 2)
	{
		for (id in aIC[i])
		{
			obj = element(aIC[i][id]);
			if (obj) obj.className = aIC[i + 1];
		}
	}
}

function send_order(formId, fieldStr)
{
	var el = element(formId);
	el.action.value = 'order';

	var fields = fieldStr.split('|');
	var missing = null;
	for (var i = 0; !missing && i < fields.length; i++)
	{
		if (el[fields[i]].value.length == 0)
		{
			missing = fields[i];
		}
	}
	if (missing)
	{
		alert("Niet alle verplichte velden zijn ingevuld.");
		el[missing].focus();
	}
	else
	{
		el.submit();
	}
}

function cancel_order(formId)
{
	if (confirm('Druk op OK om uw bestelling te annuleren.'))
	{
		var el = element(formId);
		el.action.value = 'clear';
		el.submit();
	}
}

var visibleRegion;

function go_region(regionName, longName)
{
	var tmp = element('explain_outer');
	if (tmp)
	{
		if (tmp.hideAdd)
		{
			tmp.hideAdd.style.visibility = tmp.hideAdd.oldVisibility;
		}
		tmp.className='hidden';
	}
	element('regionname').innerHTML = 'Regio ' + longName;
	if (visibleRegion)
	{
		element('region_' + visibleRegion).className = 'hidden';
	}
	element('region_' + regionName).className = 'visible';
	var els = elements('td');
	var done = false
	var str = '';
	for (var i = 0; !done && i < els.length; i++)
	{
		if (els[i].id.indexOf('city_' + regionName) == 0)
		{
			var len = ('city_' + regionName).length;
			var s = parseInt(els[i].id.substring(len, els[i].id.length));
			show_city(regionName, s);
			done = true;
		}
	}
	visibleRegion = regionName;
	boxPos = 0;
	check_scrollbox('box');
	scroll_box('box', -1);
}

var selCity;

function show_city(region, id)
{
	if (selCity)
	{
		selCity.className = selCity.prevClass;
	}
	selCity = element('city_' + region + id);
	if (selCity)
	{
		selCity.prevClass = selCity.className;
		selCity.className = 'city_act';
	}
	element("cityinfo").src = "cityinfo.php?city=" + id;
}

var visibleProvince;

function go_province(provinceName, longName)
{
	var tmp = element('explain_outer');
	if (tmp)
	{
		if (tmp.hideAdd)
		{
			tmp.hideAdd.style.visibility = tmp.hideAdd.oldVisibility;
		}
		tmp.className='hidden';
	}
	element('provincename').innerHTML = 'Provincie ' + longName;
	if (visibleProvince)
	{
		element('province_' + visibleProvince).className = 'hidden';
	}
	element('province_' + provinceName).className = 'visible';
	var els = elements('td');
	var done = false
	var str = '';
	for (var i = 0; !done && i < els.length; i++)
	{
		if (els[i].id.indexOf('center_' + provinceName) == 0)
		{
			var len = ('center_' + provinceName).length;
			var s = parseInt(els[i].id.substring(len, els[i].id.length));
			show_center(provinceName, s);
			done = true;
		}
	}
	visibleProvince = provinceName;
	boxPos = 0;
	check_scrollbox('box');
	scroll_box('box', -1);
}

var selCenter;

function show_center(province, id)
{
	if (selCenter)
	{
		selCenter.className = selCenter.prevClass;
	}
	selCenter = element('center_' + province + id);
	if (selCenter)
	{
		selCenter.prevClass = selCenter.className;
		selCenter.className = 'city_act';
	}
	element("centerinfo").src = "centerinfo.php?center=" + id;
}

function open_regionmap()
{
	var sect   = element('sect').value;
	var cat    = element('cat').value;
	var prodnr = element('prodnr').value;
	location.href = 'regionmap.php?sect=' + sect + '&cat=' + cat + '&prodnr=' + prodnr;
}

function open_loadexam()
{
	var sect   = element('sect').value;
	var cat    = element('cat').value;
	var prodnr = element('prodnr').value;
	location.href = 'load_exam.php?sect=' + sect + '&cat=' + cat + '&prodnr=' + prodnr;
	// openFull('exam.php');
}

function move_to(obj, dst, className, hideAdd)
{
	var o = element(obj);
	var d = element(dst);

	o.style.left = getPageLeft(d);
	o.style.top = getPageTop(d);
	o.className = className;
	if (hideAdd)
	{
		var h = element(hideAdd);
		o.hideAdd = h;
		h.oldVisibility = h.style.visibility;
		h.style.visibility = 'hidden';
	}
}

function openFull(url)
{
	if (screen.width < 1024 && screen.height < 768)
	{
		if (url.indexOf("?") > -1)
			url = url + "&scale=yes";
		else
			url = url + "?scale=yes";
	}
	var nw = window.open(url, '_blank', 'fullscreen=yes,width=800,height=616');
}

function OpenPopup(url,w,h)
{
	var nw = window.open(url, '_blank', 'width='+w+',height='+h);
}


function ParentRefresh()
{
	if (window.opener)
		window.opener.location.reload();
}

function ParentOpen(url)
{
	if (window.opener)
	{
		window.opener.location.href = url;
		window.close();
	}
	else
		location.href = url;
}

function SelfOpen(url)
{
	location.href = url;
}

function CloseAll()
{
	if (window.opener)
	{
		//window.opener.opener = self;
		//window.opener.close();
	}
	window.close();
}

function PlayExam(cat,exam)
{
	var str="test=" + Math.random();
	document.cookie = str;
	if (document.cookie.indexOf(str) < 0)
		alert("Uw browser accepteert geen session-cookies.\nDeze zijn echter wel nodig voor het goed\nfunctioneren van de examens.\n\nIndien gewenst kunt u deze instelling aanpassen\nbij 'privacy', via de opties of instellingen\n(afhankelijk van uw type browser).")
	else
		openFull((detectFlash(6) ? "examflash" : "exam") + ".php?cat=" + cat + "&exam=" + exam);
}


// Flash detection unit
var flash2Installed = false;
var flash3Installed = false;
var flash4Installed = false;
var flash5Installed = false;
var flash6Installed = false;
var flash7Installed = false;
var flash8Installed = false;
var flash9Installed = false;
var flash10Installed = false;
var maxVersion = 10;             // highest version we can actually detect
var actualVersion = 0;
var hasRightVersion = false;
var jsVersion = 1.0;            // the version of javascript supported

var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false;

// This is a js1.1 code block, so make note that js1.1 is supported.
jsVersion = 1.1;

// IE on Windows doesn't support regular JavaScript plugins array detection.
if(isIE && isWin){
  document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
  document.write('on error resume next \n');
  document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
  document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
  document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
  document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');  
  document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');  
  document.write('flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n');  
  document.write('flash8Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.8"))) \n');
  document.write('flash9Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.9"))) \n');
  document.write('flash10Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.10"))) \n');
  document.write('</SCR' + 'IPT\> \n'); // break up end tag so it doesn't end our script
}

// Flashdetection has been put in a function, so it loads before it runs.
function detectFlash(requiredVersion)
{  
  if (navigator.plugins)
  {
    // check for flash 2 or flash 3+.
    if (navigator.plugins["Shockwave Flash 2.0"]
        || navigator.plugins["Shockwave Flash"]) {

      var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;

      // A flash plugin-description looks like this: Shockwave Flash 4.0 r5
      // We can get the major version by grabbing the character before the period
      var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
      if (flashDescription.charAt(flashDescription.indexOf(".") - 2) != ' ')
        flashVersion += 10*parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 2));

      flash2Installed = flashVersion == 2;    
      flash3Installed = flashVersion == 3;
      flash4Installed = flashVersion == 4;
      flash5Installed = flashVersion == 5;
      flash6Installed = flashVersion == 6;
      flash7Installed = flashVersion == 7;
      flash8Installed = flashVersion == 8;
      flash9Installed = flashVersion == 9;
      flash10Installed = flashVersion >= 10;
    }
  }
  
  // Loop through all versions we're checking, and
  // set actualVersion to highest detected version.
  for (var i = 2; i <= maxVersion; i++)
  {  
    if (eval("flash" + i + "Installed") == true) actualVersion = i;
  }
  
  if(navigator.userAgent.indexOf("WebTV") != -1) actualVersion = 3;  
  
  // return true if the user has a new enough version...
	//return confirm("flash gebruiken?");
  return (actualVersion >= requiredVersion);
}

// vim: set ts=2 sw=2 ai :
