//Life Cover Shortfall

//Variables

//mortgage
var mortgage_min = 25000;
var mortgage_max = 700000;
var mortgage_initial = 100000;
var current_mortgage_value = mortgage_initial;
var mortgageValues = null;

//loans
var loans_min = 0;
var loans_max = 50000;
var loans_initial = 1000;
var current_loans_value = loans_initial;
var loansValues = null;

//funeral
var funeral_min = 0;
var funeral_max = 5000;
var funeral_initial = 1000;
var current_funeral_value = funeral_initial;
var funeralValues = null;

//education
var education_min = 0;
var education_max = 60000;
var education_initial = 10000;
var current_education_value = education_initial;
var educationValues = null;

//otherexpenses
var otherexpenses_min = 0;
var otherexpenses_max = 50000;
var otherexpenses_initial = 1000;
var current_otherexpenses_value = otherexpenses_initial;
var otherexpensesValues = null;

//lumpsum
var lumpsum_min = 0;
var lumpsum_max = 100000;
var lumpsum_initial = 10000;
var current_lumpsum_value = lumpsum_initial;
var lumpsumValues = null;

//existingcover
var existingcover_min = 0;
var existingcover_max = 600000;
var existingcover_initial = 0;
var current_existingcover_value = existingcover_initial;
var existingcoverValues = null;


//Only call the function when you need to - on mouse up
var cancelAjax = true;


// Set Inital Values and create slider Arrays

function InitializeHowMuchCanIBorrowCalculator()
{   

//mortgage slider values
	mortgageValues = CreateSliderValues(new Array(), mortgage_min, 10000, 500);
    mortgageValues = CreateSliderValues(mortgageValues, 10500, mortgage_max, 1000);
	
//loans slider values
	loansValues = CreateSliderValues(new Array(), loans_min, 10000, 500);
    loansValues = CreateSliderValues(loansValues, 10500, loans_max, 1000);
	
//funeral slider values
	funeralValues = CreateSliderValues(new Array(), funeral_min, 10000, 500);
    funeralValues = CreateSliderValues(funeralValues, 10500, funeral_max, 1000);
	
//education slider values
	educationValues = CreateSliderValues(new Array(), education_min, 10000, 500);
    educationValues = CreateSliderValues(educationValues, 10500, education_max, 1000);
	
//otherexpenses slider values
	otherexpensesValues = CreateSliderValues(new Array(), otherexpenses_min, 10000, 500);
    otherexpensesValues = CreateSliderValues(otherexpensesValues, 10500, otherexpenses_max, 1000);
	
//lumpsum slider values
	lumpsumValues = CreateSliderValues(new Array(), lumpsum_min, 10000, 500);
    lumpsumValues = CreateSliderValues(lumpsumValues, 10500, lumpsum_max, 1000);
	
//existingcover slider values
	existingcoverValues = CreateSliderValues(new Array(), existingcover_min, 10000, 500);
    existingcoverValues = CreateSliderValues(existingcoverValues, 10500, existingcover_max, 1000);
	

	//mortgage slider
	   $('#mortgageTrack').slider({
        min: 0,
        max: mortgageValues.length - 1,
        steps: mortgageValues.length,
        change: function(e, ui)
		
        {
            current_mortgage_value = mortgageValues[ui.value]; 
            ShowResult();
        },
        slide: function(e, ui)
        {
            $('#mortgageTextInput').val(mortgageValues[ui.value]);
        }
    });



//loans slider
   $('#loansTrack').slider({
        min: 0,
        max: loansValues.length - 1,
        steps: loansValues.length,
        change: function(e, ui)
		
        {
            current_loans_value = loansValues[ui.value]; 
            ShowResult();
        },
        slide: function(e, ui)
        {
            $('#loansTextInput').val(loansValues[ui.value]);
        }
    });



//funeral slider
   $('#funeralTrack').slider({
        min: 0,
        max: funeralValues.length - 1,
        steps: funeralValues.length,
        change: function(e, ui)
		
        {
            current_funeral_value = funeralValues[ui.value]; 
            ShowResult();
        },
        slide: function(e, ui)
        {
            $('#funeralTextInput').val(funeralValues[ui.value]);
        }
    });



//education slider
   $('#educationTrack').slider({
        min: 0,
        max: educationValues.length - 1,
        steps: educationValues.length,
        change: function(e, ui)
		
        {
            current_education_value = educationValues[ui.value]; 
            ShowResult();
        },
        slide: function(e, ui)
        {
            $('#educationTextInput').val(educationValues[ui.value]);
        }
    });



//otherexpenses slider
   $('#otherexpensesTrack').slider({
        min: 0,
        max: otherexpensesValues.length - 1,
        steps: otherexpensesValues.length,
        change: function(e, ui)
		
        {
            current_otherexpenses_value = otherexpensesValues[ui.value]; 
            ShowResult();
        },
        slide: function(e, ui)
        {
            $('#otherexpensesTextInput').val(otherexpensesValues[ui.value]);
        }
    });



//lumpsum slider
   $('#lumpsumTrack').slider({
        min: 0,
        max: lumpsumValues.length - 1,
        steps: lumpsumValues.length,
        change: function(e, ui)
		
        {
            current_lumpsum_value = lumpsumValues[ui.value]; 
            ShowResult();
        },
        slide: function(e, ui)
        {
            $('#lumpsumTextInput').val(lumpsumValues[ui.value]);
        }
    });



//existingcover slider
   $('#existingcoverTrack').slider({
        min: 0,
        max: existingcoverValues.length - 1,
        steps: existingcoverValues.length,
        change: function(e, ui)
		
        {
            current_existingcover_value = existingcoverValues[ui.value]; 
            ShowResult();
        },
        slide: function(e, ui)
        {
            $('#existingcoverTextInput').val(existingcoverValues[ui.value]);
        }
    });


	
	  
//mortgage
    $('#mortgageTextInput').change(function(eventArgs) { return handleKeyPress(eventArgs, mortgageValues, 'mortgageTrack'); });
    $('#mortgageTextInput').val(current_mortgage_value);
	
//loans
    $('#loansTextInput').change(function(eventArgs) { return handleKeyPress(eventArgs, loansValues, 'loansTrack'); });
    $('#loansTextInput').val(current_loans_value);
	
//funeral
    $('#funeralTextInput').change(function(eventArgs) { return handleKeyPress(eventArgs, funeralValues, 'funeralTrack'); });
    $('#funeralTextInput').val(current_funeral_value);
	
//education
    $('#educationTextInput').change(function(eventArgs) { return handleKeyPress(eventArgs, educationValues, 'educationTrack'); });
    $('#educationTextInput').val(current_education_value);
	
//otherexpenses
    $('#otherexpensesTextInput').change(function(eventArgs) { return handleKeyPress(eventArgs, otherexpensesValues, 'otherexpensesTrack'); });
    $('#otherexpensesTextInput').val(current_otherexpenses_value);
	
//lumpsum
    $('#lumpsumTextInput').change(function(eventArgs) { return handleKeyPress(eventArgs, lumpsumValues, 'lumpsumTrack'); });
    $('#lumpsumTextInput').val(current_lumpsum_value);
	
//existingcover    
    $('#existingcoverTextInput').change(function(eventArgs) { return handleKeyPress(eventArgs, existingcoverValues, 'existingcoverTrack'); });
    $('#existingcoverTextInput').val(current_existingcover_value);
	
//mortgage
    $('#mortgageTrack').slider("value", FindValueIndex(mortgageValues, current_mortgage_value));

//loans
    $('#loansTrack').slider("value", FindValueIndex(loansValues, current_loans_value));

//funeral
    $('#funeralTrack').slider("value", FindValueIndex(funeralValues, current_funeral_value));

//education
    $('#educationTrack').slider("value", FindValueIndex(educationValues, current_education_value));

//otherexpenses
    $('#otherexpensesTrack').slider("value", FindValueIndex(otherexpensesValues, current_otherexpenses_value));

//lumpsum
    $('#lumpsumTrack').slider("value", FindValueIndex(lumpsumValues, current_lumpsum_value));

//existingcover	
    $('#existingcoverTrack').slider("value", FindValueIndex(existingcoverValues, current_existingcover_value));	

       
	// setting this to false will trigget the ajax call
	cancelAjax = false;
    
}

