var indexes = new Array();

indexes['job'] = new Array();
indexes['study'] = new Array();  
indexes['sport'] = new Array(); 

indexes['job'][1] = [1.0, 0.6];  
indexes['job'][2] = [1.2, 0.8]; 
indexes['job'][3] = [1.5, 1.0];
indexes['job']['coefficient'] = [0.3, 0.4, 0.5, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0];  

indexes['study']['sum'] = [2000, 3000, 5000];
indexes['study'][1] = [1.2, 1.5];
indexes['study'][2] = [1.0, 1.2];  
indexes['study'][3] = [0.8, 1.0]; 

indexes['sport'][1] = [0.57, 0.76, 0.95, 1.14, 1.24, 1.33, 1.43, 1.52, 1.62, 1.71, 1.8, 1.9]; 
indexes['sport'][2] = [0.9, 1.2, 1.5, 1.8, 1.95, 2.1, 2.25, 2.4, 2.55, 2.7, 2.85, 3.0]; 
indexes['sport'][3] = [1.65, 2.2, 2.75, 3.3, 3.58, 3.85, 4.13, 4.4, 4.68, 4.95, 5.25, 5.5]; 
indexes['sport'][4] = [3.81, 5.08, 6.35, 7.62, 8.25, 8.9, 9.55, 10.15, 10.80, 11.45, 12.07, 12.7];    

function change_scheme() {
    var insurance_scheme        = document.getElementById('insurance_scheme');
    var job_category_block      = document.getElementById('job_category_block'); 
    var sport_category_block    = document.getElementById('sport_category_block'); 
    var insured_person_block    = document.getElementById('insured_person_block'); 
    var sum_insured_text_block  = document.getElementById('sum_insured_text_block'); 
    var sum_insured_block       = document.getElementById('sum_insured_block');  
    var period_block            = document.getElementById('period_block');
    var sum_insured_text        = document.getElementById('sum_insured_text');   
     
    if (insurance_scheme.value == 1) {
        job_category_block.style.display        = '';
        sport_category_block.style.display      = 'none'; 
        insured_person_block.style.display      = 'none'; 
        sum_insured_text_block.style.display    = ''; 
        sum_insured_block.style.display         = 'none'; 
        period_block.style.display              = ''; 
        period_build(1); 
        sum_insured_text.onmouseover            = function () {Tip('Страхова сума визначається в межах від 1 до 30 тис. грн.', OPACITY, 70);} 
    } 
    else if (insurance_scheme.value == 2) {
        job_category_block.style.display        = '';
        sport_category_block.style.display      = 'none'; 
        insured_person_block.style.display      = 'none'; 
        sum_insured_text_block.style.display    = ''; 
        sum_insured_block.style.display         = 'none'; 
        period_block.style.display              = ''; 
        period_build(1);
        sum_insured_text.onmouseover            = function () {Tip('Страхова сума визначається в межах від 1 до 30 тис. грн.', OPACITY, 70);}   
    }
    else if (insurance_scheme.value == 3) {
        job_category_block.style.display        = 'none';
        sport_category_block.style.display      = 'none'; 
        insured_person_block.style.display      = ''; 
        sum_insured_text_block.style.display    = 'none'; 
        sum_insured_block.style.display         = ''; 
        period_block.style.display              = '';
        period_build(2); 
    }
    else if (insurance_scheme.value == 4) {
        job_category_block.style.display        = 'none';
        sport_category_block.style.display      = ''; 
        insured_person_block.style.display      = 'none'; 
        sum_insured_text_block.style.display    = ''; 
        sum_insured_block.style.display         = 'none'; 
        period_block.style.display              = '';
        period_build(1);
        sum_insured_text.onmouseover            = function () {Tip('Страхова сума визначається в межах від 1 до 30 тис. грн.', OPACITY, 70);}  
    }
    else if (insurance_scheme.value == 5) {
        job_category_block.style.display        = 'none';
        sport_category_block.style.display      = 'none'; 
        insured_person_block.style.display      = 'none'; 
        sum_insured_text_block.style.display    = ''; 
        sum_insured_block.style.display         = 'none'; 
        period_block.style.display              = ''; 
        period_build(1);
        sum_insured_text.onmouseover            = function () {Tip('Страхова сума визначається в розмірі повної вартості навчання за весь період', OPACITY, 70);}
    }
    else {
        job_category_block.style.display        = 'none';
        sport_category_block.style.display      = 'none'; 
        insured_person_block.style.display      = 'none'; 
        sum_insured_text_block.style.display    = 'none'; 
        sum_insured_block.style.display         = 'none'; 
        period_block.style.display              = 'none';     
    }
}

function period_build(type) {
    var period = document.getElementById('period');
    if (type == 1) {
        period.options[0] = new Option('обрати...', '');
        period.options[1] = new Option('1 місяць', '1');
        period.options[2] = new Option('2 місяці', '2'); 
        period.options[3] = new Option('3 місяці', '3'); 
        period.options[4] = new Option('4 місяці', '4'); 
        period.options[5] = new Option('5 місяців', '5'); 
        period.options[6] = new Option('6 місяців', '6'); 
        period.options[7] = new Option('7 місяців', '7'); 
        period.options[8] = new Option('8 місяців', '8'); 
        period.options[9] = new Option('9 місяців', '9'); 
        period.options[10] = new Option('10 місяців', '10'); 
        period.options[11] = new Option('11 місяців', '11'); 
        period.options[12] = new Option('12 місяців', '12'); 
    }
    else if (type == 2) {
        while (period.options.length) {
            period.options[0] = null;
        }
        period.options[0] = new Option('обрати...', ''); 
        period.options[1] = new Option('1 вересня – 31 травня', '1');
        period.options[2] = new Option('повний рік', '2'); 
    }
    else {
        while (period.options.length) {
            period.options[0] = null;
        }
    }    
}

