<?PHP
//================================================================================
// PLEASE DO NOT REMOVE THIS HEADER!!!
//
// COPYRIGHT NOTICE
// This script is licensed under the GPL
// 
// Copyright 2007-2008 Alias 454 Studios and Brandon Keep (c) All rights reserved.
// Created 11/18/2007   
// Brandon Keep,    http://www.openautoclassifieds.com
//                  http://www.alias454studios.com/scripts/
//
// Last Modified 04-12-2008 by
// Brandon Keep,    http://alias454studios.com
//================================================================================
// This software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//================================================================================

require_once './Includes/configs/functions.php';
require_once './Includes/libs/Smarty.class.php';
include_once "./Includes/language/$site_lang.php";

$smarty = new Smarty;

$smarty->assign("manufacturerslist",getSearchList("make"));
$smarty->assign("modelslist",getSearchList("model"));
$smarty->assign("typeslist",getSearchList("vehicle_type"));

//Check if single user mode 1 or multi-user mode 0
if ($site_mode == 0) {
	$smarty->assign("site_mode",1);
}
//The user is already logged in.
if (isset($_SESSION["logged_in"])){
	$smarty->assign("logged_in",1);
	//check privs to find out if user 
	//is a seller, admin or user
	$status = checkPrivs();
	$smarty->assign("$status",1);
}

if (isset($_POST["term"]) && $_POST["submit"] == LOAN_CALC) {
	switch($_POST["term"]) {
		case 'Weekly':
			$pay_term = 52;
			$smarty->assign("Weekly",1);
			break;
		case 'Bi_weekly':
			$pay_term = 26;
			$smarty->assign("Bi_weekly",1);
			break;
		case 'Monthly':
			$pay_term = 12;
			$smarty->assign("Monthly",1);
			break;
		default:
			$pay_term = 12;
			$smarty->assign("Monthly",1);
			break;
	}
} else {
	$pay_term = 12;
	$smarty->assign("Monthly",1);
}

if (!empty($_GET["price"])) {
	$starting_price = $_GET["price"];
} elseif (!empty($_POST["loan_amount"])) {
	$starting_price = $_POST["loan_amount"];
}

$price             = isset($_POST["loan_amount"])    ? number_format($starting_price, 2, '.', '') : $starting_price;
$loan_length       = isset($_POST["loan_length"])    ? $_POST["loan_length"] : 3;
$down_payment      = isset($_POST["amount_down"])    ? number_format($_POST["amount_down"], 2, '.', '') : 0;
$annual_interest   = isset($_POST["interest_rate"])  ? $_POST["interest_rate"] : 10;

//Do math and figure out what the dollar amounts are
$loan_amount       = $price -$down_payment;
$total_periods     = $loan_length * $pay_term;
$interest_percent  = $annual_interest / 100;
$period_interest   = $interest_percent / $pay_term;
$c_period_payment  = $loan_amount *($period_interest / (1 - pow((1 + $period_interest), -($total_periods))));
$total_paid        = number_format($c_period_payment * $total_periods, 2, '.', '');
$total_interest    = number_format($c_period_payment * $total_periods - $loan_amount, 2, '.', '');
$total_principal   = number_format($loan_amount, 2, '.', '');

$total_loan_amount = number_format($loan_amount + $total_interest, 2, '.', '');
$annual_interest   = number_format($annual_interest, 2, '.', '');
$period_payment    = number_format($c_period_payment, 2, '.', '');

//Assign variables to the Templates
$smarty->assign("price",$price);
$smarty->assign("down_payment",$down_payment);
$smarty->assign("total_principal",$total_principal);
$smarty->assign("total_interest",$total_interest);
$smarty->assign("annual_interest_rate",$annual_interest);
$smarty->assign("total_loan_amount",$total_loan_amount);
$smarty->assign("period_payment",$period_payment);

$smarty->assign("formprice",$price);
$smarty->assign("formlength",$loan_length);
$smarty->assign("formamount",$down_payment);
$smarty->assign("forminterest",$annual_interest);

$smarty->display('paycalc.tpl');
?>