/*************************************************************************************
*	Basic Search
**************************************************************************************/

function getUsedCarCount() {

	new Ajax.Request( 'ajax.php', {
		method: 'post',
		parameters: 'type=getUsedBasicSearchCounter&' + $('searchForm').serialize(),
		encoding:     'UTF-8',
		onSuccess: function ( r ) {

			$('carCount').innerHTML = r.responseText;
			$('carCountContainer').style.display = '';
			//alert(r.responseText);

		}
	});

}

function getNewCarCount() {

	new Ajax.Request( 'ajax.php', {
		method: 'post',
		parameters: 'type=getNewBasicSearchCounter&' + $('searchForm').serialize(),
		encoding:     'UTF-8',
		onSuccess: function ( r ) {

			$('carCount').innerHTML = r.responseText;
			$('carCountContainer').style.display = '';
			//alert(r.responseText);

		}
	});

}

function getModels() {
    //marqueDetailId

    var target = baseHref + 'ajax.php';
    var params = 'type=getMarqueModels&id=' + $('marqueDetailId').options[$('marqueDetailId').selectedIndex].value;

    var myAjax = new Ajax.Request(target,
    {
        method: 'post',
        parameters: params,
        onSuccess: function (request) {

            var i;
            for(i = $('model').options.length - 1 ; i >= 0 ; i-- ) {
                $('model').remove(i);
            }

            var outText = request.responseText;
            outText = outText.split(',');

            if ( outText.length > 0 ) {
                for (i = 0 ; i < outText.length ; i++) {

                    //alert(outText[i]);
                    $('model').options[ $('model').options.length ] = new Option( outText[i], outText[i] );
                    if( outText[i] == searchModel ){
                        $('model').options[ $('model').options.length -1 ].selected = true;
                    }

                }
            }
            
            getUsedCarCount();

        },

        onFailure: function(request) {
            alert( 'request failed, please try again' );
        }
    });

}

function getNewModels() {

	var target = baseHref + 'ajax.php';
    var params = 'type=getNewModels&make=' + $('newMarque').options[$('newMarque').selectedIndex].value;

    var myAjax = new Ajax.Request(target,
    {
        method: 'post',
        parameters: params,
        onSuccess: function (request) {

            var i;
            for(i = $('newModel').options.length - 1 ; i >= 0 ; i-- ) {
                $('newModel').remove(i);
            }

            var outText = request.responseText;
            outText = outText.split(',');

            if ( outText.length > 0 ) {
                for (i = 0 ; i < outText.length ; i++) {

                    //alert(outText[i]);
                    $('newModel').options[ $('newModel').options.length ] = new Option( outText[i], outText[i] );

                }
            }

        },

        onFailure: function(request) {
            alert( 'request failed, please try again' );
        }
    });


}

/*************************************************************************************
*	Category Search
**************************************************************************************/

function changeCategory( categoryId, activeId ) {
	// Clear active class from all
	$$('.categoryJump').invoke('removeClassName','active');
	// Add active class to selected category
	$(activeId).addClassName('active');
	// Change hidden field to have new category id
	$('categoryType').value = categoryId;

	getCategoryCarCount();
}


function getCategoryCarCount() {

	if ( $('categoryNewSearch').value == 1 ) {
		var searchType = 'getNewCategorySearchCounter';
	} else {
		var searchType = 'getUsedCategorySearchCounter';
	}

	new Ajax.Request( 'ajax.php', {
		method: 'post',
		parameters: 'type=' + searchType + '&' + $('categorySearchForm').serialize(),
		encoding:     'UTF-8',
		onSuccess: function ( r ) {

			$('carCountCategory').innerHTML = r.responseText;
			$('carCountCategoryContainer').style.display = '';
			//alert(r.responseText);

		}
	});

}


/*************************************************************************************
*	Budget Search
**************************************************************************************/

function budgetSearch() {

    if( $('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);
        $('minPriceBudget').value = 0;

        $('minPriceBudgetFake').selectedIndex = 0;
        $('maxPriceBudgetFake').selectedIndex = 0;

    }

}


function changeBudgetMinPrice() {
	$('minPriceBudget').value = $('minPriceBudgetFake').value;
	$('monthlyBudget').value = '';
}


function changeBudgetMaxPrice() {
	$('maxPriceBudget').value = $('maxPriceBudgetFake').value;
	$('monthlyBudget').value = '';
}

function getBudgetCarCount() {

	if ( $('budgetNewSearch').value == 1 ) {
		var searchType = 'getNewBudgetSearchCounter';
	} else {
		var searchType = 'getUsedBudgetSearchCounter';
	}

	new Ajax.Request( 'ajax.php', {
		method: 'post',
		parameters: 'type=' + searchType + '&' + $('budgetSearchForm').serialize(),
		encoding:     'UTF-8',
		onSuccess: function ( r ) {

			$('carCountBudget').innerHTML = r.responseText;
			$('carCountBudgetContainer').style.display = '';
			//alert(r.responseText);

		}
	});

}

/*************************************************************************************
*	Green Search
**************************************************************************************/

function getGreenCarCount() {

	if ( $('greenNewSearch').value == 1 ) {
		var searchType = 'getNewGreenSearchCounter';
	} else {
		var searchType = 'getUsedGreenSearchCounter';
	}

	new Ajax.Request( 'ajax.php', {
		method: 'post',
		parameters: 'type=' + searchType + '&' + $('greenSearchForm').serialize(),
		encoding:     'UTF-8',
		onSuccess: function ( r ) {

			$('carCountGreen').innerHTML = r.responseText;
			$('carCountGreenContainer').style.display = '';

		}
	});

}
Event.observe(document, 'dom:loaded', function() {
    getModels();
});

