function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function validate_contact(theForm) {
	var msg = ""
	if(theForm.last_name.value == "") {
		msg += "-" + lbl_contact_familienaam + " \n";	
	}
	if(theForm.firma.value == "") {
		msg += "-" + lbl_contact_firma + " \n";	
	}
	if(theForm.email.value == "") {
		msg += "-" + lbl_contact_email + " \n";	
	}
	if (msg != "") {
		msg = lbl_contact_mandatory + " : \n" + msg;
		alert(msg);
		return false;
	}
	return true;
}
function contact() {
	var frm = document.getElementById("contactformulier");
	if (frm) {
		frm.onsubmit = function() {
			return validate_contact(this);
		}
	}

}

/*function printLink() {
	var lnk = document.getElementById("printBut");
	if (lnk) {
		lnk.onclick = function() {
			print();
			return false;
		}
	}
}*/

function doNewsEffect() {
	var divke = document.getElementById("demo-effect-grow");
	if (divke) {
		$('#demo-effect-grow').hide();
		document.getElementById("hotnewstitel").innerHTML = titels[j];
		document.getElementById("hotnewsintro").innerHTML = intros[j];
		document.getElementById("butMore").href = links[j];
		j++;
		if (titels.length == j) {
			j = 0;
		}
		//$('#demo-effect-grow').show('slow');
		$('#demo-effect-grow').slideToggle();
		setTimeout("doNewsEffect()",6000);
	}
}

function hotnews() {
	var hotnews = document.getElementById("demo-effect-grow");
	if (hotnews) {
		setTimeout("doNewsEffect()",3600);
	}
}

function ImageLinks() {
	var linkul = document.getElementById("imgList");
	if (linkul) {
		var links=linkul.getElementsByTagName("a");
		for(i=0;i<links.length;i++) {
			if (links[i].className.indexOf("linkfoto") >= 0) {
				links[i].onclick=function() {	
					var thisid = this.id;
					var image1 = document.getElementById("imgBig1");
					var image2 = document.getElementById("imgBig2");
					var image3 = document.getElementById("imgBig3");
					if (thisid == "small_image1") {
						if (image1) {
							image1.style.display = "block";
						}
						if (image2) {
							image2.style.display = "none";
						}
						if (image3) {
							image3.style.display = "none";
						}
					}
					if (thisid == "small_image2") {
						if (image1) {
							image1.style.display = "none";
						}
						if (image2) {
							image2.style.display = "block";
						}
						if (image3) {
							image3.style.display = "none";
						}
					}
					if (thisid == "small_image3") {
						if (image1) {
							image1.style.display = "none";
						}
						if (image2) {
							image2.style.display = "none";
						}
						if (image3) {
							image3.style.display = "block";
						}
					}

					return false;
				}
			}
		}
	}	
}

function menu() {
	//if (window.attachEvent) { 
		// IE does not do :hover on <li> ==> add a class onmouseover
		sfHover("tableJaar");
	//} 
}

function sfHover(id) {
	var BUA = navigator.userAgent;
	var BIE = BUA.indexOf("MSIE");
	var BIsIE = BIE>=0;

	var ul = document.getElementById(id);
	if(ul) {
		var lis = ul.getElementsByTagName('tr');
		for(i=0;i<lis.length;i++) {
				// enkel die LIs die subitems hebben
				lis[i].onmouseover=function() {this.className+=" sfhover";}
				lis[i].onmouseout=function() {this.className=this.className.replace(new RegExp(" sfhover\\b"), "");}
							
				//all is normal till here. If we're using IE then this tells any first level item to have a width of 1px
				//lis[i].style.width = '1px';
		}
	}
}
	
var allowDivOnClick = true;
function locations() {
	var map=document.getElementById('locationmap');
	if(map) {
		var locations=map.getElementsByTagName('div');
		for (var i=0;i<locations.length;i++) {
			locations[i].onmouseover = function() {
				this.style.cursor="pointer";
				this.className+=" locBlink";
				desactivate();
				var obj2 = document.getElementById(this.getAttributeNode('id').value.replace("locatie",""));
				obj2.className = "active";
			}
			locations[i].onmouseout = function() {
				if (this.className.indexOf("locBlink") > 0) {
						var locations=map.getElementsByTagName('div');
						for (var i=0;i<locations.length;i++) {
							locations[i].className=locations[i].className.replace(new RegExp(" locBlink\\b"), "");
						}
				}
				desactivate();
			}
			locations[i].onclick = function() { 
				if(allowDivOnClick) {
					if (this.className.indexOf("locHover") < 0) {
						// alles sluiten
						var locations=map.getElementsByTagName('div');
						for (var i=0;i<locations.length;i++) {
							locations[i].className=locations[i].className.replace(new RegExp(" locHover\\b"), "");
							locations[i].className=locations[i].className.replace(new RegExp(" locBlink\\b"), "");
						}
						// 1 openen
						desactivate();
						var obj2 = document.getElementById(this.getAttributeNode('id').value.replace("locatie",""));
						obj2.className = "active";
						this.className+=" locHover";
						this.style.cursor="default";
					}
				} else {
					allowDivOnClick = true;
				}
				return false;
			}
			em=locations[i].getElementsByTagName('em')[0];
			closelink=em.getElementsByTagName('a')[0];
			if(closelink) { 
				closelink.onclick=function() {
					this.parentNode.parentNode.className=this.parentNode.parentNode.className.replace(new RegExp(" locHover\\b"), "");
					allowDivOnClick = false;
					var locations=document.getElementById('locationmap').getElementsByTagName('div');
					for (var i=0;i<locations.length;i++) {
						locations[i].className=locations[i].className.replace(new RegExp(" locHover\\b"), "");
						locations[i].className=locations[i].className.replace(new RegExp(" locBlink\\b"), "");
					}
					desactivate();
					return false;
				}
			}
		}
	}
}

