﻿//elements
var dest_select;
var location_select;
var sdate_select;
var length_select;

var sdate_required = false;
var length_required = false;

//containers
var market_code;
var culture_code;
var selectArr = new Array();
var current_dest;
var empty_opt_text;
var na_opt_text;
var nopref_opt_text;
var regions_text;
var cities_text;
var states_text;
var schools_text;
var cities;
var nopref_value;

var selected_dest_dict = new Dictionary();
var location_fee_dict;
var course_fee_dict;

var location_price = 0;
var course_price = 0;
var programInfo = null;
        
function getCoursePrice() {
    return course_price != null ? course_price : 0;
}

function getLocationPrice(){
    return location_price;
}



function initialize() {
    selectArr[0] = location_select;
    selectArr[1] = sdate_select;
    selectArr[2] = length_select;
    
    if(location_select != null || sdate_select != null || length_select != null)
        dest_select.onchange = onDestinationChange;

    if (location_select != null)
        location_select.onchange = onLocatioinChange;

    if(sdate_select != null)
        sdate_select.onchange = onStartDateChange;
    
    if(length_select != null)
        length_select.onchange = onLengthChange;
}

function onDestinationChange(){
    RegisterForAsyncCall(dest_select);
    clearTotalPrice();
    course_price = 0;
    location_price = 0;
    location_fee_dict = new Dictionary();
    course_fee_dict = new Dictionary();

    disableSubSelect(dest_select, new Array(location_select, sdate_select, length_select));

    if (this.value != '') {
        current_dest = selected_dest_dict.getValue(dest_select.value);

        if (current_dest == null) {
            PreAppService.GetDestination(culture_code, dest_select.value, onDestinationSelectCallback, null);
        }
        else {
            onDestinationSelect();
        }
    }
}

function onLocatioinChange(){
    RegisterForAsyncCall(location_select);
    clearTotalPrice();
    location_price = location_fee_dict.getValue(location_select.value);
}

function onStartDateChange(){
    RegisterForAsyncCall(sdate_select);
    clearTotalPrice();
    disableSubSelect(sdate_select, new Array(length_select));

    if (sdate_select.value != '' && length_select != null) {
        fillLengthSelect(sdate_select.value);
        length_select.disabled = false;
        length_select.focus();
    }
}

function onLengthChange(){
    RegisterForAsyncCall(length_select);
    
    if (length_select.value != '') {
        var courseCode = length_select.value.split('/')[1];
        course_price = course_fee_dict.getValue(courseCode);
        showTotalPrice();
    }
    else {
        clearTotalPrice();
    }
}

function onDestinationSelectCallback(result) {
    current_dest = result;
    selected_dest_dict.add(result.Code, result);        
    onDestinationSelect();
}

function onDestinationSelect() {
    if(sdate_select != null) {
        sdate_select.disabled = false;
        fillStartDateSelect();
    }
    else if(length_select != null) {
        length_select.disabled = false;
        fillLengthSelect(nopref_value);
    }
    
    if (location_select == null || !fillLocationSelect()) {
        
        if(sdate_select != null)
            sdate_select.disabled = false;
        
        if(location_select != null)
            enableLocationValidation(false);
        
        if(sdate_select != null)
            sdate_select.focus();
    }
    else {
        
        location_select.parentNode.style.display = ''
        location_select.disabled = false;
        enableLocationValidation(true);
        location_select.focus();
    }
}



function enableLocationValidation(enabled) {
    for (var i = 0; i < Page_Validators.length; i++) {
        var val = Page_Validators[i];
        if (val.controltovalidate == location_select.id) {
            val.enabled = enabled;
            break;
        }    
    }
}

function fillLocationSelect() {
    var stateArr = current_dest.StateFees;
    var regionArr = current_dest.RegionFees;
    var cityArr = current_dest.CityFees;
    var schoolArr = current_dest.SchoolFees;
    
    if (stateArr.length != 0 || regionArr.length != 0 || cityArr.length != 0) {
        location_fee_dict.add(nopref_value, 0);
        
        var noprefOpt = document.createElement('option');
        noprefOpt.value = nopref_value;
        noprefOpt.innerHTML = nopref_opt_text;

        location_select.appendChild(noprefOpt);

        if (stateArr.length != 0) {

            var stateOptGroup = document.createElement('optGroup');
            stateOptGroup.label = states_text;
            for (var i = 0; i < stateArr.length; i++) {
                location_fee_dict.add(stateArr[i].Name, stateArr[i].Price);
            
                var opt = document.createElement('option');
                opt.value = stateArr[i].Name;
                opt.innerHTML = stateArr[i].Name;
                stateOptGroup.appendChild(opt);
            }
            location_select.appendChild(stateOptGroup);
        }

        if (regionArr.length != 0) {
            var regionOptGroup = document.createElement('optGroup');
            regionOptGroup.label = regions_text;
            for (var i = 0; i < regionArr.length; i++) {
                location_fee_dict.add(regionArr[i].Name, regionArr[i].Price);
            
                var opt = document.createElement('option');
                opt.value = regionArr[i].Name;
                opt.innerHTML = regionArr[i].Name;
                regionOptGroup.appendChild(opt);
            }
            location_select.appendChild(regionOptGroup);
        }

        if (cityArr.length != 0) {
            var cityOptGroup = document.createElement('optGroup');
            cityOptGroup.label = cities_text;
            for (var i = 0; i < cityArr.length; i++) {
                location_fee_dict.add(cityArr[i].Name, cityArr[i].Price);
                
                var opt = document.createElement('option');
                opt.value = cityArr[i].Name;
                opt.innerHTML = cityArr[i].Name;
                cityOptGroup.appendChild(opt);
            }
            location_select.appendChild(cityOptGroup);
        }

        if (schoolArr.length != 0) {
            var schoolOptGroup = document.createElement('optGroup');
            schoolOptGroup.label = schools_text;
            for (var i = 0; i < schoolArr.length; i++) {
                location_fee_dict.add(schoolArr[i].Name, schoolArr[i].Price);

                var opt = document.createElement('option');
                opt.value = schoolArr[i].Name;
                opt.innerHTML = schoolArr[i].Name;
                schoolOptGroup.appendChild(opt);
            }
            location_select.appendChild(schoolOptGroup);
        }
        
        if(document.getElementById('location_container').style.display == 'none'){
            if(jQuery.browser.msie)
                document.getElementById('location_container').disabled = false;
            else
                jQuery('#location_container').slideDown(500);
        }   
        if(programInfo != null && programInfo.LocationChoice != null){
            location_select.value = programInfo.LocationChoice;
            onLocatioinChange();
        }
            
        return true;
    }
    else {
        if(jQuery.browser.msie)
            document.getElementById('location_container').disabled = true;
        else
            jQuery('#location_container').slideUp(500);

        return false;
    }
}

