// ONLOAD

$(function() {
    // <IE7 Navigation
    if ($.browser.msie && $.browser.version < 7) {
        // Emulate menu hover
        $("#navigation li").mouseover(function() {
            $(this).addClass("hover");
        });
        $("#navigation").mouseout(function() {
            $("li", this).removeClass("hover");
        });
        // Style required labels
        $("span.required + label").addClass("required");
    }
    $("table.sortable").tablesorter();
    
    // Send to a friend
    $("#send_to_friend").insertBefore("#main");
    $("#send_to_friend_link").click(function() {
        $("#send_to_friend").toggle();
    });
});

// FUNCTIONS

function calWin(f, e) {
    dateField   = document.forms[f].elements[e];
    dateType    = 'date';
    window.open('admin/calendar.html', 'calendar', 'width=200, height=175, resizable=yes, status=no');
}
function resetAndHide(ele) {
    $("dd.mandatory").remove();
    $(ele).parents("form").css("display", "none");
}
function validate(form) {
    var valid = true;
    $("dd.mandatory").remove();
    $("#" + form + " :input.required").each(function() {
        if ($(this).attr("type") == "radio") {
            var name    = $(this).attr("name");
            var checked = $(":input[@name="+ name +"]:checked");
            if (checked.length < 1) {
                if (valid) {
                    // Give focus to the first invalid item
                    $(this).focus();
                }
                $(this).parents("dd").after("<dd class='mandatory'>"+ lang['required'] +"</dd>");
                valid = false;
            }
        } else {
            if ($(this).val() === "") {
                if (valid) {
                    // Give focus to the first invalid item
                    $(this).focus();
                }
                $(this).parents("dd").after("<dd class='mandatory'>"+ lang['required'] +"</dd>");
                valid = false;
            }
        }
    });
    $("#" + form + " :input.email").each(function() {
        if ($(this).val() !== "") {
            var isEmail = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[_a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/i.test($(this).val());
            if (!isEmail) {
                if (valid) {
                    // Give focus to the first invalid item
                    $(this).focus();
                }
                $(this).parents("dd").after("<dd class='mandatory'>"+ lang['valid_email'] +"</dd>");
                valid = false;
            }
        }
    });
    var i    = 0;
    var last = null;
    $("#" + form + " :input.match").each(function() {
        var val = $(this).val();
        if (i > 0 && val != last) {
            if (valid) {
                // Give focus to the first invalid item
                $(this).focus();
            }
            $(this).parents("dd").after("<dd class='mandatory'>"+ lang['do_not_match'] +"</dd>");
            valid = false;
        }
        last = val;
        i++;
    });
    $("dd.mandatory+dd.mandatory").remove();
    return valid;
}
