////////////////////////////////////////////////////////////////
// Location information for index
////////////////////////////////////////////////////////////////
function show_city_footer(id){
	if (document.getElementById(id)){
		document.getElementById(id).style.display = '';
	}
}  
function hide_city_footer(){
	if (document.getElementById('city_footer_1')){
		document.getElementById('city_footer_1').style.display = 'none';
	}
	if (document.getElementById('city_footer_2')){
		document.getElementById('city_footer_2').style.display = 'none';
	}
	if (document.getElementById('city_footer_3')){
		document.getElementById('city_footer_3').style.display = 'none';
	}
}   

////////////////////////////////////////////////////////////////
// Google campaign conversions - centralized in case we add more campaigns so we can organize better and make fast changes
////////////////////////////////////////////////////////////////
function makeFrame(url) {

   ifrm = document.createElement("IFRAME");
   ifrm.setAttribute("src", url);
   ifrm.style.width = 1+"px";
   ifrm.style.height = 1+"px";
   ifrm.style.visibility = "hidden";
   document.body.appendChild(ifrm);
   
} 

function trackCampaignConversion(campaign, formtype) {
	
	// This function isnt as needed now that I changed a lot but if we need some custom stuff before calling the conversion then
	// this may be useful enough to keep for future
	switch (campaign.toLowerCase())
	{
		default:
			makeFrame('http://www.usedcarking.com/iconversion.asp?group=' + campaign + '&form=' + formtype);
	}
	
}

////////////////////////////////////////////////////////////////
// Tab information for index
////////////////////////////////////////////////////////////////
var tab_counter = 0;

function change_tab(tab_id, call_ajax, imagecoll){
				
	// hide all tab content
	for (tab_counter = 0; tab_counter < imagecoll.length; tab_counter++)
		document.getElementById(imagecoll[tab_counter][0] + "_content").style.display = 'none';
			
	// Reset all of the tabs to regular
	for (tab_counter = 0; tab_counter < imagecoll.length; tab_counter++)
		document.getElementById(imagecoll[tab_counter][0]).className = "search_img " + imagecoll[tab_counter][0];
			
	// Change the selected tab to selected.
	document.getElementById(tab_id).className = "search_img " + tab_id + "_on";
	
	// Display the correct tab panel.
	var tab_to_show = tab_id + '_content';
	document.getElementById(tab_to_show).style.display = '';
	
	// ajax call for side text
	// NOTE*** this only is used on index page where side content changes per tab click versus search page that just shows info for THAT search despite any tab clicking
	// and recently changed from ajax calls to hiding divs for seo purposes so pardon variable names at moment
	if (call_ajax == true) {
		var sideId = tab_id.split("_");
		document.getElementById('sideText-default').style.display = 'none';
		document.getElementById('sideText-payment').style.display = 'none';
		document.getElementById('sideText-mpg').style.display = 'none';
		document.getElementById('sideText-arrivals').style.display = 'none';
		document.getElementById('sideText-advanced').style.display = 'none';
		document.getElementById('sideText-' + sideId[2]).style.display = 'block';
	}
	
}  

////////////////////////////////////////////////////////////////
// Functionality for showing the expandable content area
////////////////////////////////////////////////////////////////
function expand_or_contract() {

	var expand_box = document.getElementById('expandable');
	var expand_current_state = "";
	
	if (expand_box.className == 'expand_content start_height')
		expand_current_state = "closed";
	else
		expand_current_state = "open";
		
	if (expand_current_state == "closed") {
		expand_box.className = 'expand_content reset_height';
		document.getElementById('expand_button').className = 'minimize';
		document.getElementById('expand_collapsed').style.display = 'none';
		document.getElementById('expand_opened').style.display = '';
	}
	else {
		expand_box.className = 'expand_content start_height';
		document.getElementById('expand_button').className = 'maximize';
		document.getElementById('expand_collapsed').style.display = '';
		document.getElementById('expand_opened').style.display = 'none';
	}

}

