// E-mail Form - Validator
function checkData(){
	var correct = true
	var regexpemail =/^[a-zA-Z0-9\-\.\_]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; //regexp for e-mail address
	var str = document.form.email.value; // value to compare
	if (document.form.naam.value == '') {correct = false; alert("Vul a.u.b. uw naam in.");document.form.naam.focus(); return correct}
	if (regexpemail.test(str) == false){
	correct = false; alert("Vul s.v.p een geldig e-mail adres in.");document.form.email.focus(); return correct}
	if (document.form.opmerking.value == '') {correct = false; alert("Vul s.v.p. uw vraag of opmerking in.");document.form.opmerking.focus(); return correct}
	return correct;
}

function showHideNext(theEl){
	theEl.nextSibling.style.display=(theEl.nextSibling.style.display=='none')?"":"none";
	theEl.nextSibling.nextSibling.style.display=(theEl.nextSibling.nextSibling.style.display=='none')?"":"none";
}

function setFilter(theInput){
	var value=theInput.checked?1:0;
	makeCookie(theInput.name,value,7);
	makeCookie('filterenabled',1,7);
}
function setSelectFilter(theInput){
	var value=theInput.value;
	makeCookie(theInput.name,value,7);
	makeCookie('filterenabled',1,7);
}

function stopFilter(){
	theItems=document.getElementsByTagName('INPUT');
	for (i=0;i<theItems.length;i++) {
        theItem = theItems[i];
		if(theItem.getAttribute('type')=='checkbox'){
			makeCookie(theItem.name,0,7);
			theItem.checked=false;
		}
	}
	theEl=document.getElementById('select_branche');
	theEl.selectedIndex=0;
	theEl.value=-1;
	theEl=document.getElementById('select_cat');
	theEl.selectedIndex=0;
	theEl.value=-1;
	makeCookie('select_branche',-1,7);
	makeCookie('select_cat',-1,7);
	makeCookie('filterenabled',0,7);
	location.reload(true);
}

function stopFilter2(){
	theItems=document.getElementsByTagName('INPUT');
	for (i=0;i<theItems.length;i++) {
        theItem = theItems[i];
		if(theItem.getAttribute('type')=='checkbox'){
			makeCookie(theItem.name,0,7);
			theItem.checked=false;
		}
	}
	theEl=document.getElementById('select_tipbranche');
	theEl.selectedIndex=0;
	theEl.value=-1;
	makeCookie('select_tipbranche',-1,7);
	makeCookie('filterenabled',0,7);
	location.reload(true);
}

function startFilter(){
	makeCookie('filterenabled',1,7);
	location.reload(true);
}

function makeCookie(Name,Value,Expiry,Path,Domain,Secure) {
	if (Expiry != null) {
		var datenow = new Date();
		   //get a date
		datenow.setTime(datenow.getTime() + Math.round(86400000*Expiry));
		   //mutiply the number to make it represent days
		Expiry = datenow.toGMTString();
		   //convert to GMT time
	}

	Expiry = (Expiry != null) ? '; expires='+Expiry : '';
	   //has an expiration?
	Path = (Path != null)?'; path='+Path:'';
	   //has a path?
	Domain = (Domain != null) ? '; domain='+Domain : '';
	   //has a domain?
	Secure = (Secure != null) ? '; secure' : '';
	   //Secure?
	document.cookie = Name + '=' + escape(Value) + Expiry + Path + Domain + Secure;
	   //Make the cookie!
}

function openWaardering(theEle){
	// Toon de waardering div op de muispositie
	document.getElementById('con_waardering').style.left=findPosX(theEle) + 'px';
	document.getElementById('con_waardering_back').style.left=findPosX(theEle) + 'px';
	// Het Y-coordinaat corrigeren met -213 vanwege de positionering IN de contentcontainer!
	document.getElementById('con_waardering').style.top=findPosY(theEle)-213-180 + 'px';
	document.getElementById('con_waardering_back').style.top=findPosY(theEle)-213-180 + 'px';
	document.getElementById('con_waardering').style.display='block';
	document.getElementById('con_waardering_back').style.display='block';
}

function saveWaardering(){
	setTimeout("hideWaardering()",400);
}
function hideWaardering(){
	document.getElementById('con_waardering').style.display='none';
	document.getElementById('con_waardering_back').style.display='none';
	theRadio=document.waarderingform.waardering;
	unsetCheckedValue(theRadio);
}

function unsetCheckedValue(radioObj) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
	}
	radioObj[radioLength-1].checked = true;
}


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// POPUP

var win = null;

function openPopup(theURL,winName,w,h){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,resizable=no'
	win = window.open(theURL,winName,settings)
}
/// Mootools Form Check implementation   MOOCHECK
window.addEvent('domready', mooCheckInit);
function mooCheckInit(){
	$$('form.MooCheck').each(
		function(theForm){
			theForm.addEvent('submit',mooCheckSubmit);
			theForm.getElements('.MooCheck').each(
				function(theFormElement){
					theFormElement.addEvent('blur',mooFieldCheck);
				}
			);
		}
	);
}
function mooFieldCheck(){
	// Only return on errors..  after passing another error could occur..
	if(this.hasClass('MooNotEmpty') && this.getValue().trim()==''){
		mooRaiseError(this);
		return;
	} else if(this.hasClass('MooNotEmpty')){
		mooRemoveError(this);
	}
	if(this.hasClass('MooValidEmail') && /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,4}$/.test(this.getValue()) ){
		mooRemoveError(this);
	} else if(this.hasClass('MooValidEmail')){
		mooRaiseError(this);
		return;
	}
	if(this.hasClass('MooNotFirstOption') && this.selectedIndex==0){ // Selectbox may not have first option selected.
		mooRaiseError(this);
		return;
	} else if(this.hasClass('MooNotFirstOption')){
		mooRemoveError(this);
	}
}
function mooRaiseError(theElement){
	// Check if there is an _error field for this..
	if(theElement.hasClass('hasError')){
		return;
	}
	var theErrorInfo;
	if(theErrorInfo=$(theElement.getProperty('id')+'_error')){
		var ErrorElement = new Element('span',{'class':'error'});
		ErrorElement.appendText(theErrorInfo.getValue());
		ErrorElement.injectBefore(theErrorInfo);
		theElement.addClass('hasError');
	}
}
function mooRemoveError(theElement){
	var ErrorElement;
	if(theElement.hasClass('hasError')){
		ErrorElement=$(theElement.getProperty('id')+'_error').getPrevious();
		if(ErrorElement.getTag()=='span' && ErrorElement.hasClass('error')){
			ErrorElement.remove();
			theElement.removeClass('hasError');
		}
	}
}
function mooCheckSubmit(event){
	var event = new Event(event);
	this.getElements('.MooCheck').each(
		function(formElement){
			formElement.fireEvent('blur');
		}
	)
	if(this.getElements('.hasError').length>0){
		event.stop();
		alert('Please correct all errors');
		return;
	}
}