﻿jQuery(document).ready(function () {
    loadStartUp();
});

// ====================================================================================================================
// Description: Checks to see if an element exists.
// Returns: N/A
// Remarks:
// Khoa Vuong - Method created [08/6/2011]
// ====================================================================================================================
jQuery.fn.exists = function(){return jQuery(this).length>0;}

// ====================================================================================================================
// Description: Function called at the beginning of all async post backs.
// Returns: N/A
// Remarks:
// Khoa Vuong - Method created [15/4/2011]
// ====================================================================================================================
function onBeginRequest(sender, args) {
    
}

function onEndRequest(sender, args) {
    loadStartUp();
}
// ====================================================================================================================
// Description: Loads page parameters and javascript features.
// Returns: N/A
// Remarks:
// Khoa Vuong - Method created
// ====================================================================================================================
function loadStartUp() {
    // Set pager buttons
    jQuery('.PPagerButton').addClass("ui-button ui-widget ui-state-default");
    jQuery('.PPagerLinkSelect').addClass("ui-button ui-widget ui-state-active");                                       
    jQuery('.PPagerButton').hover(
        function () {
            jQuery(this).addClass("ui-state-hover");
        },
        function () {
            jQuery(this).removeClass("ui-state-hover");
        }
    );

    // Setup hover state of all buttons
    jQuery('.jQButton').addClass("ui-button ui-widget ui-state-default ui-corner-all");
    jQuery('.jQButton').hover(
        function () {
            jQuery(this).addClass("ui-state-hover");
        },
        function () {
            jQuery(this).removeClass("ui-state-hover");
        }
    );

    // Setup modal for login popup
    var loginModal = jQuery('.miniLoginModal').dialog({
                                title: 'Login',
                                autoOpen: false,
                                modal: true
                            });
    loginModal.parent().appendTo(jQuery("form:first"));

    // Setup search results filter drop down list
    jQuery('#searchResultsFilterFlyout').menu({
        content: jQuery('.searchResultsFilterList').html(),
        width: 200,
        flyOut: true
    });

    // Setup tabs for short courses
    jQuery('#tabsCourse > li').addClass("inactiveTab");
    jQuery('#tabsCourse > li').first()
        .removeClass("inactiveTab")
        .addClass("activeTab");
    jQuery('#tabsCourse > li').click(function () {
        var selectedIndex = jQuery('#tabsCourse > li').index(this);
        activateTab(selectedIndex);
    });
    jQuery('.pe_courses').css("display", "block");
    jQuery('.courses_lmp').css("display", "none");
    jQuery('.courses_mb').css("display", "none");

    // Setup rotating banner
    jQuery('.slideshow').cycle({
        fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        speed: '5000',
        speedIn: '1000',
        speedOut: '1000'
    });

    // Setup login modal for SAT
    setupSAT();

    // Setup valiation for searches
    jQuery(jQuery("form:first")).validate();

    if(jQuery('.cvform').exists())
    {
        LoadCVForm();
    }
}

// ====================================================================================================================
// Description: Validates vouchers before proceeding with checkout
// Returns: N/A
// Remarks:
// Khoa Vuong [24/6/2011] - Method created
// ====================================================================================================================
function ClientCheckoutValidations()
{
    var isValid = true;

    // Setup voucher validations
    if(jQuery('.vouchertextbox').exists())
    {
        // Confirm the check out button exists
        if(jQuery('.lnb_checkout').exists())
        {
            // Get all voucher textboxes
            jQuery('.vouchertextbox').each(function(){
                if(jQuery(this).val() != "")
                {
                    // Check to see if label already exists
                    if(jQuery(this).siblings("label").exists() == false)
                    {
                        // Add label control
                        jQuery(this).parent().append('<label class="voucherErrorLbl" for="' + jQuery(this).attr("id") + '">Voucher has not been applied.</label>');
                    }
                    // If label already exists, change the error message
                    else
                    {
                        jQuery(this).siblings("label").html("Voucher has not been applied.");
                    }

                    isValid = false;
                }
            });
        }
    }

    return isValid;
}

