// ############################################################
// ############ global variables holding values from fields
var valGrade = '';
var valStep = 0;
var valExchangeRate = 1;
var valPostAdjustment = 0;
var valThresholdPercentage = 0;
var valCompletedYears = 0;
var valHousehold = 0;
var valMonthlyNetSalary = 0;
var valPostAdjustmentCalculated = 0;
var valTotalrentalSubsidy = 0;
var valThresholdAmountCalculated = 0;
var valMonthlyRent = 0;
var valMonthlyRentConverted = 0;
var valElectricity = 0;
var valBedrooms = 0;
var valElectricityValue = 0;
var valReasonableMaximum = 0;
var valReasonableMaximumCalculated = 0;
var valPurposesOfSubsidyCalculation = 0;
var valLessIndividualThresholdAmount = 0;
var valDecliningPercentage = 80;
var valDecliningPercentageValue = 0;
var valRentForPurposeOfCalculation = 0;
var valRentalSubsidyFinal = 0;
var valYourPortion = 0;
var valDividedMaxRent = 0;
var valAgentsFeePaid = 0;
var valAgentsFeePaidCalculated = 0;
var valAgentsFeeMultiply = 0;
var valExcessFee = 0;
var valReimbursement = 0;
var blnUseReasonableMaximum = false;


// added 28-11
var blnIsDepandentRate = false;
var valDutyStaion = '';
var valDutyStationText = '';
var blnThresHoldManually = false;
// ############################################################

function init() {
    var objSelect;
    // get option for grade select start
    objSelect = document.forms[0].fmGrade;
    for (i = 0; i < Grades.length; i++) {
        objSelect.options[objSelect.options.length] = new Option(Grades[i].Name, Grades[i].Name);
    }
    // get option for grade select end

    // get option for dutyStation select start
    objSelect = document.forms[0].fmDutyStaion;
    for (i = 0; i < DutyStations.length; i++) {
        // change 28-11
        objSelect.options[objSelect.options.length] = new Option(DutyStations[i].Country + ', ' + DutyStations[i].City, DutyStations[i].Country);
        // original
        //			objSelect.options[objSelect.options.length] = new Option(DutyStations[i].Name,DutyStations[i].Name);
    }
    // get option for dutyStation select end

    // hide hidden fields
    document.getElementById('divCompletedYears').style.display = 'none';
    document.getElementById('divHouseHold').style.display = 'none';
    document.getElementById('divReasonableMaximumRentLevel').style.display = 'none';

    //document.onkeyup = reCalc;
}

function setStepsFromGrade() {
    // reset monthly salery
    document.forms[0].fmMonthlyNetSalary.value = 0
    valMonthlyNetSalary = 0;
    var objSelect = document.forms[0].fmStep
    var objGradeSelect = document.forms[0].fmGrade;
    var intSelectedIndex = objGradeSelect.selectedIndex;
    valGrade = objGradeSelect.options[intSelectedIndex].value;
    if (intSelectedIndex > 0) {
        document.forms[0].fmStep.disabled = false;
    }
    else {
        document.forms[0].fmStep.disabled = true;
    }
    // if select has options delete all
    if (objSelect.options.length > 1) {
        for (var i = objSelect.options.length; i >= 1; i--) {
            objSelect.options[i] = null;
        }
    }
    // get levels for selected grade
    tmp = Grades[intSelectedIndex - 1]
    for (var tm in tmp) {
        if (!isNaN(tmp[tm]))
            if (tmp[tm] > 0) {
            objSelect.options[objSelect.options.length] = new Option(tm.substring(1), tmp[tm])
        }
    }
}

function setMonthlyNetSalary() {
    var objSelect = document.forms[0].fmStep;
    valStep = objSelect.options[objSelect.selectedIndex].value;
    valStep = valStep / 12
    valMonthlyNetSalary = valStep
    //alert(valStep)
}

