/*****************************************************************************************************
*********	browse/master.js -- javascript functions commonly used on browse page only
*************************************************************************************************** */

function compareVehicles(alertid, checkboxgroup){

	// purpose: compare all checked vehicles side by side
	
	var field = document.compareVehiclesForm.compare;
	var amountChecked = 0;
	for (i = 0; i < field.length; i++)
	{
		if (field[i].checked == true)
			amountChecked = amountChecked + 1;
	}
	if (amountChecked > 1 && amountChecked < 5)
		document.getElementById("compareVehiclesForm").submit();
	else if(amountChecked >= 5)
		inlineMsg(alertid, '<strong>Error</strong><br />Please select less than five vehicles in order to compare them.', 2);
	else
		inlineMsg(alertid, '<strong>Error</strong><br />Please select more than one vehicle in order to compare them.', 2);
	
} 

function soryBy() {
	
	// purpose: go to loaded sortby url in option value
	var sortbybox = document.getElementById('sortby');
	var sortbyvalue = sortbybox.options[sortbybox.selectedIndex].value;
	redirect(sortbyvalue);
	
}

function showSearch() {
		
	// Show the background screen and form and set the interval to move them as the user scrolls
	searchContent('sYear');
	jQuery('#searchBox, body.browserIE6 #ie6SearchBehind').fadeIn("slow");
	document.getElementById('screen').style.display = '';
	document.getElementById('searchBox').style.top = ((document.documentElement.scrollTop || document.body.scrollTop) + 100) + 'px';
	document.getElementById('ie6SearchBehind').style.top = ((document.documentElement.scrollTop || document.body.scrollTop) + 100) + 'px';
	intervalID = setInterval(function() {
		document.getElementById('screen').style.top = (document.documentElement.scrollTop || document.body.scrollTop) + 'px';
	}, 100);
		
}

function hideSearch() {
		
	//Reset screen and hide main form area
	clearInterval(intervalID);
	jQuery('#searchBox, body.browserIE6 #ie6SearchBehind').fadeOut("fast");
    document.getElementById('screen').style.display = 'none';
	
}

function resetSearchClass() {

	document.getElementById('sYear').className = 'searchfield';
	if (document.getElementById('sMake'))
		document.getElementById('sMake').className = 'searchfield';
	if (document.getElementById('sModel'))
		document.getElementById('sModel').className = 'searchfield';
	document.getElementById('sPrice').className = 'searchfield';
	if (document.getElementById('sMileage'))
		document.getElementById('sMileage').className = 'searchfield';
	document.getElementById('sExtColor').className = 'searchfield';
	document.getElementById('sIntColor').className = 'searchfield';
	document.getElementById('sTrans').className = 'searchfield';
	document.getElementById('sMisc').className = 'searchfield';

}

function searchContent(contentid) {
		
	// purpose: show search content
	// tab control and effects
	resetSearchClass();
	document.getElementById(contentid).className = 'searchfield on';
	
	// get content
	if (contentid == "sExtColor")
		document.getElementById('sContent').innerHTML = document.getElementById('ExtColor').innerHTML;
	else if (contentid == "sIntColor")
		document.getElementById('sContent').innerHTML = document.getElementById('IntColor').innerHTML;
	else if (contentid == "sMake")
		document.getElementById('sContent').innerHTML = document.getElementById('Make').innerHTML;
	else if (contentid == "sModel") {
		if (document.getElementById('Model'))
			document.getElementById('sContent').innerHTML = document.getElementById('Model').innerHTML;
		}
	else if (contentid == "sTrans")
		document.getElementById('sContent').innerHTML = document.getElementById('Trans').innerHTML;
	else if (contentid == "sYear")
		document.getElementById('sContent').innerHTML = document.getElementById('rYear').innerHTML;
	else if (contentid == "sPrice")
		document.getElementById('sContent').innerHTML = document.getElementById('rPrice').innerHTML;
	else if (contentid == "sMisc")
		document.getElementById('sContent').innerHTML = document.getElementById('rMisc').innerHTML;
	else
		document.getElementById('sContent').innerHTML = document.getElementById('rMileage').innerHTML;
}