// ====================================================================================================================
// Description: Check to ensure that when user clicks Add Voucher, a voucher number is specified.
// Returns: N/A
// Remarks:
// Khoa Vuong [24/6/2011] - Method created
// ====================================================================================================================
function VoucherAddValidation(button)
{
    var isValid = true;
    
    if(jQuery(button).exists())
    {
        // Get the voucher textbox associated with this button
        if(jQuery(button).siblings(".vouchertextbox").exists())
        {
            var voucherNumberTxtBox = jQuery(button).siblings(".vouchertextbox");

            // If no voucher number is entered, display error message.
            if(jQuery(voucherNumberTxtBox).val() == "")
            {
                // Check to see if label already exists
                if(jQuery(voucherNumberTxtBox).siblings("label").exists() == false)
                {
                    // Add label control
                    jQuery(voucherNumberTxtBox).parent().append('<label class="voucherErrorLbl" for="' + jQuery(voucherNumberTxtBox).attr("id") + '">Please enter voucher number.</label>');
                }
                // If label already exists, change the error message
                else
                {
                    jQuery(voucherNumberTxtBox).siblings("label").html("Please enter voucher number.");
                }

                isValid = false;
            }
        }
    }

    return isValid;
}
    
function activateTab(tabIndex) {
    jQuery('#tabsCourse > li').each(function (index) {
        // Tab has been made active
        if (index == tabIndex) {
            if (jQuery(this).hasClass("inactiveTab")) {
                jQuery(this).removeClass("inactiveTab")
                    .addClass("activeTab");

            }
        }
        // Deactivate other tabs
        else {
            if (jQuery(this).hasClass("activeTab")) {
                jQuery(this).removeClass("activeTab")
                    .addClass("inactiveTab");

            }
        }
    });

    switch (tabIndex) {
        case 0:
            jQuery('.pe_courses').css("display", "block");
            jQuery('.courses_lmp').css("display", "none");
            jQuery('.courses_mb').css("display", "none");
            break;
        case 1:
            jQuery('.pe_courses').css("display", "none");
            jQuery('.courses_lmp').css("display", "block");
            jQuery('.courses_mb').css("display", "none");
            break;
        case 2:
            jQuery('.pe_courses').css("display", "none");
            jQuery('.courses_lmp').css("display", "none");
            jQuery('.courses_mb').css("display", "block");
            break;
    }
}

// ====================================================================================================================
// Description: Prepares SAT parameters.
// Returns: N/A
// Remarks:
// Khoa Vuong - Method created [6/4/2011]
// ====================================================================================================================
function setupSAT() {
    if (jQuery('.page_main_324 .loginCtrl') != '') {
        var satDialog = jQuery('.page_main_324 .loginCtrl').dialog({
            title: 'Login',
            autoOpen: false,
            modal: true
        });

        satDialog.parent().appendTo(jQuery("form:first"));

        userID = getParameterByName('uid');

        if (userID == "") {
            jQuery('.saveSurveyOpt').css('display', 'block');
        }
        else {
            jQuery('.saveSurveyOpt').css('display', 'none');
        }
    }
}

// ====================================================================================================================
// Description: Gets the value of a key from URL query string.
// Returns: Value of specified key name as a string.
// Remarks:
// Khoa Vuong - Method created [6/4/2011]
// ====================================================================================================================
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

// ====================================================================================================================
// Description: Displays the specified modal dialog.
// Returns: N/A
// Remarks:
// Khoa Vuong - Method created [14/4/2011]
// ====================================================================================================================
function showDialog(dialogClassName, commandName) {
    if (commandName == 'login') {
        if (jQuery('.mainLoginCtrl').html() == 'Login') {
            jQuery('.' + dialogClassName).dialog('open');
            return false;
        }
        else {
            return true;
        }
    }
}

// ====================================================================================================================
// Description: Search feature validation.
// Returns: N/A
// Remarks:
// Khoa Vuong - Method created [13/5/2011]
// ====================================================================================================================
function validateSearch() {
    jQuery.validator.addMethod(
        "punctuation",
        function (value, element) {
            re = /[/"]/;
            if (element.value.match(re)) {
                return false;
            }
            else {
                return true;
            }
        },
        ""
    );
    jQuery('#txt_search').rules(
        "add",
        {
            required: true,
            punctuation: true,
            messages:
            {
                required: "Please provide a search string.",
                punctuation: "Search string cannot contain quotation marks."
            }
        }
    );

    return jQuery(jQuery("form:first")).validate().element("#txt_search");
}
