var validator = {};
var descriptor = {};

function validate(formname) {
	var theForm = document.getElementById(formname);
	if (!theForm) {
		alert('Oeps, het invulformulier is niet aanwezig.\nProbeer het morgen nog eens.');
		return false;
	}
	
	// clear objects
	validator = {};
	descriptor = {}; 
	
	parseElement(theForm);
	var errorMsg = "Je bent iets vergeten bij het invullen!\n\n";
	var showError = false;
	
	for (var elName in validator) {
		switch(validator[elName]) {
			case "empty" :
				errorMsg += "Het veld '" + descriptor[elName] + "' is nog leeg.\n";
				showError = true;				
				break;
			case "notEmail" :
				errorMsg += "Het e-mail adres op veld '" + descriptor[elName] + "' klopt niet.\n";
				showError = true;				
				break;
			case "moreEmails" :
				errorMsg += "Het e-mail adres op veld '" + descriptor[elName] + "' klopt niet.\nLet op dat je maar 1 e-mail adres opgeeft!\n";
				showError = true;				
				break;
			case "notChecked" :
				errorMsg += "Je hebt geen '" + descriptor[elName] + "' gekozen.\n";
				showError = true;				
				break;	
		
		}
	}
	
	if (showError) {
		alert(errorMsg);
		return false;
	} else {
		return true;
	}
}

function parseElement(el) {	
	var rules = null;
	try {	
		if (el.getAttribute('validation')) {
			var theName = el.name;
			var theValue = el.value;
			var checked = el.checked;
			var theDescription = el.getAttribute('descriptor');
			descriptor[theName] = theDescription;
			rules = el.getAttribute('validation').split('|');
			// apply validation rules
			for (var i=0; i<rules.length; i++) {
				if (rules[i].indexOf('=')!=0) {
					var temp = rules[i].split('=');
					rules[i] = temp[0];
					var param = temp[1];
				}
				switch(rules[i]) {
					case "isEmail":
						if (theValue.length>0) {
							while (theValue.lastIndexOf(" ")==theValue.length-1) theValue=theValue.substring(0, theValue.length-1);
							if ((theValue.indexOf(" ") != -1) || (theValue.indexOf(":") != -1) || (theValue.indexOf(";") != -1) || (theValue.indexOf(",") != -1)) {
								validator[theName] = "moreEmails";
								break;
							}
							var emailaddress = theValue.match(/\b[\w\.\-]+\@\w+[\w\.\-]*\.\w{2,3}/g);
						}
						if (emailaddress == null) validator[theName] = "notEmail";
						break;
					case "checked":
						validator[theName] = checked? "checked" : (validator[theName] == "checked"? "checked" : "notChecked");
						break;
					case "notEmpty":
						if (theValue=="") validator[theName] = "empty";
						break;
					case "notEmptyWhenNotChecked":
						var otherEl = document.getElementById(param);
						var otherElChecked = otherEl.getAttribute('checked');
						if ((!otherElChecked) && (theValue=="")) validator[theName] = "empty";
						break;
					case "notEmptyWhenChecked":
						var otherEl = document.getElementById(param);
						var otherElChecked = otherEl.getAttribute('checked');
						if ((otherElChecked) && (theValue=="")) validator[theName] = "empty";
						break;
				}
			}		
		}		
	} catch(e) { }
	
	var elements = el.elements? el.elements.length : 0;

	if (elements && !isNaN(elements) && (elements > 0)) {
		for (var i=0; i<elements; i++)
			//for (var j in el.elements[i] ) if (el.elements[i][j].indexOf("FILE")>0)alert (el.elements[i][j]);
			parseElement(el.elements[i]);
	}
}

//maak een browser object
function Browser() {
	this.name = navigator.appName;
	if (this.name == "Netscape") this.name = "nn";
	else if (this.name == "Microsoft Internet Explorer") this.name = "ie";
	this.version = parseFloat(navigator.appVersion);

	this.ie = (this.name == "ie");
	this.nn = (this.name == "nn");
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0);
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0);
	this.nn4 = ((this.name == "nn") && (this.version >= 4) && (this.version < 5));
	this.nn6 = ((this.name == "nn") && (this.version >= 5));
	this.mac = (navigator.appVersion.indexOf("Macintosh") > 0);
	this.win = (navigator.appVersion.indexOf("Win") > 0);
	this.ie5mac = (this.ie5 && this.mac);

//	if (this.nn6) this.name = "ie";
}

var browser = new Browser();

function getObjectTop(o) {
	if (!o) return -1;
	var t = o.offsetTop;
	if (browser.mac && browser.ie4) var p = o.parentElement;
	else var p = o.offsetParent;
	while (p) {
		t += p.offsetTop;
		if (browser.mac && browser.ie4) p = p.parentElement;
		else p = p.offsetParent;
	}
	return t;

}


