$(function() {
		var arrGebruikers;
		
		$.get("/inc/checkusername.asp", function(response) {
				arrGebruikers = [ response ];
			});
		
		var loginnaam = $("#username2"), 
			wachtwoord  = $("#password2"),
			naam  = $("#naam"),
			gemeente  = $("#gemeente"),
			provincie  = $("#provincie"),
			geboortedatum  = $("#geboortedatum"),
			email  = $("#email"),
			gebruikersnaam  = $("#gebruikersnaam"),
			ww  = $("#ww"),
			wwherhaal  = $("#wwherhaal"),

			allFields = $([]).add(loginnaam).add(wachtwoord).add(naam).add(gemeente).add(provincie).add(geboortedatum).add(email).add(gebruikersnaam).add(ww).add(wwherhaal),
			tips = $("#validateTips");
				
		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
				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;
			}

		}
		
		function checkNumber(o,n) {
			var val;
			
			val = parseInt(o.val());
			if (val != o.val()) {
				o.addClass('ui-state-error');
				//updateTips(n);
				return false;
			}
			else {
					return true;
			}

		}
		
		function checkUsername(o,n) {
			var bodyContent = $.ajax({
				url: "/inc/checkusername.asp",
				global: false,
				type: "POST",
				data: {n : o.val().toLowerCase()},
				dataType: "html",
				async:false,
				success: function(msg){
					//alert(msg);
				      }   
				}).responseText;
			
			if (bodyContent == 'ok')
			{
				return true;
			}
			else
			{
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			}		
			  
		}
		
		$("#login-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 270,
			width: 350,
			modal: true,
			buttons: {
				'Verzenden': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(loginnaam,"Login",2,2000);
					bValid = bValid && checkLength(wachtwoord,"Paswoord",1,2000);
				
					if (bValid) {
					    document.frmlogin2.submit();
						$(this).dialog('close');
					}
				},
				'Annuleren': function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});

		
		$('#frmlogin2').keyup(function(e) {
			//alert(e.keyCode);
			if(e.keyCode == 13) {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(loginnaam,"Login",2,2000);
				bValid = bValid && checkLength(wachtwoord,"Paswoord",1,2000);
			
				if (bValid) {
					document.frmlogin2.submit();
					$(this).dialog('close');
				}
			}
		});
		
		$("#bewerk-profiel-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 650,
			width: 540,
			modal: true,
			buttons: {
				'Verzenden': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(naam,"Voor- en achternaam",2,2000);
					bValid = bValid && checkLength(gemeente,"gemeente",2,2000);
					bValid = bValid && checkNumber(provincie,"Gelieve een provincie te kiezen");
					bValid = bValid && checkLength(geboortedatum,"geboortedatum",10,10);
					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,"vb. info@mtb-you.be");
					bValid = bValid && checkLength(gebruikersnaam,"gebruikersnaam",2,50);					
					bValid = bValid && checkUsername(gebruikersnaam, "Deze gebruikersnaam bestaat al.");
					
					bValid = bValid && checkLength(ww,"wachtwoord",2,50);
					bValid = bValid && checkLength(wwherhaal,"wachtwoord herhalen",2,50);
				 
				 	if (ww.val() != wwherhaal.val())
					 {
						ww.addClass('ui-state-error');
						wwherhaal.addClass('ui-state-error');
						updateTips("U dient 2x hetzelfde wachtwoord in te vullen.");
						bValid = false;
					 }
					
					if (bValid) {
					    document.frmBewerkProfiel.submit();
						$(this).dialog('close');
					}
				},
				'Annuleren': function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		$('#frmBewerkProfiel').keyup(function(e) {
			//alert(e.keyCode);
			if(e.keyCode == 13) {
				var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(naam,"Voor- en achternaam",2,2000);
					bValid = bValid && checkLength(gemeente,"gemeente",2,2000);
					bValid = bValid && checkNumber(provincie,"Gelieve een provincie te kiezen");
					bValid = bValid && checkLength(geboortedatum,"geboortedatum",10,10);
					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,"vb. info@mtb-you.be");
					bValid = bValid && checkLength(gebruikersnaam,"gebruikersnaam",2,50);
					bValid = bValid && checkUsername(gebruikersnaam, "Deze gebruikersnaam bestaat al.");
					bValid = bValid && checkLength(ww,"wachtwoord",2,50);
					bValid = bValid && checkLength(wwherhaal,"wachtwoord herhalen",2,50);
				 
					if (ww.val() != wwherhaal.val())
					 {
						ww.addClass('ui-state-error');
						wwherhaal.addClass('ui-state-error');
						updateTips("U dient 2x hetzelfde wachtwoord in te vullen.");
						bValid = false;
					 }
					
					if (bValid) {
					    document.frmBewerkProfiel.submit();
						$(this).dialog('close');
					}
			}
		});
		
		$('.create-login2').click(function() {
					$('#login-dialog').dialog('open');
				})
				
		$('.bewerk-profiel').click(function() {
			$('#bewerk-profiel-dialog').dialog('open');
		})
		
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});
		
		$("#geboortedatum" ).datepicker( {changeMonth:true, changeYear:true, yearRange: '1900:<%=year(date())%>', defaultDate:'02/07/1979',altField: "#alternate" } );
		$("#geboortedatum" ).datepicker( "option", $.datepicker.regional["nl"] );
		
		$(".clickdiv").click(function () {
			var imgHTML = $(this).parent().parent().find(".tekstje").html();
			var divHeight = $(this).parent().parent().find(".tekstje").height();
			$("#divke").slideUp("slow",function(){
				$("#divke").height(divHeight);
				$("#divke").html(imgHTML);
				$("#divke").slideDown("slow", function(){
			
				});
			});
			
			//$("#divke").html(imgHTML).slideToggle("slow", function(){			
			//});
			
		});
	
	
	});


function GetData(wat, lang) {
	var xhr_object = null;
	if(window.XMLHttpRequest) { xhr_object = new XMLHttpRequest(); }
	else if(window.ActiveXObject) { xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); }
	else { alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");return; }
	var method 				= "POST";
	var filename			= "/inc/get"+wat+".asp";
	var data 				= "lang="+lang;

	xhr_object.open(method, filename, true);
	xhr_object.onreadystatechange = function() {
		if(xhr_object.readyState == 4) {
			var response = xhr_object.responseText;
			var update = new Array();
			if(response.indexOf('|') != -1) 
			   {
				  update = response.split('|');
				  var drelement = document.getElementById(wat);
				  drelement.innerHTML = update[1].toString();
			   }
		}
	}
	if(method == "POST") {
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr_object.send(data);
	}
}	