function setCompletedYears() {
    var objSelect = document.forms[0].fmDutyStaion;
    var strCountryOffice = objSelect[objSelect.selectedIndex].value;
    valDutyStaion = strCountryOffice;
    valDutyStationText = objSelect[objSelect.selectedIndex].text;
    for (var i = 0; i < DecliningPercentnages.length; i++) {
        if (DecliningPercentnages[i].DutyStation == strCountryOffice) {
            document.getElementById('divCompletedYears').style.display = 'block';
            /*if(DecliningPercentnages[i].DutyStation == 'US'){
            document.getElementById('divHouseHold').style.display = 'block';
            document.getElementById('divReasonableMaximumRentLevel').style.display = 'block';
            blnUseReasonableMaximum = true;
            }*/
            break;
        }
        else {
            document.getElementById('divCompletedYears').style.display = 'none';
            /*document.getElementById('divHouseHold').style.display = 'none';
            document.getElementById('divReasonableMaximumRentLevel').style.display = 'none';
            blnUseReasonableMaximum = false;*/
        }
    }
}

function getReasonableMaximumRentLevel() {
    var objSelect = document.forms[0].fmDutyStaion;
    var strCountryOffice = objSelect[objSelect.selectedIndex].value;
    valDutyStaion = strCountryOffice;
    valDutyStationText = objSelect[objSelect.selectedIndex].text;
    for (var i = 0; i < MaxRents.length; i++) {
        if (MaxRents[i].DutyStation == valDutyStationText) {
            document.getElementById('divHouseHold').style.display = 'block';
            document.getElementById('divReasonableMaximumRentLevel').style.display = 'block';
            blnUseReasonableMaximum = true;
            break;
        }
        else {
            document.getElementById('divHouseHold').style.display = 'none';
            document.getElementById('divReasonableMaximumRentLevel').style.display = 'none';
            blnUseReasonableMaximum = false;
        }
    }
}

function setOptionsReasonableMaximumRentLevel() {
    var objSelect = document.forms[0].fmDutyStaion
    var strCountryOffice = objSelect.options[objSelect.selectedIndex].text;
    var objHouseHoldSelect = document.forms[0].fmHousehold
    // if select has options delete all, except first one
    if (objHouseHoldSelect.options.length > 1) {
        for (var i = objHouseHoldSelect.options.length; i >= 1; i--) {
            objHouseHoldSelect.options[i] = null;
        }
    }

    // make options for selected dutystation	
    if (objSelect.selectedIndex > 0) {
        for (var i = 0; i < MaxRents.length; i++) {
            if (MaxRents[i].DutyStation == strCountryOffice) { // && MaxRents[i].DependantRate == blnIsDepandentRate
                objHouseHoldSelect.options[objHouseHoldSelect.options.length] = new Option(MaxRents[i].Household, MaxRents[i].Household)
            }
        }
    }
}

function setReasonableMaximumRentLevel() {
    var objSelect = document.forms[0].fmDutyStaion
    var strCountryOffice = objSelect.options[objSelect.selectedIndex].text;
    var objSelectHouseHold = document.forms[0].fmHousehold
    var strHouseHold
    if (objSelect.selectedIndex > 0) {
        strHouseHold = objSelectHouseHold.options[objSelectHouseHold.selectedIndex].value
        //alert(strCountryOffice +' - ' + strHouseHold)
        for (var i = 0; i < MaxRents.length; i++) {
            if (MaxRents[i].DutyStation == strCountryOffice && MaxRents[i].Household == strHouseHold) {
                valReasonableMaximum = MaxRents[i].NonFurnished
                //alert(valReasonableMaximum)
            }
        }
    }
}

function setBedrooms(blnDisplay) {
    if (blnDisplay) {
        document.getElementById('divBedrooms').style.display = 'inline';
    }
    else {
        document.getElementById('divBedrooms').style.display = 'none';
        document.forms[0].fmBedrooms.selectedIndex = 0;
        document.forms[0].fmElectricityValue.value = '0.00';
        valElectricityValue = 0;
    }
}

