$(document).ready(function () {

    $('#name').focus(function () {
	$('#name').removeClass('valid');
    });
    $('#surname').focus(function () {
	$('#surname').removeClass('valid');
    });
    $('#email').focus(function () {
	$('#email').removeClass('valid');
    });
    $('#phone').focus(function () {
	$('#phone').removeClass('valid');
    });
    $('#note').focus(function () {
	$('#note').removeClass('valid');
    });



    $.fn.insert = function () {
	waitingDialog.show('Bitte warten...');


	var salutationval = $("#salutation").val();
	var nameval = $("#name").val();
	var surnameval = $("#surname").val();
	var phoneval = $("#phone").val();
	var emailval = $("#email").val();
	var noteval = $("#note").val();

	var dataString = 'MODEL=KONTAKTSENDEN&SALUTATION=' + salutationval + '&NAME=' + nameval + '&SURNAME=' + surnameval + '&PHONE=' + phoneval + '&EMAIL=' + emailval + '&NOTE=' + noteval;
	//  alert(dataString);
	// return false;
	// return false;
	$.ajax
		({
		    type: "POST",
		    url: "app/php/API.php",
		    data: dataString,
//                dataType: "json",
		    cache: false,
		    success: function (html) {
			waitingDialog.hide();
			if (html === 'ERROR') {
			    window.scrollTo(0, 0);
			    waitingDialog.hide();
			    swal({
				title: "FEHLER!",
				text: "Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später noch einmal.",
				html: true
			    });

			    return false;
			} else {
			    window.scrollTo(0, 0);
			    waitingDialog.hide();
			    swal({
				title: "VIELEN DANK!",
				text: "Ihre Nachricht wurde erfolgreich empfangen. Wir werden uns so bald wie möglich kontaktieren.",
				html: true
			    },
				    function () {
					waitingDialog.hide();
					$("#formreset")[0].reset();

				    });


			}
		    }
		});
    };
    $.fn.validate = function () {

	var nameval = $("#name").val();
	var surnameval = $("#surname").val();
	var emailval = $("#email").val();
	var phoneval = $("#phone").val();
	var noteval = $("#note").val();
	if (nameval == '') {
	    $().info('name', 'Bitte Vorname eingeben.');
	    return false;
	}
	if (surnameval == '') {
	    $().info('surname', 'Bitte Nachname eingeben');
	    return false;
	}
	if (phoneval == '') {
	    $().info('phone', 'Bitte Telefonnummer eingeben');
	    return false;
	}
	if (noteval == '') {
	    $().info('note', 'Bitte Nachricht eingeben');
	    return false;
	}
	if (emailval != '') {
	    var re = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/igm;
	    if (!re.test(emailval)) {
		$().info('email', 'Ungültige E-Mail Adresse.');
		return false;
	    }
	} else {
	    $().info('email', 'Bitte E-Mail Adresse eingeben.');
	    return false;
	}

	$().insert();
    }

    $.fn.info = function (id, msg) {
	$('#' + id).addClass('valid');

	swal({
	    title: "ACHTUNG!",
	    text: "" + msg + "",
	    html: true
	},
		function () {
		    $('html, body').animate({
			scrollTop: $("#" + id).offset().top - 150
		    }, {complete: function () {
//                        $("#shake-" + id).effect('highlight', 500);
			}}, 1000);
		    return false;
		});
    };







    var waitingDialog = waitingDialog || (function ($) {
	'use strict';

	// Creating modal dialog's DOM
	var $dialog = $(
		'<div class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-hidden="true" style="padding-top:15%; overflow-y:visible;">' +
		'<div class="modal-dialog modal-m">' +
		'<div class="modal-content">' +
		'<div class="modal-header"><h3 style="margin:0;"></h3></div>' +
		'<div class="modal-body">' +
		'<div class="progress progress-striped active" style="margin-bottom:0;"><div class="progress-bar" style="width: 100%"></div></div>' +
		'</div>' +
		'</div></div></div>');

	return {
	    /**
	     * Opens our dialog
	     * @param message Custom message
	     * @param options Custom options:
	     * 				  options.dialogSize - bootstrap postfix for dialog size, e.g. "sm", "m";
	     * 				  options.progressType - bootstrap postfix for progress bar type, e.g. "success", "warning".
	     */
	    show: function (message, options) {
		// Assigning defaults
		if (typeof options === 'undefined') {
		    options = {};
		}
		if (typeof message === 'undefined') {
		    message = 'Loading';
		}
		var settings = $.extend({
		    dialogSize: 'm',
		    progressType: '',
		    onHide: null // This callback runs after the dialog was hidden
		}, options);

		// Configuring dialog
		$dialog.find('.modal-dialog').attr('class', 'modal-dialog').addClass('modal-' + settings.dialogSize);
		$dialog.find('.progress-bar').attr('class', 'progress-bar');
		if (settings.progressType) {
		    $dialog.find('.progress-bar').addClass('progress-bar-' + settings.progressType);
		}
		$dialog.find('h3').text(message);
		// Adding callbacks
		if (typeof settings.onHide === 'function') {
		    $dialog.off('hidden.bs.modal').on('hidden.bs.modal', function (e) {
			settings.onHide.call($dialog);
		    });
		}
		// Opening dialog
		$dialog.modal();
	    },
	    /**
	     * Closes dialog
	     */
	    hide: function () {
		$dialog.modal('hide');
	    }
	};

    })(jQuery);











});



