﻿//===============================================================================
// * Namespace:   YouFound.Forms.Signup
// * Description: Provides functionality for the signup form 
//===============================================================================
YouFound.Forms.Signup = (function () {

    return {

        /* 
        ==========================================================*/
        ConfirmationPage: function () {
            $("input[name=paymentType]").change(function () {
                $('#creditCardContent').hide();
                $('#directDebitContent').hide();
                $('#invoiceMeContent').hide();
                var paymentType = $(this).val();
                switch (paymentType) {
                    case 'CreditCard':
                        $('#creditCardContent').show();
                        break;
                    case 'DirectDebit':
                        $('#directDebitContent').show();
                        break;
                    case 'InvoiceMe':
                        $('#invoiceMeContent').show();
                        break;
                }
            });
        },

        /* Recreates the file upload slots based on the currently
        * selected template type.
        ==========================================================*/
        UpdateContainer: function () {

            var maxFileCount = 1;
            var templateId = $("input[name=templateId]:checked").val();

            switch (templateId) {
                case "1":
                    maxFileCount = 5;
                    break;
                case "2":
                    maxFileCount = 1;
                    break;
                case "3":
                    maxFileCount = 6;
                    break;
            }

            var existingFileCount = $("#existingImages img").length;
            var remainingFileCount = maxFileCount - existingFileCount;

            $("#imagesMaxFileCount").text(maxFileCount);
            $("#imagesRemainingFile").text(remainingFileCount);

            // Update the file upload slots html
            var container = $("#imagesFieldsContainer");
            container.html("");

            for (var i = 0; i < remainingFileCount; i++) {

                var fileField = $('<input name="uploadImage" class="textInput" type="file" />');
                var descriptionField = $('<input name="uploadDescription" class="textInput countText" type="text" maxlength="120" />');

                var outer = $('<div/>');

                outer.append('<span>File:</span>');
                outer.append(fileField);
                outer.append('<span>Description:</span>');
                outer.append(descriptionField);

                container.append(outer);
            }

        },

        /* Called once on page load to initialise the namespace
        ==========================================================*/
        Initialise: function () {

            // Limit the input for the ABN field
            $("#companyABNField input[type='text']").mask("99 999 999 999");

            // Force an update of the file upload slots
            this.UpdateContainer();

            // Make sure the file upload slots on the page details page
            // get updated when the template type changes
            //$("#templateId").click(jQuery.proxy(this.UpdateContainer, this));
            $("input[name=templateId]").change(jQuery.proxy(this.UpdateContainer, this));

            // Init the drop downs
            // TODO: Is this needed, and also surely this functionality can be cleaned up!!
            getAreas($("#state").val(), "area");
            getBusinessIndustries($("#category").val(), "industry");
            getAreas($("#customerState").val(), "customerArea");

            // Setup the form validator
            FormValidator.Initialise("SignupForm", "PreviousBtn", "NextBtn", "SubmitBtn");

            // Add the pages we want to be validated
            FormValidator.AddPage(
				new FormValidator.Page(
					{
					    pageId: "signUpStepCreateAccount",
					    elements: this.CreateAccountRules()
					}
				));

            FormValidator.AddPage(
				new FormValidator.Page(
					{
					    pageId: "signUpStepAddOptions",
					    elements: this.AddOptionsRules()
					}
				));

            FormValidator.AddPage(
				new FormValidator.Page(
					{
					    pageId: "signUpStepSelectTemplate",
					    elements: this.SelectTemplateRules()
					}
				));

            FormValidator.AddPage(
				new FormValidator.Page(
					{
					    pageId: "signUpStepPageDetails",
					    elements: this.PageDetailsRules(),
					    onPageActivated: this.UpdateContainer
					}
				));
        },

        /* Add Options Page Rules
        ====================================*/
        AddOptionsRules: function () {
            return [];
        },

        /* Select Template Page Rules
        ====================================*/
        SelectTemplateRules: function () {
            return [];
        },

        /* Page Details Page Rules
        ====================================*/
        PageDetailsRules: function () {
            return [
				YouFound.ValidationElements.NavNameField,
				YouFound.ValidationElements.PageHeadingField,
		        YouFound.ValidationElements.PageHeadingField,
		        YouFound.ValidationElements.PageSummaryField,
		        YouFound.ValidationElements.PageKeywordsField,
            // TODO: BI: Temporarily disabled to allow signup form to complete. YouFound.ValidationElements.ContentField,
		        YouFound.ValidationElements.FirstNameField,
		        YouFound.ValidationElements.SurnameField,
		        YouFound.ValidationElements.TermsAgreeField
			];
        },

        /* Create Account Page Rules
        ====================================*/
        CreateAccountRules: function () {

            return [
				YouFound.ValidationElements.UsernameField,
		        YouFound.ValidationElements.EmailField,
		        YouFound.ValidationElements.PasswordField,
		        YouFound.ValidationElements.ConfirmPasswordField,
		        YouFound.ValidationElements.BusinessNameField,
				YouFound.ValidationElements.CompanyABNField,
		        YouFound.ValidationElements.RelativeShortcutUrlField,
		        YouFound.ValidationElements.CategoryField,
				YouFound.ValidationElements.IndustryField,
		        YouFound.ValidationElements.StateField,
		        YouFound.ValidationElements.AreaField
			];
        }
    };
})();
