﻿var linktosend;

$(function() {
    $('.wlbubbleInfo').each(function() {
        var yfname = $("#yfname"),
			yfemail = $("#yfemail"),
			linkquery = $("#linkquery"),
			yname = $("#yname"),
			yemail = $("#yemail")
        allFields = $([]).add(yfname).add(yfemail).add(linkquery).add(yname).add(yemail),
			tips = $("#slvalidateTips"),
			ltos = $("#LinkToSend");

        var sharelinkTrigger = $('#ListingShare', this);
        var sharelinkPopupForm = $('#sharelinkPopupForm', this);

        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;
            }
        }

        $('#sharelinkPopupForm').dialog({
            bgiframe: false,
            autoOpen: false,
            height: 500,
            width: 470,
            modal: true,
            resizable: false,
            buttons: {
                'Share Link': function() {
                    var bValid = true;
                    allFields.removeClass('ui-state-error');

                    bValid = bValid && checkLength(yname, "Your Name", 1, 200);
                    bValid = bValid && checkLength(yemail, "Your Email", 1, 80);
                    bValid = bValid && checkLength(yfname, "Your Friends Name", 1, 200);
                    bValid = bValid && checkLength(yfemail, "Your Friends Email", 1, 80);
                    bValid = bValid && checkLength(linkquery, "The Message", 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(yfemail, /^((([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. friendsname@somedomain.com");

                    if (bValid) {
                        $('#users tbody').append('<tr>' +
							'<td>' + yfname.val() + '</td>' +
							'<td>' + yfemail.val() + '</td>' +
							'<td>' + linkquery.val() + '</td>' +
							'</tr>');
                        var slf = $("#sendShareLink");
                        var slaction = slf.attr("action");
                        var serializedSLForm = slf.serialize();

                        var ls = '&linktosend=' + linktosend;

                        var slForm = serializedSLForm + ls;

                        $.post(slaction, slForm, postSent);

                        //tips.text("The shared link email has been sent").effect("highlight", {}, 1500);

                        //$(this).dialog('close');
                    }
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            },
            close: function() {
                allFields.val('').removeClass('ui-state-error');
            }
        });


        // Fire the popup form
        $(sharelinkTrigger)
		.click(function() {
		    $('#sharelinkPopupForm').dialog('open');
		});

        function postSent(data) {
            //$("#formOL").slideUp("normal", postSentMessage);
            tips.text("The shared link email has been sent").effect("highlight", {}, 1500);
            $('#sharelinkPopupForm').dialog('close');
        }

        function postSentMessage() {
            //$("#formOL").before('<h3>Thank You!</h3><p>A customer representative will get in touch with you shortly.</p>');
            tips.text("The shared link email has been sent").effect("highlight", {}, 1500);
        }

    });
});

function sl(linktos) {
    linktosend = linktos;
    $("#LinkToSend").text(linktosend).effect("highlight", {}, 150000);
};