function setfmDecliningPercentnageValue() {
    var objSelect = document.forms[0].fmCompletedYears;
    var strCountryOffice = document.forms[0].fmDutyStaion.options[document.forms[0].fmDutyStaion.selectedIndex].value;

    if (objSelect.selectedIndex > 0) {
        for (var i = 0; i < DecliningPercentnages.length; i++) {
            if (DecliningPercentnages[i].DutyStation == strCountryOffice) {
                document.getElementById('spanDeclining').innerText = eval('DecliningPercentnages[i].Year' + objSelect.selectedIndex)
                document.forms[0].fmDecliningPercentnageValue.value = eval('DecliningPercentnages[i].Year' + objSelect.selectedIndex)
                valDecliningPercentage = eval('DecliningPercentnages[i].Year' + objSelect.selectedIndex);
                document.getElementById('spanDeclining').innerHTML = valDecliningPercentage + '';
                document.forms[0].fmDecliningPercentnageValue.value = valDecliningPercentage;
                //alert(valDecliningPercentage)
                break;
            }
            else {
                document.getElementById('spanDeclining').innerHTML = "80"
                document.forms[0].fmDecliningPercentnageValue.value = 80;
                valDecliningPercentage = 80;
            }
        }
    }
    else {
        document.getElementById('spanDeclining').innerHTML = "80"
        document.forms[0].fmDecliningPercentnageValue.value = 80;
        valDecliningPercentage = 80;
    }
}

function calculatePostAdjustment() {
    if (document.forms[0].fmMonthlyNetSalary.value != '' && document.forms[0].fmPostAdjustment.value != '') {
        var salary = document.forms[0].fmMonthlyNetSalary.value;
        var postAjustment = document.forms[0].fmPostAdjustment.value;
        var postAjustmentCalculated = get2DigetRounded(postAjustment * salary / 100)

        document.forms[0].fmPostAdjustmentCalculated.value = postAjustmentCalculated;
        document.forms[0].fmTotalrentalSubsidy.value = get2DigetRounded((parseFloat(salary) + parseFloat(postAjustmentCalculated)));
    }
}
function setElectricityValue() {
    var objSelect = document.forms[0].fmBedrooms
    if (objSelect.selectedIndex > 0) {
        valElectricityValue = objSelect.options[objSelect.selectedIndex].value
    }
    else {
        valElectricityValue = 0;
    }
}

function getTotalrentalSubsidy() {
    var salary = document.forms[0].fmMonthlyNetSalary.value - 0;
    var postAjustment = document.forms[0].fmPostAdjustmentCalculated.value - 0;
    document.forms[0].fmTotalrentalSubsidy.value = salary + postAjustment
}

function getMonthlyRentConverted(objInput) {
    var ExchangeRate
    if (document.forms[0].fmExchangeRate.value != '') {
        ExchangeRate = new Number(parseFloat(valExchangeRate));
    }
    else {
        ExchangeRate = new Number(1);
    }

    var MonthlyRentConverted = parseFloat(document.forms[0].fmMonthlyRent.value) / ExchangeRate;
    document.forms[0].fmMonthlyRentConverted.value = get2DigetRounded(MonthlyRentConverted)
}

function getThresholdAmountCalculated() {
    alert(valTotalrentalSubsidy)
}

function setExchangeRate() {
    var objInput = document.forms[0].fmExchangeRate;

    //objInput.value = testInputValue(objInput.value,objInput);		

    if (objInput.value != '' && objInput.value > 0) {
        valExchangeRate = objInput.value;
    }
    else {
        valExchangeRate = 1;
    }
}
function setPostAdjustment() {
    var objInput = document.forms[0].fmPostAdjustment;

    objInput.value = testInputValue(objInput.value, objInput);

    if (objInput.value != '' && objInput.value > 0) {
        valPostAdjustment = objInput.value;
    }
    else {
        valPostAdjustment = 0;
    }
}

function setThresholdPercentage() {
    //	alert('function setThresholdPercentage')
    var objInput = document.forms[0].fmThresholdPercentage;

    objInput.value = testInputValue(objInput.value, objInput);

    if (objInput.value != '' && objInput.value > 0) {
        valThresholdPercentage = objInput.value;
    }
    else {
        valThresholdPercentage = 0;
    }
}

function setMonthlyRent() {
    var objInput = document.forms[0].fmMonthlyRent;

    objInput.value = testInputValue(objInput.value, objInput);

    if (objInput.value != '' && objInput.value > 0) {
        valMonthlyRent = objInput.value;
    }
    else {
        valMonthlyRent = 0;
    }
}

function setAgentsFeePaid() {
    var objInput = document.forms[0].fmAgentsFeePaid;

    objInput.value = testInputValue(objInput.value, objInput);

    if (objInput.value != '' && objInput.value > 0) {
        valAgentsFeePaid = objInput.value;
    }
    else {
        valAgentsFeePaid = 0;
    }
}
// added 28-11 start
function setPostadjustmentValue(intVal) {
    intVal--;
    document.forms[0].fmPostAdjustment.value = DutyStations[intVal].PostAdjustMultiplier;
    valPostAdjustment = DutyStations[intVal].PostAdjustMultiplier;
}

