﻿$(document).ready(function() {

    var validator = $("#orderForm").validate({

        debug: true,
        rules: {
            tbFName: "required",
            tbLName: "required",
            tbEmail: {
                required: true,
                email: true,
                remote: {
                    url: "IsValidEmailAddress.aspx",
                    type: "POST"
                }
            },
            tbZipCode: {
                required: true,
                digits: true,
                rangelength: [5, 5]
            },
            tbDOB_Month: {
                required: true,
                min: 1,
                max: 12
            },
            tbDOB_Day: {
                required: true,
                min: 1,
                max: 31
            },
            tbDOB_Year: {
                required: true,
                min: 1900,
                max: 2010
            }
        },
        messages: {
            tbFName: {
                required: "Required"
            },
            tbLName: {
                required: "Required"
            },
            tbEmail: {
                required: "Required",
                email: "Required",
                remote: "Email already used"
            },
            tbZipCode: {
                required: "Required",
                digits: "Five digit zip code required",
                rangelength: "Five digit zip code required"
            },
            tbDOB_Month: {
                required: "Month",
                min: "Invalid",
                max: "Invalid"
            },
            tbDOB_Day: {
                required: "Day",
                min: "Invalid",
                max: "Invalid"
            },
            tbDOB_Year: {
                required: "Year",
                min: "Invalid",
                max: "Invalid"
            }
        },
        errorPlacement: function(error, element) {
            
            // The status divs for date of birth are not placed
            // next to the element being validated.
            // First check if element is the month, day, or year
            // input. If it is, append the error to the correct div.
            if (element.is("input#tbDOB_Month"))
            {
                error.appendTo("div#DobInputBox div.statusBox_DobMonth");
            }
            else if (element.is("input#tbDOB_Day"))
            {
                error.appendTo("div#DobInputBox div.statusBox_DobDay");
            }
            else if (element.is("input#tbDOB_Year"))
            {
                error.appendTo("div#DobInputBox div.statusBox_DobYear");
            }
            else
            {
                // all other status divs are next to the
                // element being validated
                error.appendTo(element.next());
            }
        },
        submitHandler: function(form) {
            // disable elements of type submit
            $(":submit").disabled = true;
            form.submit();
        }
    });
});
