﻿var About = {
    aspxPrefix: "ctl00_ContentPlaceHolder_",
    aspxMasterPrefix: "ctl00_",
    isBusySignUp: false,

    nextStep: function(sender, step) {
        if (sender != null)
            sender.get(0).style.display = "none";

        var nextStep = step + 1;
        $("#" + About.aspxPrefix + "AboutStep" + step).slideUp(1000, function() {
            $("#" + About.aspxPrefix + "AboutStep" + nextStep).get(0).className = "AboutStep";
            $("#" + About.aspxPrefix + "AboutStep" + nextStep).slideUp(0);
            $("#" + About.aspxPrefix + "AboutStep" + nextStep).slideDown(1000);
        });
    },

    validateSubmit: function() {
        if (About.isBusySignUp)
            return false;

        var valid = true;

        document.getElementById("GenderError").style.display = "none"
        document.getElementById("ScrollOfFameError").style.display = "none";
        document.getElementById("EditorError").style.display = "none";
        document.getElementById("EmailError").style.display = "none";
        document.getElementById("AgeError").style.display = "none";
        document.getElementById(About.aspxPrefix + "OverallErrorMessage").style.display = "none";
        var validator = new FormValidator();

        // Check if the required fields are filled.
        validator.validationStart();
        validator.validate(About.aspxPrefix + "FirstnameTextBox", "FirstnameError");
        validator.validate(About.aspxPrefix + "EmailTextBox", "EmailMissingError");
        validator.validate(About.aspxPrefix + "AgeTextBox", "AgeMissingError");

        valid = validator.validationComplete();

        if (!document.getElementById(About.aspxPrefix + "MaleRadioButton").checked && !document.getElementById(About.aspxPrefix + "FemaleRadioButton").checked) {
            valid = false;
            document.getElementById("GenderError").style.display = "block";
        }
        if (!document.getElementById(About.aspxPrefix + "EditorYesRadioButton").checked && !document.getElementById(About.aspxPrefix + "EditorNoRadioButton").checked) {
            valid = false;
            document.getElementById("EditorError").style.display = "block";
        }
        if (!document.getElementById(About.aspxPrefix + "ScrollOfFameYesRadioButton").checked && !document.getElementById(About.aspxPrefix + "ScrollOfFameNoRadioButton").checked) {
            valid = false;
            document.getElementById("ScrollOfFameError").style.display = "block";
        }

        if (!valid) {
            document.getElementById(About.aspxPrefix + "OverallErrorMessage").style.display = "block";
            return false;
        }

        // Validate email
        validator.validationStart();
        validator.validateEmail(About.aspxPrefix + "EmailTextBox", "EmailError");

        valid = validator.validationComplete();

        if (!valid) {
            document.getElementById(About.aspxPrefix + "OverallErrorMessage").style.display = "block";
            return false;
        }

        if (valid) {
            validator.validationStart();
            validator.validateIsNumeric(About.aspxPrefix + "AgeTextBox", "AgeError");

            valid = validator.validationComplete();

            if (!valid) {
                document.getElementById(About.aspxPrefix + "OverallErrorMessage").style.display = "block";
                return false;
            }
        }

        $('#SignupAddBuddyButton').fadeTo(250, .5);
        About.isBusySignUp = true;
        About.createUser();
    },

    createUser: function() {
        var firstname = document.getElementById(About.aspxPrefix + "FirstnameTextBox", "FirstnameError").value;
        var email = document.getElementById(About.aspxPrefix + "EmailTextBox", "EmailMissingError").value;
        var age = document.getElementById(About.aspxPrefix + "AgeTextBox", "AgeMissingError").value;

        var male = false;
        if (document.getElementById(About.aspxPrefix + "MaleRadioButton").checked)
            male = true;

        var editor = false;
        if (document.getElementById(About.aspxPrefix + "EditorYesRadioButton").checked)
            editor = true;

        var scrollOfFame = false;
        if (document.getElementById(About.aspxPrefix + "ScrollOfFameYesRadioButton").checked)
            scrollOfFame = true;

        var avatarColorID = About.getCheckedColor("ctl00$ContentPlaceHolder$AvatarColorsRadioButtonList");
        var personalMessage = document.getElementById(About.aspxPrefix + "PersonalMessageTextBox").value;

        PageMethods.GetConsentFormUrl(firstname, email, age, male, editor, scrollOfFame, personalMessage, avatarColorID, function(consentUrl) {
            window.location = consentUrl;
        },
        function(err) {
            $('#SignupAddBuddyButton').fadeTo(250, 1);
            alert("Er is een onverwachte fout opgetreden, probeer het nog eens (foutmelding: " + err + ")");
            About.isBusySignUp = false;
        });
    },

    getCheckedColor: function(name) {
        var radioButtons = document.getElementsByName(name);
        for (var x = 0; x < radioButtons.length; x++) {
            if (radioButtons[x].checked) {
                return radioButtons[x].value;
            }
        }
    },

    setCheckedColor: function(name, value) {
        var radioButtons = document.getElementsByName(name);
        for (var x = 0; x < radioButtons.length; x++) {
            if (radioButtons[x].value == value) {
                radioButtons[x].checked = true;
            }
        }
    },

    tryLogin: function() {
        var token = document.getElementById(About.aspxMasterPrefix + "DelegatedTokenHiddenField").value;
        if (token == "")
            return;

        BuddyAdd.inviteBotAndRegister(token, function() {
            About.nextStep(null, 2);
        });
    },

    validateSubmitStep4: function() {
        if (About.isBusySignUp)
            return false;

        var valid = true;

        document.getElementById("ScrollSelectionError").style.display = "none";
        document.getElementById(About.aspxPrefix + "OverallErrorMessage2").style.display = "none";

        // Check if the required fields are filled.
        if (!document.getElementById(About.aspxPrefix + "ScrollYesRadioButton").checked && !document.getElementById(About.aspxPrefix + "ScrollNoRadioButton").checked) {
            valid = false;
            document.getElementById("ScrollSelectionError").style.display = "block";
        }

        if (!valid) {
            document.getElementById(About.aspxPrefix + "OverallErrorMessage2").style.display = "block";
            return false;
        }

        document.getElementById(About.aspxPrefix + "ScrollOfFameYesRadioButton").checked = document.getElementById(About.aspxPrefix + "ScrollYesRadioButton").checked;
        document.getElementById(About.aspxPrefix + "ScrollOfFameNoRadioButton").checked = document.getElementById(About.aspxPrefix + "ScrollNoRadioButton").checked;
        About.setCheckedColor("ctl00$ContentPlaceHolder$AvatarColorsRadioButtonList", About.getCheckedColor("ctl00$ContentPlaceHolder$AvatarColorsRadioButtonList2"));
        document.getElementById(About.aspxPrefix + "PersonalMessageTextBox").value = document.getElementById(About.aspxPrefix + "PersonalMessageTextBox2").value;

        $('#AboutAddBuddySpan').fadeTo(250, .5);
        About.isBusySignUp = true;
        About.createUser();
    }
}

$(document).ready(function() {
    $("#ctl00_ContentPlaceHolder_StepOneLink").click(function() {
        About.nextStep($(this), 1);
        return false;
    });
    $("#ctl00_ContentPlaceHolder_StepOneButton").click(function() {
        About.nextStep($(this), 1);
        return false;
    });
    $("#SignUpButton").click(function() {
        About.validateSubmit();
        return false;
    });
    $("#AboutAddBuddyButton").click(function() {
        About.validateSubmitStep4();
        return false;
    });


    // eww
    if (window.location.href.indexOf("callback3=true") != -1)
        About.tryLogin();
});