﻿String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
String.prototype.isValidEmail = function() {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(this) == false) {
        return false;
    }
    return true;
}

jQuery(document).ready(function($) {
    $('a[rel*=facebox]').facebox({
        closeImage: 'javascript/facebox/closelabel.gif',
        loadingImage: 'javascript/facebox/loading.gif'
    });

    $(document).bind('reveal.facebox', function() {
        var message = $('#facebox #error-message:first');
        var m = $('#facebox #message:first');
        $('#facebox').css({ top: DownloadBrochurePopupConfig.Top, width: '400', left: '400' });
        $('#facebox input:text:first:visible').focus();
        $('#facebox input:button').bind('click', function(e) {
			$('#facebox input:text').keyup();
            var errors = $('#facebox .x-error');
            if (errors.length > 0) {
                errors.get(0).focus();
                return;
            }
            $.post('download-brochure.asp', {
                'firstname': $('#facebox #download-brochure-firstname:first').attr('value'),
                'lastname': $('#facebox #download-brochure-lastname:first').attr('value'),
                'phone': $('#facebox #download-brochure-phone:first').attr('value'),
                'email': $('#facebox #download-brochure-email:first').attr('value'),
                'download-brochure-submit': "true"
            }, function(data) {
                if (data.status) {
                    // replace the text with the download link
                    window.location = data.link;
                } else {
                    // display the necessary errors
                    $.each(data.errors, function(idx, o) {
                        var name = "#facebox #download-brochure-" + o.name;
                        $(name).addClass('x-error');
                    });
                    $("#facebox input.x-error:first").focus();
                }
            }, "json");
        });

        $('#facebox input:text').bind('keyup', function(e) {
            var o = $(this);
            if (o.attr('id') == 'download-brochure-email') return;
            var value = o.attr('value');
            if (value.trim() == "") {
                message.html("* Required field");
                message.removeClass('hidden');
                m.addClass('hidden');
                o.addClass('x-error');
            } else {
                message.addClass('hidden');
                o.removeClass('x-error');
                m.removeClass('hidden');
            }
        });

        $('#facebox #download-brochure-email:first').bind('keyup', function(e) {
            var o = $(this);
            var value = o.attr('value').trim();
            if (value == "") {
                message.html("* Required field");
                message.removeClass('hidden');
                m.addClass('hidden');
                o.addClass('x-error');
            } else if (!value.isValidEmail()) {
                message.html("* Invalid email address");
                message.removeClass('hidden');
                m.addClass('hidden');
                o.addClass('x-error');
            } else {
                message.addClass('hidden');
                m.removeClass('hidden');
                o.removeClass('x-error');
            }
        });
    });
});
