


jQuery().ready(function() {

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
}
 
jQuery.fn.digits = function(){ 
    return this.each(function(){ 
        jQuery(this).text( jQuery(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
}

    jQuery(".autoClear").focus(function(srcc)
    {
        if (jQuery(this).val() == jQuery(this)[0].title)
        {
            jQuery(this).removeClass("autoClearActive");
            jQuery(this).val("");
        }
    });
	
    jQuery(".autoClear").blur(function()
    {
        if (jQuery(this).val() == "")
        {
            jQuery(this).addClass("autoClearActive");
            jQuery(this).val(jQuery(this)[0].title);
        }
    });
	
    jQuery(".autoClear").blur();
	
	var debtAmountDefault = 10000;
	var debtAmountMin = 1500;
	var debtAmountMax = 50000;
	
	var perMonthDefault = 300;
	var perMonthMin = 50;
	var perMonthMax = 800;
	
	var width = (debtAmountDefault / debtAmountMax) - 0.03 ;
				
				var width = (width * 100)+"%";
				
				jQuery(".debtAmount-slider").css({width:width});
				
				
				var width = (perMonthDefault / perMonthMax) - 0.05 ;
				
				var width = (width * 100)+"%";
				
				jQuery(".debtMonth-slider").css({width:width});

	
	if (jQuery("#confirmationSubmit").length > 0)
	{
		jQuery("#confirmationSubmit").click(function(e) {
			if ((jQuery("#confirmationDebtAmount").val().length > 1) &&
				(jQuery("#confirmationPerMonth").val().length > 1) &&
				(jQuery("#confirmationName").val().length > 0) &&
				(jQuery("#confirmationEmail").val().length > 0) &&
				(jQuery("#confirmationPhone").val().length > 0))
			{
				jQuery("#confirmationForm").submit();
			}
			else
			{
				alert("Error: All fields must be filled in properly");
				return false;
			}
		});
	}
	
	if (jQuery("#debtAmountSlider").length > 0)
	{
		jQuery("#debtAmountSlider").slider({
			value: debtAmountDefault,
			min: debtAmountMin,
			max: debtAmountMax,
			step: 100,
			animate: "slow",
			slide: function( event, ui ) {
				
				
				
				jQuery("#debtAmount").val(ui.value);
				
				var width = (ui.value / debtAmountMax) - 0.03 ;
				
				var width = (width * 100)+"%";
				
				jQuery(".debtAmount-slider").css({width:width});
				
				
				recalculate();
			}
		});
		
		jQuery("#perMonthSlider").slider({
			value: perMonthDefault,
			min: perMonthMin,
			max: perMonthMax,
			step: 10,
			animate: "slow",
			slide: function( event, ui ) {
				jQuery("#perMonth").val(ui.value);
				
				var width = (ui.value / perMonthMax) - 0.05 ;
				
				var width = (width * 100)+"%";
				
				jQuery(".debtMonth-slider").css({width:width});
				recalculate();
			}
		});
		
		jQuery("#debtAmountText").change(function () {
			jQuery("#debtAmountText").val(jQuery("#debtAmount").val());
			if (isInt(jQuery("#debtAmountText").val())) {
				var debtAmount = parseInt(jQuery("#debtAmountText").val());
				
				if (debtAmount < debtAmountMin) {
					debtAmount = debtAmountMin;
				} else if (debtAmount > debtAmountMax) {
					debtAmount = debtAmountMax;
				}
				
				jQuery("#debtAmountSlider").slider("value", debtAmount);
				jQuery("#debtAmount").val(debtAmount);
			} else {
				jQuery("#debtAmountSlider").slider("value", debtAmountDefault);
				jQuery("#debtAmount").val(debtAmountDefault);
			}
			recalculate();
		});
		
		jQuery("#perMonthText").change(function () {
			jQuery("#perMonthText").val(jQuery("#perMonthText").val().replace(/[^0-9]/g, ''));
			if (isInt(jQuery("#perMonthText").val())) {
				var perMonth = parseInt(jQuery("#perMonthText").val());
				
				if (perMonth < perMonthMin) {
					perMonth = perMonthMin;
				} else if (perMonth > perMonthMax) {
					perMonth = perMonthMax;
				}
				
				jQuery("#perMonthSlider").slider("value", perMonth);
				jQuery("#perMonth").val(perMonth);
			} else {
				jQuery("#perMonthSlider").slider("value", perMonthDefault);
				jQuery("#perMonth").val(perMonthDefault);
			}
			recalculate();
		});
		
		recalculate();
	};
	
	if (jQuery("#thinSlider").length > 0)
	{
		jQuery("#thinDebtAmountSlider").slider({
			value: debtAmountDefault,
			min: debtAmountMin,
			max: debtAmountMax,
			step: 100,
			animate: "slow",
			slide: function( event, ui ) {
				jQuery("#debtAmount").val(ui.value);
				
				recalculate();
			}
		});
		
		jQuery("#thinPerMonthSlider").slider({
			value: perMonthDefault,
			min: perMonthMin,
			max: perMonthMax,
			step: 10,
			animate: "slow",
			slide: function( event, ui ) {
				jQuery("#perMonth").val(ui.value);
				recalculate();
			}
		});
		
		recalculate();
	};
});



function recalculate()
{
	var debtAmount = parseInt(jQuery("#debtAmount").val().replace(/,/g, ''));
	
	var perMonth = parseInt(jQuery("#perMonth").val());
	
		
		
		
	
	
	
	var returnMonths = 0;
	
	if (perMonth < 175)
	{
		returnMonths = Math.round(debtAmount / (perMonth - 25).toFixed(1));
	}
	else
	{
		returnMonths = Math.round(debtAmount / (perMonth * 0.85));
	}
	
	if (jQuery("#debtAmountSlider").length > 0)
	{
	
		jQuery("#debtAmountText").val("\u00A3" + debtAmount);
		jQuery("#debtAmountText2").html("\u00A3" + debtAmount).digits();
		
		jQuery("#perMonthText").val("\u00A3" + perMonth);
		jQuery("#perMonthText2").html("\u00A3" + perMonth).digits();
		jQuery("#repaymentTermText").html(returnMonths).digits();
		jQuery("#repaymentMonths").val(returnMonths);
	};
	if (jQuery("#thinSlider").length > 0)
	{
		jQuery("#thinDebtAmount").html("\u00A3" + debtAmount).digits();
		jQuery("#thinPerMonth").html("\u00A3" + perMonth).digits();
		jQuery("#thinRepaymentTerm").html(returnMonths);
		jQuery("#repaymentMonths").val(returnMonths);
	};
}
