/*
	Deze functie controleert de invoer van formulieren. 
	
	Er wordt gecontroleerd of:
	-Alle verplichte velden zijn ingevuld
	-Een vinkje is gezet bij het vakje 'akkoord met algemene voorwaarden'
	-Een correct e-mailadres is ingevuld
*/
function hideWaitress(){
	document.getElementById('waitress').style.display='none';	
}
function showWaitress(){

		var html = '<iframe src="/waitress.php" width="750" height="550" scrolling="no" frameborder="0" allowtransparency="true" style="margin-left:60px;margin-top:60px"></iframe>';
		var objBody = document.getElementsByTagName("body").item(0);
		var objOverlay = document.createElement("div");
		var arrayPageSize = getPageSize();
		objOverlay.setAttribute('id','waitress');
		objOverlay.style.display = 'block';
		objOverlay.style.width = arrayPageSize[0];
		objOverlay.style.height = arrayPageSize[1];	
		objOverlay.innerHTML = html;
		objOverlay.onclick = function() { hideWaitress(); }
		objBody.appendChild(objOverlay);
		Set_Cookie('shown','true','7','/','','');

}
function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
function check(which, verplicht, error, emailerror){
	var empty = false;
	for (i=0;i<which.length;i++){
		var veld=which.elements[i];
		if(veld.type=="text"){
			//Maak alle achtergronden (weer) wit
			veld.style.background="#FFFFFF";
		}
		for (j=0;j<verplicht.length;j++){
			if(veld.type=="text" && veld.name==verplicht[j][0] && veld.value==''){
				veld.style.background="#FFBB99";
				empty = true;
			}
			if(veld.type=="textarea" && veld.name==verplicht[j][0] && veld.value==''){
				veld.style.background="#FFBB99";
				empty = true;
			}
			if(veld.type=="checkbox" && veld.name==verplicht[j][0] && !veld.checked && verplicht[j][1]=='voorwaarden'){
				veld.style.background="#FFBB99";
				alert("U moet wel akkoord gaan met de voorwaarden");
				return false;
			}
			if(veld.type=="text" && veld.value!='' && veld.name=='postcode' && verplicht[j][1]=='postcode') {
				postcode = veld.value.toUpperCase();
				veld.value = postcode;
				rExp = /^[1-9]{1}[0-9]{3}[A-Z]{2}$/; // van 1000AA tot 8999ZZ - Let op, geen spatie!
				if (!rExp.exec(postcode)) {
					alert('U heeft het veld postcode niet juist ingevuld.\nVul de postcode in met notatie 1234AB');
					return false;
				}
			}

			if(veld.type=="text" && veld.value!='' && veld.name=='email' && verplicht[j][1]=='email'){
				// E-mail check!
				var email=/^[_\.0-9a-z-]+\@([0-9a-z][0-9a-z-]*\.)+([a-z]{2,4})+$/i
				if(!(email.test(veld.value))){
					veld.style.background="#FF818C";
					alert("Geen geldig emailadres");
					return false;
				}
			}
		}
	}
	if(empty){
		alert("Niet alle velden zijn ingevuld.");
		return false;
	} else {
		document.getElementById("spamcheck").value = "Geen SPAM";
		return true;
	}
}
