﻿
var currentItemName;
var currentItemValue;
var COST_SUFFIX = "USD";
var isMilitary = false;
var isOtherEntry = false;


function AdmissionsOptions_Change() {

    if ($('#AdmissionOptions').val() != -1) {

        if ($('#AdmissionOptions').val() != "other") {

            ToggleHiddenRows(false);       

            AssignCurrentItems('AdmissionOptions', false);
            $('#AddmissionsAddToCart_Btn').attr('disabled', false);
            $('#AdmissionsOptionsCost').html("( " + currentItemValue + " " + COST_SUFFIX + " )");            
            
        }
        else {

            ToggleHiddenRows(true);
            
            $('#AdmissionsOptionsCost').html('');
            $('#AddmissionsAddToCart_Btn').attr('disabled', 'disabled');
        
        }
       
    }
    else {

        $('#AddmissionsAddToCart_Btn').attr('disabled', 'disabled');
        $('#AdmissionsOptionsCost').html('');
        ToggleHiddenRows(false);
    
    }

}

function ToggleHiddenRows(isVisible) {

    isOtherEntry = isVisible;

    if (isVisible) {
    
        $('.HiddenOption').show();
        
    }
    else {

        $('.HiddenOption').hide();
        $('#OtherAmount').val('');
        $('#OtherName').val('');
    }

}

function OtherInput_KeyUp() {

    if ($('#OtherAmount').val().length > 0 && $('#OtherName').val().length > 0) {

        $('#AddmissionsAddToCart_Btn').attr('disabled', false);

    }
    else {

        $('#AddmissionsAddToCart_Btn').attr('disabled', 'disabled');
    
    }
    
    
}

function DegreePrograms_Change() {

    
    if ($('#DegreePrograms').val() != -1) {

        $('#DegreeCourseOptions').attr('disabled', false);
        $('#DegreeAddToCart_Btn').attr('disabled', false);
        $('#DegreeMilitary_CB').attr('disabled', false);


        $('#DegreeCourseOptions').html(GetCourses($('#DegreePrograms').val(), "reg"));
        DegreeCourseOptions_Change();
    }
    else {
    
        $('#DegreeOptionsCost').html('');
        $('#DegreeCourseOptions').html('');
        $('#DegreeCourseOptions').attr('disabled', 'disabled');
        $('#DegreeAddToCart_Btn').attr('disabled', 'disabled');
        $('#DegreeMilitary_CB').attr('disabled', 'disabled');
        $('#DegreeMilitary_CB').attr('checked', false);

    }
}

function DegreeCourseOptions_Change() {

    AssignCurrentItems('DegreeCourseOptions', false);
    $('#DegreeOptionsCost').html("( " + currentItemValue + " " + COST_SUFFIX + " )");

}

function ElectivePrograms_Change() {

    if ($('#ElectivePrograms').val() != -1) {

        $('#ElectiveCourseOptions').attr('disabled', false);
        $('#ElectiveAddToCart_Btn').attr('disabled', false);
        $('#ElectiveMilitary_CB').attr('disabled', false);
        $('#ElectiveCourseOptions').html(GetCourses($('#ElectivePrograms').val(), "advanced"));

        ElectiveCourseOptions_Change()
       
    }
    else {

        $('#ElectiveOptionCost').html('');
        $('#ElectiveCourseOptions').html('');
        $('#ElectiveCourseOptions').attr('disabled', 'disabled');
        $('#ElectiveAddToCart_Btn').attr('disabled', 'disabled');
        $('#ElectiveMilitary_CB').attr('disabled', 'disabled');
        $('#ElectiveMilitary_CB').attr('checked', false);
    }
   
}

function ElectiveCourseOptions_Change() {

    AssignCurrentItems('ElectiveCourseOptions', false);
    $('#ElectiveOptionCost').html("( " + currentItemValue + " " + COST_SUFFIX + " )");
}

function GetCourses(degreeProgram, type) {

    var options;

    $.ajax({

        type: "POST",
        url: "/Services/DegreePrograms.asmx/GetCoursesByDegreeProgram",
        data: "{'program':'" + degreeProgram + "', 'type':'" + type + "'}",
        dataType: "json",
        async: false,
        contentType: "application/json; charset=utf-8",
        success: function(result) {

            options = result.d;

        },
        error: function(msg) {

            alert("There was an error processing your request. Please select try again.");
        }
    });

    return options;
}

function DonationChange() {

    if ($('#DonationAmount').val().length > 0) {

        $('#DonationAddToCart_Btn').attr('disabled', false);
        
    }
    else {

        $('#DonationAddToCart_Btn').attr('disabled', 'disabled');

    }

}

function toggleIsMilitary(section) {

    isMilitary = (section == 'Degree') ? $('#DegreeMilitary_CB').attr('checked') : $('#ElectiveMilitary_CB').attr('checked');

    if (section == 'Degree') {

        if (isMilitary) {

            $('#DegreeOptionsCost').html('( ' + $('#DegreeCourseOptions option:selected').attr('id') + " ) " + COST_SUFFIX);
        }
        else {

            $('#DegreeOptionsCost').html("( " + currentItemValue + " " + COST_SUFFIX + " )");

        }

    }
    else {

        if (isMilitary) {

            $('#ElectiveOptionCost').html('( ' + $('#ElectiveCourseOptions option:selected').attr('id') + " ) " + COST_SUFFIX);
        }
        else {

            $('#ElectiveOptionCost').html("( " + currentItemValue + " " + COST_SUFFIX + " )");

        }
    }
}


function AddToPPCart(cartSection, checkMilitaryDiscount) {

    AssignCurrentItems(cartSection, checkMilitaryDiscount);    
    CreatePayPalForm();       
}


function AssignCurrentItems(section, checkMilitaryDiscount) {

    if (section != 'Donation') {

        if (isOtherEntry && section == 'AdmissionOptions') {

            currentItemName = $('#OtherName').val();
            currentItemValue = $('#OtherAmount').val();
        
        }
        else {
        
            currentItemValue = (checkMilitaryDiscount && isMilitary) ? $('#' + section + ' option:selected').attr('id') : $('#' + section).val();
            currentItemName = $('#' + section + ' option:selected').text();
        }
    }
    else {
    
        currentItemValue = $('#DonationAmount').val();
        currentItemName = "Colorado Theological Seminary Donation";
    }
}


function CreatePayPalForm() {

    $.get('/TextFiles/PayPalForm/ShoppingCartPayPal.htm', function(data) {

        $(data).appendTo('body');
        $('[name=item_name]').val(currentItemName);
        $('[name=amount]').val(currentItemValue);
       
        $('#payPalForm').submit();
    });

}

