var re_email = /^.*?((?:[\w.-])+\@(?:(?:[\w-])+\.)+(?:[\w]{2,4})).*?$/;

/*
Bepaald de onload, functie toevoegen aan body onload d.m.v.: window.onload.actions.push(function_naam);
*/
window.onload = function () {
    for (var i=0;arguments.callee.actions.length>i;i++)
        arguments.callee.actions[i]();
};
window.onload.actions = [];

function ClearInputEvents(id,str) {
	obj = document.getElementById(id);
	val = obj.value;
	
	obj.startvalue = val;
	obj.value = val;
	
	obj.onfocus = function() { if(this.value == this.startvalue) { this.value = '';}}
	obj.onblur =  function() { if(this.value == '') { this.value = this.startvalue; }}

}

/* POPUP SCRIPT 
----------------------------------------------------------------*/
function calc_position(popWidth, popHeight) {
	popLeft = (screen.width/2)-(popWidth/2);
	popTop = (screen.height/2)-(popHeight/2);
	var returnRay = new Array(popWidth, popHeight, popLeft, popTop);
	return returnRay;
}

function open_popup(type, w, h) {
	popProps = calc_position(w, h);
	popup = window.open('/popup/' + type + '.php', 'popup_window', 'scrollbars=yes, width='+popProps[0]+', height='+popProps[1]+', left='+popProps[2]+', top='+popProps[3]);
}

