//How much can I borrow Calculator

//Variables

// House Value
var val_min = 0;
var val_max = 900000;
var val_initial = 100000;
var current_val_value = val_initial;
var valValues = null;

//Only call the function when you need to - on mouse up
var cancelAjax = true;


// Set Inital Values and create slider Arrays

function InitializeHowMuchCanIBorrowCalculator()
{   

    // loan slider values
    valValues = CreateSliderValues(new Array(), val_min, 25000, 1000);
    valValues = CreateSliderValues(valValues, 26000, val_max, 1000);
	
	

    // loan slider
    $('#valTrack').slider({
        min: 0,
        max: valValues.length - 1,
        steps: valValues.length,
        change: function(e, ui)
		
        {
            current_val_value = valValues[ui.value]; 
            ShowResult();
        },
        slide: function(e, ui)
        {
            $('#valTextInput').val(valValues[ui.value]);
        }
    });
	   
    $('#valTextInput').change(function(eventArgs) { return handleKeyPress(eventArgs, valValues, 'valTrack'); });
    $('#valTextInput').val(current_val_value);
	
	  $('#valTrack').slider("value", FindValueIndex(valValues, current_val_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	house_price		=document.getElementById("valTextInput").value;
	house_price	=	parseFloat(house_price.replace(/[^0-9.]/g, ''));
	house_price			=	parseFloat(house_price);
	var	stampDuty		=	new Number(0);
	var FTB = document.getElementById("ftb").value;
	
		
	if(house_price<125001){
		stampDuty		=	0;
	}
	else if(house_price<250001){
	
	if (FTB=="Yes")
	{		stampDuty		= 0;}
	if (FTB=="No")
	{		stampDuty		= .01 * house_price;}
	
	}
	else if(house_price <500001){
		stampDuty		= .03 * house_price;
	}
	else{
		stampDuty		= .04 * house_price;
	}
	
	stampDuty = Math.round(stampDuty);
	
stampDuty = addCommas(stampDuty);
house_price2 = addCommas(house_price);
				 

	document.getElementById('value').innerHTML = house_price2;
	document.getElementById('stampduty').innerHTML = stampDuty;	
	document.getElementById('resultPane').style.display = "block";
	

	if (FTB=="Yes" && house_price <250000 && house_price >125001)
	{			
		 {document.getElementById('ftbmessage').style.display = "block";}
	}
	else
		{		document.getElementById('ftbmessage').style.display = "none";}
	

}

$(document).ready(InitializeHowMuchCanIBorrowCalculator);






 
