var WebinarTitle = "";
$(document).ready(function() {
    $("#ajax-container").load("/modal-content/webinarform.html", function() {
        //mask phone inputs
        $.getScript("/assets/js/jquery.maskedinput-1.2.2.min.js", function() {
            $(".phonemask").mask("999-999-9999", { completed: function() { } });
        });

        $(".webinarmodal").click(function(e) {
            WebinarTitle = $(this).attr("rel");
            $("#SubmitBtn").show();
            $("#PopupButton").hide();
            e.preventDefault();
            OpenWebinarModal();
        });

        $(".modalclose").click(function(e) {
            e.preventDefault();
            $(".modal").fadeOut(200);
            $("#overlay").fadeOut(200, function() {
                $("#webinar-regform").show();
                $("#forgot-password-form").hide();
                $("#ForgotPasswordMsg").html("");
            });
            
        });

        //only allow numbers
        $(".numbersonly").keypress(function(e) {
            if ((e.which >= 48 && e.which <= 57) || e.which == 8 || e.which == 0) {
                return true;
            }
            return false;
        });

        $("#SubmitBtn").click(function(e) {
            e.preventDefault();
            if ($("#HasAccountYes").attr("checked") != "") {
                if ($("#Username").val() != "" && $("#Password").val() != "") {
                    SubmitForm();
                }
            } else {
                if (ValidateForm()) {
                    SubmitForm();
                }
            }
        });

        $("#ForgotSubmitBtn").click(function(e) {
            e.preventDefault();
            if ($("#txtForgotUsername").val() != "") {
                SubmitForgotPasswordForm();
            }
        });

        $("#HasAccountNo").click(function(e) {
            $("#WebinarRegInfo").slideDown(200);
        });

        $("#HasAccountYes").click(function(e) {
            $("#WebinarRegInfo").slideUp(200);
        });

        $("#ForgotPasswordBtn").click(function(e) {
            $("#webinar-regform").slideUp(200, function() {
                $("#forgot-password-form").slideDown(200);
            });
        });

        $("#ForgotBackBtn").click(function(e) {
        $("#forgot-password-form").slideUp(200, function() {
            $("#webinar-regform").slideDown(200);
            $("#ForgotPasswordMsg").html("");
            });
        });
    });
});

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;
}

function SubmitForm() {
    $("#SubmitBtn").fadeTo(200, 0.5);
    $("#smAjaxLoader").show();
    //submit to web service
    if (XHR != null) {
        XHR.abort();
    }
    XHR = $.ajax({
        type: "POST",
        url: "/SSTWebService.asmx/InsertWebinarRegistration",
        data: "{'FName' : '" + $("#FName").val() + "', " +
                       "'Name' : '" + $("#Name").val() + "', " +
                       "'HasAccount' : '" + $("#HasAccountYes").attr("checked") + "', " +
                       "'Username' : '" + $("#Username").val() + "', " +
                       "'Password' : '" + $("#Password").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() + "', " +
                       "'USCitizen' : '" + $("#USCitizen option:selected").val() + "', " +
                       "'WebinarTitle' : '" + WebinarTitle + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            if (result.d == "Error") {
            }
            else {
                $("#SubmitBtn").fadeTo(200, 1);
                if (result.d == "") {
                    $("#SubmitBtn").hide();
                    //$("#PopupButton").attr("onclick", "window.open('/WebinarRedirect.aspx?WebinarTitle=" + WebinarTitle + "');").fadeIn();
					                    $("#PopupButton").click(function(){
					                    window.open("/WebinarRedirect.aspx?WebinarTitle=" + WebinarTitle);
                    }).fadeIn();
                }
                else {
                    alert(result.d);
                }

            }

        }
    });
}

function SubmitForgotPasswordForm() {
    $("#ForgotPasswordBtn").fadeTo(200, 0.5);
    //submit to web service
    if (XHR != null) {
        XHR.abort();
    }
    XHR = $.ajax({
        type: "POST",
        url: "/SSTWebService.asmx/ForgotWebinarPassword",
        data: "{'FName' : '" + $("#FName").val() + "', " +
                       "'Name' : '" + $("#Name").val() + "', " +
                       "'Username' : '" + $("#txtForgotUsername").val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            if (result.d == "Error") {
            }
            else {
                if (result.d == "") {
                    $("#ForgotPasswordMsg").html("Email has been sent.");
                }
                else {
                    $("#ForgotPasswordMsg").html(result.d);
                }
            }
        }
    });
}
