$(document).ready(function() {
	$('.notice').delay(5000).slideUp();	
	$('#melding').delay(5000).slideUp();
	$('.toegevoegd').delay(1000).animate({ backgroundColor: "#ececec" }, "slow", function() {
		$(this).css({'background-color': 'transparent'});
	});
	
	$('#gebruikersnaam').keypress(function(){
		return alpha(event,emails);	
	});
	
	$('#nk_acc').click(function(){ //alert('open');
		$('#nk_fld').slideDown();
		$(this).slideUp();
	});
	
	$('.datum').each(function(){
		$(this).datepicker({
			'option' : $.datepicker.regional['nl'], 
			dateFormat : 'dd-mm-yy', 
			changeYear: true, 
			changeMonth: true, 
			numberOfMonths: 1, 
			showButtonPanel: true,
			onClose: function(dateText, inst) { 	}
		});
	});
	$('.geboortedatum').each(function(){
		$(this).datepicker({
			'option' : $.datepicker.regional['nl'], 
			dateFormat : 'dd-mm-yy', 
			changeYear: true, 
			changeMonth: true, 
			numberOfMonths: 1,			
			yearRange: '-80:-18',
			showButtonPanel: true,
			onClose: function(dateText, inst) { 	}
		});
	});
	
	$('#print').click(function(){
		window.print();
	});
	
	$('.delitem').each(function(){
		$(this).click(function(){
			var action = 'wkwVerwijderItem';
			var pnr = $(this).attr('name');
			alert (pnr);
		});
	});
	$('#bestel_form .aantal').each(function(){
		$(this).change(function(){
			var action 	= 'wkwAantalItem';
			var pid 	= $(this).parent().parent().parent().attr('class');
			var aantal	= $(this).val();
			var valu	= $('#valu').val();
			var vzk		= $('#vzk').val();
			$('#awkw').empty().html(aantal);
			$.post('/ajax/ajax_general.php',{action:action,pid:pid,aantal:aantal,vzk:vzk},function(data){ 
					if(data){ //alert (data);
						var dat = data.split('|');
						//$('#subtotaal_'+dat[0]).empty().html($('#valu').val() + ' ' + dat[2]);
						checkWkw();
					}
			});
		});
	});
	
	
	$('#bestel_form #ccode').blur(function(){
		checkCcode();
	});
	
	//$('#bestel_form #bst_rmb').click(function(){ 
	//	if ($(this).is(':checked')) {
			$(".rembours").colorbox({inline:true, width:"640px", height:"400px"});	
	//	} else {
			
	//	}
	//});
	
	
	$('#geboortedatum').keypress(function(){
		return alpha(event,dates);	
	});
	$('#email1').keypress(function(){
		return alpha(event,emails);	
	});
	$('#email2').keypress(function(){
		return alpha(event,emails);	
	});
	$('#telefoon1').keypress(function(){
		return alpha(event,phones);	
	});
	$('#telefoon2').keypress(function(){
		return alpha(event,phones);	
	});
	$('#postcode').keypress(function(){
		return alpha(event,zips);	
	});
	$('#bezorg_postcode').keypress(function(){
		return alpha(event,zips);	
	});
	
	$('#bestel_form #email1').blur(function(){
		bestaatEmail();
	});
	
	$('#bestel_bt').click(function(){
	/* verplicht controle */
	var cfm = true;
	var vp = '';
	if ($('#bst_av').is(':checked')==false) {
		$.prompt(akkoordmetav);
		return false;	
	}
	
	if ($('#cid').length>0) { // bestaande klant
		if ($('#cid').val()=='') {
			cfm = false;
			return false;
		}
		$('#nk_submit').val('');
	} else { // nieuwe klant
	 	if (vp!='') { vp +=','; }
		vp += 'voornaam,achternaam,straat,huisnummer,postcode,woonplaats,land,geboortedatum,email1,telefoon1';
		if ($('#ccode').val()!='') {
			var cc = checkCcode();
			if (cc==false) {
				$.prompt(ccode_ongeldig);
				cfm = false;
			}
		}
		var nk = $('#ibiobst').val();
		$('#nk_submit').val(nk);
	}
		
	if ($('#bestel_aadcb').is(':checked')) {
			if (vp!='') { vp +=','; }
			vp += 'bezorg_naam,bezorg_straat,bezorg_huisnummer,bezorg_postcode,bezorg_woonplaats,bezorg_land';
	}	
	if (vp!='') { 
		vps = vp.split(','); 
		for (i=0;i<vps.length;++i) {
			if ($('#'+vps[i]).val()=='') {
				$('#'+vps[i]).css({backgroundColor: '#390683', color: '#fff'});	
				cfm = false;
			} else {
				$('#'+vps[i]).css({backgroundColor: '#fff', color: '#390683'});
			}
		}
	}
	if (cfm==false) {
		$.prompt(verplicht_txt);
		return false;
	} else { // start transactie			
			$('#bestel_bt').val('..........');
			if ($('#action').val()!='makeKey') {
				$('#action').val('makeKey');
			} else {
				$('#action').val('');
			}
			$('#bestel_form').submit();
	}
	});
	
	$('#bestel_aadcb').click(function(){
		if ($(this).is(':checked')==true) {
			$('#bestel_aad').show();	
		} else {
			$('#bestel_aad').hide();
		}
	});
	
	$('#pwr_bt').click(function(){
		var gn = $('#gebruikersnaam').val();
		if (gn!='' && isGeldigeEmail(gn)) {
			var action 	= 'pwr_maillink';
			var taal 	= $('#taal').val();
			$.post('/ajax/ajax_general.php',{action:action,gn:gn,taal:taal},function(data){ 
					if(data) {
						dat = data.split('|');
						if (dat[0]=='true'){
							$('#pwr_txt').empty().html(dat[1]);
							$('.ad_ai').slideUp();
						} else {
							$('.ad_ai').prepend('<p class="notice">'+dat[1]+'</p>');
							$('.notice').delay(5000).slideUp();
							$('#gebruikersnaam').val('');	
						}
					}
			});
			
		} 
	});
	
	/* CYCLES */
		
	$('#home_slides').cycle({
		fx: 'fade',
		timeout:	9000,
		speed:      2000,
		before:		function() {
			$('#ticker_ps').cycle('next');
		}
	});
	
	$('#home_lm').click(function(){
		$('#home_ol').animate({
				height: '+=110',
				top: '-=110'
			});
		$('#home_lm').hide();
		$('#home_ol2').hide();
		$('#home_ol3').show();
	});
	

	
	$("#acties li a").each(function(){
	  if ($(this).height() < 140) $(this).css({paddingTop: ((140 - $(this).height()) / 2) +"px"});
	});
	
	$("#acties").each(function(){
	  var obj = this;
	  if ($('#ibiopg').val()=='KL.mFfxEqotvE' || $('#ibiopg').val()=='KLyuDhha3pFHA') {
		  var ln = 3;
	  } else {
		  var ln = 1;
	  }
	  if ($("li", obj).length > ln) {
		$(obj).wrapInner("<div class=container/>");
		obj.container = $(".container", obj);
		obj.timer = null;
		obj.sleep = 5000;
		obj.animRun = false;
		obj.start = function(){obj.timer = setTimeout(obj.showNext, obj.sleep)};
		obj.showNext = function(single){
		  clearTimeout(obj.timer);
		  if (obj.animRun) {setTimeout(obj.showNext, 50);return;}
		  obj.animRun = true;
		  $("li:first-child", obj).animate({marginLeft: "-270px"}, 738, "swing", function(){
			$("li:first-child", obj).appendTo("#acties ul").css({marginLeft: "0px"});
			if (single !== true) obj.start();
			obj.animRun = false;
		  });
		};		
		$(obj).mousemove(function(){clearTimeout(obj.timer)});
		$(obj).mouseout(obj.start);
		
		obj.start();
	  } else if ($('#ibiopg').val()=='KL.mFfxEqotvE' || $('#ibiopg').val()=='KLyuDhha3pFHA') {
		  $('#acties ul').css('width','1000px');
	  }
	});
	
	$('#hwh_t1').click(function(){
		hoeWerktHet();
	});
	
	$('#hwh_lm').click(function(){
		$(this).hide();
		$('#hwh_txt2').slideDown();
	});
	
	$("a[rel]").overlay({

		mask: 'darkred',
		effect: 'apple',

		onBeforeLoad: function() {

			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".contentWrap");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}

	});
	
	/* CONTACT */
	$('#emlbt').click(function(){ alert('email');
		var e1 = 'info';
		var e2 = 'i-bio';
		var e3 = '.com';
		location.href = 'mailto:' + e1 +'@'+e2+e3;
	});
});

