// General template functions
// Author: Patrick Bennett

//gets the models
function showAllModels(make) {
	$("txtModels").append("Loading...");
	//set the url
	var url = '/api/get/carfulldb.php?request=getmakes&make='+make;
	$("#carAlertsModels").load(url);
}

//deletes original value of an input box
function clearDefaultVal(box) {
	if(box.defaultValue == box.value)
		box.value = '';
}

//validates user input for alerts
var checkAlertForm = function () {
	//check make box
	if($('#alertmake').val() == "all") {
		$('#carAlertBox .msg').html('<span>Oops! Please select the Make.</span>');
		return false;
	}
	
	//check zip
	if($('#zip').val() == "") {
		$('#carAlertBox .msg').html('<span>Oops! We need your zip code so we can find the car closest to your location.</span>');
		return false;
	}
	
	//check name
	if($('#alertName').val() == "") {
		$('#carAlertBox .msg').html('<span>Oops! Please provide your name so we can address you properly.</span>');
		return false;
	}
	
	//check email
	if($('#alertEmail').val() == "") {
		$('#carAlertBox .msg').html('<span>Oops! We need your email so we can notify you when we find a match.</span>');
		return false;
	}
}


//jquery listener functions

$(document).ready(function(){

	//contact form click leads to fade
	$("#contactFormButton, .cButton").click(function () {
		$("#contactFormBox .iconClosed").addClass("iconOpen");
		$("#contactDealerForm").css({ display: "block" });
		$("#contactDealer").animate({ backgroundColor: "red", color: "white"}, 1000);
		$(".cButton").css({ color: "white"});
		$("#contactFormBox").animate({ backgroundColor: "#fcf8da" }, 1000);
	});
	
	//vehicle history click leads to fade
	$("#vehicleHistoryButton, .vhButton").click(function () {
		$("#vehicleHistoryBox .iconClosed").addClass("iconOpen");
		$("#vehicleHistoryForm").css({ display: "block" });
		$("#vehicleHistoryReport").animate({ backgroundColor: "red", color: "white"}, 1000);
		$(".vhButton").css({ color: "white"});
		$("#vehicleHistoryBox").animate({ backgroundColor: "#fcf8da" }, 1000);
	});
	
	//payment calculator click leads to fade
	$("#paymentCalculatorButton, .pButton").click(function () {
		$("#paymentCalculatorBox .iconClosed").addClass("iconOpen");												  
		$("#paymentCalculatorForm").css({ display: "block" });
		$("#paymentCalculatorReport").animate({ backgroundColor: "#26690c", color: "white"}, 1000);
		$(".pButton").css({ color: "white"});
		$("#paymentCalculatorBox").animate({ backgroundColor: "#fcf8da" }, 1000);
	});
	
	//test drive click leads to fade
	$("#testDriveButton, .tdButton").click(function () {
		$("#testDriveBox .iconClosed").addClass("iconOpen");
		$("#testDriveForm").css({ display: "block" });
		$("#testDriveReport").animate({ backgroundColor: "#034876", color: "white"}, 1000);
		$(".tdButton").css({ color: "white"});
		$("#testDriveBox").animate({ backgroundColor: "#fcf8da" }, 1000);
	});	
	
	//fade a contact alert message and then have it dissapear
	if ( $("#containerMessage").length > 0 ) {
		$("#containerMessage").css({ backgroundColor: "#ffffff"});
		$("#containerMessage").animate({ 
			backgroundColor: "#FFFF33"}, 2000, "linear",
			function() { $("#containerMessage").animate({ 
				backgroundColor: "#ffffff", color: "#ffffff"}, 3000, "linear",
				function(){ $("#containerMessage").css({ display: "none" })	
			});
		});	
	}
	
	//show models automatically on search results page
	/*
	if ( $("#make").length > 0 ) {
		showModels( $("#make").val() )	
	}
	*/
	
	//dealership map
	if ( $("#map").length > 0 ) {
		loadMap();
	}
	
	//search toggle
	$("#searchLink a").attr("href","#search");
	$("#searchLink a").click(function () {
		$("#searchLink").css({display: "none"});
		$("#mainBox").addClass("showSearch");
		$("#searchBox").css({display: "block"});
		$("#keysearch").focus();
	});
	
	//car divs in results clickable
	$(".carDetails").hover(
		function() {
			$(this).addClass("active");
			$(this).find(".carname a:first").addClass("active");
		},
		function() {
			$(this).removeClass("active");
		}
	);
	
	$(".carDetails").click(function() {
		window.location = $(this).find(".pic a:first").attr("href");
	});
	
	//keyword search
	$("#keysearch").autocomplete('api/get/search.php', {
		minChars: 3,
		autoFill: false,
		mustMatch: false,
		cacheLength: 10,
		scrollHeight: 220,
		delay:100,
		selectFirst: false,
		max:7		
	});
	
	
	//map clicks
	$(".maplink1").click(function(event) {
		event.preventDefault();
		window.location = $(this).attr("target");			   
	});

	$(".maplink2").click(function(event) {
		event.preventDefault();
		window.location = $(this).attr("target");			   
	});
	
	$(".weblink1").click(function(event) {
		event.preventDefault();
		window.location = $(this).attr("target");			   
	});
	
	$(".weblink2").click(function(event) {
		event.preventDefault();
		window.location = $(this).attr("target");			   
	});	

});