//////////////////////////////////////////////////////////////////////
// Functionality for showing sales rep box on call me now form and details pages
/////////////////////////////////////////////////////////////////////
function showDet(repID) {
	document.contactForm.salesRepName.value = document.getElementById('salesReps').value;
	if (repID != "Not Specified")
		document.getElementById("salesRepDet").innerHTML = document.getElementById(repID).innerHTML;
	else
		document.getElementById("salesRepDet").innerHTML = "";
}

function showContactDet(repID) {
	document.contactus.salesRepName.value = document.getElementById('salesReps').value;
	if (repID != "Not Specified")
		document.getElementById("salesRepDet").innerHTML = document.getElementById(repID).innerHTML;
	else
		document.getElementById("salesRepDet").innerHTML = "";
}

function cmshowDet(repID) {
	document.send_callme.cmsalesRepName.value = document.getElementById('cmLocation').value;
	if (repID != "Not Specified" && repID.indexOf('NoPreference') == -1) { // pick a rep
		document.getElementById("cmsalesRepDet").innerHTML = document.getElementById('cm' + repID).innerHTML;
		}
	else if (repID.indexOf('NoPreference') != -1) { // pick a store but no rep
		if (repID.indexOf('_Chevy') != -1)
			document.getElementById("cmsalesRepDet").innerHTML = "<div style='margin-left:5px;text-align:left;'>Any Rep from Sun Chevrolet</div>";
		else if (repID.indexOf('_Cicero') != -1)
			document.getElementById("cmsalesRepDet").innerHTML = "<div style='margin-left:5px;text-align:left;'>Any Rep from Sun Auto Warehouse Cicero</div>";
		else
			document.getElementById("cmsalesRepDet").innerHTML = "<div style='margin-left:5px;text-align:left;'>Any Rep from Sun Auto Warehouse Cortland</div>";
		}
	else {
		document.getElementById("cmsalesRepDet").innerHTML = ""; // no store no rep
		}
}

////////////////////////////////////////////////////////////////
// Functionality for showing the specs
////////////////////////////////////////////////////////////////
function expand_specs() {

	var expand_specs = document.getElementById('expandable');
	var specs_current_state = "";
	
	if (expand_specs.className == 'specs_content unexpand_height')
		specs_current_state = "closed";
	else
		specs_current_state = "open";
		
	if (specs_current_state == "closed") {
		expand_specs.className = 'specs_content expand_height';
		document.getElementById('expand_button').className = 'minimize';
	}
	else {
		expand_specs.className = 'specs_content unexpand_height';
		document.getElementById('expand_button').className = 'maximize';
	}

}

// On search result page, give the function to search again from results
function search_results_again(search_result_status) {

	if (search_result_status == 'again') {
		document.getElementById('default_search_again').style.display = '';
		document.getElementById('default_search_results').style.display = 'none';
	}
	else {
		document.getElementById('default_search_again').style.display = 'none';
		document.getElementById('default_search_results').style.display = '';
	}

}

function gourl(link) {
	location.href = link;
}

////////////////////////////////////////////////////////////////
// Year data for year range boxes that keep you in appropriate min/max range
////////////////////////////////////////////////////////////////
var yearsToShow = 20;
var thisDate = new Date();
var thisYear = thisDate.getFullYear();
var year_list = new Array(yearsToShow);
for (iYear = thisYear-yearsToShow; iYear <= thisYear; iYear++)
	year_list[iYear-(thisYear-yearsToShow)] = iYear;

function populate_yeardrops(objMin,objMax) {
	for (i=0; i<year_list.length; i++){
		year = year_list[i];
		add_list_item(document.getElementById(objMin),year,year);
		add_list_item(document.getElementById(objMax),year,year);
	}
}

function year_min_changed(objMin,objMax) {
	// Get the index of the min drop box.
	var min_index = document.getElementById(objMin).selectedIndex;
	// Clear out the options from the max drop box.
	empty_list(document.getElementById(objMax))
	// Add the list items starting with the min index.
	for (i = min_index; i < year_list.length; i++){
		year = year_list[i];
		add_list_item(document.getElementById(objMax),year,year)
	}
}

