﻿$(function() {
    $('.wlbubbleInfo').each(function() {
        var name = $("#csName"),
			email = $("#csEmail"),
			query = $("#csQuery"),
			listemail = $("#listemail")
        allFields = $([]).add(name).add(email).add(query).add(listemail),
			tips = $("#validateTips");

        var emailTrigger = $('#ListingEmailCS', this);
        var csPopupForm = $('#csPopupForm', 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;
            }
        }

        $('#csPopupForm').dialog({
            bgiframe: false,
            autoOpen: false,
            height: 470,
            width: 470,
            modal: true,
            resizable: false,
            buttons: {
                'Send Email': function() {
                    var bValid = true;
                    allFields.removeClass('ui-state-error');

                    bValid = bValid && checkLength(name, "csName", 1, 200);
                    bValid = bValid && checkLength(email, "csEmail", 1, 80);
                    bValid = bValid && checkLength(query, "csQuery", 1, 800);

                    //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. yourname@somedomain.com");

                    if (bValid) {
                        $('#users tbody').append('<tr>' +
							'<td>' + name.val() + '</td>' +
							'<td>' + email.val() + '</td>' +
							'<td>' + query.val() + '</td>' +
							'</tr>');
                        /*
                        var _send_button = $('.ui-dialog-buttonpane button:contains(Send)');
                        var original_text = _send_button.text();
                        _send_button.text('Please wait...');
                        _send_button.addClass('ui-state-disabled');
                        _send_button.attr('disabled', 'disabled');
                        _send_button.fadeTo(500, 0.2);
                        */
                        var f = $("#CommentsSuggestions");
                        var action = f.attr("action");
                        var serializedForm = f.serialize();

                        $.post(action, serializedForm, postSent);

                        //$(this).dialog('close');
                    }
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            },
            close: function() {
                allFields.val('').removeClass('ui-state-error');
            }
        });


        // Fire the popup form
        $(emailTrigger)
		.click(function() {
		    $('#csPopupForm').dialog('open');
		    emailto = $(this).attr('title');
		});

        function postSent(data) {
            $("#formOL").slideUp("normal", postSentMessage);
            //tips.text("<h3>Thank You!</h3><p>A customer representative will get in touch with you shortly.</p>").effect("highlight", {}, 1500);
        }

        function postSentMessage() {
            $("#formOL").before('<h3>Thank You!</h3><p>A customer representative will get in touch with you shortly.</p>');
        }

    });
});

function cs() {
    //$('#csPopupForm').dialog('open');
};