/* BEGIN FUNCTIES */
function checkWkw() {
	var vzk 	= $('#vzk').val();
	var action 	= 'checkWkw'; 
	$.post('/ajax/ajax_general.php',{action:action,vzk:vzk},function(data){ 
		  if(data) { //alert(data);
		  	var dat = data.split('|');
		  	$('#bstt').empty().html(dat[0]);
			$('#bvzk').empty().html(dat[1]);
			$('#btt').empty().html('&euro;&nbsp;' + dat[2]);
		  }
	});
}

function checkCcode(){
	var cc = $('#ccode').val();
	if (cc!='undefined' && cc!='' && cc!=undefined) {
		var action = 'checkCcode';
		$.post('/ajax/ajax_general.php',{action:action,cc:cc},function(data){
						if(data=='') {
							$('#ccode').val('');
							$('#ccj').remove();
							$.prompt(ccode_ongeldig);
							$('#vzk').val($('#ovzk').val());
							checkWkw();
							return false;
						} else if (data) {
							$('#ccj').remove();
							$('#ccode').after('<img src="https://www.i-bio.com/resources/accept.png" class="iconlm" id="ccj">');
							$('#vzk').val(0);							
							checkWkw();
						}
		});
	}
}

function bestaatEmail() {
	var mail = $('#email1').val();
	var action = 'bestaatEmail';
	$.post('/ajax/ajax_general.php',{action:action,mail:mail},function(data){ 
					if(data=='ja') { 
						$.prompt(emailBestaatAl);
						$('#email1').val('').focus();
					} 
	});
}