function desactivate() {
	var lst=document.getElementById('locationlist').getElementsByTagName('li');
	if (lst) {
		for (var i=0;i<lst.length;i++) {
			var a=lst[i].getElementsByTagName('a')[0];
			a.className = "";
		}	
	}
}

function listLocationsShow() {
	var list=document.getElementById('locationlist');
	if(list) {
		var LIs=list.getElementsByTagName('li');
		for (var i=0;i<LIs.length;i++) {
			var a=LIs[i].getElementsByTagName('a')[0];
			a.onmouseover = function() {
				var nr='locatie'+this.getAttributeNode('id').value;
				var obj = document.getElementById(nr);
				obj.style.cursor="pointer";
				obj.className+=" locBlink";
			}
			a.onmouseout = function() {
				var nr='locatie'+this.getAttributeNode('id').value;
				var obj = document.getElementById(nr);
				if (obj.className.indexOf("locBlink") > 0) {
						var locations=document.getElementById('locationmap').getElementsByTagName('div');
						for (var i=0;i<locations.length;i++) {
							locations[i].className=locations[i].className.replace(new RegExp(" locBlink\\b"), "");
						}
				}
			}
			a.onclick = function() {
				var nr='locatie'+this.getAttributeNode('id').value;
				var obj = document.getElementById(nr);
				desactivate();
				var obj2 = document.getElementById(this.getAttributeNode('id').value);
				obj2.className = "active";
				if(allowDivOnClick) {
					if (obj.className.indexOf("locHover") < 0) {
						// alles sluiten
						var locations=document.getElementById('locationmap').getElementsByTagName('div');
						for (var i=0;i<locations.length;i++) {
							locations[i].className=locations[i].className.replace(new RegExp(" locHover\\b"), "");
							locations[i].className=locations[i].className.replace(new RegExp(" locBlink\\b"), "");
						}
						// 1 openen
						obj.className+=" locHover";
						obj.style.cursor="default";
					}
				} else {
					allowDivOnClick = true;
				}
				return false;
			}
		}
	}
}

function validateInquiry(theForm) {
	var msg = ""
	if(theForm.last_name.value == "") {
		msg += "-" + lbl_contact_familienaam + " \n";	
	}
	if(theForm.first_name.value == "") {
		msg += "-" + lbl_contact_voornaam + " \n";	
	}
	if(theForm.firma.value == "") {
		msg += "-" + lbl_contact_firma + " \n";	
	}
	if(theForm.street.value == "" || theForm.number.value == "") {
		msg += "-" + lbl_contact_straat_nummer + " \n";	
	}
	if(theForm.zipcode.value == "" || theForm.city.value == "") {
		msg += "-" + lbl_contact_postcode_gemeente + " \n";	
	}
	if(theForm.email.value == "") {
		msg += "-" + lbl_contact_email + " \n";	
	}
	if (msg != "") {
		msg = lbl_contact_mandatory + " : \n" + msg;
		alert(msg);
		return false;
	}
	return true;
}

function inquiryforms() {
	var frm1 = document.getElementById("inquiryForm");
	if (frm1) {
		frm1.onsubmit = function() {
			return validateInquiry(this);
		}
	}
}

addLoadEvent(inquiryforms);
addLoadEvent(locations);
addLoadEvent(listLocationsShow);
addLoadEvent(menu);
addLoadEvent(ImageLinks);
addLoadEvent(hotnews);
/*addLoadEvent(printLink);*/
addLoadEvent(contact);


$(document).ready(function(){

	// product images \\
	$('.blokbeeldenNav a.bigimglink').click(function() {
		var image = parseInt($(this).attr('id').replace('bigimglink',''));
		var gp = $(this).parent().parent().attr('id');
		$('#' + gp + ' .blokbeelden .imgBig').hide();
		$('#' + gp + ' .imgBig' + image).show();
		$('#' + gp + ' .blokbeeldenNav a.bigimglink').removeClass('active');
		$(this).addClass('active');
		//alert('test');
		return false;
	});

});
