﻿$(function() {

    var youremail = $("#youremail"),
		friendsemail = $("#friendsemail"),
		allFields = $([]).add(youremail).add(friendsemail),
		tips = $("#validateTips");

    function updateTips(t) {
        tips.text(t);
    }

    function checkLength(o, n, min, max) {

        if (o.val().length > max || o.val().length < min) {
            o.addClass('ui-state-error');
            updateTips("Length of " + n + " must be between " + min + " and " + max + " characters.");
            return false;
        } else {
            return true;
        }

    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val().split(' ').join('')))) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#dialog").dialog({
        open: function(event, ui){ $('.flash').css("display", "none"); },
        bgiframe: true,
        autoOpen: false,
        height: 177,
        width: 372,
        modal: true,
        resizable: false,
        buttons: {
            'Send to a friend': function() {
                // send HBX tag on opening of the dialog
                _hbLink("SendToAFriend-Send");

                var bValid = true;
                allFields.removeClass('ui-state-error');

                // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
                bValid = bValid && checkRegexp(youremail, /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/, "Please enter a valid email address in the format name@domain.com.");
                bValid = bValid && checkRegexp(friendsemail, /^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([,.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/, "Please enter one more more valid email addresses in the format name@domain.com.");

                if (bValid) {
                    var data = { youremail: youremail.val(), friendsemail: friendsemail.val(), location: location.href }
                    // $.blockUI({ message: "<h1>Sending Email. Please Wait...</h1>" });
                    $.blockUI({ message: null });

                    $.post("/SendToFriend.aspx",
                            data,
                            function(responseData) {
                                // $.unblockUI();
                            },
                            "json");
                    var t = setTimeout("$.unblockUI()", 1000);
                    $(this).dialog('close')
                }
            }
            /*
            ,
            Cancel: function() {
            $(this).dialog('close');
            }*/
        },
        close: function() {
            allFields.val('').removeClass('ui-state-error');
            $('.flash').css("display", "block");
        }
    });

    // main nav send to a friend
    $('#send-to-friend, #article-send-to-friend').click(function(e) {
        e.preventDefault();
        _hbLink("SendToAFriend-Open"); // send HBX tag
        $('#dialog').dialog('open');
    })
});