function handleKeyPress(eventArgs, valuesArr, trackId)
{
//    if (eventArgs.keyCode == 13)
//    {
        var v = parseFloat($(eventArgs.target).val());

        // is it a number?
        if (isNaN(v))
        {
            alert('Sorry, the amount is invalid.');
            return false;
        }
        else
        {
            var sliderIdx = FindValueIndex(valuesArr, v);

            if (!isNaN(sliderIdx))
            {
                // cancel this ajax request - we'll call it later on
                cancelAjax = true;
                $('#' + trackId).slider("value", sliderIdx);
                cancelAjax = false;
            }
            else
            {
                alert('Please enter a value higher than ' + valuesArr[0] + ' and lower than ' + valuesArr[valuesArr.length - 1] + '.');
                return false;
            }
        }

        $(eventArgs.target).val(v);

       
		
		
        ShowResult(); // calling the ajax request here will send the correct "current" values
        //return false;
 
}



function ShowResult()
{

// don't keep doing it, honestly, everything will go crazy.  
    if (cancelAjax) { return; }    
	
	
var lumpsum = document.getElementById("lumpsumTextInput").value; 
lumpsum = parseFloat(lumpsum.replace(/[^0-9.]/g, ''));

var mortgage = document.getElementById("mortgageTextInput").value; 
mortgage = parseFloat(mortgage.replace(/[^0-9.]/g, ''));
var loans = document.getElementById("loansTextInput").value; 
loans = parseFloat(loans.replace(/[^0-9.]/g, ''));
var funeral = document.getElementById("funeralTextInput").value; 
funeral = parseFloat(funeral.replace(/[^0-9.]/g, ''));
var education = document.getElementById("educationTextInput").value; 
education = parseFloat(education.replace(/[^0-9.]/g, ''));
var other = document.getElementById("otherexpensesTextInput").value; 
other = parseFloat(other.replace(/[^0-9.]/g, ''));
var existing_cover = document.getElementById("existingcoverTextInput").value; 
existing_cover = parseFloat(existing_cover.replace(/[^0-9.]/g, ''));

var shortfall = lumpsum + mortgage + loans + funeral + education + other - existing_cover

if (shortfall<0){shortfall=0};


shortfall = addCommas(shortfall);
	
	
	
	
	document.getElementById('shortfall').innerHTML = shortfall;
	
	
	document.getElementById('resultPane').style.display = "block";

}



$(document).ready(InitializeHowMuchCanIBorrowCalculator);






 