function sortStartDate(date1, date2){
    return date1 - date2;
}

function fillStartDateSelect() {
    if(!sdate_required)
        addNoPreferenceOpt(sdate_select);
    
    var temp_dict = new Dictionary();
    var dateArr = new Array();
    
    for(var i =0; i < current_dest.Courses.length; i++)
    {
        var course = current_dest.Courses[i];

        for (var j = 0; j < course.Prices.length; j++) {
            var price = course.Prices[j];
            var year = price.CourseYear;
            var month = price.CourseMonth;
            var value = year + '/' + month;

            if (temp_dict.getValue(value) == null) {
                var d = new Date();
                d.setFullYear(year, month - 1, 1);
                dateArr[dateArr.length] = d;
                temp_dict.add(value, value);
            }
        }
    }
    
    if(dateArr.length != 0){
        dateArr.sort(sortStartDate);
        
        for(var i = 0; i < dateArr.length; i++) {
            var d = dateArr[i];
            var localMonthName = Sys.CultureInfo.CurrentCulture.dateTimeFormat.MonthNames[d.getMonth()];
            var value = d.getFullYear() + '/' + (d.getMonth() + 1);
            var opt = document.createElement('option');
            
            opt.value = value;
            opt.innerHTML = localMonthName + ' ' + d.getFullYear();
            sdate_select.appendChild(opt);
        }
    }

    if(programInfo != null && programInfo.StartDate != null) {
        sdate_select.value = programInfo.StartDate;
        onStartDateChange();
    }
}

function fillLengthSelect(yearMonthValue) {
    if(!length_required)
        addNoPreferenceOpt(length_select);
    
    var year = parseInt(yearMonthValue.split('/')[0]);
    var month = parseInt(yearMonthValue.split('/')[1]);
    
    for(var i =0; i < current_dest.Courses.length; i++){
        var course = current_dest.Courses[i];

        for (var j = 0; j < course.Prices.length; j++) {
            var price = course.Prices[j];
            var courseYear = price.CourseYear;
            var courseMonth = price.CourseMonth;
            
            if ((yearMonthValue == nopref_value) || (courseYear == year && courseMonth == month)) {
                course_fee_dict.add(price.CourseCode, price.Value);
                
                var opt = document.createElement('option');
                opt.value = course.Length + '/' + price.CourseCode;
                
                if (course.Destination2 == null)
                    opt.innerHTML = course.Name;
                else {
                    var dest1 = __market.Destinations.getValue(current_dest.Code);
                    var dest2 = __market.Destinations.getValue(course.Destination2);
                    opt.innerHTML = dest1 + ' / ' + dest2;
                }
                length_select.appendChild(opt);

                break;
            }
        }
    }
    
    if(programInfo != null) {
        if(programInfo.CourseLength == nopref_value){
            length_select.value = nopref_value;
            onLengthChange();
        }
        else if(programInfo.CourseLength != null && programInfo.CourseCode != null){
            length_select.value = programInfo.CourseLength + '/' + programInfo.CourseCode;
            onLengthChange();
        }
    }
}

function disableSubSelect(sender, selectArr) {
    for (var i = 0; i < selectArr.length; i++) {
        var obj = selectArr[i];
        if(obj != null) {
            var empty_opt = document.createElement('option');
            
            empty_opt.value = '';
            empty_opt.innerHTML = empty_opt_text;
            obj.innerHTML = '';
            obj.appendChild(empty_opt);
            obj.disabled = true;
            
            for (var x = 0; x < Page_Validators.length; x++) {
                var val = Page_Validators[x];
                if (val.controltovalidate == obj.id) {
                    val.isvalid = true;
                    ValidatorUpdateDisplay(val);
                    break;
                }    
            }
        }
    }
}

function addNoPreferenceOpt(select) {
    var noprefOpt = document.createElement('option');
    noprefOpt.value = nopref_value;
    noprefOpt.innerHTML = nopref_opt_text;
    select.appendChild(noprefOpt);
}