// redirects to same page replacing existing querystring argument with new one specified, keeping all other arguments the same
function go_url(_argument, value){
	var current_url = unescape(location.href);
	var q_pos = current_url.indexOf('?');
	var querystring = current_url.substring(q_pos + 1);
	var new_querystring = '';
	var found__argument = false;
	_arguments = querystring.split('&');
	for (i = 0; i < _arguments.length ; i++){
		if (_arguments[i].indexOf(_argument + '=') >= 0){
			_arguments[i] = _argument + '=' + value;
			found__argument = true;
		}
		new_querystring += _arguments[i] + '&';
	}
	//eliminate trailing ampersand
	new_querystring = new_querystring.substring(0, new_querystring.length - 1);
	if (found__argument == false){
		new_querystring = querystring + '&' + _argument + '=' + value;
	}
	newLocationString = '?' + new_querystring;
	location.href= newLocationString;
}  

// 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 = imagecoll[tab_counter][0];
			
	// Change the selected tab to selected.
	document.getElementById(tab_id).className = 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
	//if (call_ajax == true)
	//	document.getElementById("sideText").innerHTML = populateText(tab_id);
	
}  

// 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 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 = '';
	}

}

// Change from new or used on a search
function type_changed_search(newtype,source) {

	if (source == 'home') {
		location.href = '/index.asp?type=' + newtype;
	}
	else {
		go_url('type',newtype);
	}

}

function gourl(link) {
	location.href = link;
}

// Rollovers for mygarage
var last_img = "";
function mygarage_bling(state,vin) {

	if (state == "over") {
		last_img = document.getElementById('mygarage_'+vin).src;
		document.getElementById('mygarage_'+vin).src = '/mygarage/images/money_icon.jpg';
	}
	else {
		document.getElementById('mygarage_'+vin).src = last_img;
	}

}

function PostStockSearch(tagid) {
	// Check the stock number field...
	if (document.getElementById(tagid).value != ""){
		var url = '/inventory/details.asp?vehicle=' + document.getElementById(tagid).value;
		location.href = url;
	}
}

function removeElement(parent,div){
	var d1=document.getElementById(parent);
	var d2=document.getElementById(div);
	d1.removeChild(d2);
} 

function printPage(stock) {
	var link = "print_page.asp?vehicle_id=" + stock;
	window.open (link,"WindowSticker","status=1,scrollbars=1,menubar=1,resizable=1,width=850,height=900"); 

}

function find_dealer() {
	var zipcode = document.getElementById('zipcode').value;
	var radius = document.getElementById("radius").options[document.getElementById("radius").selectedIndex].value
	var errorlog = "";
	
	// error checking for zip code search
	if (radius != 'any') {
		// make sure its filled in first and foremost
		if (zipcode.length == 0) {
			errorlog = errorlog + "* Zip code must be specified when not searching Any Distance\n";
		}
		else { // then make sure its 5 characters and those characters are numeric
			if (zipcode.length != 5) {
				errorlog = errorlog + "* Zip code must only be 5 digits\n";
			}
			if (IsNumeric(zipcode) == false) {
				errorlog = errorlog + "* Zip code must numeric characters only\n";
			}
		}
	}
	
	// if any distance is selected then do the usual function and go
	if (radius == 'any') {
		location.href = '/dealers.asp';
	}
	else {
		if (errorlog == "") { // if zipcode is specified then add on its parameters assuming no errors
			location.href = '/dealers.asp?zipcode=' + zipcode + '&radius=' + radius;
		}
		else {
			alert('Please make corrections to your search: \n' + errorlog); // if errors then show and do not redirect yet
		}
	}
}

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';
}

// Details page stuff
function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}

function VehicleSpecsTab_Click(){

	var obj = null;

	obj = document.getElementById("tab_options");
	if(obj != null){
		obj.style.display = "none";
	}

	obj = document.getElementById("tab_techspecs");
	if(obj != null){
		obj.style.display = "none";
	}

	obj = document.getElementById("tab_vehiclespecs");
	obj.style.display = "";
	obj = document.getElementById("btn_vehiclespecs");
	obj.className = "vehicle_specs_over";
	obj = document.getElementById("btn_techspecs");
	obj.className = "tech_specs";
	obj = document.getElementById("btn_options");
	obj.className = "options";

	return void(0);
}

function OptionsTab_Click(){

	var obj = null;

	obj = document.getElementById("tab_vehiclespecs");
	if(obj != null){
		obj.style.display = "none";
	}

	obj = document.getElementById("tab_techspecs");
	if(obj != null){
		obj.style.display = "none";
	}

	obj = document.getElementById("tab_options");
	obj.style.display = "";
	obj = document.getElementById("btn_options");
	obj.className = "options_over";
	obj = document.getElementById("btn_techspecs");
	obj.className = "tech_specs";
	obj = document.getElementById("btn_vehiclespecs");
	obj.className = "vehicle_specs";

	return void(0);
}

function TechSpecsTab_Click(){

	var obj = null;

	obj = document.getElementById("tab_vehiclespecs");
	if(obj != null){
		obj.style.display = "none";
	}

	obj = document.getElementById("tab_options");
	if(obj != null){
		obj.style.display = "none";
	}

	obj = document.getElementById("tab_techspecs");
	obj.style.display = "";
	obj = document.getElementById("btn_techspecs");
	obj.className = "tech_specs_over";
	obj = document.getElementById("btn_vehiclespecs");
	obj.className = "vehicle_specs";
	obj = document.getElementById("btn_options");
	obj.className = "options";

	return void(0);
}

function display_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 + 100) + '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';
}

function redirect(loc) {
	location.href = loc;
}

function opennew(loc) {
	window.open(loc,'Carfax','width=700,height=600,resizable=yes,scrollbars=yes');
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

