jQuery(function() {
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		var name = $( "#msgNAME" ),
			phone = $( "#msgPHONE" ),
			allFields = $( [] ).add( name ).add( phone ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, msg ) {
			if ( o.val().length < 1  ) {
				o.addClass( "ui-state-error" );
				updateTips( msg );
				return false;
			} else {
				return true;
			}
		}
    
    
    jQuery( "#dialog-form" ).dialog({
             autoOpen: false,
             height: 225,
             width: 380,
             modal: true,
             buttons: {
                 "Mitteilung abschicken": 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,"Korrekte E-Mail Adresse eingeben (z.B. info@sander.de)" );
                     bValid = bValid && checkLength(name,"Bitte Name eingeben.");
                    bValid = bValid && checkLength(phone,"Bitte Telefonnummer eingeben.");

                     if ( bValid ) {
                         // per eid eintrag aus datum und email in erinnerungstabelle eintragen
                        msgNAME = jQuery("#msgNAME").val();
                        msgPHONE = jQuery("#msgPHONE").val();

                         jQuery.ajax({
                             url: '/wir_rufen_zurueck.php?name='+msgNAME+'&phone='+msgPHONE,
                             success: function(data) {
                                 jQuery("#dialog-form").text("Ihre Nachricht wurde erfolgreich übermittelt.");
                                 jQuery(".ui-dialog-buttonset").hide();
                                 setTimeout(function() { jQuery( "#dialog-form" ).dialog( "close" ); },2000);
                             }
                         });

                         /*jQuery( this ).dialog( "close" );*/
                     }
                 },
                 "Schliessen": function() {
                     jQuery( this ).dialog( "close" );
                 }
             }
         });



     // kommt auffe kontaktknopf
     jQuery('.callback').click(function() {
             jQuery( "#dialog-form" ).dialog( "open" );
             return false;
     });
});


