﻿$(document).ready(function() {
    $(".request-catalog").click(function(e) {
        $("#Comments").html($(this).attr("rel"));
        e.preventDefault();
        $("#overlay").fadeIn(200);
        $("#modalform").fadeIn(200);
        if ($("body").height() > $(document).height()) {
            $("#overlay").height($("body").height() + 50);
        }
        else {
            $("#overlay").height($(document).height());
        }
    });

    $("#overlay").click(function() {
        $(".modal").fadeOut(200);
        $("#overlay").fadeOut(200);
    });

    $("#ClearBtn").click(function(e) {
        e.preventDefault();
        $("fieldset>input, textarea").val("");
        $("select option:selected").removeAttr("selected");
    });
    
    $("#SubmitBtn").click(function(e) {
        e.preventDefault();
        if (ValidateForm()) {

            //submit to web service
            if (XHR != null) {
                XHR.abort();
            }
            XHR = $.ajax({
                type: "POST",
                url: "/SSTWebService.asmx/InsertRequestCatalogFormSubmission",
                data: "{'FName' : '" + $("#FName").val() + "', " +
                       "'Name' : '" + $("#Name").val() + "', " +
                       "'FirstName' : '" + $("#FirstName").val() + "', " +
                       "'LastName' : '" + $("#LastName").val() + "', " +
                       "'Title' : '" + $("#Title").val() + "', " +
                       "'Company' : '" + $("#Company").val() + "', " +
                       "'Address' : '" + $("#Address").val() + "', " +
                       "'City' : '" + $("#City").val() + "', " +
                       "'State' : '" + $("#State option:selected").val() + "', " +
                       "'Zip' : '" + $("#Zip").val() + "', " +
                       "'Email' : '" + $("#Email").val() + "', " +
                       "'Telephone' : '" + $("#Telephone").val() + "', " +
                       "'Fax' : '" + $("#Fax").val() + "', " +
                       "'Comments' : '" + $("#Comments").val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(result) {
                    $(".modal").fadeOut(200);
                    $("#overlay").fadeOut(200);
                }
            });
        }
    });

    $(".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;
    });
});
$(window).resize(function() {
    if ($("body").height() > $(document).height()) {
        $("#overlay").height($("body").height());
    }
    else {
        $("#overlay").height($(document).height());
    }
});

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;
}

