

$(document).click(function(e) {
	// hide calendar
	if (e.target.className == 'img_dateinput' || e.target.className == 'calendar_container' || $(e.target).parents('.calendar_container').length > 0)
		return;
	$('.calendar_container').remove();
	
});

$(document).ready(function() {
	$('a.lightbox, area.lightbox').lightBox();
});


function help_popup(o, text, width, height) {
	var p = findPos(o);
	
	var d = document.createElement('div');
	d.style.position = 'absolute';
	d.style.width  = width + 'px';
	d.style.height = height + 'px';
	d.style.border = '1px solid #a092b4';
	d.style.backgroundColor = '#ffffff';
	d.style.top = p[1] + 'px';
	d.style.left = p[0] + 'px';
	d.style.paddingTop    = '5px';
	d.style.paddingLeft   = '5px';
	d.style.paddingRight  = '5px';
	d.style.paddingBottom = '5px';
	
	divClose = document.createElement('div');
	divClose.style.styleFloat = 'right';
	divClose.style.cssFloat = 'right';
	img = document.createElement('img');
	img.src = '/images/winkel/icon_close.gif';
//	img.style.marginTop = '5px';
//	img.style.marginRight = '5px';
	img.style.cursor = 'pointer';
	img.onclick = function() {
		d.parentNode.removeChild(d);
	}
	
	divClose.appendChild(img);
	
	divText = document.createElement('div');
	divText.innerHTML = text;
	
	d.appendChild(divClose);
	d.appendChild(divText);
	
	b = document.body;
	b.insertBefore(d, b.childNodes[0]);
	
	
}


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function format_currency(v) {
	price = v.toString();
	
	if (price.indexOf('.') == -1)
		price = price + ',00';
	else
		price = price.replace('.', ',');
	
	suffix = price.substr(price.indexOf(',')+1);
	if (suffix.length == 1)
		price = price + '0';
	
	return '&euro; ' + price;
}

function valid_date(value) {
	
	if (value == '00-00-0000' || value == '0000-00-00')
		return false;
	if (value.match(/^\d{2}-\d{2}-\d{4}$/))
		return true;
	if (value.match(/^\d{4}-\d{2}-\d{2}$/))
		return true;
	
	return false;
}

function select2input(obj) {
	var parent = obj.parentNode;
	var input = document.createElement('input');
	
	input.type = 'text';
	input.id = obj.id;
	input.name = obj.name;
	input.value = obj.value;
	
	parent.insertBefore(input, obj);
	
	parent.removeChild(obj);
	
	input.focus();
}

function show_loading() {
	var div = document.createElement('div');
	
	div.className = 'show_loading';
	div.innerHTML = '<img src="/images/ajax-loader.gif" />';
	
	
	var div_background = document.createElement('div');
	div_background.className = 'bg_show_loading';
	
	$(document.body).prepend(div);
	$(document.body).prepend(div_background);
}

function show_loading_popup() {
	var div = document.createElement('div');
	
	div.className = 'show_loading_small';
	div.innerHTML = '<img src="/images/ajax-loader-circle.gif" />&nbsp;Loading..';
	
	$(document.body).prepend(div);
}
function remove_loading_popup() {
	$('.show_loading_small').remove();
}


function remove_loading() {
	$('.show_loading').remove();
	$('.bg_show_loading').remove();
}



function text_popup(obj, text) {
	$('.text_popup').remove();		// remove old popups
	
	// container
	var div = document.createElement('div');
	div.className = 'text_popup';
	var pos = findPos(obj);
	div.style.left = (pos[0]+20) + 'px';
	div.innerHTML = text;
	div.style.top  = (pos[1]-obj.clientHeight) + 'px';
	
	var img_close = document.createElement('img');
	img_close.src = '/images/knop-close.png';
	img_close.className = 'text_popup_close';
	img_close.onclick = function() { $('.text_popup').remove() };
	$(div).prepend(img_close);

	
	$(document.body).prepend(div);
	
	div.style.top = (pos[1]-div.clientHeight+obj.clientHeight) + 'px';
}



function toon_contactformulier(nr) {
	var w = 545;
	var h = 600;
	
	url = '/contactformulier.php?n=' + nr;
	window.open(url, '_blank', 'status=1,toolbar=1,width=545,height=600,resizeable=1,scrollbars=1');
}


function valid_date(v) {
	if (v.match(/^\d{2}-\d{2}-\d{4}$/) && v !=  '00-00-0000')
		return true;
	else
		return false;
}

