jQuery.alnum = function(value, whiteSpace){
	return String(value).replace(whiteSpace ? /[^a-zA-Z0-9\-\s]/gi : /[^a-zA-Z0-9\-]/gi,'');
}

jQuery.fn.alnum = function() {
	
  	return this.each(function(whiteSpace){
  		var input = jQuery(this);
  		var filter = function(){
		  	input.val(jQuery.alnum(this.value, whiteSpace));
		}
  		input.change(filter)
		/*.keyup(filter)*/;
		  
  	});
};

var HistoryManager = {
	getHash: function() {
		var href = top.location.href;
		var pos = href.indexOf('#') + 1;
		return (pos) ? href.substr(pos) : '';
	}
}
	
	
	/**
 	*	NETVLIES.NL JAVASCRIPTS
 	*	Author:	Sebastiaan Smid, Netvlies
 	*	Author: Stephan van Opstal, Netvlies 	
 	*
 	*	Dependencies: 
 	*	- jQuery v 1.2.2
 	*	- jQuery.lightbox 0.4
 	*/
	$(document).ready(function()
	{
		$('#action-winkelmand-wrapper').scrollFollow({
			container: 'content-box'
		});

	
        $('#prinsenkade_stream').nvsMediaPopup({});
		// Flash on 'homepage'
		placeFlashHeaders();
	
		jQuery('#domain_domain').change(function(){
			$('#domain_domain').val(dc.format_domain($('#domain_domain').val())).val()
		}).alnum(false);
	
		// Recentwork on the homepage 
		$('.recentwork').css({ cursor: "pointer" });
	
		$('.recentwork').click(function()
		{
			var div_id = this.id;
			var a_id = div_id.replace ('div', 'a');
			document.location = $('#'+a_id).attr("href");
		});
	
		$('.recentwork').hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		});
		
		//Replace the name of the bijlage with a new input type="file"
		$('#bijlage_verwijderen').click(function(){
			$($(this).parent()).replaceWith('<div><input type="file" name="bijlage" /></div>');
		});
	
		//	Open a new window without using target="_blank" for XHTML 1.1
		//	Usage: <a href="http://www.netvlies.nl" rel="external">netvlies.nl</a>
		$('a[rel$=external]').click(function(){
			this.target = '_blank';
		});
		
		// Clears input onclick
		$("#frm-search input[value!=][type=text]").focus(function () { 
			if(!$(this).attr("startvalue")) {  
				$(this).attr("startvalue", $(this).val());
			} 
			if($(this).val() == $(this).attr("startvalue")) {
				$(this).val('');	
			}
		}).blur(function () { 
			if($(this).val() == '') {
				$(this).val($(this).attr("startvalue"));
			}
		});

		// Lightbox
		$('a[rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
		$('.presentation').flashBox();
		
		$('a[rel*=nvsalbum]').nvsAlbum({overlayBackground: '#e7e5e4', overlayTransparancy: '0.8', popUpBackground: 'white'});

		// Print button
		if(window.print && document.getElementById("btn-print")) { 
			$('#btn_print').css({display: "inline"});
			$('#btn_print').click(
				function() { 
					window.print();
				}
			);
		}
		
		// Inline print function (routebeschrijving)
		if(window.print && ($('.inline_print').length > 0)) { 
			$('.inline_print').click(
				function() { 
					window.print();
				}
			);
		}
		
		//login script 
		$('#client-login').click(function() {
			setTimeout(_login_show, 0);
		});
		
		if(HistoryManager.getHash() == 'login'){
 			$('#client-login').click();
		}
		
		// IE6 PNG fix
		if (window.DD_belatedPNG) {
			DD_belatedPNG.fix('#frm-contact div span.error');
		}
		
		var mand = $('#action-winkelmand');
		if(mand[0]){
			dc.cart.init();
		}
		// HOSTING SCRIPTS
		$('a.moreinfo').toggle(
			function () {
				$(this).siblings('.pakketinfo').css({"display":"block"});
				$(this).children('img').css({ "position" : "absolute", "top" : "-13px" })
			},
			function () {
				$(this).siblings('.pakketinfo').css({"display":"none"});
				$(this).children('img').css({ "position" : "relative", "top" : "0" })
			}					   
		)
		
		
		factuuradres = false;
		$("#factuuradresbtn").change( function() {
			if($(this).attr('checked')){
				$("#factuuradres").show();
				factuuradres = true;
			} else { 
				$("#factuuradres").hide();
				factuuradres = true;
			}
			return false;
		}).change();
	
		$("#betaling input[type='radio']").change( function() {
			$("#betaling .payment-part").hide()
			obj = "." + $(this).attr('id') + "-part";
			$(obj).show();
		});
		if($('#bedrijf')[0]){
			var show_bedrijfsgegevens = function() {
				if($('#bedrijf')[0].checked){
					$('#bedrijfgegevens').show();
				} else {
					$('#bedrijfgegevens').hide();
				} 
	// 			if($(this).attr('id') == 'bedrijf') {
	// 				$('#bedrijfgegevens').show();
	// 				$('tr.postbusfield').show();
	// 			} else {
	// 				$('#bedrijfgegevens').hide();
	// 				$('tr.postbusfield').hide();
	// 			}
			}
			show_bedrijfsgegevens();
			$("#naw input[type='radio']").change(show_bedrijfsgegevens);
		}
		if($('#hosting_step2')[0]){
			dc.pack.init();
		}
		
	});
	
	function placeFlashHeaders()
	{
		var headers = new Array();
		headers[0] = 'swf/header_nvs.swf';
		headers[1] = 'swf/header_nvs.swf';
		headers[2] = 'swf/header_nvs.swf';
		var randomnumber = Math.floor(Math.random()*3);

		$('#flash_banner').flash({
			src: headers[randomnumber],
			width: 910,
			height: 168,
		}, {
			update: false
		});
		
		$('#flash_banner_image').css({ visibility: 'visible' });
	}