function result() {
    var insurance_scheme    = document.getElementById('insurance_scheme');    
    var job_category        = document.getElementById('job_category'); 
    var sport_category      = document.getElementById('sport_category');   
    var insured_person      = document.getElementById('insured_person');   
    var sum_insured_text    = document.getElementById('sum_insured_text');   
    var sum_insured         = document.getElementById('sum_insured'); 
    var period              = document.getElementById('period');   
     
    var add_order           = document.getElementById('add_order'); 
    var res_block           = document.getElementById('res_block');    
    var res_text            = document.getElementById('res_text'); 
    
    var sum                 = 0;   
    var tariff              = 0;   
    var res                 = 0;
    if (insurance_scheme.value == 1) {
        if (job_category.value == '') {
            window.alert('Ви не вибрали категорію професії!');
            return false;
        }
        if (sum_insured_text.value == '') {
            window.alert('Ви не вибрали страхову суму!');
            return false;
        }
        else if (isNaN(sum_insured_text.value)) {
            window.alert('Ви ввели некоректне значення страхової суми!');
            return false;
        }
        else if ((Number(sum_insured_text.value) < 1000) || (Number(sum_insured_text.value) > 30000)) {
            window.alert('Страхова сума визначається в межах від 1 до 30 тис. грн.!');
            return false;
        }
        else {
            sum = sum_insured_text.value;
        }
        if (period.value == '') {
            window.alert('Ви не вибрали період страхування!');
            return false;
        }
        tariff = indexes['job'][job_category.value][0]; 
        tariff = tariff * indexes['job']['coefficient'][period.value - 1];  
    } 
    else if (insurance_scheme.value == 2) {
        if (job_category.value == '') {
            window.alert('Ви не вибрали категорію професії!');
            return false;
        }
        if (sum_insured_text.value == '') {
            window.alert('Ви не вибрали страхову суму!');
            return false;
        }
        else if (isNaN(sum_insured_text.value)) {
            window.alert('Ви ввели некоректне значення страхової суми!');
            return false;
        }
        else if ((Number(sum_insured_text.value) < 1000) || (Number(sum_insured_text.value) > 30000)) {
            window.alert('Страхова сума визначається в межах від 1 до 30 тис. грн.!');
            return false;
        }
        else {
            sum = sum_insured_text.value;
        }
        if (period.value == '') {
            window.alert('Ви не вибрали період страхування!');
            return false;
        }
        tariff = indexes['job'][job_category.value][1]; 
        tariff = tariff * indexes['job']['coefficient'][period.value - 1];
    }
    else if (insurance_scheme.value == 3) {
        if (insured_person.value == '') {
            window.alert('Ви не вибрали застраховану особу!');
            return false;
        }
        if (sum_insured.value == '') {
            window.alert('Ви не вибрали страхову суму!');
            return false;
        }
        else {
            sum = indexes['study']['sum'][sum_insured.value - 1];    
        }
        if (period.value == '') {
            window.alert('Ви не вибрали період страхування!');
            return false;
        }
        tariff = indexes['study'][insured_person.value][period.value - 1]; 
    }
    else if (insurance_scheme.value == 4) {
        if (sport_category.value == '') {
            window.alert('Ви не вибрали групу видів спорту!');
            return false;
        }
        if (sum_insured_text.value == '') {
            window.alert('Ви не вибрали страхову суму!');
            return false;
        }
        else if (isNaN(sum_insured_text.value)) {
            window.alert('Ви ввели некоректне значення страхової суми!');
            return false;
        }
        else if ((Number(sum_insured_text.value) < 1000) || (Number(sum_insured_text.value) > 30000)) {
            window.alert('Страхова сума визначається в межах від 1 до 30 тис. грн.!');
            return false;
        }
        else {
            sum = sum_insured_text.value;
        }
        if (period.value == '') {
            window.alert('Ви не вибрали період страхування!');
            return false;
        }
        tariff = indexes['sport'][sport_category.value][period.value - 1];  
    }
    else if (insurance_scheme.value == 5) {
        if (sum_insured_text.value == '') {
            window.alert('Ви не вибрали страхову суму!');
            return false;
        }
        else if (isNaN(sum_insured_text.value)) {
            window.alert('Ви ввели некоректне значення страхової суми!');
            return false;
        }
        else {
            sum     = sum_insured_text.value;
            tariff  = 1.5;
        }
        if (period.value == '') {
            window.alert('Ви не вибрали період страхування!');
            return false;
        } 
    }
    else {
        window.alert('Ви не вибрали програму страхування!');
        return false;  
    } 
    res = (Number(sum) / 100) * Number(tariff);  
    res = Math.round(Number(res) * 100) / 100;
    res_block.style.display = ''; 
    res_text.innerHTML = "<input type=\"hidden\" name=\"cost\" value=\"" + res + "\">\nВартість полісу: " + res + " грн";    
    add_order.onclick = function() {                 
        var insurance_type = document.getElementById('insurance_type');
        document.getElementById('message_ok').style.display = 'none'; 
        document.getElementById('i_order').style.display = 'block'; 
        document.getElementById('i_type_sel').value = '5';  
        insurance_type.disabled = true; 
        insurance_type.value = '5';
        return false;
    }
    document.getElementById('submit_calc').value = 'yes';
}  
