﻿var newContainerHeight = 0;


$(document).ready(function() {

    // Shipping same as Billing Address
    var chkSameAddress = $('#chkAddress');
    $(chkSameAddress).click(function() {
        if ($(this).is(':checked')) {
            $('#billingFields > div > input').each(function(i) {
                $('#shippingFields > div > input').eq(i).val($(this).val());
            });
            $('#billingFields > div > select').each(function(i) {
                $('#shippingFields > div > select').eq(i).val($(this).val());
            });
        } else {
            $('#billingFields > div > input').each(function(i) {
                $('#shippingFields > div > input').eq(i).val("");
            });
            $('#billingFields > div > select').each(function(i) {
                $('#shippingFields > div > select').eq(i).val(0);
            });
        }
    });
    /*
    // Review Order Field Validation
    var validOptns = {
    reqField:      $('.validationField'),
    errorMsg:      '',
    btnUpdate:     $('.btnUpdate'),
    btnContinue:   $('.btnContinue'),
    countErrors:   0,
    isValid:        true,
    showingErrors:  false,
    pageName:       ''
    };
    
    $(validOptns.btnContinue).click(function() {
    var invalidFields;
    validOptns.pageName = 'reviewOrder';
    invalidFields = validateFields();   
    if (invalidFields > 0) {
    return false; // Halt postback to display errors
    }
    });
        
    function validateFields() {
    validOptns.countErrors = 0;
    $(validOptns.reqField).each(function(i) {
    if ($(this).val() == "" || $(this).val() == 0) {
    // Is invalid
    displayErrors($(this), validOptns.pageName, i);
    validOptns.countErrors += 1;
    validOptns.isValid =  false;
    console.log(i + 'Showing errors: ' + validOptns.showingErrors);
    } 
    else {
    validOptns.countErrors += 0;
    validOptns.isValid = true;
    validOptns.showingErrors = false;
    $('.errorMessage').hide();
    console.log(i + 'Showing errors: ' + validOptns.showingErrors);
    }
    });
    return validOptns.countErrors;
    }
    function displayErrors(elem, pageName, i) {
    // BUG: Each iteration needs to have a showing
    //      errors property
    if (validOptns.showingErrors == true) {
    return;
    } 
    else {
    if (pageName == 'reviewOrder') {
    var invalidRow = $(elem).parent().parent();
    $(invalidRow).addClass('invalidRow');
    $(invalidRow).children('td:first').addClass('first');
    $(invalidRow).children('td:last').addClass('last');
    validOptns.errorMsg = 'Please enter a quantity for ' + $(invalidRow).children('td:first').text();
    $(invalidRow).after('<tr class="invalidMessage"><td colspan="5">' + validOptns.errorMsg + '</td></tr>');
    }
    if (pageName == 'submission') {
    $('.errorMessage').eq(i).show();
    console.log('Show errors for number ' + i);
    }
    validOptns.showingErrors = true;
    }
    }
    
    // Disable Form Buttons on Submit
    var button = $('.btnContinue');
    $(button).click(function() {
    if (validOptns.isValid) {
    var btnImage = $(this).attr('src');
    var btnClass = 'btnSpacer';
    var newBtn = '<img src="' + btnImage + '" class="' + btnClass + '" />';
    $(this).hide();
    $(this).after(newBtn);
    $(btnClass).show();
    }
    });
    // Validate and disable MyTrafford Submission Form Buttons on Submit
    
    var sbmButton = $('.btnFormSubmission');
    
    $(sbmButton).click(function() {
    validOptns.pageName = 'submission';
    var invalidFields;
    invalidFields = validateFields();   
    if (invalidFields > 0) {
    return false; // Halt postback to display errors
    }
    else {
    var btnImage = $(this).attr('src');
    var btnClass = 'btnFormSubmitted';
    var newBtn = '<img src="' + btnImage + '" class="' + btnClass + '" />';
    $(this).hide();
    $(this).after(newBtn);
    $(btnClass).show();
    }
    });
    
    //    $(sbmButton).click(function() {
    //        var btnImage = $(this).attr('src');
    //        var btnClass = 'btnFormSubmitted';
    //        var newBtn = '<img src="' + btnImage + '" class="' + btnClass + '" />';
    //        $(this).hide();
    //        $(this).after(newBtn);
    //        $(btnClass).show();
    //    });
    */
    // Dashboard Functions
    var messageOptions = {
        messageList: $('#myMessages-list'),
        originalHeight: 0,
        listHeight: 0,
        messageCount: 0
    };
    /*
    var newHeight = $('#myBooksContainer').height();
    $('#myBooksSidebarList').height(newHeight); */

    messageOptions.messageCount = messageOptions.messageList.children('li').size();

    if (messageOptions.messageCount >= 3) {
        messageOptions.originalHeight = messageOptions.messageList.height(); // Save original height
        messageOptions.messageList.children('li:lt(3)').each(function() {
            messageOptions.listHeight += parseInt($(this).outerHeight()); // Create new cropped height
        });
        messageOptions.messageList.height(messageOptions.listHeight);
        messageOptions.messageList.after('<div id="showHideMessages"><a href="#">View all messages</a></div>');

        $('#showHideMessages a').toggle(
            function() {
                messageOptions.messageList.animate({
                    height: messageOptions.originalHeight
                }, 1000);
                $('#showHideMessages a')
                    .text('Hide old messages')
                    .addClass('opened');
                return false;
            },
            function() {
                messageOptions.messageList.animate({
                    height: messageOptions.listHeight
                }, 1000);
                $('#showHideMessages a')
                    .text('View all messages')
                    .removeClass('opened');
                return false;
            }
        );
    }
    if (messageOptions.messageCount == 0) {
        messageOptions.messageList.hide();
    }
    /*
    // Better 'update' functions
    var updateUI = {
    successValid:   $('.updateSuccess'),
    visibleSuccess: $('.updateSuccess:visible'),
    successMsg:     $('.updateSuccess:visible').text(),
    displayedMsg:   '',
    msgLocation:    $('.wideColumnLeft') // Append message before this column
    }
    
    if (updateUI.successValid.is(':visible')) {
        
    updateUI.visibleSuccess.each(function() {
    updateUI.displayedMsg += '<li>' + $(this).text() + '</li>';
    });
        
    updateUI.msgLocation.before('<div id="updateMessage"><ul id="updateMsgList">' + updateUI.displayedMsg + '</ul><p>' + '</p></div>');
    updateUI.successValid.hide();
    }
    */




    var bookOptions = {
        messageList: $('#myBooksSidebarList'),
        originalHeight: 0,
        listHeight: 0,
        messageCount: 0
    };

    bookOptions.messageCount = bookOptions.messageList.children('li').size();
    var height = bookOptions.messageCount * 115;
    var containerHeight = $('#myBooksContent').height();

    if (bookOptions.messageCount > 3) {
        if (height > containerHeight) {

            $('#myBooksContainer').height(height);
        }
        else {
            $('#myBooksContainer').height(containerHeight);

        }
    }






});