var dc = {
	common_ext: {},
	all_ext: {},
	create_domainrow: function(options){
		var img = '';
		var link_class = '';
		if(options.status == 'Vrij'){
			img = '<img src="/img/domein_ico_registreren.gif" alt="Registreren" />';
			link_class = 'add_cart';
		} else if(options.status == 'Bezet') {
			img = '<img src="/img/domein_ico_verhuizen.gif" alt="Verhuizen" />';
			link_class = 'domain_registered';
		} else {
			img = '';
		}
		var tr = jQuery(
			'<tr id="'+options.id+'">'+
				'<td class="first"><a class="'+link_class+'">'+img+'</a></td>'+
				'<td>'+options.domain+'</td>'+
				'<td>'+options.status+'</td>'+
				'<td></td>'+
				'<td>&euro; '+options.price+'</td>'+
			'</tr>'
		);
		tr.find('.add_cart, .domain_registered').click(function(){
			dc.cart.add_domain(options.domain);
		});
		return tr;
	},
	format_domain: function(domain){
		return domain.split('.')[0];
	},
	send_form: function(){
		$('#domain_list').empty();
		//Format the domain
		var domain = $('#domain_domain').val(dc.format_domain($('#domain_domain').val())).val();
		
		var exts = jQuery('#domain_extensions').val() === 'common' ? dc.common_ext : dc.all_ext ; 
		jQuery(exts).each(function(index, domainext){
			var data = 'ext='+domainext.extension+'&'+jQuery('#hosting_step1').serialize();
			$('#domain_list').prepend(dc.create_domainrow({
				id: 'domain_row_'+domainext.domain_extension_id,
				domain: domain+'.'+domainext.extension,
				status: 'Controleren',
				price: domainext.price
			}));

			$.ajax({
				url: "/ajax/domaincheck.ajax.php",
 				data: data, 
 				dataType: 'json',
				success: function(data){
					$('#results').show(200);
					$('#domain_row_'+domainext.domain_extension_id).replaceWith(dc.create_domainrow({
						id: 'domain_row_'+domainext.domain_extension_id,
						domain: domain+'.'+domainext.extension,
						status: data.status,
						price: domainext.price
					}));
				}
			});
		});
		
		return false;
	},
	cart: {},
	pack: {}
}

dc.cart = {
	add_domain: function(domain){
		var data = 'domain='+domain+'&action=add_domain';
		$.ajax({
			url: "/ajax/domaincart.ajax.php",
			data: data,
			beforeSend: function() { $('#action-winkelmand-wrapper').fadeOut(); },
			complete: dc.cart.refresh
// 			dataType: 'json',
// 			success: function(data){
// 				alert(data);
// 			}
		});
	},
	init: function(){
		$('#action-winkelmand .del').click(function(){
			dc.cart.remove(this.id.split('_')[2]);
		});
	},
	remove: function(id){
		$.ajax({
			url: "/ajax/domaincart.ajax.php",
			data: 'action=remove_domain&domain_cart_id='+id, 
			dataType: 'html',
			complete: dc.cart.refresh,
			success: function(){
				if($('#hosting_step2')[0]){
					window.location.href = String(window.location.href);
				} else {
					dc.cart.refresh();
				}
			}
		});
	},
	refresh: function(){
		$.ajax({
			url: "/ajax/domaincart.ajax.php",
			data: 'action=list'+($('#hosting_step1')[0] ? '&allow_delete_last=1' : ''), 
			dataType: 'html',
			success: function(data){
				$('#action-winkelmand').html(data);
				dc.cart.init();
			},
			complete: function() { $('#action-winkelmand-wrapper').fadeIn() }
		});
	}
};

dc.pack = {
	init: function(){
		var form = $('#hosting_step2');
		form.submit(dc.pack.send_form);
		form.find('input').change(function(){ 
			form.submit();
		});
	},
	send_form: function(){
		$.ajax({
			url: "/ajax/domaincart.ajax.php",
			data: 'action=set_package&'+$('#hosting_step2').serialize(),
			complete: function(){
				dc.cart.refresh();
			} 
// 			dataType: 'jsson',
// 			success: function(data){
// 				$('#results').show(200);
// 				$('#domain_row_'+domainext.domain_extension_id).replaceWith(dc.create_domainrow({
// 					id: 'domain_row_'+domainext.domain_extension_id,
// 					domain: domain+'.'+domainext.extension,
// 					status: data.status,
// 					price: domainext.price
// 				}));
// 			}
		});
		return false;
	}
}
