﻿$(document).ready(function() {

    //mask phone inputs
    $(".phonemask").mask("999-999-9999", { completed: function() { alert(this.val()); } });

    //only allow numbers
    $(".numbersonly").keypress(function(e) {
        if ((e.which >= 48 && e.which <= 57) || e.which == 8 || e.which == 0) {
            return true;
        }
        return false;
    });

    //hook up form submit event
    $("form").submit(function() {
        if (ValidateForm()) {
            return true;
        }
        return false;
    });

    //hook up clear button
    $("#ClearBtn").click(function(e) {
        e.preventDefault();
        $("fieldset>input, textarea").val("");
        $("select option:selected").removeAttr("selected");
    });
});

function ValidateForm() {
    var IsValid = true;
    $("input.req").each(function() {
        if (this.value == "") {
            $(this).addClass("error");
            IsValid = false;
        }
        else {
            if ($(this).hasClass("email")) {
                if (!IsValidEmail(this.value)) {
                    $(this).addClass("error");
                    IsValid = false;
                }
                else {
                    $(this).removeClass("error");
                }
            }
            else {
                $(this).removeClass("error");
            }
        }
    });

    $("select.req option:selected").each(function() {
        if (this.value == "") {
            $(this).parent().addClass("error");
            IsValid = false;
        }
        else {
            $(this).parent().removeClass("error");
        }
    });
    

    return IsValid;
}