var banners;

$(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");
    }

    // Alternate row colours
    $("table.enhancedtable tr:even").addClass("even");
});

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").hide();
}

// Form validation
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'>This is a required field</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'>This is a required field</dd>");
                valid = false;
            }
        }
    });
    $("#" + form + " :input.elserequired").each(function() {
        var elserequired = false;
        var dd = $(this).parents("dd");
        $(":input.elserequired", dd).each(function() {
            elserequired = elserequired || validateRequired(this);
        });
        if (!elserequired) {
            if (valid) {
                // Give focus to the first invalid item
                $(this).focus();
            }
            $(this).parents("dd").after("<dd class='mandatory'>This is a required field</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'>Please input a valid e-mail address</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'>Values entered do not match</dd>");
            valid = false;
        }
        last = val;
        i++;
    });
    $("dd.mandatory+dd.mandatory").remove();
    return valid;
}