function getDutyStationThresHold() {
    var fltThresHold = 0
    var blnFoundThreshold = false;
    if (valGrade != '') {
        if (valGrade.indexOf('Single') != -1) {
            blnIsDepandentRate = false;
        }
        else {
            blnIsDepandentRate = true;
        }
        if (valDutyStationText != '') {
            for (var i = 0; i < DutyStationsThresholds.length; i++) {
                if (valDutyStationText == (DutyStationsThresholds[i].Country + ', ' + DutyStationsThresholds[i].City)) {
                    if (blnIsDepandentRate) {
                        if (!blnThresHoldManually) {
                            document.forms[0].fmThresholdPercentage.value = DutyStationsThresholds[i].RentalThresholdDep;
                            valThresholdPercentage = DutyStationsThresholds[i].RentalThresholdDep;
                        }
                    }
                    else {
                        if (!blnThresHoldManually) {
                            document.forms[0].fmThresholdPercentage.value = DutyStationsThresholds[i].RentalThresholdSingle;
                            valThresholdPercentage = DutyStationsThresholds[i].RentalThresholdSingle;
                        }
                    }
                    blnFoundThreshold = true;
                    return;
                }
            }
            // we didn't find ds via country and city, så we try to find it by country
            if (!blnFoundThreshold) {
                for (var i = 0; i < DutyStationsThresholds.length; i++) {
                    if (valDutyStaion == (DutyStationsThresholds[i].Country)) {
                        if (blnIsDepandentRate) {
                            if (fltThresHold != 0) {
                                if (fltThresHold > DutyStationsThresholds[i].RentalThresholdDep) {
                                    fltThresHold = DutyStationsThresholds[i].RentalThresholdDep;
                                }
                            }
                            else {
                                fltThresHold = DutyStationsThresholds[i].RentalThresholdDep;
                            }
                        }
                        else {
                            fltThresHold = DutyStationsThresholds[i].RentalThresholdSingle;

                        }
                        if (!blnThresHoldManually) {
                            document.forms[0].fmThresholdPercentage.value = fltThresHold;
                            valThresholdPercentage = fltThresHold;
                        }
                    }
                }
            }
        }
        //fmThresholdPercentage
    }
}

// added 28-11 end

function reCalc() {
    // get current values, must be done first
    //getAllValues()

    getDutyStationThresHold();

    getPostAdjustmentCalculated();
    getTotalrentalSubsidy();
    getThresholdAmountCalculated();
    getMonthlyRentConverted();
    getReasonableMaximumCalculated();
    getPurposesOfSubsidyCalculation();
    getLessIndividualThresholdAmount();
    getDecliningPercentnageValue();
    getRentForPurposeOfCalculation();
    getRentalSubsidyFinal();
    getYourPortion();

    // set current values, must be done last
    setAllValues()
}


//##################################################################
//################# helpers start ##################################
function get2DigetRounded(fltNbr) {
    var tmp = Math.round(fltNbr * 100) / 100;
    switch (tmp.toString().indexOf('.')) {
        case -1:
            return tmp + '.00'
            break;
        case tmp.toString().length - 2:
            return tmp + '0'
            break;
        default:
            return tmp
            break;
    }

}
function testInputValue(strValue, objInput) {
    if (isNaN(strValue)) {
        alert('You have not entered a valid value!!\nPlease reenter the correct value.')
        objInput.value = 0;
        objInput.focus();
        objInput.select();
        return 0;
    }
    else
        return strValue
}

function getFieldValue(objFieldName, objvar) {
    if (document.forms[0].elements[objFieldName].type == 'text') {
        objvar = document.forms[0].elements[objFieldName].value;
    } else if (document.forms[0].elements[objFieldName].type == 'select-one') {
        objvar = document.forms[0].elements[objFieldName].options[document.forms[0].elements[objFieldName].selectedIndex].value
    }
    else {
        objvar = 0
    }
    return objvar
    if (objvar != '') {
        //alert(objvar)
        //alert(document.forms[0].elements[objFieldName].type)
    }
}

