
/**
 * Boooking process - Step #1
 *
 * At this step we need to check the selected language id number against the
 * local and study abroad courses. Depending on the result, the process will
 * continue with step 2 or step 3.
 */
function lbw_bookingStep1(language_id){

    $.ajax({
        url: '/ajax/bookingstep1/language_id/'+language_id+'/lang/'+$('#lbw_lang').val(),

        beforeSend: function(){
            $('.loadingIndicator').show();
        },
        success: function(result){

            $('.loadingIndicator').hide();

            if(result == 1){
                //step 2 - local resident?
                $('#lbw_booking_local_resident').attr('disabled', false);
                $('#lbw_booking_local_resident').show('highlight');
            }else{
                //step 3 - select course
                lbw_bookingStep3();
            }
        }
    });
}

function lbw_bookingStep3(local_resident){

    if(local_resident == 1){
        lbw_selectCourseCategory();
    }else{
        if($('#lbw_lang').val() == 'en'){
            window.location.href='http://www.lsi.edu/en/book-online.php';
        }else if($('#lbw_lang').val() == 'de'){
            window.location.href='http://www.lsizh.ch/sprachaufenthalte';
        }else if($('#lbw_lang').val() == 'fr'){
            window.location.href='http://www.lsizh.ch/sejours-linguistiques';
        }else if($('#lbw_lang').val() == 'it'){
            window.location.href='http://www.lsizh.ch/soggiorni-linguistici';
        }else{
            window.location.href='http://www.lsi.edu/en/book-online.php';
        }
    }

}

function lbw_selectCourseCategory(){

        $.ajax({
            url: '/ajax/getselectcategory/',
            
                data: {
                    school_id: $('#lbw_booking_school_id').val(),
                    language_id: $('#lbw_booking_language_id').val(),
                    output: 'select',
                    lang: $('#lbw_lang').val()
                },
                beforeSend: function(){
                    $('.loadingIndicator').show();
                },
                success: function(transport){

                    $('.loadingIndicator').hide();

                    $('#lbw_category_id option').not(':first').remove();

                    $(transport).children('option').each(function(i,e){
                        if(e.value > 0){
                            $('#lbw_category_id').append(e);
                        }
                    });

                    $('#lbw_category_id').val(-1);

                    $('#lbw_category_id').attr('disabled', false);
                    $('#lbw_category_id').show('highlight');
                }
            }
        );

}

function lbw_toggleSubmit(){
    if($('#lbw_category_id').val() > 0){
        $('#lbw_submit').attr('disabled', false);
    }
}

/**
 * Show a list of schools with courses for the selected language.
 */
function lsiSelectSchool(school_id){

    $.ajaxq('lsibooking', {
        url: '/ajax/getschoolsbylanguage/',
        async: false,
            data: {
                language_id: $('#lsi_language_id').val(),
                lang: $('#lbw_lang').val(),
                sub_site: $('#sub_site').val(),
                school_id: school_id
            },
            beforeSend: function(){
                $('.loadingIndicator').show();
            },
            success: function(transport){

                $('.loadingIndicator').hide();

                $('#lsi_school_id').parent('p').html(transport);

                $('#lsi_school_id').attr('disabled', false).show('highlight');

                $('#lsi_school_id').change(function(){
                    lsiSelectCourse(this.value);
                });
            }
        }
    );
}

function lsiPriceType(){

    $.ajax({
        url: '/ajax/getlsipricetype',
        data: {
            course_id: $('#lsi_course_id').val()
        },
        success: function(price_type_id){
            return price_type_id;
        }
    });
}

function lsiSelectCourse(course_id){

    $.ajaxq('lsibooking', {
        url: '/ajax/getcoursetypes/',
        async: false,
            data: {
                school_id: $('#lsi_school_id').val(),
                language_id: $('#lsi_language_id').val(),
                lang: $('#lbw_lang').val(),
                output: 'booking',
                sub_site: $('#sub_site').val(),
                course_id: course_id
            },
            beforeSend: function(){
                $('.loadingIndicator').show();
            },
            success: function(transport){

                $('.loadingIndicator').hide();

                $('#lsi_course_id').parent('p').html(transport);

                $('#lsi_course_id').attr('disabled', false).show('highlight');

                $('#lsi_course_id').change(function(){

                    $.ajax({
                        url: '/ajax/getlsipricetype',
                        data: {
                            course_id: $('#lsi_course_id').val()
                        },
                        success: function(price_type_id){

                            if(price_type_id == 2){
                                $('#lsi_level_id').parent().hide();
                                lsiSelectStartDate();
                            }else{
                                lsiSelectLevel();
                            }
                        }
                    });
                    
                });

            }
        }
    );
}

function lsiSelectLevel(){

    /**
     * Check the selected course price type and call the levels, if it is not 2
     */

    $.ajaxq('lsibooking', {
        url: '/ajax/getlevelsbycoursetype/',

            data: {
                school_id: $('#lsi_school_id').val(),
                language_id: $('#lsi_language_id').val(),
                lang: $('#lbw_lang').val(),
                course_id: $('#lsi_course_id').val(),
                local_resident: 0,
                output: 'booking'
            },
            beforeSend: function(){
                $('.loadingIndicator').show();
            },
            success: function(transport){

                $('.loadingIndicator').hide();

                $('#lsi_level_id').parent('p').html(transport);

                $('#lsi_level_id').attr('disabled', false).show('highlight');

                $('#lsi_level_id').change(function(){
                    lsiSelectStartDate();
                });
            }
        }
    );

}

function lsiSelectStartDate(){

    $.ajax({
        url: '/ajax/getlsistartdates/',

            data: {
                school_id: $('#lsi_school_id').val(),
                language_id: $('#lsi_language_id').val(),
                lang: $('#lbw_lang').val(),
                course_id: $('#lsi_course_id').val(),
                level_id: $('#lsi_level_id').val()
            },
            beforeSend: function(){
                $('.loadingIndicator').show();
            },
            success: function(transport){

                $('.loadingIndicator').hide();

                $('#lsi_start_date').parent('p').html(transport);

                $('#lsi_start_date').attr('disabled', false).show('highlight');

                $('#lsi_start_date').change(function(){
                    lsiSelectDuration();
                });
                
            }
        }
    );

}

function lsiSelectDuration(){

    $.ajax({
        url: '/ajax/getlsicourselength/',

            data: {
                school_id: $('#lsi_school_id').val(),
                language_id: $('#lsi_language_id').val(),
                lang: $('#lbw_lang').val(),
                course_id: $('#lsi_course_id').val(),
                level_id: $('#lsi_level_id').val(),
                start_date: $('#lsi_start_date').val(),
                sub_site: $('#sub_site').val()
            },
            beforeSend: function(){
                $('.loadingIndicator').show();
            },
            success: function(transport){

                $('.loadingIndicator').hide();

                $('.loadingIndicator').hide();

                $('#lsi_duration').parent('p').html(transport);

                $('#lsi_duration').attr('disabled', false).show('highlight');

                $('#lsi_duration').change(function(){
                    $('#lsi_submit').attr('disabled', false);
                });
                
            }
        }
    );

}
