﻿$(document).ready(function () {
    $("a.uc_alerts_button").bind('click', function () {
        if ($("#uc_alerts_form").is(":hidden")) {
            $("#uc_alerts_form").slideDown("fast");
            $("a#ucalerts_button").unbind('click.fb');
        } else {
            $("#uc_alerts_form").slideUp('fast');
        }
    });

    //cancel enter key on input fields
    $("#uc_alerts_form :input").keypress(function (event) { return event.keyCode == 13 ? false : true; });

    //handle click on register button
    $("#btnAlertsReg").click(function () {
        if ($("#uc_alerts_password").val() != $("#uc_alerts_conf_password").val()) {
            alert("Passwords do not match.");
            return false;
        }
        var freqChecked = 1;
        if ($("input:radio[@name=uc_alerts_frequency]:checked").val()) //if radio is checked
            freqChecked = $("input:radio[@name=uc_alerts_frequency]:checked").val();
        var inputData = { email: $('#uc_alerts_email').val(), firstName: $('#uc_alerts_first_name').val(), lastName: $('#uc_alerts_last_name').val(), company: $('#uc_alerts_company').val(), password: $('#uc_alerts_password').val(), group: $('#uc_alerts_group').val(), frequency: freqChecked, companywebsite: "", lineofbusiness: "", jobtitle: "" };
        var dataString = JSON.stringify(inputData);
        $("#btnAlertsReg").hide();
        $("#imgAlertsProgress").show();
        $.ajax({
            type: "POST",
            url: "/webservices/UserRegistration.asmx/RegisterUser",
            data: dataString,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.hasOwnProperty('d'))
                    msg = msg.d;

                if (msg.valid) {
                    //hide progress indicator
                    $("#imgAlertsProgress").hide();
                    //show thankyou message
                    alert("Thank you for signing up!");
                    //hide form
                    $("#uc_alerts_button .form_wrapper").slideUp('fast');
                    //redirect w/ QS value so GA knows we have registered
                    if (document.location.search.substring(1)) //has querystring
                        document.location = document.location + "&sbreg=1";
                    else
                        document.location = document.location + "?sbreg=1";
                    //document.location.reload(true);
                }
                else {
                    //hide progress indicator
                    $("#imgAlertsProgress").hide();
                    //show button for re-submitting
                    $("#btnAlertsReg").show();
                    //show error message
                    alert(msg.message);
                }

            },
            error: function (xhr, desc, exceptionobj) {
                //alert(xhr.responseText);
                //hide progress indicator
                $("#imgAlertsProgress").hide();
                //show button for re-submitting
                $("#btnAlertsReg").show();
                //show error message
                alert("Sorry, we were unable to process your registration request.");
            }
        });
    });
});
