/****************************************************
*********** Basic Search Totals *********************
*****************************************************/

function getTotalCarsBasic() {
        
        var target = baseHref + 'ajax.php';
        var params = 'type=getTotalCars&' + $('searchForm').serialize();

        var myAjax = new Ajax.Request(target,
        {
            
            method: 'post',
            parameters: params,
            onSuccess: function (request) {
            	if( request.responseText == "1" ){
            		$('advCnt').innerHTML=request.responseText+' Vehicle in stock';
            	}else{
                	$('advCnt').innerHTML=request.responseText+' Vehicles in stock';
            	}
             
            },
            
            onFailure: function(request) {
                alert( 'request failed, please try again' );
            }
        });
    
   
    }
    
    
/****************************************************
*********** Budget Search Totals ********************
*****************************************************/
    
 function getTotalCarsBudget() {
 	
	//get vars for ajax
 
    var c 	= $('minPrice').value;
    var d 	= $('maxPrice').value;
   
    var target = baseHref + 'ajax.php';
    var params = 'type=getTotalCars&c='+c+'&d='+d;

    var myAjax = new Ajax.Request(target,
    {
        method: 'post',
        parameters: params,
        onSuccess: function (request) {    
        	if( request.responseText == "1" ){
            	$('budgCnt').innerHTML=request.responseText+' Vehicle in stock';
        	}else{ 
       			$('budgCnt').innerHTML=request.responseText+' Vehicles in stock';     
        	}
        },

        onFailure: function(request) {
            alert( 'request failed, please try again' );
        }
    });
}

/****************************************************
*********** Model/Category Search Totals ************
*****************************************************/

 function displayTotalCarsModel( total ) {
 	

    if( total == "1" ){
        $('modCnt').innerHTML=total + ' Vehicle in stock';
    }else{ 
        $('modCnt').innerHTML=total + ' Vehicles in stock';
    }

 }



/****************************************************
*********** Budget Calculation and functions ********
*****************************************************/

function financeSearch() {

    if( $('monthlyBudget').value != '' && !isNaN( $('monthlyBudget').value ) ){
        
    	var apr = 13.3;
    	var deposit = 1000;
    	var term = 60;

        var monthly_apr = ( parseFloat( apr ) / 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 < term; i++  ) {				                
            if( i == 0 ) {
                monthly_interest_payment[i] = parseFloat( $('monthlyBudget').value ) * parseFloat( monthly_apr );
                monthly_total_payment[i] = parseFloat( $('monthlyBudget').value ) - parseFloat( monthly_interest_payment[i] );
            } else {
                monthly_interest_payment[i] = ( parseFloat( $('monthlyBudget').value ) + parseFloat( monthly_total_payment[ i -1 ] ) )* parseFloat( monthly_apr ); 
                monthly_total_payment[i] = ( parseFloat( $('monthlyBudget').value ) + parseFloat( monthly_total_payment[ i -1 ] ) ) - parseFloat( monthly_interest_payment[i] );
                totalFunds = monthly_total_payment[i];
                
            }    
            totalInterest = parseFloat( totalInterest + monthly_interest_payment[i] );
        }
        
        var finance_total = parseFloat( totalInterest ) + parseFloat( totalFunds);
        var finance_total_funds = parseFloat( totalFunds ) + parseFloat( deposit );

        var maxPrice = finance_total_funds * 1;
        $('maxPriceBudget').value = maxPrice.toFixed(2);                
        $('GTCmaxPrice').value = maxPrice.toFixed(2);                
        $('minPriceBudget').value = 0;     
    }
    
}


function changeBudgetMinPrice() {	
	$('minPriceBudget').value = $('minPriceBudgetFake').value;	

}


function changeBudgetMaxPrice() {	
	$('maxPriceBudget').value = $('maxPriceBudgetFake').value;		
}


//*****************************************************
//*********** Switch tabs function ********************
//*****************************************************//*

function switchVehicleSearchTabs(theTag, openingDiv) {
	tagList = $('tabCarTypes').getElementsByTagName('a');
	for (i = 0; i < tagList.length; i++) {
		tagList[i].className = '';
		theTag.className = 'active';
	}

	$('tabContentBasic').hide();
	$('tabContentBudget').hide();
	$('tabContentModel').hide();
	$(openingDiv).show();
}