function hoeWerktHet() {
	$('#hwh_t1').fadeOut('slow');
	$('#hwh').cycle({
		fx: 'fade',
		timeout:	1000,
		speed:      1000,
		autostop:	true,
		autostopCount: 4,
		end: function(){
			$('#hwh_t1').fadeIn('slow');
		}
	});
	$('#hwh_st').cycle({
		fx: 'fade',
		timeout:	4000,
		speed:      1000,
		autostop:	true,
		autostopCount: 3
	});
}


function homeOnAfter() {
	$('#ps1').toggle(); 
	$('#ps2').toggle();	
}

function lz(getal, dgt) {
	if (dgt==2 && getal<10) {
		getal = '0'+getal;
	} else if (dgt==3 && getal<100) {
		getal = '00'+getal;
	} else if (dgt==4 && getal<1000) {
		getal = '000'+getal;
	}
	return getal;
}
function isGeldigeEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;	
	//email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.info)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi)

	if (reg.test(email)) {
		return true;
	} else { 
		return false;
	}
}
var letters		=	' ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ'
var numbers		=	'1234567890'
var digits		=	'1234567890.'
var time		=	'1234567890\'\".'
var signs		=	',.:;@-\''
var mathsigns	=	'+-=()*/'
var custom		=	'<>#$%&?¿'
var dates		=	'1234567890-'
var values		=	'1234567890,'
var bedragen	=	'1234567890,.'
var zips		=	'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
var phones		=	'+-0123456789'
var phone		=	'0123456789'
var emails		=	'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.@'

function alpha(e,allow) { 
	var k;
	k=document.all?parseInt(e.keyCode): parseInt(e.which);
	if (k==8 || k==9) {
		return;
	} else {
		
		return (allow.indexOf(String.fromCharCode(k))!=-1);
	}
}

function toUrl(url, sec) {
	window.setTimeout(location.href = url,(sec*1000));
}
