// Load
$(function() {
    if (!$.browser.safari) {
        $("div.column.sidebar").corner();
    }

    // <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");
    }
});

// Dynamic form rows
function addRow(ele) {
    var clone = $(ele).parents("dd").clone();
    $(":input", clone).val('');
    if ($("img.delete_row", clone).length < 1) {
        var add = $("img.add_row", clone);
        add.after('<img src="images/delete.png" width="16" height="16" alt="Remove" title="Remove Child" class="delete_row" onclick="deleteRow(this)" \/>');
        add.remove();
    }
    $(ele).parents("dd").after(clone);
    return clone;
}
function deleteRow(ele) {
    $(ele).parents("dd").remove();
}
function setNames() {
    $("dd.child").each(function(i) {
        $(":input", this).each(function(j) {
            if (!$(this).attr("oldname")) {
                $(this).attr("oldname", this.name);
            }
            $(this).attr("name", function() { return $(this).attr("oldname") + i; });
        });
    });
}

// Files
function addFile(ele) {
    var clone = $(ele).parents("tr.document").clone();
    if ($("a.delete_row", clone).length < 1) {
        var add = $("img.add_row", clone);
        add.after('<img src="images/delete.png" width="16" height="16" alt="Remove" title="Remove File" class="delete_row" onclick="deleteFile(this)" \/>');
        add.remove();
    }
    $(ele).parents("tr.document").after(clone);
    return clone;
}
function deleteFile(ele) {
    $(ele).parents("tr.document").remove();
}
function setFiles() {
    $("tr.document").each(function(i) {
        $(":input", this).attr('name', function() { return  this.name + i; } );
    });
}

// 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.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;
}