$(document).ready(function(){
	
	$('#dp_10').attr("checked","checked")
	
	function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '' + dblValue + '.' + strCents);
}

	function formatCurrency2(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+''+ //comma
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '' + dblValue + '.' + strCents); //'$'
}


	
	
	var bop_slide = function(e, ui){
    $('#txt_bop').val(ui.value)
    
    if (ui.value >= 40 && ui.value < 60)
    {
			$('.not_silver').css("display","none");
			$('.silver').css("display","block");
			//$('#showimg').attr("src","http://www.utaz.com/images/silver.jpg");
    }
    if (ui.value >= 60 && ui.value < 90)
    {
			$('.not_gold').css("display","none");
			$('.gold').css("display","block");
			//$('#showimg').attr("src","http://www.utaz.com/images/gold.jpg");
    }
    
    if (ui.value >= 90 && ui.value <= 100)
    {
			$('.not_platinum').css("display","none");
			$('.platinum').css("display","block");
			//$('#showimg').attr("src","http://www.utaz.com/images/platinum.jpg");
    }
    
    changefunc();
   
	}
	
	
	var sqft_slide = function(e, ui){
    $('#txt_sqft').val(ui.value)
    changefunc();
    
    
	}
	
	var ppsqft_slide = function(e, ui){
    $('#txt_ppsqft').val(ui.value)
    
    changefunc();
	}
	
	
	
	function sqft()
	{
		return ttn('txt_sqft');
	}
	
	
	var tp = function()
	{
		return ttn('txt_tp')
	}
	
	
	
	var ttn = function(id)
	{
		
		var sqft = $('#' + id).val()
		sqft = sqft.replace(/,/g,"");
		return sqft - 0;
	}
	
	var ppsqft = function(){
			return ttn('txt_ppsqft')
	}
	
	var bop = function(){
		return ttn('txt_bop');
	}
	
	
	var term = function()
	{
		return ttn('txt_term')
	}
	
	var ir = function()
	{
		//alert(ttn('txt_ir'))
		return ttn('txt_ir')
		
	}
	
	var dp = function()
	{
		return ttn('txt_dp')
		
	}
	
	var pplsqft = function()
	{
    return ttn('txt_pplsqft')
	}
	
	var fa = function()
	{
    return ttn('txt_fa')
	}
	
	var tih = function()
	{
		return ttn('txt_tih')
	}
	
	var tot_mmp = function()
	{
		return ttn('txt_mmp')
	}
	
	var tot_mlp = function()
	{
		return ttn('txt_mlp')
	}
	
	
	var dp_slide = function(e,ui)
	{
    $("#l_percent").text(ui.value + "%");
    $('#txt_dp').val(formatCurrency(tp() * ui.value * 0.01))
     changefunc();
	}
	
	$('#dp_5').click(function(){
		
		//dp_slide("",{value:5})
		$('#slider_dp').slider('value',5);
		$('#l_percent').text("5%")
		changefunc()
		
	})
	
	$('#dp_10').click(function(){
		//dp_slide("",{value:10})
		$('#slider_dp').slider('value',10);
		$('#l_percent').text("10%")
		changefunc()
	})
	
		$('#dp_20').click(function(){
		//dp_slide("",{value:20})
		$('#slider_dp').slider('value',20);
		$('#l_percent').text("20%")
		changefunc()
	})

	
	var changefunc = function(){
	
		if ($('#dp_5')[0].checked  == true)
		{
			//console.log('5%')
		totalval = sqft() * (ppsqft() + bop())
		//console.log($('#txt_sqft').val()," + ", $('#txt_bop').val()), " = ",)
		
		
		newdp = 0.10 * sqft() * bop() + 0.05 * sqft() * ppsqft()
		
		$('#txt_tp').val(formatCurrency(totalval))
		
		$('#txt_dp').val(formatCurrency(newdp))
		
		amtfin = 0.9 * totalval 
		
		
		
		$('#txt_fa').val(formatCurrency(totalval - newdp))
		$('#l_percent').text(
			formatCurrency((100 *(1 - ((totalval - newdp) / totalval)))) + "%")
		$('#txt_mmp').val(formatCurrency(getPayment(amtfin,term(), 0.01 * ir()) + getPayment(sqft() * ppsqft() * 0.05 ,term(),0.0825)))
		
		
		}

		else
		{
			
		totalval = sqft() * (ppsqft() + bop())
		//console.log($('#txt_sqft').val()," + ", $('#txt_bop').val()), " = ",)
		$('#txt_tp').val(formatCurrency(totalval))
		$('#txt_dp').val(formatCurrency(tp() * $('#slider_dp').slider('value') * 0.01))
		amtfin = totalval - dp()
		//alert(dp())
		$('#txt_fa').val(formatCurrency(amtfin))
		$('#txt_mmp').val(formatCurrency(getPayment(amtfin,term(), 0.01 * ir())))
		
		}
		
		
		
		var mlp = sqft() * pplsqft()
	mlp = mlp / 12;
	//console.log("div 12",mlp)
	//mlp = mlp + (sqft() * bop())// - 35))
	adj_bop = bop() - tih()
	
	if (adj_bop < 0)
	{
		adj_bop = 0
	}
	
	xyzpdq = mlp
	mlp = mlp + getPayment(sqft() * adj_bop, 5, 0.115)
	//console.log("mopay",getPayment(sqft() * bop(), 5, 0.09))
	$('#txt_mlp').val(formatCurrency(mlp))


		$('#mosav').text('$' + formatCurrency(tot_mlp() - tot_mmp()))
		$('#fiveyrsav').text('$' + formatCurrency(((xyzpdq * 64.99587072) + ((mlp - xyzpdq)*60)) - (tot_mmp() * 60)))
		$('#persav').text('(' + formatCurrency((100 *(tot_mlp() - tot_mmp()) / tot_mlp()) ) + "%)")
	}
	
	
	
	
	
	
	function getPayment(princ,years,interest)
	{
		//for formula see: http://en.wikipedia.org/wiki/Amortization_schedule
		var p = princ;
		var i = interest / 12;
		var n = years * 12
		return (i * p * Math.pow((1+i),n)) / (Math.pow((1+i),n) - 1)
	}
	

	
	$("input:not(#txt_dp, input[type='radio'])").change(changefunc)
	
	
	$('#slider_sqft').slider({max: 12000, min: 1000, slide: sqft_slide, value:2000, 'step':100});
	
	$('#slider_ppsqft').slider({max: 300, min: 150, slide: ppsqft_slide, value:199});
	
	$('#slider_bop').slider({max: 150, min: 40, slide: bop_slide, value: 50, step:5});
	
	$('#slider_dp').slider({max: 20, min: 5, slide: dp_slide, value: 10});
	
	changefunc()


  $('#txt_sqft').change(function(){
  //console.log(this.value - 0)
      $('#slider_sqft').slider('value',this.value-0)
			
  })
  
   $('#txt_ppsqft').change(function(){
      $('#slider_ppsqft').slider('value',this.value-0)
      	
  })
  
   $('#txt_bop').change(function(){
      $('#slider_bop').slider('value',this.value -0)
      	
  })
  
  
  $('#txt_dp').change(function(){
    
        thep = this.value.replace(/,/g,"") / fa() * 100
        //console.log(this.value.replace(/,/g,""),fa())
        
        
        $('#l_percent').text(Math.floor(thep) + "%")

      $('#slider_dp').slider('value',thep -0)
      	
  })
  
  
  
  
  
  
  $('#txt_sqft').keypress(function(e){
  //console.log(this.value - 0)
     
      if (e.which == 13){ $('#slider_sqft').slider('value',this.value-0);
			changefunc()
			//document.title = ""
			}
  })
  
   $('#txt_ppsqft').keypress(function(e){
       if (e.which == 13) { $('#slider_ppsqft').slider('value',this.value-0);
      	changefunc()
      	}
  })
  
   $('#txt_bop').keypress(function(e){
       if (e.which == 13){
        $('#slider_bop').slider('value',this.value -0);
      	changefunc()
      	}
  })
  
  
  $('#txt_dp').keypress(function(e){
     if (e.which == 13)
     {
        thep = this.value.replace(/,/g,"") / fa() * 100
        //console.log(this.value.replace(/,/g,""),fa())
        
        
        $('#l_percent').text(Math.floor(thep) + "%")

      $('#slider_dp').slider('value',thep -0)
      changefunc()
      }
      	
  })
  
   $('#txt_tih').keypress(function(e){
       if (e.which == 13){
      	changefunc()
      	}
  })
  
  
     $('#txt_tih').keypress(function(e){
       if (e.which == 13){
      	changefunc()
      	}
  })
     $('#txt_pplsqft').keypress(function(e){
       if (e.which == 13){
      	changefunc()
      	}
  })
     $('#txt_ir').keypress(function(e){
       if (e.which == 13){
      	changefunc()
      	}
  })
  
       $('#txt_ir').keypress(function(e){
       if (e.which == 13){
      	changefunc()
      	}
  })
   
         $('#txt_term').keypress(function(e){
       if (e.which == 13){
      	changefunc()
      	}
  })
	

})