﻿
//PAGE SPECIFIC for ~/signup.aspx

var FormCount;

$(document).ready(function()
    {
        //setup events
        $("#Buttons input.Previous").click(PreviousButton_OnClick);
        $("#Buttons input.Next").click(NextButton_OnClick);
        $("#ReviewArea input.Finish").click(FinishButton_OnClick);
        $("#aEditGeneralInfo").click(aEditGeneralInfo_OnClick);
        $("#aEditSecurity").click(aEditSecurity_OnClick);
        $("#aEditBilling").click(aEditBilling_OnClick);
        
        $(ddlSecurityQuestion).change(ddlSecurityQuestion_OnChange);

        FormCount = $("#SignupForms").children().length;

        //focus
        if (SetFocusControl != null && SetFocusControl.focus)
        {
            SetFocus(SetFocusControl);
        }
        else
        {
            SetDefaultFocus();
        }
    }
);

function ddlSecurityQuestion_OnChange()
{
    if (ddlSecurityQuestion.options[ddlSecurityQuestion.selectedIndex].value != -1)
    {
        $("#SecurityQuestion").css("display", "");
    }
    else
    {
        $("#SecurityQuestion").css("display", "block");
        txtSecurityQuestion.focus();
    }
}
function FinishButton_OnClick()
{
    $("#ReviewArea input.Finish").val("Please wait...");
    $("#ReviewArea input.Finish").attr("disabled", "disabled");
    lnkSubmit_Click();
}
function aEditGeneralInfo_OnClick()
{
    TransitionTo(0);
}
function aEditSecurity_OnClick()
{
    TransitionTo(1);
}
function aEditBilling_OnClick()
{
    TransitionTo(2);
}
function Validates()
{
    switch (CurrentFormIndex)
    {
        case 0: //general info
            return ValidatesGeneralInfoForm();
            break;
        case 1: //remember
            return ValidatesRememberForm();
            break;
        case 2: //billing
            return ValidatesBillingForm();
            break;
    }

    return true;
}
function ValidatesGeneralInfoForm()
{
    ErrorClear(txtFirstName);
    ErrorClear(txtLastName);
    ErrorClear(txtEmailAddress);
    ErrorClear(txtEmailAddressConfirm);
    ErrorClear(txtUserName);
    ErrorClear(txtPassword);
    ErrorClear(txtPasswordConfirm);

    if (txtFirstName.value.length == 0)
    {
        ErrorSet(txtFirstName, "Please enter your First Name.");
        return false;
    }

    if (txtLastName.value.length == 0)
    {
        ErrorSet(txtLastName, "Please enter your Last Name.");
        return false;
    }

    if (txtEmailAddress.value.length == 0)
    {
        ErrorSet(txtEmailAddress, "Please enter your Email Address.");
        return false;
    }
    else
    {
        if (!IsValidTextEmailAddress(txtEmailAddress.value))
        {
            ErrorSet(txtEmailAddress, "This is not a valid Email Address.  Please correct it.");
            return false;
        }
    }

    if (txtEmailAddressConfirm.value.length == 0)
    {
        ErrorSet(txtEmailAddressConfirm, "Please re-enter the exact same Email Address you entered above.");
        return false;
    }
    else
    {
        if (txtEmailAddress.value != txtEmailAddressConfirm.value)
        {
            ErrorSet(txtEmailAddressConfirm, "Both Email Addresses must be the same.");
            return false;
        }
        else if (ExistsEmailAddress(txtEmailAddress.value))
        {
			ErrorSet(txtEmailAddress, "Another user is already using this Email Address.  Please enter a different one.");
            return false;
        }
    }

    if (txtUserName.value.length == 0)
    {
        ErrorSet(txtUserName, "Please select the User Name you'd like to use. (4-32 characters long)");
        return false;
    }
    else
    {
        if (txtUserName.value.length < 4)
        {
            ErrorSet(txtUserName, "The User Name you selected is too small.  It must be at least 4 characters long.");
            return false;
        }
        else if (ExistsUserName(txtUserName.value))
        {
			ErrorSet(txtUserName, "Another user already registered this User Name.  Please enter a different one.");
            return false;
        }
    }

    if (txtPassword.value.length == 0)
    {
        ErrorSet(txtPassword, "Please select a Password. (8-32 characters long)");
        return false;
    }
    else
    {
        if (txtPassword.value.length < 8)
        {
            ErrorSet(txtPassword, "The Password you entered is too small.  It must be at least 8 characters long.");
            return false;
        }
    }

    if (txtPasswordConfirm.value.length == 0)
    {
        ErrorSet(txtPasswordConfirm, "Please re-enter the exact same Password you entered above.");
        return false;
    }
    else
    {
        if (txtPassword.value != txtPasswordConfirm.value)
        {
            ErrorSet(txtPasswordConfirm, "Both Passwords must be the same.");
            return false;
        }
    }

    return true;
}
function ValidatesRememberForm()
{
    ErrorClear(txtPasswordHint);
    ErrorClear(ddlSecurityQuestion);
    ErrorClear(txtSecurityQuestion);
    ErrorClear(txtSecurityAnswer);
    ErrorClear(txtLastFourOfSsn);

    if (txtPasswordHint.value.length == 0)
    {
        ErrorSet(txtPasswordHint, "Please enter a Password Hint.");
        return false;
    }

    if (ddlSecurityQuestion.options[ddlSecurityQuestion.selectedIndex].value == 0)
    {
        ErrorSet(ddlSecurityQuestion, "Please select a Security Question.");
        return false;
    }
    else
    {
        if (ddlSecurityQuestion.options[ddlSecurityQuestion.selectedIndex].value == -1 &&
            txtSecurityQuestion.value.length == 0)
        {
            ErrorSet(txtSecurityQuestion, "Please select a custom Security Question or select one from the drop-down list.");
            return false;
        }
    }

    if (txtSecurityAnswer.value.length == 0)
    {
        ErrorSet(txtSecurityAnswer, "Please enter an Answer for the Security Question.");
        return false;
    }

    if (txtLastFourOfSsn.value.length != 4)
    {
        ErrorSet(txtLastFourOfSsn, "Please enter the last four digits of your Social Security Number.");
        return false;
    }
    else
    {
        if (!IsValidTextNumeric(txtLastFourOfSsn.value))
        {
            ErrorSet(txtLastFourOfSsn, "The last four digits of your Social Security Number should all be numeric.");
            return false;
        }
    }

    return true;
}
function ValidatesBillingForm()
{
    ErrorClear(txtCcNumber);
    ErrorClear(txtCcName);
    ErrorClear(ddlCcExpMonth);
    ErrorClear(ddlCcExpYear);
    ErrorClear(txtCcCvv);
    ErrorClear(txtBillingStreet1);
    ErrorClear(txtBillingStreet2);
    ErrorClear(txtBillingCity);
    ErrorClear(ddlBillingStateId);
    ErrorClear(txtBillingZipCode);
    ErrorClear(txtBillingPhoneNumber);
    ErrorClear(txtBillingFaxNumber);

    if (!ValidatePlanSelection())
    {
        return false;
    }

    $("#WeAccept").removeAttr("class");

    if (txtCcNumber.value.length == 0)
    {
        ErrorSet(txtCcNumber, "Please enter your Credit Card Number.");
        $("#WeAccept").attr("class", "Hidden");
        return false;
    }
    else
    {
        if (!IsValidTextCreditCardNumber(txtCcNumber.value))
        {
            ErrorSet(txtCcNumber, "The Credit Card Number you entered is not valid.");
            $("#WeAccept").attr("class", "Hidden");
            return false;
        }
    }
    
    if (txtCcName.value.length == 0)
    {
        ErrorSet(txtCcName, "Please enter your the name on your credit card.");
        $("#WeAccept").attr("class", "Hidden");
        return false;
    }

    if (txtCcCvv.value.length == 0)
    {
        ErrorSet(txtCcCvv, "Please enter the Security Code for your Credit Card.");
        $("#WeAccept").attr("class", "Hidden");
        return false;
    }
    else
    {
        if (!IsValidTextNumeric(txtCcCvv.value))
        {
            ErrorSet(txtCcCvv, "The Security Code you entered is not valid.  It must be numeric.");
            $("#WeAccept").attr("class", "Hidden");
            return false;
        }
    }

    if (txtBillingStreet1.value.length == 0)
    {
        ErrorSet(txtBillingStreet1, "Please enter your Billing Street.");
        return false;
    }

    if (txtBillingCity.value.length == 0)
    {
        ErrorSet(txtBillingCity, "Please enter your City.");
        return false;
    }

    if (ddlBillingStateId.selectedIndex == -1 ||
        ddlBillingStateId.options[ddlBillingStateId.selectedIndex].value <= 0)
    {
        ErrorSet(ddlBillingStateId, "Please select your State from the list.");
        return false;
    }

    if (txtBillingZipCode.value.length == 0)
    {
        ErrorSet(txtBillingZipCode, "Please enter your Zip Code.");
        return false;
    }

    if (txtBillingPhoneNumber.value.length == 0)
    {
        ErrorSet(txtBillingPhoneNumber, "Please enter your Phone Number.");
        return false;
    }

    return true;
}
function ValidatePlanSelection()
{
    var inputs = $(Plans).find("input");
    var span = $(Plans).find("span.Message");

    $(Plans).attr("class", "Control");
    span.html("");

    for (var i = 0; i < inputs.length; i++)
    {
        if (inputs.get(i).checked)
        {
            return true;
        }
    }

    $(Plans).attr("class", "Control Errored");
    span.html("Please select a plan");
    window.scrollTo(0, 0);
    return false;
}
function ErrorClear(el)
{
    var par = $(el).parent()

    par.attr("class", "Control");
    par.children("span.Message").html("");
}
function ErrorSet(el, Message)
{
    var par = $(el).parent()

    par.attr("class", "Control Errored");
    par.children("span.Message").html(Message);
    el.focus();
}
function PreviousButton_OnClick()
{
    TransitionPrevious();
}
function NextButton_OnClick()
{
    if (Validates())
    {
        TransitionNext();
    }
}
function TransitionPrevious()
{
    var NewFormIndex = CurrentFormIndex;

    if (NewFormIndex > 0)
    {
        NewFormIndex--;
    }
    else
    {
        NewFormIndex = (FormCount - 1);
    }

    TransitionTo(NewFormIndex);
}
function TransitionNext()
{
    var NewFormIndex = CurrentFormIndex;

    if (NewFormIndex < (FormCount - 1))
    {
        NewFormIndex++;
        TransitionTo(NewFormIndex);
    }
    else
    {
        ShowFinish();
    }
}
function TransitionTo(Index)
{
    var holder = $("#SignupForms");
    var current = holder.children()[CurrentFormIndex];
    var next = holder.children()[Index];

    $("#ReviewArea").attr("class", "Hidden");
    $("#SetupArea").removeAttr("class");

    $(current).attr("class", "Hidden");
    $(next).removeAttr("class");

    if (Index > 0)
    {
        $("#Buttons input.Previous").removeAttr("disabled");
    }
    else
    {
        $("#Buttons input.Previous").attr("disabled", "true");
    }

    self.scrollTo(0, 0);

    CurrentFormIndex = Index;
    SetDefaultFocus();
}
function SetDefaultFocus()
{
    switch (CurrentFormIndex)
    {
        case 0: //general info
            SetFocus(txtFirstName);
            break;
        case 1: //remember
            SetFocus(txtPasswordHint);
            break;
        case 2: //billing
            SetFocus(txtCcNumber);
            break;
    }
}
function SetFocus(obj)
{
    if (obj != null && obj.focus)
    {
        obj.focus();
    }
}
function ShowFinish()
{
    $(divRevUserName).html(txtUserName.value);
    $(divRevName).html(txtFirstName.value + " " + txtLastName.value);
    $(divRevEmailAddress).html(txtEmailAddress.value);
    $(divRevPasswordHint).html(txtPasswordHint.value);
    $(divRevSecurityQuestion).html(txtSecurityQuestion.value);
    $(divRevSecurityAnswer).html(txtSecurityAnswer.value);

    if (ddlSecurityQuestion.options[ddlSecurityQuestion.selectedIndex].value != -1)
    {
        $(divRevSecurityQuestion).html(ddlSecurityQuestion.options[ddlSecurityQuestion.selectedIndex].value);
    }

    var planItems = $(Plans).find("input");

    for (var i = 0; i < planItems.length; i++)
    {
        var planItem = planItems.get(i);

        if (planItem.checked)
        {
            var value = $(planItem).val();
            var parts = value.split(",");

            if (parts.length > 1)
            {
                $(divRevPlan).html(value.substring(parts[0].length + 1));
                $(hfPlan).val(parts[0]);
            }

            break;
        }
    }

    var cc = "****" + txtCcNumber.value.substring(txtCcNumber.value.length - 4, txtCcNumber.value.length);

    cc += " (" + ddlCcExpMonth.options[ddlCcExpMonth.selectedIndex].value + "/" + ddlCcExpYear.options[ddlCcExpYear.selectedIndex].value + ")";

    $(divRevCreditCard).html(cc);

    var address = txtBillingStreet1.value;

    if (txtBillingStreet2.value.length > 0)
    {
        address += "<br/>" + txtBillingStreet2.value;
    }

    address += "<br/>" + txtBillingCity.value + ", " + ddlBillingStateId.options[ddlBillingStateId.selectedIndex].value + " " + txtBillingZipCode.value;

    $(divRevBillingAddress).html(address);

    var phone = txtBillingPhoneNumber.value + "<span>PHONE</span>";

    if (txtBillingFaxNumber.value.length > 0)
    {
        phone += "<br/>" + txtBillingFaxNumber.value + "<span>FAX</span>";
    }

    $(divRevBillingPhone).html(phone);

    $("#SetupArea").attr("class", "Hidden");
    $("#ReviewArea").removeAttr("class");

    self.scrollTo(0, 0);
}