function setFieldValue(objFieldName, value) {
    if (document.forms[0].elements[objFieldName].type == 'text') {
        if (value != undefined) {
            document.forms[0].elements[objFieldName].value = value;
        }
    }
}

function setElmDisplay(objElm, strDisplay) {
    objElm.style.display = strDisplay;
}

//################# helpers end ####################################
//##################################################################


//##################################################################
//##################################################################
function Grade_onchange() {
    var objGradeSelect = document.forms[0].fmGrade;
    var intSelectedIndex = objGradeSelect.selectedIndex;
    valGrade = objGradeSelect.options[intSelectedIndex].value;
    if (valGrade.indexOf('Single') != -1) {
        blnIsDepandentRate = false;
    }
    else {
        blnIsDepandentRate = true;
    }
    setStepsFromGrade();
    setOptionsReasonableMaximumRentLevel();
    valMonthlyNetSalary = 0;
    valPostAdjustmentCalculated = 0;
    valThresholdAmountCalculated = 0;
    valTotalrentalSubsidy = 0;
    valReasonableMaximum = 0;

    reCalc();
}
function Step_onchange() {
    setMonthlyNetSalary();
    reCalc();
}
function DutyStaion_onchange() {
    //	document.forms[0].fmCompletedYears.selectedIndex = 0;
    blnThresHoldManually = false;
    setCompletedYears();
    setPostadjustmentValue(document.forms[0].fmDutyStaion.selectedIndex);
    getReasonableMaximumRentLevel();
    setOptionsReasonableMaximumRentLevel();
    reCalc();
}
function CompletedYears_onchange() {
    setfmDecliningPercentnageValue();
    reCalc();
}
function ExchangeRate_onchange() {
    setExchangeRate();
    // agents fee start
    setAgentsFeePaid();
    DividedMaxRent();
    AgentsFeePaidCalculated();
    AgentsFeeMultiply();
    ExcessFee();
    Reimbursement();
    // agents fee end
    reCalc();
}
function MonthlyRent_onchange() {
    setMonthlyRent();
    reCalc();
}
function PostAdjustment_onchange() {
    setPostAdjustment();
    reCalc();
}

function ThresholdPercentage_onchange() {
    //alert(document.forms[0].fmThresholdPercentage.value + blnThresHoldManually)
    blnThresHoldManually = true;
    valThresholdPercentage = document.forms[0].fmThresholdPercentage.value;
    setThresholdPercentage();
    reCalc();
}

function Bedrooms_onchange() {
    setElectricityValue();
    reCalc();
}

function Household_onchange() {
    setReasonableMaximumRentLevel();
    reCalc();
}
function AgentsFeePaid_onchange() {
    setAgentsFeePaid();
    DividedMaxRent();
    AgentsFeePaidCalculated();
    AgentsFeeMultiply();
    ExcessFee();
    Reimbursement();
    reCalc();
}

function fmElectricity_onclick(blnDisplay) {
    setBedrooms(blnDisplay);
    reCalc();
}
//##################################################################
//##################################################################


//##################################################################
//################# calculate values start #########################
//valPostAdjustmentCalculated			valMonthlyNetSalary * valPostAdjustment / 100
function getPostAdjustmentCalculated() {
    if (valMonthlyNetSalary > 0 && valPostAdjustment > 0) {
        valPostAdjustmentCalculated = valMonthlyNetSalary * valPostAdjustment / 100;
    }
}

//valTotalrentalSubsidy				valMonthlyNetSalary + valPostAdjustmentCalculated
function getTotalrentalSubsidy() {
    if (valMonthlyNetSalary > 0 && valPostAdjustmentCalculated > 0) {
        valTotalrentalSubsidy = (valMonthlyNetSalary - 0) + valPostAdjustmentCalculated;
    }
}

//valThresholdAmountCalculated		valThresholdPercentage * valTotalrentalSubsidy
function getThresholdAmountCalculated() {
    if (valThresholdPercentage > 0 && valTotalrentalSubsidy > 0) {
        valThresholdAmountCalculated = valThresholdPercentage * valTotalrentalSubsidy / 100;
    }
}

//valMonthlyRentConverted				valMonthlyRent/valExchangeRate
function getMonthlyRentConverted() {
    if (valMonthlyRent > 0 && valExchangeRate > 0) {
        valMonthlyRentConverted = valMonthlyRent / valExchangeRate;
    }
}