//loan calulator
function calculatePayment() {
	
	//get the form values
	var price = $("#calcPrice").val();
	var down = $("#calcDownPayment").val();
	var rate = $("#calcRate").val();
	var years = $("#calcYears").val();
	
	//strip commas and money signs out of the price
	var newPrice = price.replace(/^\$|,/g,"");
	var newDown = down.replace(/^\$|,/g,"");
	
	//make sure certain ones aren't blank
	if( price.length < 1) {
		alert("Please provide the vehicle price");
		return false;
	}
	if( rate.length < 1 ) {
		alert ("Please provide an interest rate");	
		return false;
	}
	if( years.length < 1) {
		alert ("Please provide the length of the loan (in years)");	
		return false;
	}
	
	//make the calculation
	var power = Math.pow((1 + (rate / 1200)), (years * 12)); 
    var total = (newPrice - newDown) ; 
    var payment = (total * power) * (rate / 1200) / (power - 1);
	
	//only show 2 decimal places
	payment = payment.toFixed(2);
	
	//show the payment
	$("#monthlyPayment").html('$' + payment + ' per month');
	
	return false;

}

//validate contact form
function checkContactForm() {
	var cname = $("#your_name").val();
	var cemail = $("#your_email").val();
	var cphone = $("#your_phone").val();
	var cmessage = $("#message").val();
	
	if( cname.length < 1) {
		alert ("Please provide your name.");
		return false;
	}
	
	if( cemail.length < 1 && cphone < 1) {
		alert ("Please provider your email or phone so that we can get back to you");
		return false;
	}
	
	if( cmessage.length < 1) {
		alert ("Please provide a short message of why you are contacting us regarding this car");
		return false;
	}
	
	return true;
}

//validate vh form
function checkVHForm() {
	var cname = $("#vh_your_name").val();
	var cemail = $("#vh_your_email").val();
	var cphone = $("#vh_your_phone").val();

	if( cname.length < 1) {
		alert ("Please provide your name.");
		return false;
	}
	
	if( cemail.length < 1 && cphone < 1) {
		alert ("Please provider your email or phone so that we can get back to you");
		return false;
	}
	
	return true;	
}

//validate test drive
function checkTDForm() {
	var cname = $("#td_your_name").val();
	var cemail = $("#td_your_email").val();
	var cphone = $("#td_your_phone").val();
	var cmessage = $("#td_your_comments").val();
	
	if( cname.length < 1) {
		alert ("Please provide your name.");
		return false;
	}
	
	if( cemail.length < 1 && cphone < 1) {
		alert ("Please provider your email or phone so that we can get back to you");
		return false;
	}
	
	if( cmessage.length < 1) {
		alert ("Please provide a short message of when you want to come in to test drive");
		return false;
	}
	
	return true;	
}