
function grond() {}
function do_submit() {}

function testimonial_meer(what) {
	testimonial_minder();
	$('.testimonial_'+what+' .testimonial_intro').slideUp(function() {
		$('.testimonial_'+what+' .testimonial_full').slideDown();
	});
}
function testimonial_minder() {
	$('.testimonial').each(function() {
		var t = this;
		$('.testimonial_full', t).slideUp(function() {
			$('.testimonial_intro', t).slideDown();
		});
	});
}

function loggedout() {
	if(window.refreshOnLogout) {
	 	window.location.reload();
	} else {
		$(".menuitem.loggedout").show();
		$(".menuitem.loggedin").hide();
		/* // Turned off by Terry @ request Gideon - 13-12-2010
		if(views>=2){
			$('a.limited').bind('click',function(e) {
				e.preventDefault();
				open_overpage($(this).attr('rel'),{target:$(this).attr('href')});
			});
		}
		*/
	}
}
function loggedin() {
	$(".menuitem.loggedin").show();
	$(".menuitem.loggedout").hide();
	//$('a.limited').unbind('click');
}

function logout() {
	args = {script:'logout'};
	do_act(args, function(data) {
		if(data.data.result==true) {
			//window.location.reload();
			loggedout();
		} else {
			alert('An error occured');
		}
	});
}
function isNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

function do_act(args, success, async) {

	if((args == 'undefined') || (args.script == 'undefined')) {
		alert('do_act error: script not defined');
		return false;
	}

	if(!success) {
		success = function(data) {
			if(data.length > 0) {
				alert("'"+args.script+"' says:\n"+data.out);
			}
		}
	}

	$.ajax({
		url: window.siteroot + "act/gateway.php",
		dataType: 'json',
		type: 'POST',
		data: args,
		success: success,
		async: async ? async : true
	});
}

function form_submit(what, script, callback) {

	$('.'+what+' .errormsg').slideUp(function() {
	
		$('.'+what+' .error').removeClass('error');
		$('.'+what+' .errormsg .dynamic_error').html('');
	
		var args = {'script': 'formhandler.'+script};
		$('.jsform'+(what ? '.'+what : '')+' input').each(function() {
			switch($(this).attr('type')) {
				case 'checkbox' :
					args[$(this).attr('name')] = $(this).attr('checked') ? 'true' : 'false';
					break;
				case 'radio':
					if($(this).attr('checked')) { args[$(this).attr('name')] = $(this).val(); }
					break;
				default:
					args[$(this).attr('name')] = $(this).val();
			}
		});
		$('.jsform'+(what ? '.'+what : '')+' select, .jsform'+(what ? '.'+what : '')+' textarea').each(function() {
			args[$(this).attr('name')] = $(this).val();
		});
		
		$('.'+what+' .errormsg .dynamic_error').html('');
		
		// TODO: validate input fields. post hidden fields with validation metadata.
		// - which fields should be validated
		// - validator (number, phone number, zip code, email address, ...)
		// - dependency (only validate if a specified field has or does not have a specified value)
		var validated = true;
		for(i in args) {
			if(i == 'email') {
				if(what!='meerinfo' || args['optin']=='true') {
					var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
					if(!(emailPattern.test(args[i]))) {
						validated = false;
						$('.'+what+' [name='+i+']').addClass('error');
						$('.'+what+' .errormsg .dynamic_error').html($('.'+what+' .errormsg .dynamic_error').html()+'Het e-mail adres is incorrect.<br/>');
					}
				}
			} 
			if((i=='telephone') || (i=='phone')) {
				var filtered = args[i].split('-').join('').split(' ').join('');
				if(!isNumeric(filtered)) {
					validated = false;
					$('.'+what+' [name='+i+']').addClass('error');
					$('.'+what+' .errormsg .dynamic_error').html($('.'+what+' .errormsg .dynamic_error').html()+'Het telefoonnummer bevat ongeldige tekens.<br/>');
//				} else if(filtered.length != 10) {
				} else if(filtered.length < 10) {
					validated = false;
					$('.'+what+' [name='+i+']').addClass('error');
					$('.'+what+' .errormsg .dynamic_error').html($('.'+what+' .errormsg .dynamic_error').html()+'Het telefoonnummer moet minimaal 10 cijfers bevatten.<br/>');
				} else if(filtered.length > 150) {
					validated = false;
					$('.'+what+' [name='+i+']').addClass('error');
					$('.'+what+' .errormsg .dynamic_error').html($('.'+what+' .errormsg .dynamic_error').html()+'Het telefoonnummer mag niet meer dan 150 cijfers bevatten.<br/>');
				}
				
			}
		}
		
		if(!validated) {
			$('.'+what+' .errormsg').slideDown();
		} else {
			
			do_act(args, function(data) {
				if(data.data.success == 'true') {
					if(callback) { callback(); }
				} else {
					if(data.data.errormsg){
						$('.'+what+' .errormsg .dynamic_error').html(data.data.errormsg);
					}
					for(i in data.data.error) {
						$('.'+what+' [name='+data.data.error[i]+']').addClass('error');
						$('.'+what+' [name='+data.data.error[i]+'_fake]').addClass('error');
					}
					$('.'+what+' .errormsg').slideDown();
				}
			});
		}
	});
}

function init_overpage_links() {
	$('a.overpage').bind('click',function(e) {
		e.preventDefault();
		open_overpage($(this).attr('rel'),{target:$(this).attr('href')});
	});
	
	/* // Turned off by Terry @ request Gideon - 13-12-2010
	if(views>=2&&limit_active==true){
		$('a.limited').bind('click',function(e) {
			e.preventDefault();
			open_overpage($(this).attr('rel'),{target:$(this).attr('href'), blockedbylimit:true});
		});
	}
	*/
/*
	// close overpage with click on shade
	$('#overpage_shade').click(function(e) {
		e.preventDefault();
		close_overpage();
	});
*/
}
	
