function popWin(mypage, myoptions) {
    var w = (myoptions) ? myoptions.width : 800;
    var h = (myoptions) ? myoptions.height : 800;

    var myname = "newWindow";
    var LeftPosition    = (screen.width) ? (screen.width-w) / 2 : 100;
    var TopPosition     = (screen.height) ? (screen.height-h) / 2 : 100;
    var settings        = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
    var win = window.open(mypage, myname, settings);
    win.focus();
}

// 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;
}