function findMakes(link) {

	var makes = "";
	var cbxMakes = document.sContent.cbMake;
	var canProceed = false;
	
	if( cbxMakes.length ){
		for (i = 0; i < cbxMakes.length; i++)
		{
			if (cbxMakes[i].checked)
			{
				makes = makes + "make_" + cbxMakes[i].value + "/";
				canProceed = true;
			}
		}
	} else {
		if (cbxMakes.checked)
		{
			makes = makes + "make_" + cbxMakes.value + "/";
			canProceed = true;
		}
	}
	var pos = link.indexOf("/make_") + 1;
	var redirect = link.replace(/make_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	// we need to kill models too or else theyll be locked in a model search with unrelated makes and thus no matches
	redirect = redirect.replace(/model_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	redirect = redirect.replace(/extcolor_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	redirect = redirect.replace(/intcolor_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	redirect = redirect.replace(/trans_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	
	if ((pos-1) < 0) {
		redirect = link + makes;
		if (Right(redirect, 1) != "/")
			redirect = redirect + "/"
	}
	else {
		var firstHalf = Left(redirect, pos);
		var lastHalf = Right(redirect, redirect.length - pos + 1);
		redirect = firstHalf + Left(makes, makes.length - 1) + lastHalf;
		// get rid of dbl slash mainly if filtering and unselecting all makes
		redirect = redirect.replace(/\/\//gi, "/");
		redirect = redirect.replace(/http:\//gi, "http://");
		if (Right(redirect, 1) != "/")
			redirect = redirect + "/"
	}
	
	if (canProceed = true) {
		window.location = redirect;
	}

}

function findModels(link) {

	var models = "";
	var cbxModels = document.sContent.cbModel;
	var canProceed = false;
	
	if( cbxModels.length ){
		for (i = 0; i < cbxModels.length; i++)
		{
			if (cbxModels[i].checked)
			{
				models = models + "model_" + cbxModels[i].value + "/";
				canProceed = true;
			}
		}
	} else {
		if (cbxModels.checked)
		{
			models = models + "model_" + cbxModels.value + "/";
			canProceed = true;
		}
	}
	
	var pos = link.indexOf("/model_") + 1;
	var redirect = link.replace(/model_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	
	if ((pos-1) < 0) {
		redirect = link + models;
	}
	else {
		var firstHalf = Left(redirect, pos);
		var lastHalf = Right(redirect, redirect.length - pos + 1);
		redirect = firstHalf + Left(models, models.length - 1) + lastHalf;
		// get rid of dbl slash mainly if filtering and unselecting all models
		redirect = redirect.replace(/\/\//gi, "/");
		redirect = redirect.replace(/http:\//gi, "http://");
	}
	
	if (canProceed = true) {
		//alert(redirect);
		window.location = redirect;
	}

}

function findTrims(link) {

	var trims = "";
	var cbxTrims = document.sContent.cbTrim;
	var canProceed = false;
	
	if( cbxTrims.length ){
		for (i = 0; i < cbxTrims.length; i++)
		{
			if (cbxTrims[i].checked)
			{
				trims = trims + "trim_" + cbxTrims[i].value + "/";
				canProceed = true;
			}
		}
	} else {
		if (cbxTrims.checked)
		{
			trims = trims + "trim_" + cbxTrims.value + "/";
			canProceed = true;
		}
	}
	
	var pos = link.indexOf("/trim_") + 1;
	var redirect = link.replace(/trim_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	
	if ((pos-1) < 0) {
		redirect = link + trims;
	}
	else {
		var firstHalf = Left(redirect, pos);
		var lastHalf = Right(redirect, redirect.length - pos + 1);
		redirect = firstHalf + Left(trims, trims.length - 1) + lastHalf;
		// get rid of dbl slash mainly if filtering and unselecting all trims
		redirect = redirect.replace(/\/\//gi, "/");
		redirect = redirect.replace(/http:\//gi, "http://");
	}
	
	if (canProceed = true) {
		//alert(redirect);
		window.location = redirect;
	}

}

function findExtColor(link) {

	var colors = "";
	var cbxExtColor = document.sContent.cbExtColor;
	var canProceed = false;

	if( cbxExtColor.length ){
		for (i = 0; i < cbxExtColor.length; i++)
		{
			if (cbxExtColor[i].checked)
			{
				colors = colors + "extcolor_" + cbxExtColor[i].value + "/";
				canProceed = true;
			}
		}
	} else {
		if (cbxExtColor.checked)
		{
			colors = colors + "extcolor_" + cbxExtColor.value + "/";
			canProceed = true;
		}
	}
	var pos = link.indexOf("/extcolor_") + 1;
	var redirect = link.replace(/extcolor_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	
	if ((pos-1) < 0) {
		redirect = link + colors;
	}
	else {
		var firstHalf = Left(redirect, pos);
		var lastHalf = Right(redirect, redirect.length - pos + 1);
		redirect = firstHalf + Left(colors, colors.length - 1) + lastHalf;
		// get rid of dbl slash mainly if filtering and unselecting all colors
		redirect = redirect.replace(/\/\//gi, "/");
		redirect = redirect.replace(/http:\//gi, "http://");
	}
	
	if (canProceed = true) {
		//alert(redirect);
		window.location = redirect;
	}

}

function findIntColor(link) {

	var colors = "";
	var cbxIntColor = document.sContent.cbIntColor;
	var canProceed = false;

	if( cbxIntColor.length ){
		for (i = 0; i < cbxIntColor.length; i++)
		{
			if (cbxIntColor[i].checked)
			{
				colors = colors + "intcolor_" + cbxIntColor[i].value + "/";
				canProceed = true;
			}
		}
	} else {
		if (cbxIntColor.checked)
		{
			colors = colors + "intcolor_" + cbxIntColor.value + "/";
			canProceed = true;
		}
	}
	
	var pos = link.indexOf("/intcolor_") + 1;
	var redirect = link.replace(/intcolor_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	
	if ((pos-1) < 0) {
		redirect = link + colors;
	}
	else {
		var firstHalf = Left(redirect, pos);
		var lastHalf = Right(redirect, redirect.length - pos + 1);
		redirect = firstHalf + Left(colors, colors.length - 1) + lastHalf;
		// get rid of dbl slash mainly if filtering and unselecting all colors
		redirect = redirect.replace(/\/\//gi, "/");
		redirect = redirect.replace(/http:\//gi, "http://");
	}
	
	if (canProceed = true) {
		//alert(redirect);
		window.location = redirect;
	}

}

function findTrans(link) {

	var trans = "";
	var cbxTrans = document.sContent.cbTrans;
	var canProceed = false;

	if( cbxTrans.length ){
		for (i = 0; i < cbxTrans.length; i++)
		{
			if (cbxTrans[i].checked)
			{
				trans = trans + "trans_" + cbxTrans[i].value + "/";
				canProceed = true;
			}
		}
	} else {
		if (cbxTrans.checked)
		{
			trans = trans + "trans_" + cbxTrans.value + "/";
			canProceed = true;
		}
	}
	
	var pos = link.indexOf("/trans_") + 1;
	var redirect = link.replace(/trans_[A-Za-z0-9_-]+[\/]{1}/gi, "");
	
	if ((pos-1) < 0) {
		redirect = link + trans;
	}
	else {
		var firstHalf = Left(redirect, pos);
		var lastHalf = Right(redirect, redirect.length - pos + 1);
		redirect = firstHalf + Left(trans, trans.length - 1) + lastHalf;
		// get rid of dbl slash mainly if filtering and unselecting all trans
		redirect = redirect.replace(/\/\//gi, "/");
		redirect = redirect.replace(/http:\//gi, "http://");
	}
	
	if (canProceed = true) {
		//alert(redirect);
		window.location = redirect;
	}

}

function findBlanks(link, thetype) {

	var high;
	var low;
	var pos = link.indexOf("/" + thetype + "_") + 1;
	var redirect;
	var temp;
	
	if (thetype == "year") {
		low = parseInt(trim(document.sContent.rYearFrom.value));
		high = parseInt(trim(document.sContent.rYearTo.value));
		if (IsNumeric(low) == false || IsNumeric(high) == false) {
			alert('Year must be a four digit number');
			return;
		}
		if (low > high) {
			temp = low;
			low = high;
			high = temp;
		}
		redirect = link.replace(/year_[0-9]+_[0-9]+[\/]{1}/gi, "");
	}
	else if (thetype == "price") {
		low = parseInt(trim(document.sContent.rPriceFrom.value));
		high = parseInt(trim(document.sContent.rPriceTo.value));
		if(low == 0 && high == 0){
			return;
		}
		if (IsNumeric(low) == false || IsNumeric(high) == false) {
			alert('Price must be a number');
			return;
		}
		if (low > high) {
			temp = low;
			low = high;
			high = temp;
		}
		redirect = link.replace(/price_[0-9]+_[0-9]+[\/]{1}/gi, "");
	}
	else if (thetype == "miles") {
		low = parseInt(trim(document.sContent.rMileageFrom.value));
		high = parseInt(trim(document.sContent.rMileageTo.value));
		if (IsNumeric(low) == false || IsNumeric(high) == false) {
			alert('Miles must be a number');
			return;
		}
		if(low == 0 && high == 0){
			return;
		}
		if (low > high) {
			temp = low;
			low = high;
			high = temp;
		}
		redirect = link.replace(/miles_[0-9]+_[0-9]+[\/]{1}/gi, "");
	}
	else {
		return; // no flag found so ignore
	}
	
	var newparam = thetype + "_" + low + "_" + high + "/";
	
	if ((pos-1) < 0) {
		redirect = link + newparam;
	}
	else {
		var firstHalf = Left(redirect, pos);
		var lastHalf = Right(redirect, redirect.length - pos + 1);
		redirect = firstHalf + newparam + lastHalf;
		// get rid of dbl slash mainly if filtering and unselecting all trans
		redirect = redirect.replace(/\/\//gi, "/");
		redirect = redirect.replace(/http:\//gi, "http://");
	}
	
	window.location = redirect;

}

function miscSearch(link){
	
	var keywords = trim(document.sContent.rKeyword.value);
	//remove excess whitespace but preserve separate words
	keywords = "keywords_" + keywords.replace(/([^\w\d\-]\s*)|(\s*[^\w\d\-])/g,",") + "/";
	
	var type = document.sContent.rType.value;
	var posK = link.indexOf("/keywords_") + 1;
	var newparam;
	
	if(type == 'used' || type == 'new' || type == 'both'){
		type = 'type_' + type + '/';
		link = link.replace(/type_(new|used|both)[\/]{1}/gi, "");
		
		var posT = link.indexOf("/type_") + 1;
		newparam = type;
		if ((posT-1) < 0) {
			link = link + newparam;
		}
		else {
			var firstHalf = Left(link, posT);
			var lastHalf = Right(link, link.length - posT + 1);
			link = firstHalf + newparam + lastHalf;
			// get rid of dbl slash mainly if filtering and unselecting all trans
			link = link.replace(/\/\//gi, "/");
			link = link.replace(/http:\//gi, "http://");
		}
	}
	
	if( keywords != 'keywords_/'){
		newparam = keywords;
		link = link.replace(/keywords_[\w\d,]+[\/]{1}/gi, "");
		
		if ((posK-1) < 0) {
			link = link + newparam;
		}
		else {
			var firstHalf = Left(link, posK);
			var lastHalf = Right(link, link.length - posK + 1);
			link = firstHalf + newparam + lastHalf;
			// get rid of dbl slash mainly if filtering and unselecting all trans
			link = link.replace(/\/\//gi, "/");
			link = link.replace(/http:\//gi, "http://");
		}
	}

	window.location = link;
	
}