function open_overpage(what, args) {
	if(!args) { args = {}; }
	args['script'] = 'overpage.'+what;
	do_act(args, function(data) {
		$('#overpage').show();
		$('#overpage #overpage_shade').fadeIn(function() {
			$('#overpage #overpage_box').slideUp(function() {
				$('#overpage #overpage_content').html(data.out);
				
				// jsform behaviour fixes: submit on enter
				$('#overpage #overpage_content form.jsform').each(function() {
					var jsform = this;
					$(jsform).prepend('<input class="submit" type="submit" value="submit" style="display:none;" />');
					
					$('input', jsform).bind('keydown', function(e) {
						if((e.keyCode ? e.keyCode : e.which)==13) {
							e.preventDefault();
							$('a.submit', jsform).click();
						}
					});
				});
				// end fixes
				
				$('#overpage #overpage_box').slideDown();
			});
		});
	});
}

function close_overpage(fun) {
	$('#overpage #overpage_box').slideUp(function() {
		$('#overpage #overpage_shade').fadeOut(function() {
			$('#overpage').hide();
			$('#overpage #overpage_content').html('');
			if(fun) fun();
		});
	});
}

function slide_in(what, duration, callback) {
	if(!duration) duration = 1500;
	$(what).css('left', 0).css('marginLeft', 0 - $(what).width()).show().animate({
		'marginLeft': 0
	}, duration, function() {
		if(callback) callback();
	});
}
function slide_out(what, duration, callback) {
	if(!duration) duration = 1500;
	$(what).animate({
		'marginLeft': 0 - $(what).width()
	}, duration, function() {
		$(what).hide();
		if(callback) callback();
	});
}

function slide_in_right(what, duration, callback) {
	if(!duration) duration = 1500;
	$(what).css('left', '100%').show().animate({
		'marginLeft': 0 - $(what).width()
	}, duration, function() {
		if(callback) callback();
	});
}
function slide_out_right(what, duration, callback) {
	if(!duration) duration = 1500;
	$(what).animate({
		'marginLeft': 0
	}, duration, function() {
		$(what).hide();
		if(callback) callback();
	});
}

function do_actionpanel_campaign(callback) {
	$('#actionpanel .actie span').html(window.actionpanel_campaign);
	slide_in('#actionpanel .actie', 1500, function() {
		if(callback) callback();
	});
}

function actionpanel_init(interval, inspeed, outspeed) {
	window.actionpanel = {
		'items': new Array,
		'active': 0,
		'interval': interval ? interval : 6000,
		'inspeed': inspeed ? inspeed : 2000,
		'outspeed': outspeed ? outspeed : 1000,
		'handle': false
	};
	
	$('#actionpanel .actionpanelitem').each(function() {
		window.actionpanel.items.push({
			id:$(this).attr('id').split('_').pop(),
			flash:$('.panel_bg', this).hasClass('panel_flash') ? $.flash.create({
				swf:$('.panel_bg span', this).html(),
				height:'100%',
				width:'100%'
			}) : false
		});
	});
	
	$('#actionpanel .panel_bg.panel_flash').html('');
	
	if(window.actionpanel.items[window.actionpanel.active].flash) {
		$('#actionpanel #actionpanelitem_'+window.actionpanel.items[window.actionpanel.active].id+' .panel_bg').html(window.actionpanel.items[window.actionpanel.active].flash);
	}
	slide_in_right('#actionpanel #actionpanelitem_'+window.actionpanel.items[window.actionpanel.active].id+' .payoff', 1000, function() {
		if(window.actionpanel_campaign) {
			window.setTimeout("do_actionpanel_campaign(actionpanel_start)", 1500);
		} else {
			actionpanel_start();
		}
	});
}
function actionpanel_start() {
	actionpanel_stop();
	window.actionpanel.handle = window.setTimeout("actionpanel_tick();", window.actionpanel.interval);
}
function actionpanel_stop() {
	if(window.actionpanel && window.actionpanel.handle) {
		window.clearTimeout(window.actionpanel.handle);
		window.actionpanel.handle = false;
	}
}
function actionpanel_tick() {
	if(window.actionpanel && (window.actionpanel.items.length > 0)) {
		slide_out_right('#actionpanel #actionpanelitem_'+window.actionpanel.items[window.actionpanel.active].id+' .payoff', 1000, function() {
			$('#actionpanel #actionpanelitem_'+window.actionpanel.items[window.actionpanel.active].id).fadeOut(window.actionpanel.outspeed, function() {
				if(window.actionpanel.items[window.actionpanel.active].flash)  {
					$('#actionpanel #actionpanelitem_'+window.actionpanel.items[window.actionpanel.active].id+' .panel_bg').html('');
				}
				window.actionpanel.active = (window.actionpanel.active + 1) % window.actionpanel.items.length;
				if(window.actionpanel.items[window.actionpanel.active].flash)  {
					$('#actionpanel #actionpanelitem_'+window.actionpanel.items[window.actionpanel.active].id+' .panel_bg').html(window.actionpanel.items[window.actionpanel.active].flash);
				}
				$('#actionpanel #actionpanelitem_'+window.actionpanel.items[window.actionpanel.active].id).fadeIn(window.actionpanel.inspeed, function() {
					slide_in_right('#actionpanel #actionpanelitem_'+window.actionpanel.items[window.actionpanel.active].id+' .payoff', 1000);
					window.actionpanel.handle = window.setTimeout('actionpanel_tick();', window.actionpanel.interval);
				});
			});
		});
	}
}