////////////////////////////////////////////////////////////////
// MyGarage Functions
////////////////////////////////////////////////////////////////
var last_img = "";
function mygarage_bling(state,stock) {

	if (state == "over") {
		last_img = document.getElementById('mygarage_'+stock).src;
		document.getElementById('mygarage_'+stock).src = '/images/money_icon.jpg';
	}
	else {
		document.getElementById('mygarage_'+stock).src = last_img;
	}

}

function mygarageContact(memo) {
	document.getElementById('cmComments').value = memo;
	show_form('call_me_form');
}

////////////////////////////////////////////////////////////////
// Misc functions
////////////////////////////////////////////////////////////////
function PostStockSearch(tagid) {
	// Check the stock number field...
	if (document.getElementById(tagid).value != ""){
		var url = '/inventory/details2.asp?vehicle_id=' + document.getElementById(tagid).value + '#tippytop';
		location.href = url;
	}
}

function printPage(stock) {
	var link = "/inventory/print_page.asp?vehicle_id=" + stock;
	window.open (link,"WindowSticker","status=1,scrollbars=1,menubar=1,resizable=1,width=850,height=900"); 

}

////////////////////////////////////////////////////////////////
// Menu functionality for option menu and about us page menu using jquery
////////////////////////////////////////////////////////////////
function openMenu(headerid) {
	var header = document.getElementById(headerid);
	if (header.className == 'menuHeadingon')
		header.className = 'menuHeading';
	else
		header.className = 'menuHeadingon';
}

function openParentMenu(headerid) {
	var header = document.getElementById(headerid);
	if (header.className == 'parentMenuHeadingon')
		header.className = 'parentMenuHeading';
	else
		header.className = 'parentMenuHeading';
}

////////////////////////////////////////////////////////////////
// Basic list box functions
////////////////////////////////////////////////////////////////
function add_list_item(ob, value, text){
	var option = document.createElement("OPTION");
	IE = ((document.all)&&(navigator.appVersion.indexOf("MSIE")!=-1)) ? true : false;

	option.value = value;
	option.text = text;

	if (IE)
		ob.add(option);
	else
		ob.add(option, null);
}
		
function empty_list(ob){
	while(ob.length){
		ob.remove(0);
	}
}

////////////////////////////////////////////////////////////////
// Basic functions for popup ajax forms like call me or carfinder
////////////////////////////////////////////////////////////////
function show_form(form_name){
	document.getElementById('screen').style.display = '';
	intervalID = setInterval(move_screen, 100);
    document.getElementById(form_name).style.display = '';
	document.getElementById(form_name).style.top = (document.documentElement.scrollTop + 200) + 'px';
}

function move_screen() {
    var offset = 0; // set offset (likely equal to your css top)
    var element = document.getElementById('screen');
    element.style.top = (document.documentElement.scrollTop + offset) + 'px';
}

////////////////////////////////////////////////////////////////
// Functions for allowing user to press enter to submit stock number form
////////////////////////////////////////////////////////////////
function submitenter(myfield,e){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   PostStockSearch('find_stock');
   return false;
   }
else
   return true;
}

////////////////////////////////////////////////////////////////
// Reused functions
////////////////////////////////////////////////////////////////
function isNumber(val){
	if (isNaN(val)){
		return false;
	}else{
		return true;
	} 
}

function removeElement(parent,div){
	var d1=document.getElementById(parent);
	var d2=document.getElementById(div);
	d1.removeChild(d2);
} 
	
function formatCurrency(strValue){
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	dblValue = Math.floor(dblValue/100).toString();

	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue);
}

function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)){
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function expand_me(panel_id,button_id){
	if(document.getElementById(panel_id).style.height == ''){
		document.getElementById(panel_id).style.height = '60px';
		if(document.getElementById(button_id)){
			document.getElementById(button_id).innerHTML = 'Expand';
		}
	}else{
		document.getElementById(panel_id).style.height = '';
		if(document.getElementById(button_id)){
			document.getElementById(button_id).innerHTML = 'Hide';
		}            
	}
} 

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++){
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
			theForm[z].checked = theElement.checked;
		}
	}
}
