function saveSearch() {
	alert('Saving Search Stub');
	if (document.savesearch) {
		document.savesearch.submit();
	}
}

function clearSelect(box) {
	for (var i=box.length-1; i>=0; i--) {
		box.options[i]=null;
	}	
}

function addModels(makeid, modelBox, makeName) {
	var cap = '';
	var inc;
	//alert('adding '+makeid);
	if (modInc) { inc=modInc; } else { inc=2; }
	if (makemodel && makemodel[makeid]) {
		var mods=makemodel[makeid].split(",");
		for (var i=0; i<mods.length; i+=inc) {
			var cap=makeName+' '+mods[i+1];
			if (showCounts) cap+=' ('+mods[i+2]+')';
			var newOption=new Option(cap, mods[i], false, false); //This should create an UNSELECTED option, but doesn't appear to in NS
			modelBox.options[modelBox.options.length] = newOption;
		}
	}
}

function n_Change(theForm) {
	if (theForm.n) { theForm.action=theForm.pagename.value; theForm.submit(); }
}

function clearForm(theForm) {
	//Resets the form back to starting point (excluded preselected options like in a "modify search" scenario...
	theForm.reset();
	vdmm_Change(theForm);
}

function vdmm_Change(theForm) {
	//clear moids
	//alert('clearing models');
	if (!theForm) { return null; }

	if (theForm=='') {
		//Try to find form context
		if (document.quicksearch) { theForm=document.quicksearch; }
		if (document.advsearch) { theForm=document.advsearch; }
	}

	clearSelect(theForm.moids);

	var mks=theForm.mkids
	var foundMakes=false;

	//Go through all selected makes, and loop through individual model arrays to populate moids list.
	for (var i=0; i<mks.length; i++) {
			var mkCaption=new String(mks.options[i].text);
			if (mkCaption.indexOf("(") > -1) {
				mkCaption=mkCaption.substring(0, mkCaption.indexOf('('));
			}
			if (mks.options[i].selected) {
			addModels(mks.options[i].value, theForm.moids, mkCaption.toString());
			foundMakes=true;
		}
	}

	if ((theForm.moids.options.length == 0) && (foundMakes==true)) {
		var newOption=new Option('--No models available--', '', false, false);
		theForm.moids.options[theForm.moids.options.length] = newOption;
	} else {
		if (!foundMakes) {
		var newOption=new Option('--Select one or more makes--', '', false, false);
		theForm.moids.options[theForm.moids.options.length] = newOption;
		}
	}
}

function priceChange(varItem, min, max) {
	//if min changed, make sure it's not greater than the max
	if (varItem=='min') {
		if (max.value!='' && max.value!='' && (max.value < min.value)) {
			max.options[max.selectedIndex].selected=false;
			max.options[0].selected=true;
		}
	} else {
		if (min.value!=='' && parseFloat(min.value) > parseFloat(max.value)) {
			min.options[min.selectedIndex].selected=false;
			min.options[0].selected=true;
		}
	}
}

function submitGlobalMMChange() {
	//Need to submit document.vehiclesearch.submit but also make sure mkids param is removed from the form action.
	//alert(document.location.href);
	//alert('Should post to '+getQueryStringFromForm(document.vehiclesearch));
	document.vehiclesearch.action=getQueryStringFromForm(document.vehiclesearch);
	document.vehiclesearch.submit();
}

function RangeChange(varItem, min, max) {
	//if min changed, make sure it's not greater than the max
	if (varItem=='min') {
		if (max.value!='' && max.value!='' && (max.value < min.value)) {
			max.options[max.selectedIndex].selected=false;
			max.options[0].selected=true;
		}
	} else {
		if (min.value!=='' && parseFloat(min.value) > parseFloat(max.value)) {
			min.options[min.selectedIndex].selected=false;
			min.options[0].selected=true;
		}
	}	
}

function submitSearch(theForm, destPage) {
	var qs=getQueryStringFromForm(theForm);
	var submitPage='';
	
	//If no destination page passed in, assume results.asp.  Otherwise use the parameter.
	if (!destPage || destPage=='') { submitPage='results.asp'; } else { submitPage=destPage; }
	if (qs.length>2048) {
		alert('You have selected too many makes/models.  Please refine your search and resubmit.');
	} else {
		location.href=submitPage+qs;
	}
}

function getQueryStringFromForm(theForm) {
	var queryString='';
	var name='';
	var values='';
	var fids='';
	var n='';
	var o='';
		
	for (var i=0; i<theForm.length; i++) {
		name=theForm.elements[i].name;
		values=getFormItemValues(theForm.elements[i]);
		//Handling FeatureIDs is the exception to the looping rule.  Feature names contain the ID value.
		//	If the form element name begins with fids, then it's a feature.  Parse out the feature ID and
		//	add it to the list.  Finally, the list will be added to the querystring.

		//Run special case for features.
		if (name.indexOf('fids')>-1) {
			values='';
			if (theForm.elements[i].checked) {
				if (fids!='') { fids+=','}
				fids+=name.substring(4);
			}
		}
		
		if (name=='n') {
			//Run special case for new/used.
			values='';

			//If this is a hidden field, or if it is the selected option in a group of radio buttons then assign its value.
			if (theForm.elements[i].type=='hidden' || theForm.elements[i].checked) {
				n=theForm.elements[i].value;
			}
		}
		
		if (name=='o') {
			//Run special case for Ordering.
			values='';
			if (theForm.elements[i].checked) {
				o=theForm.elements[i].value
			}
		}
	
		if (values!='') { queryString=updateQueryString(queryString, name, values); }
	}

	if (fids!='') { queryString=updateQueryString(queryString, 'fids', fids); }
	if (n!='') { queryString=updateQueryString(queryString, 'n', n); }
	if (o!='') { queryString=updateQueryString(queryString, 'o', o); }
	if (queryString!='') { queryString='?'+queryString; }
	return queryString;
}

function getFormItemValues(formItem) {
	var values='';

	if (formItem.type.indexOf('select')==0) {
		//This is a select box.
		for (var i=0; i<formItem.options.length; i++) {
			if (formItem.options[i].selected && formItem.options[i].value.indexOf('--')!=0) {
				if (values!='') values+=',';
				values+=formItem.options[i].value;
			}
		}
	} else {
		//This is single-valued item.
		values=formItem.value;
	}
	return values;
}