
$(function(){
	  
      $( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		var anrede = $("#anrede"),
		    vorname = $( "#vorname" ),
		    nachname = $("#nachname"),
		    strasse = $("#strasse"),
		    notiz = $("#notiz"),
		    hn = $('#hn'),
		    plz = $('#plz'),
		    ort = $('#ort'),
		    tel = $('#tel'),
			email = $( "#email" ),
			option = $("#option"),
			password = $( "#password" ),
			allFields = $( [] ).add( vorname ).add( nachname ).add( strasse).add(hn).add(plz),
			tips = $( ".validateTips" );
		   
				
				
				$('#dialog-datenschutz').dialog({
					autoOpen: false,
					height: 600,
					width: 800,
					modal: true,
					resizable: false,
					title: "Datenschutz",
					buttons: {
						
						"Schließen": function() { 
							$(this).dialog("close"); 
						} 
					}
				});
				
				$('#dialog-impressum').dialog({
					autoOpen: false,
					height: 500,
					width: 720,
					modal: true,
					resizable: false,
					title: "VDSL - Impressum",
					buttons: {
						
						"Schließen": function() { 
							$(this).dialog("close"); 
						} 
					}
				});
				// dynamischer Dialog Link
				$('#infoLink').click(function(){
					$('#mehrInfos').load('mehrinfos.html');
					$('#mehrInfos').dialog('open');
					return false;
				});

				$('#impLink').click(function(){
					$('#dialog-impressum').dialog('open');
					return false;
				});
				$('#formLink').click(function(){
					$('#dialog-form').dialog('open');
					return false;
				});
			
				function updateTips( t ) {
					tips
						.text( t )
						.addClass( "ui-state-highlight" );
					setTimeout(function() {
						tips.removeClass( "ui-state-highlight", 1500 );
					}, 500 );
				}
				
				function checkLength( o, min, max, msg) {
					if ( o.val().length > max || o.val().length < min ) {
						o.addClass( "ui-state-error" );
						updateTips(msg);
						return false;
					} else {
						return true;
					}
				}
				
				function validateCaptcha(msg)
				{
					
					challengeField = $("input#recaptcha_challenge_field").val();
					responseField = $("input#recaptcha_response_field").val();
					
					var html = $.ajax({
						type: "POST",
						url: "checkcaptcha.php",
						data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
						async: false
						}).responseText;
						
					
					
					if(html == "success")
					{
						//$("#captchaStatus").html("Submitting...");
						return true;
						
					}else{
						
						//$("#captchaStatus").html(htmlC);
						//o.addClass( "ui-state-error" );
						
						Recaptcha.reload();
						alert('Fehler beim Captcha Geben Sie erneut die Wörter ein.');
						
						return false;
					
					}
				}


				function checkRegexp( o, regexp, msg ) {
					if ( !( regexp.test( o.val() ) ) ) {
						o.addClass( "ui-state-error" );
						updateTips( msg );
						return false;
					} else {
						return true;
					}
				}

				$('#dialog-details').dialog({
					autoOpen: false,
					height: 500,
					width: 720,
					modal: true,
					resizable: false,
					title: "Details",
					buttons: {
						
						"Schließen": function() { 
							$(this).dialog("close"); 
						} 
					}
				});
				$( "#dialog-form" ).dialog({
					autoOpen: false,
					height: 600,
					width: 500,
					draggable: false,
					resizable: false,
					modal: true,
					buttons: {
						
						"abbrechen": function() {
							allFields.val( "" ).removeClass( "ui-state-error" );
							$( this ).dialog( "close" );
						}
					,
						"Formular senden": function() {
							var bValid = true;
							allFields.removeClass( "ui-state-error" );

							bValid = bValid && checkLength( vorname, 3, 16, "Bitte einen gültigen Vornamen eingeben." );
							bValid = bValid && checkLength( nachname, 3, 16, "Bitte einen gültigen Nachnamen eingeben." );
							bValid = bValid && checkLength( strasse, 3, 16, "Bitte eine gültige Straße eingeben." );
							bValid = bValid && checkRegexp( hn, /^\d{1,3}$/, "Bitte eine gültige Hausnummer eingeben.");
							bValid = bValid && checkRegexp( plz, /^[0-9]{5}$/, "Bitte eine gültige Postleitzahl eingeben.");
							bValid = bValid && checkLength( ort, 3, 16, "Bitte einen gültigen Ort angeben.");
							bValid = bValid && checkRegexp( tel, /^[0-9 +]{4,}$/, "Bitte eine gültige Rufnummer eingeben.");
							bValid = bValid && validateCaptcha();
							
							
							//bValid = bValid && checkLength( password, "password", 5, 16 );

							//bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
							// 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, "eg. ui@jquery.com" );
							//bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );

							if ( bValid ) {
								
							
								$('#dialog-form').empty();
								//$.post('server.php', $("#mainform").serialize(), function(data){
									//$('#dialog-form').html(data);
								$.post("server.php", { 
									
									anrede: anrede.val(),
									vorname: vorname.val(), 
									nachname: nachname.val(),
									strasse: strasse.val(),
									hn: hn.val(),
									plz: plz.val(),
									ort: ort.val(),
									tel: tel.val(),
									notiz: notiz.val(),
									option: option.val(),
									send: true,
										}, function(data){$('#dialog-form').html(data)} );
								//});
							

								var options = {
									    buttons: {
									        Schließen: function () {
									            $(this).dialog('close');
									            // add code here
									        }
									    }
									};
									$('#dialog-form').dialog('option', options);

								
							//	$( "#users tbody" ).append( "<tr>" +
								//	"<td>" + name.val() + "</td>" + 
									//"<td>" + email.val() + "</td>" + 
								//	"<td>" + password.val() + "</td>" +
								//"</tr>" ); 
								//$( this ).dialog( "close" );
							}
						}
					}
				});

				
				
				
				
			});