function selectDate(target, date, last_year) {
	if (!last_year)
		last_year=0;

	// remove old ones
	$('.calendar_container').remove();
	
//	var p = findPos($('#'+target).get(0)); // Object position
	var p = $('#'+target).offset();
	var x = parseInt(p.left);
	var y = parseInt(p.top);
	
	
	container = document.createElement('div');
	container.className = 'calendar_container';
	container.id = 'calendar_container';
	
	container.style.top = y+"px";
	container.style.left = (x+180)+"px";
	
	$('body').prepend(container);
	
	var d = date;
	$.get('/ajx/popup_calendar.php?id='+target+'&date='+d+'&last_year='+last_year,
			function(data) {
				$('#calendar_container').html(data);
			}
	);
}


function chose_date(id, date) {
	$('.calendar_container').remove();
	
	$('#'+id).val(date);
	$('#'+id).change();
}

function inputdate_KeyPress(evt, o) {
	keyCode = evt.keyCode ? evt.keyCode : evt.which;
	if (keyCode < 47 || keyCode > 122)			// backspace
		return true;
	
	
	pos = caret_position(o);
	// max 10 karakters
	if (pos >= 10)
		return false;
	
	
	v = o.value;
	
	if (pos != 2 && pos != 5 && keyCode == '-'.charCodeAt(0))
		return false;
	if ((pos == 2 || pos == 5) && keyCode != '-'.charCodeAt(0)) {
		v = set_char_at(v, pos, '-');
		pos++;
	}
	
	o.value = set_char_at(v, pos, String.fromCharCode(keyCode));
	
	set_caret_position(o, pos+1);
	
	return false;
}

function set_char_at(str, index, char) {
	left = str.substring(0, index);
	right = str.substring(index+1);
	
	return left + char + right;
}


function caret_position(field) {
	if (document.selection) {												// Internet Explorer
		var oSel = document.selection.createRange();
		oSel.moveStart ('character', -field.value.length);
		return oSel.text.length;
	} else if (field.selectionStart || field.selectionStart == '0') {		// Firefox
		return field.selectionStart;
	}
}


function set_caret_position(field, pos) {
	field.focus();
	
	if (document.selection) {												// Internet Explorer
		var r = document.selection.createRange();
		r.moveStart ('character', -field.value.length);
		r.select();
		r.move('character', pos);
		r.select();
	} else if (field.selectionStart || field.selectionStart == '0') {		// Firefox
		field.selectionStart = pos;
		field.selectionEnd = pos;
	}
}


function scrollTo(id) {
	$('html,body').animate(
		{
			scrollTop: $('#'+id).offset().top
		} , 800
	);
}


// dropdown js-code
function dropdown_open(id) {
	$('.dropdown_items').hide();		// close others
	$('#' + id).css('position', 'relative');
	$('#' + id + ' .dropdown_items').show();
}
function dropdown_close() { $('.dropdown_items').hide(); }

$(document).click(function(e) {
	var p = e.target;
	while (p && p.parentNode) {
		if (p.className == 'dropdownbox')
			return;
		
		p = p.parentNode;
	}
	
	$('.dropdown_items').hide();
});


function valid_email(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function wad_popup(url) {
	var w = 545;
	var h = 600;
	
	window.open(url, '_blank', 'status=1,toolbar=1,width=545,height=600,resizeable=1,scrollbars=1');
}



function printPage(url) {
	if (!url || url == '')
		url = window.location.toString();
	
	if (url.indexOf('?') == -1)
		url = url + '?print=1';
	else
		url = url + '&print=1';
	
	window.open(url, '_blank', 'width=750,height=400,scrollbars=1,resizable=1');
}



function sendMail2Friend(subject, msg) {
	$('.mailafriend').remove();
	
	show_loading_popup();
	
	$.ajax(
	{
		url: '/ajx/popup_mailafriend.php',
		data: {
			subject: subject,
			bericht: msg
		},
		success: function(e) {
			remove_loading_popup();
			
			$(document.body).prepend(e);
		},
		error: function(d) {
			remove_loading_popup();
			
			alert(d);
		}
	});
}
function sendMail2Friend_Cancel() { $('.mailafriend').remove(); }
function sendMail2Friend_Submit() {
	var data = {
		action: 'send',
		mailto:   $('#mailafriend-mailto').val(),
		mailfrom: $('#mailafriend-mailfrom').val(),
		subject:  $('#mailafriend-subject').val(),
		bericht:  $('#mailafriend-bericht').val()
	};
	
	show_loading_popup();
	
	$.ajax(
	{
		url: '/ajx/popup_mailafriend.php',
		data: data,
		success: function(e) {
			remove_loading_popup();
			
			if (!e.match(/^success/)) {
				
				if (e.match(/^error:/))
					e = e.substr(6);
				
				alert('Er is een fout opgetreden: ' + e);
				return;
			} else {
				alert('Mail verstuurd');
			}
			
			$('.mailafriend').remove();
		},
		error: function(d) {
			remove_loading_popup();
			
			alert(d);
		}
	});
}







