
var selectThing;
function anyCheck(form) {
var total = 0;
var max = form.ckbox.length;
for (var idx = 0; idx < max; idx++) {
if (eval("document.playlist.ckbox[" + idx + "].checked") == true) {
    total += 1;
   }
}
alert("You selected " + total + " boxes.");
}

jQuery(function($){$(document).ready(function() {

function displayError(image){
	alert(image);	
}
	function closeFB(){
		$(document).trigger('close.facebox')	
	}
	car_calculate = function()
{
	var price = document.getElementById("car_purchaseprice").value;
	if ( price.length == 0 )
	{
		document.getElementById('errors').innerHTML='<a href="images/ucc/priceerr.png" id="priceerrs"></a>';
		$("#priceerrs").facebox().trigger('click');
		return;
	}
	price = fixNumber(price);
	price = parseFloat(price);
	if ( isNaN(price) )
	{
		alert("Please enter the  purchase price as a number");
		return;
	}
	if ( price < 0 )
	{
		alert("Please enter a purchase price >= 0");
		return;
	}
	
	var rate = document.getElementById("car_rate").value;
	if ( rate.length == 0 )
	{
		alert("Please enter an interest rate");
		return;
	}
	rate = fixNumber(rate);
	rate = parseFloat(rate);
	if ( isNaN(rate) )
	{
		alert("Please enter the interest rate as a number");
		return;
	}
	if ( rate < 0 || rate >= 35 )
	{
		alert("Please enter a interest rate >= 0 and less than 35");
		return;
	}

	var months = document.getElementById("car_duration").value;
	if ( months.length == 0 )
	{
		alert("Please enter the # of months you loan will last");
		return;
	}
	months = fixNumber(months);
	months = parseFloat(months);
	if ( isNaN(months) )
	{
		alert("Please enter the # of months as a number");
		return;
	}
	if ( months <= 0 || months > 120 )
	{
		alert("Please enter a # of months between 1 and 120");
		return;
	}

	var downpayment = document.getElementById("car_down").value;
	if ( downpayment.length == 0 )
	{
		document.getElementById('errors').innerHTML='<a href="images/ucc/downPay.png" id="downPayment"></a>';
		$("#downPayment").facebox().trigger('click');
		return;
	}
	downpayment = fixNumber(downpayment);
	downpayment = parseFloat(downpayment);
	if ( isNaN(downpayment) )
	{
		alert("Please enter the down payment as a number");
		return;
	}
	if ( downpayment < 0 )
	{
		alert("Please enter a down payment >= 0");
		return;
	}
	if ( downpayment >= price )
	{
		alert("Your down payment exceeds the car price, so you won't need a loan.");
		return;
	}
	
	var mrate = rate / 1200;

	var loanamount = price - downpayment;
	var payment;

	if ( rate == 0 )
	{
		payment = loanamount / months;
	}
	else
	{
		var factor = Math.pow(1 + mrate, months );
		payment = loanamount * ( mrate / (1 - 1/factor) );
	}
	
	var x = document.getElementById("car_dt_results");

	//var results = formatDollars(loanamount);

	var results = formatDollars(payment);
	
	//results += "<br><br>You will pay a total of <b>" + formatDollars(((payment * months) + downpayment) - price) + "</b> in interest.";
	results = results.replace("$", "");
	x.value = results;
	x.style.display = "block";
}
$("#signIn").click(function(){
	if(selectThing == "register"){
		$(".registerBox").css('display', 'none');
	}
	$(".loginBox").css('display', 'block');
	selectThing = "login";
});
$("#closeBtn").click(function(){
	$(".loginBox").css('display', 'none');
	selectThing = null;
});
$("#register").click(function(){
	if(selectThing == "login"){
		$(".loginBox").css('display', 'none');
	}
	selectThing = "register";
	$(".registerBox").css('display', 'block');
});
$("#closeBtnReg").click(function(){
	$(".registerBox").css('display', 'none');
	selectThing = null;
});
$('#dealerLocate').click(function(){
	$('.locateBox').css('display', 'block');
});
$("#closeBtnLocate").click(function(){
	$(".locateBox").css('display', 'none');
});

$("#searchBtn").mousedown(function(){
		document.getElementById('searchBtn').className="searchBarBtnDown";
});
$("#searchBtn").mouseup(function(){
		document.getElementById('searchBtn').className="searchBarBtn";
});
	//When page loads...
	$(".panes").hide(); //Hide all content
	$("ul.quickMenu li:first").addClass("selected").show(); //Activate first tab
	$(".panes:first").show(); //Show first tab content

	//On Click Event
	var lastId;
	$("ul.quickMenu li").click(function() {
		
		$("ul.quickMenu li").removeClass("selected"); //Remove any "active" class
		$(this).addClass("selected"); //Add "active" class to selected tab
		$(".panes").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		
	});
	$("#0").click(function(){
		lastId = null;
		$('.opaqueCont').height($(document).height());
		$('#container').height($(document).height());
		
		//$("#quickSearch").css("z-index", 1);
	});
	$("#1").click(function(){
		lastId = null;
		$('.middle').css('height', 270);
		$('.opaqueCont').height($(document).height());
		$('#container').height($(document).height());
		//$(".panes").css("width", 320);
		$("#quickSearch").css("z-index", 1);
		
	});
	$("#4").click(function(){
		lastId = null;
		//$(".panes").css("width", 320);
		$('.middle').css('height', 270);
		$("#quickSearch").css("z-index", 1);
		
	});
	$('#5').click(function(){
		$('.middle').css('height', 270);
	});
	var firstItem = true;
	$("#3").click(function(){
		if(currentHeight === null){
			dropDown(firstMake);
		}else{
			$('.middle').height(currentHeight);
		}
		firstMake = null;
		dropDown($('.dropDownModels').attr('id').replace('dropdown-', ''));
		//$(".panes").css("width", 602);
		$("#quickSearch").css("z-index", 10);	
	});
	$("#2").click(function(){
		lastId = null;
		$('.opaqueCont').height($(document).height());
		$('#container').height($(document).height());
		$('.middle').css('height', 370);
		//$(".panes").css("width", 320)
		$("#quickSearch").css("z-index", 1);
		
	});
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
$('#nav li').toggle(function(){

	document.getElementById(this.id+'-options').className="display";
	document.getElementById(this.id).className="open";
	document.getElementById(this.id).style.color="#000";
}, function(){
	document.getElementById(this.id+'-options').className="displayNone";
	document.getElementById(this.id).className="closed";
	document.getElementById(this.id).style.color="#fff";
});
});



function processForm(myform,obj){
	var formId=myform.getAttribute("id");
	var hiddenField = document.createElement("input");
	hiddenField.setAttribute("type", "hidden");
	hiddenField.setAttribute("name", obj.name);
	hiddenField.setAttribute("value", obj.value);
	myform.appendChild(hiddenField);
}

 $(document).ready(function(){
 $('#overview').toggle(function(){
	$('#div1').css('display', 'none');
	
	document.getElementById('overview').src="images/ucc/overview_plus.jpg";
}, function(){
	$('#div1').css('display', 'block');
	document.getElementById('overview').src="images/ucc/overview_minus.jpg";
});

 

 $('#safety').toggle(function(){
	$('#div2').css('display', 'block');
	document.getElementById('safety').src="images/ucc/safety_minus.jpg";
}, function(){
	$('#div2').css('display', 'none');
	document.getElementById('safety').src="images/ucc/safety_plus.jpg";
});

 

 $('#features').toggle(function(){
	$('#div3').css('display', 'block');
	document.getElementById('features').src="images/ucc/features_minus.jpg";
}, function(){
	$('#div3').css('display', 'none');
	document.getElementById('features').src="images/ucc/features_plus.jpg";
});
 $('#monthly').toggle(function(){
	$('#div4').css('display', 'none');
	document.getElementById('monthly').src="images/ucc/monthlyPlus.jpg";
}, function(){
	$('#div4').css('display', 'block');
	document.getElementById('monthly').src="images/ucc/monthlyMinus.jpg";
});

car_calculate2 = function(num)
{
	var price = document.getElementById("car_purchaseprice"+num).value;
	if ( price.length == 0 )
	{
		document.getElementById('errors').innerHTML='<a href="images/ucc/priceerr.png" id="priceerrs"></a>';
		$("#priceerrs").facebox().trigger('click');
		return;
	}
	price = fixNumber(price);
	price = parseFloat(price);
	if ( isNaN(price) )
	{
		alert("Please enter the  purchase price as a number");
		return;
	}
	if ( price < 0 )
	{
		alert("Please enter a purchase price >= 0");
		return;
	}
	
	var rate = document.getElementById("car_rate"+num).value;
	if ( rate.length == 0 )
	{
		alert("Please enter an interest rate");
		return;
	}
	rate = fixNumber(rate);
	rate = parseFloat(rate);
	if ( isNaN(rate) )
	{
		alert("Please enter the interest rate as a number");
		return;
	}
	if ( rate < 0 || rate >= 35 )
	{
		alert("Please enter a interest rate >= 0 and less than 35");
		return;
	}

	var months = document.getElementById("car_duration"+num).value;
	if ( months.length == 0 )
	{
		alert("Please enter the # of months you loan will last");
		return;
	}
	months = fixNumber(months);
	months = parseFloat(months);
	if ( isNaN(months) )
	{
		alert("Please enter the # of months as a number");
		return;
	}
	if ( months <= 0 || months > 120 )
	{
		alert("Please enter a # of months between 1 and 120");
		return;
	}

	var downpayment = document.getElementById("car_down_"+num).value;
	if ( downpayment.length == 0 )
	{
		document.getElementById('errors').innerHTML='<a href="images/ucc/downPay.png" id="downPayment"></a>';
		$("#downPayment").facebox().trigger('click');
		return;
	}
	downpayment = fixNumber(downpayment);
	downpayment = parseFloat(downpayment);
	if ( isNaN(downpayment) )
	{
		alert("Please enter the down payment as a number");
		return;
	}
	if ( downpayment < 0 )
	{
		alert("Please enter a down payment >= 0");
		return;
	}
	if ( downpayment >= price )
	{
		alert("Your down payment exceeds the car price, so you won't need a loan.");
		return;
	}
	
	var mrate = rate / 1200;

	var loanamount = price - downpayment;
	var payment;

	if ( rate == 0 )
	{
		payment = loanamount / months;
	}
	else
	{
		var factor = Math.pow(1 + mrate, months );
		payment = loanamount * ( mrate / (1 - 1/factor) );
	}
	
	var x = document.getElementById("car_dt_results"+num);

	//var results = formatDollars(loanamount);

	var results = formatDollars(payment);
	
	//results += "<br><br>You will pay a total of <b>" + formatDollars(((payment * months) + downpayment) - price) + "</b> in interest.";
	results = results.replace("$", "");
	x.value = results;
	x.style.display = "block";
}



});
$('#share').toggle(function(){
	$('#shareLinks').css('display', 'block', 'position', 'relative', 'z-index', 99);
}, function(){
	$('#shareLinks').css('display', 'none');
});

});

function sortSearch(page){
	switch(document.change.sortBy.value){

		case "Price-highLow":
		//alert(page);
		window.location = page+'&sorting_field=Price&sorting_order=DESC&';
		//document.change.sortBy.PriceHighLow.selectedIndex = 1;
		break;
		case "Price-lowHigh":

		window.location = page+'&sorting_field=Price&sorting_order=ASC&';

		break;
		
		case "Miles-highLow":
		window.location = page+'&sorting_field=Mileage&sorting_order=DESC&';

		break;
		case "Miles-lowHigh":

		window.location = page+'&sorting_field=Mileage&sorting_order=ASC&';

		break;
		case "Year-highLow":
		window.location = page+'&sorting_field=Year&sorting_order=DESC&';

		break;
		case "Year-lowHigh":

		window.location = page+'&sorting_field=Year&sorting_order=ASC&';

		break;
	}
}


