$(document).ready(function() {

		$("a[href*='.pdf']").each(function(index) {
			$(this).attr("target","_blank");
		});
  
		var name = $( "#name" );
		var email = $( "#email" );
		var company = $( "#company" );
		var functie = $( "#function" );
		var telephone = $( "#telephone" );
		allFields = $( [] ).add( name ).add( email );
		tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "De lengte van uw " + n + " moet tussen de " +
					min + " en de " + max + " karakters zijn." );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			width: 400,
			modal: true,
			draggable: false,
			buttons: {
				"Bevestig": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( email, "email", 6, 80 );
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "bijv. info@ictroom.nl" );
					bValid = bValid && name.val()!="";
					bValid = bValid && company.val()!="";
					bValid = bValid && functie.val()!="";
					bValid = bValid && telephone.val()!="";

					if ( bValid ) {
						
						setCookie("gegevens",1,1);
						setCookie("name",name.val(),1);
						setCookie("email",email.val(),1);
						setCookie("company",company.val(),1);
						setCookie("functie",functie.val(),1);
						setCookie("telephone",telephone.val(),1);
						$("#dialog-form").html("<p>Bedankt voor uw contact gegevens, u kunt nu elke pdf openen.</p>");
												$(".ui-button .ui-button-text").each(function(){
							if($(this).html()=="Bevestig") {
								$(this).html("Ok");
							} else {
								$("#dialog-form").dialog( "close" );
								//$(this).parent().hide("fast");
							}
						});
						return true;
					}
				}
			},
			close: function() {
				allFields.removeClass( "ui-state-error" );
			}
		});
	
	$("a[href*='.pdf']").bind("click", function () {
		if(getCookie("gegevens") == 1) {
		$.ajax({
		  url: "http://www.ictroom.nl/assets/mail.php?pdf="+$(this).attr('href')+"&sender="+getCookie("email")+"&name="+getCookie("name")+"&company="+getCookie("company")+"&functie="+getCookie("functie")+"&telephone="+getCookie("telephone"),
		  context: document.body,
		  async: true
		});
		} else {
		$( "#dialog-form" ).dialog( "open" );
		return false;
		}
    });
	
	function setCookie(name,value,exdays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=name + "=" + c_value;
	}
	function getCookie(c_name) {
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++)
		{
			x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
			if (x==c_name)
			{
				return unescape(y);
			}
		}
		return false;
	}
	
	$(".ui-button .ui-button-text").click(function(){
		if($(this).html()=="Ok") {
			$("#dialog-form").dialog( "close" );
		}
	});
	
	$(".ui-widget-overlay").live('click',function(){
		$("#dialog-form").dialog( "close" );
	});
});