// valReasonableMaximumCalculated		valReasonableMaximum - hvis USG eller ASG + 20% - hvis D1 eller D2 + 10%
function getReasonableMaximumCalculated() {

    /***********************/
    /******** 05-09 *******/
    var objSelect = document.forms[0].fmDutyStaion
    var strCountryOffice = objSelect.options[objSelect.selectedIndex].text;
    //alert(strCountryOffice)

    var tmpExtRate = valExchangeRate;
    if (strCountryOffice.indexOf('Slovakia') != -1) {
        tmpExtRate = 1;
    }
    /******** 05-09 *******/
    /***********************/
    if (valReasonableMaximum > 0 && valGrade != '') {
        if (valGrade.indexOf('USG') != -1 || valGrade.indexOf('ASG') != -1) {
            //05-09
            valReasonableMaximumCalculated = valReasonableMaximum * 1.2 / tmpExtRate;
            //original
            //valReasonableMaximumCalculated = valReasonableMaximum * 1.2 / valExchangeRate;
        }
        else if (valGrade.indexOf('D1') != -1 || valGrade.indexOf('D2') != -1) {
            //05-09
            valReasonableMaximumCalculated = valReasonableMaximum * 1.1 / tmpExtRate;
            //original
            //valReasonableMaximumCalculated = valReasonableMaximum * 1.1 / valExchangeRate;
        }
        else {
            //05-09
            valReasonableMaximumCalculated = valReasonableMaximum / tmpExtRate;
            //original
            //valReasonableMaximumCalculated = valReasonableMaximum / valExchangeRate;
        }
    }
    else {
        valReasonableMaximumCalculated = 0;
    }

}

//valPurposesOfSubsidyCalculation		hvis (valMonthlyRentConverted-valElectricityValue < valReasonableMaximumCalculated ) så valMonthlyRentConverted-valElectricityValue ellers valReasonableMaximumCalculated
function getPurposesOfSubsidyCalculation() {
    //alert(valMonthlyRentConverted + ' - ' + valElectricityValue + ' - '+ valReasonableMaximumCalculated)

    valMonthlyRentConverted = valMonthlyRentConverted - 0;
    valElectricityValue = valElectricityValue - 0;
    valReasonableMaximumCalculated = valReasonableMaximumCalculated - 0;

    if (valMonthlyRentConverted - valElectricityValue > 0 && valReasonableMaximumCalculated > 0) {
        //alert('trace 1')
        if (valMonthlyRentConverted - valElectricityValue < valReasonableMaximumCalculated) {
            valPurposesOfSubsidyCalculation = valMonthlyRentConverted - valElectricityValue;
        }
        else {
            //½alert('trace 2')
            valPurposesOfSubsidyCalculation = valReasonableMaximumCalculated;
        }
    }
    else {
        valPurposesOfSubsidyCalculation = valMonthlyRentConverted - valElectricityValue;
    }
}

//valLessIndividualThresholdAmount	valPurposesOfSubsidyCalculation - valThresholdAmountCalculated 
function getLessIndividualThresholdAmount() {
    if (valPurposesOfSubsidyCalculation != 0 && valThresholdAmountCalculated != 0) {
        valLessIndividualThresholdAmount = valPurposesOfSubsidyCalculation - valThresholdAmountCalculated;
    }
}

//valDecliningPercentageValue		hvis US eller EU så value fra valCompletedYears ellers 80%
function getDecliningPercentnageValue() {
    if (valLessIndividualThresholdAmount != 0) {
        valDecliningPercentageValue = valLessIndividualThresholdAmount * (valDecliningPercentage / 100);
    }
}

//valRentForPurposeOfCalculation		valPurposesOfSubsidyCalculation * 0,4
function getRentForPurposeOfCalculation() {
    if (valPurposesOfSubsidyCalculation != 0) {
        valRentForPurposeOfCalculation = valPurposesOfSubsidyCalculation * 0.4;
    }
}