/* BIEDEN SCRIPT 
----------------------------------------------------------------*/
function valid_offer(minimum, titel) {
	var frm = document.getElementById('frm-bieden');
	var current=frm.elements['i_bod'].value;

	if(confirm("Weet u zeker dat u een bod van " + current + " euro wilt uitbrengen op " + titel + "?")) {
		if(parseFloat(current.replace (",",".")) < parseFloat(minimum.replace(",","."))) {
			alert('Uw bod van ' + current + ' euro is te laag. U moet minstens ' + minimum + ' euro bieden.');
			return true;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

function eurofield_keypress(evt) {
	// 48 = 0   57 = 9
	// 46 = .   44 = ,
	
	if(evt.keyCode==0 && evt.charCode>0) { // Firefox methode
		var kc=evt.charCode;
		if((kc<48 || kc>57) && kc!=44 && kc!=46) { return false; } else { return true; }
	} else if(evt.keyCode>0 && typeof evt.charCode=='undefined') { // Internet Explorer methode
		var retval=evt.keyCode;
		if(retval==46) { retval=44; }
		if((retval<48 || retval>57) && retval!=44) { retval=false; }
		evt.keyCode=retval;
		return true;
	} else { // Onbekende browser of systeemkarakter (backspace, etc.), alles wordt doorgelaten en alsnog server-side gecheckt
		return true;
	}
}
function eurofield_keyup(what) {
	// Omdat Firefox geen tekens kan replacen bij de onkeypress
	if(what.value.indexOf(".")>=0) { what.value=what.value.replace(".",","); }
}
function eurofield_blur(what) {
	var p=what.value.split(",");
	var euro=p[0];
	if(p[1]) { var centen=p[1]; } else { var centen=''; }
	if(centen.length==0) { centen="00"; }
	else if(centen.length=="1") { centen=centen+"0"; }
	else if(centen.length>2) { centen=centen.substr(0,2); }
	if(!euro) { euro="0"; }
	what.value=euro+","+centen;
}

function display_currency(nr) {
	var q=new String(nr).split(".");
	if(typeof q[1]=='undefined') { q[1]="00"; }
	else if(q[1].length==1) { q[1]=q[1]+"0"; }
	return q[0]+","+q[1];
}

/* STAF SCRIPT 
----------------------------------------------------------------*/
function init_staf() {
	doc = document
	if(doc.getElementById('staf_preview')) {
		var staf_fields = Form.getElements('frm-staf');
		for(i = 0; i < staf_fields.length; i++) {
			if(staf_fields[i].type != 'hidden' && staf_fields[i].type != 'button' && staf_fields[i].type != 'image') {
				staf_fields[i].onkeyup = staf_fields[i].onblur = function() { update_staf_preview(); return false; }
			}
		}
		update_staf_preview();		
	}
}

function update_staf_preview() {
	var submit_body = Form.serialize('frm-staf');
	new Ajax.Updater('staf_preview', '/ajax/staf_preview.php', { method:'post', postBody:submit_body, asynchronous:true, evalScripts:true } );
}

/* FAQ SCRIPT 
----------------------------------------------------------------*/
function init_faq() {
	doc = document
	if(doc.getElementById('faq')) {
		anchors = doc.getElementById('faq').getElementsByTagName('a');
		for(i = 0; i < anchors.length; i++ ) {
			anchors[i].onclick = function() { togglefaq(this.id); return false; }	
		}
	}
}

function togglefaq(id) {
	answerid = id.substr(3,id.length);
	if(document.getElementById('answer'+answerid).style.display == '' || document.getElementById('answer'+answerid).style.display == 'none' ){
		var submit_body = 'faq_id=' + answerid;
		new Ajax.Request('/ajax/faq_count.php', { method:'post', postBody:submit_body, asynchronous:true } );
		
		document.getElementById('answer'+answerid).style.display = 'block';
	} else {
		document.getElementById('answer'+answerid).style.display = 'none';
	}
}

/* NAV SCRIPT
----------------------------------------------------------------*/
function init_nav() {
	if(document.getElementById('categorieen-nav')) { 
		anchors = doc.getElementById('categorieen-nav').getElementsByTagName('a');
		for(i = 0; i < anchors.length; i++ ) {
			if(anchors[i].className == 'sub') {
				anchors[i].onclick = function() { nav_click(this); return false; }	
			}
			if(anchors[i].parentNode.className == "selected") {
				if(anchors[i].parentNode.getElementsByTagName('UL').lenght > 0) {
					anchors[i].parentNode.getElementsByTagName('UL')[0].style.display = 'block';
				}
			}
		}
	}
}

function nav_click(obj) {
	nav_hideAll()
	mySublists = obj.parentNode.getElementsByTagName('UL');
	if(mySublists.length > 0) {
		toggle_display(mySublists[0]);
	}
	obj.parentNode.className = "selected";
}

function nav_hideAll() {
	sublists = document.getElementById('categorieen-nav').getElementsByTagName('UL');
	for(i = 0; i < sublists.length; i++ ) {
		sublists[i].style.display = 'none'; 
	}
	sublistitems = document.getElementById('categorieen-nav').getElementsByTagName('LI');
	for(i = 0; i < sublistitems.length; i++ ) {
		sublistitems[i].className = "notselected";
	}
	
}

function toggle_display(obj) {
	if(obj.style.display == 'none') {
		obj.style.display = 'block';
	} else {
		obj.style.display = 'none'; 
	}
}

/* PRINT SCRIPT
----------------------------------------------------------------*/
function init_Print() {
	if(window.print && document.getElementById("print-btn")) { 
		document.getElementById("print-btn").style.display = 'block';
		document.getElementById("print-btn").onclick  = function () { window.print(); return false; }
	}
}


/* IDEAL SCRIPT
----------------------------------------------------------------*/
function check_ideal_payment() {
	if(document.forms[2]) {
		var form_action = document.forms[2].action;
		if(form_action) {
			var state = form_action.split('/');
			state = state[state.length - 1];
			
			if(state == 'accepted' || state == 'declined' || state == 'exeption' || state == 'cancelled') {
				window.setTimeout('window.location="'+form_action+'"; ',0);
			}
		}
	}
}

/* ONLOAD
----------------------------------------------------------------*/
function init() {
	ClearInputEvents('postcode');
	ClearInputEvents('keyword');
	init_Print();
	init_faq();
	init_staf();
	init_nav();
	
	check_ideal_payment();
}

window.onload.actions.push(init);

