/*

	gui.js
	
	javascript functions for the graphical user interface
	
	(c)2005 NIC

*/


// 	trigger onload when DOM is ready. for IE see ie_onload.js and the conditional 
//	comments in templates

//	(moz browsers)
if (document.addEventListener) 
{
    document.addEventListener("DOMContentLoaded", init, null);
}

function formCheck(formobj)
{
	// name of mandatory fields
	var fieldRequired = Array("naam", "woonplaats", "opleiding", "email", "telefoon");
	
	// field description to appear in the dialog box
	var fieldDescription = Array("Naam", "Woonplaats", "Opleiding", "E-mail", "Telefoon");
	
	// dialog message
	var alertMsg = "Vul de volgende velden in:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++)
	{
		var obj = formobj.elements[fieldRequired[i]];
		if (obj)
		{
			switch(obj.type)
			{
				case "select-one":
					if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
					break;
				case "select-multiple":
					if (obj.selectedIndex == -1){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
					break;
				case "text":
				case "textarea":
					if (obj.value == "" || obj.value == null){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
					break;
				default:
			}
			if (obj.type == undefined)
			{
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++)
				{
					if (obj[j].checked)
					{
						blnchecked = true;
					}
				}
				if (!blnchecked)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

function searchdefaults()
{
	document.getElementById('duur').value='all';
	document.getElementById('regio').value='all';
	document.getElementById('opleiding').value='all';
}

function init()
{
	add_events_links();
	prepare_menu();
	add_click_searchbox();
	
	setInterval("settop()", 100);
}



function settop()
{
	var knop_top = document.getElementById('top');
	var scrollTop = document.body.scrollTop || (document.documentElement && document.documentElement.scrollTop);

//	knop_top.className = (scrollTop > 0) ? "" : "out";

	if (scrollTop > 0)
	{
		if (knop_top.className != "")
			knop_top.className = "";

		var height = document.body.clientHeight;

	    if (document.documentElement && document.documentElement.clientHeight < height && document.documentElement.clientHeight > 0)
		{
			height = document.documentElement.clientHeight;
		}
	    knop_top.style.top = (height - 70 + scrollTop)+'px';
	}
	else
	{
		if (knop_top.className != "out")
			knop_top.className = "out";
	}
}

function _init_()
{
	//make sure we only get here once
	if (arguments.callee.done)
	{
		return;
	}
	arguments.callee.done = true;
	
	add_events_links();
	prepare_menu();
	add_click_searchbox();
	
	obj_voornaam = document.getElementById('voornaam');
	obj_email = document.getElementById('emailadres');
	
	if (obj_voornaam)
	{
		obj_voornaam.focus();
	}
	else if (obj_email)
	{
		obj_email.focus();
	}
}

function flash (id, pass)
{
	var field = document.getElementById(id);
	border = '2px solid red';
	if (pass % 2 == 0)
	{
		border = '2px dashed white';
	}
	field.style.border = border;
	if (pass < 15)
	{
		pass ++
		setTimeout("flash('" + id+ "','" + pass + "')",50);
	}
}


function add_events_links()
{
	var links = document.getElementsByTagName('a');
	var img = '&nbsp;<img src="/site/bouwselect.nl/images/newwindow.png" alt="(Deze link opent in een nieuw venster)" />';
	for (var i = 0; i < links.length; i++)
	{
		switch (links[i].className)
		{
			case 'skip':  
				links[i].onfocus = function () { show_skippy(this,true) }; 
				links[i].onblur = function () { show_skippy(this,false) };
			
				links[i].style.position = 'absolute';
				links[i].style.left = '-999em';
				break;
			case 'newwindow':
				links[i].setAttribute('title','[Nieuw venster] ' + links[i].getAttribute('title'));  
				links[i].onclick = newwin;
				links[i].innerHTML = links[i].innerHTML + img;
				break;
		}
	}
}

function newwin()
{
	window.open(this.href,'_blank');
	return false;
}

function show_skippy(skiplink,display)
{
	if (display)
	{
		skiplink.style.position = 'relative';
		skiplink.style.zIndex = '1000';
		skiplink.style.left = 'auto';
	}
	else
	{
		skiplink.style.position = 'absolute';
		skiplink.style.left = '-999em';
	}
}

function prepare_menu()
{
	var menu = document.getElementById('subnavlist')
	if (menu)
	{
		//collapse_all(menu);
		//apply_events(menu);
		//open_selected(menu);
	}
	var rubr = document.getElementById('rubrieken');
	if (rubr)
	{
		hilite_selected(rubr);
	}
}

function apply_events(menu)
{
	var lis = menu.getElementsByTagName('li');
	for (var n = 0; n < lis.length; n++)
	{
		if (lis[n].getElementsByTagName('ul').length > 0)
		{
			a = lis[n].getElementsByTagName('a')[0];
			if (lis[n].className.indexOf('index') == -1)
			{
				a.setAttribute('href','javascript:void(0)');
			}

			lis[n].className = 'closed';
			// nasty, but neccesary for IE...
			a.onclick = function()
						{
							toggle(this.parentNode.parentNode,true)
						};
			
			// make sure people using a keyboard can access the links
			a.onkeyup = function()
						{
							toggle(this.parentNode.parentNode,false)
						};
		}
	}
}


function add_click_searchbox()
{
	var s_box = document.getElementById('sq');
	if (s_box)
	{
		s_box.onclick = function(){clear_sbox(this)};
	}
	
}

function toggle(a_item, docollapse)
{
	var uls = a_item.getElementsByTagName('ul')[0];
	
	if (uls.style.display == 'none' || uls.style.display == '')
	{
		uls.style.display = 'block';
		a_item.className = 'opened';
	}
	else if (docollapse)
	{
		uls.style.display = 'none';
		a_item.className = 'closed';
	}	
}

function collapse_all(menu)
{
	var uls = menu.getElementsByTagName('ul');
	for (var n = 0; n < uls.length; n++)
	{
		uls[n].style.display = 'none';
	}
}

function open_selected(menu)
{
	var lis = menu.getElementsByTagName('li');
	var loc = String(document.location);
	
	for (var n = 0; n < lis.length; n++)
	{
		var href = lis[n].getElementsByTagName('a')[0];
		if (href == loc)
		{
			href.className = 'selected';
			p = lis[n].parentNode;

			// open all parents
			while(p.getAttribute('id') != 'subnavlist')
			{
				if (p.className == 'closed')
				{
					p.className = 'opened';
				}
				p.style.display='block';
				p = p.parentNode;
			}
			
			// open children
			if (ul == lis[n].getElementsByTagName('ul')[0])
			{
				ul.style.display = 'block';
			}
			break;
		}
	}

}

function clear_sbox(a_box)
{
	if (a_box.value == 'Uw zoekopdracht')
	{
		a_box.value = '';
	}
}