/// favourites stuff

	function saveToFavourites( carId ) {
		var target = baseHref + 'ajax.php';
	    var params = 'type=addToWishList&carId=' + carId;

	    var myAjax = new Ajax.Request(target,
	    {
	        method: 'post',
	        parameters: params,
	        onSuccess: function (request) { alert( request.responseText ); rebuildFavourites(); },
	        onFailure: function (request) { alert( request.responseText ) }
	    });
	}
	
	function removeFromFavourites( carId ) {
		var target = baseHref + 'ajax.php';
	    var params = 'type=removeFromWishList&carId=' + carId;

	    var myAjax = new Ajax.Request(target,
	    {
	        method: 'post',
	        parameters: params,
	        onSuccess: function (request) { rebuildFavourites();  },
	        onFailure: function (request) {  }
	    });
	    
	    // reload the used car compare page
	    location.reload(true);
	    
	}
	
	function rebuildFavourites(){
		var target = baseHref + 'ajax.php';
	    var params = 'type=rebuildWishList';
	    
	    var myAjax = new Ajax.Request(target,
	    {
	        method: 'post',
	        parameters: params,
	        onSuccess: function (request) { $('favouritesHolder').innerHTML = request.responseText;  },
	        onFailure: function (request) {  }
	    });
	}
	
	function financeSearch(){

	    if( $('budgetSearchMonthlyPayments').value != '' && $('budgetSearchTerm').value != ''){
	        
			var aprVal = $F('aprValue');
	        var monthly_apr = ( parseFloat( aprVal ) / 12 ) /100;
	        
	        var totalFunds = 0;
	        var totalInterest = 0;
	        var totalAll = 0;
	        
	        var monthly_interest_payment = new Array();
	        var monthly_total_payment = new Array();
	        
	        // Loop over the term to generate the monthly values
	        for( var i = 0; i < $('budgetSearchTerm').value; i++  ){
	            
	            if( i == 0 ){
	                monthly_interest_payment[i] = parseFloat( $('budgetSearchMonthlyPayments').value ) * parseFloat( monthly_apr );
	                monthly_total_payment[i] = parseFloat( $('budgetSearchMonthlyPayments').value ) - parseFloat( monthly_interest_payment[i] );
	            }else{
	                monthly_interest_payment[i] = ( parseFloat( $('budgetSearchMonthlyPayments').value ) + parseFloat( monthly_total_payment[ i -1 ] ) )* parseFloat( monthly_apr ); 
	                monthly_total_payment[i] = ( parseFloat( $('budgetSearchMonthlyPayments').value ) + parseFloat( monthly_total_payment[ i -1 ] ) ) - parseFloat( monthly_interest_payment[i] );
	                totalFunds = monthly_total_payment[i];
	                
	            }    
	            totalInterest = parseFloat( totalInterest + monthly_interest_payment[i] );
	        }
	        
	        totalInterest = totalInterest.toFixed(2);
	        totalFunds = totalFunds.toFixed(2);
	        
	        var finance_total = parseFloat( totalInterest ) + parseFloat( totalFunds);
	        finance_total = finance_total.toFixed(2);
	        
	        if( $('budgetSearchDeposit').value != '' ){
	        
	            var finance_total_funds = parseFloat( totalFunds ) + parseFloat( $('budgetSearchDeposit').value );
	        }else{
	            
	            var finance_total_funds = parseFloat( totalFunds );
	        }
	        finance_total_funds = finance_total_funds.toFixed(2);
	        
	        var maxPrice = finance_total_funds * 1.035;
	        $('finance_maxPrice').value = maxPrice.toFixed(0);
	        $('finance_show_maxPrice').innerHTML = maxPrice.toFixed(0);
	        $('finance_show_maxPrice_div').style.display='block';
	        
	        
	        
	    }
	    
	}