﻿$(function() {
    $('.wlbubbleInfo').each(function() {
        var name = $("#name"),
			email = $("#email"),
			address = $("#address")
        allFields = $([]).add(name).add(email).add(address),
			tips = $("#validateTips");

        var emailTrigger = $('#ListingEmailSales', this);
        var salesPopupForm = $('#salesPopupForm', this);
        var emailto;

        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("Your " + n + " must not be left blank.");
                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;
            }
        }

        $('#salesPopupForm').dialog({
            bgiframe: false,
            autoOpen: false,
            height: 850,
            width: 700,
            modal: true,
            resizable: false,
            buttons: {
                'Send Email': function() {
                    var bValid = true;
                    allFields.removeClass('ui-state-error');

                    bValid = bValid && checkLength(name, "name", 1, 200);
                    bValid = bValid && checkLength(email, "email", 1, 80);
                    bValid = bValid && checkLength(address, "address", 1, 800);

                    // 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. yourname@somedomain.com");

                    if (bValid) {
                        $('#users tbody').append('<tr>' +
							'<td>' + name.val() + '</td>' +
							'<td>' + email.val() + '</td>' +
							'<td>' + address.val() + '</td>' +
							'</tr>');
                        var f = $("#Sales");
                        var action = f.attr("action");
                        var serializedForm = f.serialize();

                        $.post(action, serializedForm, postSent);
                    }
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            },
            close: function() {
                allFields.val('').removeClass('ui-state-error');
            }
        });


        // Fire the popup form
        $(emailTrigger)
		.click(function() {
		    $('#salesPopupForm').dialog('open');
		    emailto = $(this).attr('title');
		});

        function postSent(data) {
            $("#formSales").slideUp("normal", postSentMessage);
        }

        function postSentMessage() {
            $("#formSales").before('<h3>Thank You!</h3><p>A customer representative will get in touch with you shortly.</p>');
        }

    });
});

function sales() {
    //$('#salesPopupForm').dialog('open');
};
