function setCaretTo(obj, pos) { 
    if(obj.createTextRange) { 
        /* Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */ 
        var range = obj.createTextRange(); 
        range.move("character", pos); 
        range.select(); 
    } else if(obj.selectionStart) { 
        /* Gecko is a little bit shorter on that. Simply
           focus the element and set the selection to a
           specified position
        */ 
        obj.focus(); 
        obj.setSelectionRange(pos, pos); 
    } 
} 
$().ready(function() {

	var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);

	var url = document.location ;

	
	var _chattelValue = function() {
	    var t = $('#chattelvalue');
	    _chattelValue = function() {
	        return t;
	    };
	    return _chattelValue();
	};
	
	
	//same few lines repeating, so I want to save scrolling space :)
	var _pmtc0 = function() { var t = $('#pmtc0'); _pmtc0 = function() { return t;};return _pmtc0(); };
	var _pmtc1 = function() { var t = $('#pmtc1'); _pmtc1 = function() { return t;};return _pmtc1(); };
	var _pmtc2 = function() { var t = $('#pmtc2'); _pmtc2 = function() { return t;};return _pmtc2(); };
	var _pmtc3 = function() { var t = $('#pmtc3'); _pmtc3 = function() { return t;};return _pmtc3(); };
	var _pmtc4 = function() { var t = $('#pmtc4'); _pmtc4 = function() { return t;};return _pmtc4(); };
	var _pmtc5 = function() { var t = $('#pmtc5'); _pmtc5 = function() { return t;};return _pmtc5(); };
	
	
	var _ts0 = function() { var t = $('#ts0'); _ts0 = function() { return t;};return _ts0(); };
	var _ts1 = function() { var t = $('#ts1'); _ts1 = function() { return t;};return _ts1(); };
	var _ts2 = function() { var t = $('#ts2'); _ts2 = function() { return t;};return _ts2(); };
	var _ts3 = function() { var t = $('#ts3'); _ts3 = function() { return t;};return _ts3(); };
	var _ts4 = function() { var t = $('#ts4'); _ts4 = function() { return t;};return _ts4(); };
	var _ts5 = function() { var t = $('#ts5'); _ts5 = function() { return t;};return _ts5(); };
	
	var _netCost = function() { var t = $('#net_cost'); _netCost = function() { return t;};return _netCost(); };
	var _reduction = function() { var t = $('#reduction'); _reduction = function() { return t;};return _reduction(); };
	
	var _reti0 = function() { var t = $('#reti0'); _reti0 = function() { return t;};return _reti0(); };
	var _reti1 = function() { var t = $('#reti1'); _reti1 = function() { return t;};return _reti1(); };
	var _reti2 = function() { var t = $('#reti2'); _reti2 = function() { return t;};return _reti2(); };
	var _reti3 = function() { var t = $('#reti3'); _reti3 = function() { return t;};return _reti3(); };
	var _reti4 = function() { var t = $('#reti4'); _reti4 = function() { return t;};return _reti4(); };
	var _reti5 = function() { var t = $('#reti5'); _reti5 = function() { return t;};return _reti5(); };
	
	
	
	$('#showdetailed').click(function(){
	  
	  $(".detailed").toggleClass("offscreen");
	  if($("#detailed").val() == 1){
	    
	    $("#detailed").val(0);
	    $('#showdetailed').text('Click here for a more complete contact form');
	    
	  }else{
	    
	    $("#detailed").val(1);
	    $('#showdetailed').text('Click here for the simple contact form');
	    
	  }
	  
	});
	
	$('#regarding').bind('change',function(){
	  
	  if($(this).val() == 4){
	    
	    $('#regarding-optional').removeClass('offscreen');
	    
	  }else{
	    
	    $('#regarding-optional').addClass('offscreen');
	    
	  }
	  
	});
	
	var calculateTax = function(){
	  
	  var purchasePrice = $('#purchase_price').val();
	  var chattelPercentage = $('#chattel_percentage').val();
	  var tax = $('#tax_assumption').val();
	  var cost = $('#cost_appraisal').val();
	  
	  if( $('#name').val() && $('#email').val()  ){
	  
  	  if( purchasePrice && chattelPercentage ){
  	    
  	    _chattelValue().text(Math.round(purchasePrice*(chattelPercentage/100)*100)/100);
  	    
  	  }
  	  
  	  if( purchasePrice && chattelPercentage && tax ){
  	    
  	    _pmtc0().text(Math.round(purchasePrice * _percents[0]*100)/100);
  	    _pmtc1().text(Math.round(purchasePrice * _percents[1]*100)/100);
  	    _pmtc2().text(Math.round(purchasePrice * _percents[2]*100)/100);
  	    _pmtc3().text(Math.round(purchasePrice * _percents[3]*100)/100);
  	    _pmtc4().text(Math.round(purchasePrice * _percents[4]*100)/100);
  	    _pmtc5().text(Math.round(purchasePrice * _percents[5]*100)/100);
  	    
  	    _ts0().text(Math.round(purchasePrice * _percents[0]*tax*100)/100);
  	    _ts1().text(Math.round(purchasePrice * _percents[1]*tax*100)/100);
  	    _ts2().text(Math.round(purchasePrice * _percents[2]*tax*100)/100);
  	    _ts3().text(Math.round(purchasePrice * _percents[3]*tax*100)/100);
  	    _ts4().text(Math.round(purchasePrice * _percents[4]*tax*100)/100);
  	    _ts5().text(Math.round(purchasePrice * _percents[5]*tax*100)/100);
  	    
  	    
  	  }
  	  
  	  if( purchasePrice && chattelPercentage && tax && cost > 0){
  	    
  	    _reduction().text(Math.round(cost*tax*100)/100);
  	    
  	    var netcost = Math.round((cost - (cost*tax))*100)/100;
  	     
  	    _netCost().text(netcost);
  	    
  	    
  	    
  	    _reti0().text(Math.round((purchasePrice * _percents[0])/netcost*10000)/100);
  	    _reti1().text(Math.round((purchasePrice * _percents[1])/netcost*10000)/100);
  	    _reti2().text(Math.round((purchasePrice * _percents[2])/netcost*10000)/100);
  	    _reti3().text(Math.round((purchasePrice * _percents[3])/netcost*10000)/100);
  	    _reti4().text(Math.round((purchasePrice * _percents[4])/netcost*10000)/100);
  	    _reti5().text(Math.round((purchasePrice * _percents[5])/netcost*10000)/100);
  	    
  	  }
  	  
	  }
  	  
	}
	
//	$('#purchase_price').bind('keyup', calculateTax);
//	$('#chattel_percentage').bind('keyup', calculateTax);
//	$('#tax_assumption').bind('keyup', calculateTax);
//	$('#cost_appraisal').bind('change', calculateTax);
//	$('#calculate').bind('click', calculateTax);
	
	
	if(ltie7){

		$('tr').hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});

	}
	
});
	