//valRentalSubsidyFinal				hvis valDecliningPercentageValue < valRentForPurposeOfCalculation så valDecliningPercentageValue ellers valRentForPurposeOfCalculation
function getRentalSubsidyFinal() {

    /***********************/
    /******** 05-09 ********/
    var objSelect = document.forms[0].fmDutyStaion
    var selIndex = objSelect.selectedIndex;
    var isW40 = false;
    if (selIndex > 0) {
        isW40 = DutyStations[selIndex - 1].IsW40;
    }
    /******** 05-09 ********/
    /***********************/

    if (valDecliningPercentageValue != 0 && valRentForPurposeOfCalculation != 0) {
        if (valDecliningPercentageValue < valRentForPurposeOfCalculation) {
            valRentalSubsidyFinal = valDecliningPercentageValue;
        }
        else {
            valRentalSubsidyFinal = valRentForPurposeOfCalculation;
        }
        /***********************/
        /******** 05-09 ********/
        if (isW40) {
            valRentalSubsidyFinal = valDecliningPercentageValue;
        }
        /******** 05-09 ********/
        /***********************/

        if (valRentalSubsidyFinal < 0) {
            valRentalSubsidyFinal = 0;
        }
    }
    else {
        valRentalSubsidyFinal = 0;
    }
    //alert(valRentalSubsidyFinal)
}

//valYourPortion						valMonthlyRent - (valRentalSubsidyFinal * valExchangeRate)
function getYourPortion() {
    //alert('trace')
    if (valMonthlyRent > 0 && valRentalSubsidyFinal > 0) {
        valYourPortion = valMonthlyRent - (valRentalSubsidyFinal * valExchangeRate);
        valYourPortionDollars = (valMonthlyRent / valExchangeRate) - valRentalSubsidyFinal;
    }
    else {
        valYourPortion = valMonthlyRent;
        valYourPortionDollars = (valMonthlyRent / valExchangeRate)
    }
}
//valDividedMaxRent					hvis valAgentsFeePaid = 0 så 0, hvis valReasonableMaximumCalculated = 0 så 1, hvis valReasonableMaximumCalculated / valMonthlyRent > 1 så 1 ellers valReasonableMaximumCalculated / valMonthlyRent
function DividedMaxRent() {
    if (valAgentsFeePaid == 0) {
        valDividedMaxRent = 0;
        return;
    }
    else if (valReasonableMaximumCalculated == 0) {
        valDividedMaxRent = 1;
        return;
    }
    else if (valReasonableMaximumCalculated / valMonthlyRent > 1) {
        valDividedMaxRent = 1;
        return;
    }
    else {
        valDividedMaxRent = valReasonableMaximumCalculated / valMonthlyRent;
    }
}
//valAgentsFeePaidCalculated			valAgentsFeePaid / valExchangeRate
function AgentsFeePaidCalculated() {
    if (valAgentsFeePaid > 0 && valExchangeRate > 0) {
        valAgentsFeePaidCalculated = valAgentsFeePaid / valExchangeRate;
    }
}
//valAgentsFeeMultiply				valDividedMaxRent * valAgentsFeePaidCalculated
function AgentsFeeMultiply() {
    if (valDividedMaxRent > 0 && valAgentsFeePaidCalculated > 0) {
        valAgentsFeeMultiply = valDividedMaxRent * valAgentsFeePaidCalculated;
    }
}
//valExcessFee						valAgentsFeeMultiply - valThresholdAmountCalculated
function ExcessFee() {
    if (valAgentsFeeMultiply > 0 && valThresholdAmountCalculated > 0) {
        valExcessFee = valAgentsFeeMultiply - valThresholdAmountCalculated;
    }
}
//valReimbursement					valExcessFee > 0 så valExcessFee * 0,8
function Reimbursement() {
    if (valExcessFee > 0) {
        valReimbursement = valExcessFee * 0.8;
    }
}

//################# calculate values end  ##########################
//##################################################################

//http://icsc.un.org/resources/html/cold/pac/pac_frm.htm
//http://icsc.un.org/resources/html/cold/pac/pac_tab.htm#A
function getExternalRates() {
    var winRates
    if (valDutyStaion != '') {
        var tmpHref = valDutyStaion.substring(0, 1)
        tmpHref = 'http://icsc.un.org/resources/html/cold/pac/pac_tab.htm#' + tmpHref
        winRates = window.open(tmpHref, 'externelRates');
        winRates.focus();
    }
    else {
        winRates = window.open('http://icsc.un.org/resources/html/cold/pac/pac_frm.htm', 'externelRates');
        winRates.focus();
    }
}