function getObjectLeft(o) {
	if (!o) return -1;

	var l = o.offsetLeft;
	if (browser.mac && browser.ie4) var p = o.parentElement;
	else var p = o.offsetParent;
	while (p) {
		l += p.offsetLeft;
		if (browser.mac && browser.ie4) p = p.parentElement;
		else p = p.offsetParent;
	}
	return l;

}

function getObjectWidth(o) {
	if (!o) return -1;

	if (browser.ie) return o.offsetWidth;
	else if (browser.nn) return o.clip.width;
	else return -1;
}

function getObjectHeight(o) {
	if (!o) return -1;

	if (browser.ie) return o.offsetHeight;
	else if (browser.nn) return o.clip.height;
	else return -1;
}

function getPageHeight() {
	if (browser.ie) return document.body.clientHeight;
	return window.innerHeight;
}

function getPageWidth() {
	if (browser.ie) return document.body.clientWidth;
	return window.innerWidth;
}

		
function getEventLeft(event) {
	return document.body.scrollLeft+event.clientX; 
}

function getEventTop(event) {
	return document.body.scrollTop+event.clientY; 
}

function random (n) {
	return Math.floor (Math.random()*n);
}

function showHires (pic,w,h,sound) {

	window.open("../showpicture.asp?picture="+pic+"&sound="+sound, 'Foto','height='+h+',width='+w+',toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no');
}

function flash(v) {
	try {
		var obj=new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + v);
	} catch(e) {
		return false
	}
	return true;
}




function getFlashVersion(){
	b = navigator.userAgent.toLowerCase();
	if ( (b.indexOf('msie') != -1) && (b.indexOf('win') != -1) && (b.indexOf('opera') == -1) ) {
		document.writeln('<script language="VBscript">');
		document.writeln('Function detectActiveXControl(activeXControlName)');
		document.writeln('  on error resume next');
		document.writeln('  detectActiveXControl = False');
		document.writeln('  detectActiveXControl = IsObject(CreateObject(activeXControlName))');
		document.writeln('End Function');
		document.writeln('</scr' + 'ipt>');
	}
	var installedVersion = 0;
	if ( (b.indexOf('msie') != -1) && (b.indexOf('win') != -1) && (b.indexOf('opera') == -1) ) {
		for (var i=3; i<50; i++){
			if(detectActiveXControl("ShockwaveFlash.ShockwaveFlash."+i) == true) installedVersion = i;
		}
	} else {
		if (navigator.plugins["Shockwave Flash"]) {
			var pluginDesc = navigator.plugins["Shockwave Flash"].description;

			installedVersion = parseInt(pluginDesc.substr( pluginDesc.indexOf(".")-2,2) );
			if (isNaN (installedVersion)) installedVersion = parseInt(pluginDesc.substr( pluginDesc.indexOf(".")-1,1) );
		}

		if(b.indexOf("webtv") != -1) installedVersion = 3;  
	}
	
	return installedVersion;
}



var flash = getFlashVersion ()>=7;

var ie = browser.ie;

function refresh () {
	document.location.href=document.location.href; // refresh
}

function findHighestZ() {
 	 var documentDivs = new Array();
 	documentDivs = document.getElementsByTagName("div");
	
 	 var highestZ = 0;
 	 for (var i = 0; i < documentDivs.length; i++) {
 	 	
 		Zindex = parseInt(documentDivs[i].style.zIndex);
 		highestZ = (Zindex > highestZ) ? Zindex : highestZ;
 	 }
 	// alert ("highest:"+highestZ);
 	 return highestZ;
}
	
function bringToFront (id) {

	var topZ = findHighestZ()+1;
	// breng layer met id naar voren
	
	document.getElementById(id).style.zIndex = topZ;
}

function loadBackground (expedition) {
	with (document.getElementsByTagName("body")[0].style) {
		var url = '/ima/foto_'+expedition+'.jpg';
		
		backgroundImage="url("+url+")";
		backgroundAttachment="fixed";
		backgroundRepeat ="no-repeat";
	}
}

 function getSize (size) {
	// strips 'px' from size-property
	return parseInt(size.substring(0,size.indexOf("px")));
}
 function getScreenSize () {
 		var size = {};
 		if (document.documentElement.clientHeight>0) {
 			size.h = document.documentElement.clientHeight;
 			size.w = document.documentElement.clientWidth;
 		} else {
 		 	size.h = document.body.clientHeight;
 			size.w = document.body.clientWidth;
 		}
 		return size;

}

	function reload () {
		document.location.href=document.location.href; // refresh
	}
				
function submit () {
	alert ("submit");
	//document.forms[0].submit();
	//document.getElementById("submit").disabled=true;
}
