/*---------------------- Code for Make, Model navigation Start ----------------------------*/
// close the displayed layer for make field or model field by changing its css attrubute.
function closeResults(obj_str)
{
	document.getElementById(obj_str).style.display = "none";
//	document.getElementById(obj_str).style.visibility='hidden';
}
// show the layer for make field or model field.
// while showing one hide another one.
function displayResults(obj_str)
{
	if(obj_str=="make_op")
	{
		closeResults("model_op")
		closeResults("YearFrom_op")
		closeResults("YearTo_op")
	}
	if(obj_str=="model_op")
	{
		closeResults("make_op")
		closeResults("YearFrom_op")
		closeResults("YearTo_op")
	}
	if(obj_str=="YearFrom_op")
	{
		closeResults("make_op")
		closeResults("model_op")
		closeResults("YearTo_op")
	}
	if(obj_str=="YearTo_op")
	{
		closeResults("make_op")
		closeResults("model_op")
		closeResults("YearFrom_op")
	}
	document.getElementById(obj_str).style.display = "block";
}

// assign getKey function to documents onkeyup event. getKey() function will be called whenever user will release any keyboard key on document.
document.onkeyup = getKey;
//document.onkeydown = getKey;

// variable to hold the pressed keyboard key value.
var keyval=0;

/* the use of fun variable is as follows:

1. one function named "getKey" is assigned to "onkeyup" event of document object.
2. on "onkeyup" event of "make" textbox a function is called "searchMakes".
3. page is calling "searchMakes" function first and then the "getKey" function.
4. we are getting keyboard key code in "getKey" function.
5. so "fun" variable is used to skip the execution of "searchMakes" function at first time and then reexecute the "searchMakes" function inside the getKey function.
*/
var fun=0;
// gets the keyboard key value that is pressed into "keyval" variable.
// call the searchMakes functino if key is pressed on make textbox, and calls searchModels function otherwise.
function getKey(keyStroke)
{
	keyval=(keyStroke)? keyStroke.which : event.keyCode;
	if(fun==1)
	{
		fun=0;
		searchMakes();
	}
	else if(fun==2)
	{
		fun=0;
		searchModels();
	}
	else if(fun==3)
	{
		fun=0;
		return validateform();
	}
	else if(fun==4)
	{
		fun=0;
		searchYearFrom();
	}
	else if(fun==5)
	{
		fun=0;
		searchYearTo();
	}
    return true;
}
// filters the make list according to text value of make field.
var makerow=0;
var makecol=0;
var modelrow=0;
var modelcol=0;
var YearFromrow=0;
var YearFromcol=0;
var YearTorow=0;
var YearTocol=0;

function searchMakes()
{
	if(fun==1)
		return;
	// if left arrow
	if(keyval==37)
	{
		keyval=0;
		no_highlight_make();
		do
		{
			makecol--;
			if(makecol<0)
			{
				makecol=document.getElementById("make_tab").rows[makerow].cells.length-1;
				makerow--;
				if(makerow<0)
				{
					makerow=document.getElementById("make_tab").rows.length-1;
				}
			}
		}
		while(document.getElementById("make_tab").rows[makerow].cells[makecol].innerHTML==" ")
		highlight_make();
	}
	// if up arrow is pressed then select one item above to current item in make list box.
	if(keyval==38)
	{
		keyval=0;
		no_highlight_make();
		do
		{
			makerow--;
			if(makerow<0)
			{
				makerow=document.getElementById("make_tab").rows.length-1;
				makecol--;
				if(makecol<0)
				{
					makecol=document.getElementById("make_tab").rows[makerow].cells.length-1;
				}
			}
		}
		while(document.getElementById("make_tab").rows[makerow].cells[makecol].innerHTML==" ")
		highlight_make();
	}
	// if right arrow
	if(keyval==39)
	{
		keyval=0;
		no_highlight_make();
		do
		{
			makecol++;
			if(document.getElementById("make_tab").rows[makerow].cells.length<=makecol)
			{
				makecol=0;
				makerow++;
				if(document.getElementById("make_tab").rows.length<=makerow)
				{
					makerow=0;
				}
			}
		}
		while(document.getElementById("make_tab").rows[makerow].cells[makecol].innerHTML==" ")
		highlight_make();
	}
	// if down arrow is pressed then select one item below to current item in make list box.
	else if(keyval==40)
	{
		keyval=0;
		no_highlight_make();
		do
		{
			makerow++;
			if(document.getElementById("make_tab").rows.length<=makerow)
			{
				makerow=0;
				makecol++;
				if(document.getElementById("make_tab").rows[makerow].cells.length<=makecol)
				{
					makecol=0;
				}
			}
		}
		while(document.getElementById("make_tab").rows[makerow].cells[makecol].innerHTML==" ")
		highlight_make();
	}
	// if tab key is pressed then get selected value of make list box into make text box.
	else if(keyval==9)// && document.search_listings.makelist.selectedIndex>=0)
	{
		keyval=0;
		getmakevalue(document.getElementById("make_tab").rows[makerow].cells[makecol]);
		document.search_listings.model_txt.focus();
	}
	else if(keyval==13)// && document.search_listings.makelist.selectedIndex>=0)
	{
		keyval=-1;
		getmakevalue(document.getElementById("make_tab").rows[makerow].cells[makecol]);
		document.search_listings.make_txt.focus();
	}
	else
	{
		// get the make textbox value.
		makevalue=document.search_listings.make_txt.value;
		// show the make list box.
		displayResults("make_op");
		// put the filtered values of make from makelist array into make list box.
		var makecounter=0;
		var trows=0;
		var tcols=0;
		var firstchar="";
		var initchar="";
		for(i=0;i<makelist.length;i++)
		{
			makereg=new RegExp("^"+makevalue+".*", "i");
			if(makelist[i].match(makereg))
			{
				initchar=makelist[i].substring(0,1);
				if(firstchar!="" && firstchar!=initchar)
				{
					makecounter++;
				}
				makecounter++;
				firstchar=initchar;
			}
		}
		if(makecounter==0)
		{
			document.getElementById("make_iframe").style.width="0px";
			document.getElementById("make_iframe").style.height="0px";
			document.getElementById("make_subop").innerHTML = "<div class=\"inittext\">No make found</div>";
			displayResults("make_op");
			return;
		}
		if(makecounter>50)
		{
			trows=Math.ceil(makecounter/5);
			tcols=5;
		}
		else
		{
			tcols=Math.ceil(makecounter/10);
			trows=10;
		}
		trows=Math.ceil(makecounter/tcols);
		var make2darray=new Array();
		var makeid2darray=new Array();
		for(i=0;i<trows;i++)
		{
			make2darray[i]=new Array();
			makeid2darray[i]=new Array();
		}
		var tr=0;
		var tc=-1;
		firstchar="";
		initchar="";
		for(i=0;i<makelist.length;i++)
		{
			makereg=new RegExp("^"+makevalue+".*", "i");
			if(makelist[i].match(makereg))
			{
				initchar=makelist[i].substring(0,1);
				if(firstchar!="" && firstchar!=initchar)
				{
					if(tr!=0)
					{
						//tc++;
						make2darray[tr][tc]="";
						makeid2darray[tr][tc]="";
						tr++;
					}
					if(tr==trows)
					{
						tr=0;
					}
				}
				if(tr==0)
				{
					tc++;
				}
				make2darray[tr][tc]=makelist[i];
				makeid2darray[tr][tc]=makeidlist[i];
				firstchar=initchar;
				tr++;
				if(tr==trows)
				{
					tr=0;
				}
			}
		}

		var tabstr="<table id=\"make_tab\">";
		for(i=0;i<trows;i++)
		{
			tabstr+= "<tr>";
			for(j=0;j<tcols;j++)
			{
				if(make2darray[i][j])
				{
					tabstr+="<td nowrap=\"nowrap\" class=\"layer_td\" id=\""+ makeid2darray[i][j] +"\" onclick=\"getmakevalue(this)\" onmouseover=\"no_highlight_make();getmakerow_col(this);this.className='layer_td_hover';mouseovermakevalue(this);\" onmouseout=\"removemakerow_col();this.className='layer_td'\">"+ make2darray[i][j] +"</td>";
				}
				else
				{
					tabstr+="<td> </td>";
				}
			}
			tabstr+= "</tr>";
		}
		tabstr+="</table>";
		document.getElementById("make_subop").innerHTML = tabstr;
		highlight_make();
		var dv=document.getElementById("make_subop");
		document.getElementById("make_iframe").style.width=dv.clientWidth;
		document.getElementById("make_iframe").style.height=dv.clientHeight;
	}
}
function no_highlight_make()
{
	if(document.getElementById("make_tab").rows[makerow].cells[makecol])
		document.getElementById("make_tab").rows[makerow].cells[makecol].className="layer_td";
}
function highlight_make()
{
	if(document.getElementById("make_tab").rows[makerow].cells[makecol])
		document.getElementById("make_tab").rows[makerow].cells[makecol].className="layer_td_hover";
}

function getmakerow_col(cur_cell)
{
	makerow=cur_cell.parentNode.rowIndex;
	makecol=cur_cell.cellIndex;
}
function removemakerow_col()
{
	makerow=0;
	makecol=0;
}
function getmakevalue(cur_cell)
{
	if(document.getElementById('make_tab') && document.getElementById('make_tab').rows[makerow].cells[makecol])
	{
		var cur_cell=document.getElementById('make_tab').rows[makerow].cells[makecol];
		document.search_listings.make_txt.value=cur_cell.innerHTML;
		document.search_listings.make.value=cur_cell.id;
	}
	closeResults("make_op");
	document.search_listings.model_txt.value="";
	document.search_listings.model.value="";
}
function mouseovermakevalue(cur_cell)
{
	if(document.getElementById('make_tab') && document.getElementById('make_tab').rows[makerow].cells[makecol])
	{
		var cur_cell=document.getElementById('make_tab').rows[makerow].cells[makecol];
		document.search_listings.make_txt.value=cur_cell.innerHTML;
		document.search_listings.make.value=cur_cell.id;
	}

}

function reset_make()
{
	closeResults("make_op");
	document.search_listings.make_txt.value="";
	document.search_listings.make.value="";
	document.search_listings.model_txt.value="";
	document.search_listings.model.value="";
	document.search_listings.year_from.value="";
	document.search_listings.year_to.value="";
}

function searchModels()
{
	if(fun==2)
		return;
	// if left arrow
	if(keyval==37)
	{
		keyval=0;
		no_highlight_model();
		modelcol--;
		if(modelcol<0)
		{
			modelcol=document.getElementById("model_tab").rows[modelrow].cells.length-1;
			modelrow--;
			if(modelrow<0)
			{
				modelrow=document.getElementById("model_tab").rows.length-1;
			}
		}
		highlight_model();
	}
	// if up arrow is pressed then select one item above to current item in make list box.
	if(keyval==38)
	{
		keyval=0;
		no_highlight_model();
		modelrow--;
		if(modelrow<0)
		{
			modelrow=document.getElementById("model_tab").rows.length-1;
			modelcol--;
			if(modelcol<0)
			{
				modelcol=document.getElementById("model_tab").rows[modelrow].cells.length-1;
			}
		}
		highlight_model();
	}
	// if right arrow
	if(keyval==39)
	{
		keyval=0;
		no_highlight_model();
		modelcol++;
		if(document.getElementById("model_tab").rows[modelrow].cells.length<=modelcol)
		{
			modelcol=0;
			modelrow++;
			if(document.getElementById("model_tab").rows.length<=modelrow)
			{
				modelrow=0;
			}
		}
		highlight_model();
	}
	// if down arrow is pressed then select one item below to current item in model list box.
	else if(keyval==40)
	{
		keyval=0;
		no_highlight_model();
		modelrow++;
		if(document.getElementById("model_tab").rows.length<=modelrow)
		{
			modelrow=0;
			modelcol++;
			if(document.getElementById("model_tab").rows[modelrow].cells.length<=modelcol)
			{
				modelcol=0;
			}
		}
		highlight_model();
	}
	// if tab key is pressed then get selected value of model list box into model text box.
	else if(keyval==9)// && document.search_listings.modellist.selectedIndex>=0)
	{
		keyval=0;
		getmodelvalue(document.getElementById("model_tab").rows[modelrow].cells[modelcol]);
		document.search_listings.year_from.focus();
	}
	else if(keyval==13)// && document.search_listings.modellist.selectedIndex>=0)
	{
		keyval=-1;
		getmodelvalue(document.getElementById("model_tab").rows[modelrow].cells[modelcol]);
		document.search_listings.model_txt.focus();
	}
	else
	{
		// get the model textbox value.
		modelvalue=document.search_listings.model_txt.value;
		// show the model list box.
		displayResults("model_op");
		// put the filtered values of model from modellist array into model list box.
		var modelcounter=0;
		var trows=0;
		var tcols=0;
		modelindex=document.search_listings.make.value;
		for(i=0;i<modellist[modelindex].length;i++)
		{
			modelreg=new RegExp("^"+modelvalue+".*", "i");
			if(modellist[modelindex][i].match(modelreg))
			{
				modelcounter++;
			}
		}
		if(modelcounter==0)
		{
			document.getElementById("model_iframe").style.width="0px";
			document.getElementById("model_iframe").style.height="0px";
			document.getElementById("model_subop").innerHTML = "<div class=\"inittext\">No model found</div>";
			displayResults("model_op");
			return;
		}
		if(modelcounter>50)
		{
			trows=Math.ceil(modelcounter/5);
			tcols=5;
		}
		else
		{
			tcols=Math.ceil(modelcounter/10);
			trows=10;
		}
		trows=Math.ceil(modelcounter/tcols);
		var tr=0;
		var tc=0;
		var model2darray=new Array();
		var modelid2darray=new Array();
		for(i=0;i<trows;i++)
		{
			model2darray[i]=new Array();
			modelid2darray[i]=new Array();
		}
		var tr=0;
		var tc=-1;
		for(i=0;i<modellist[modelindex].length;i++)
		{
			modelreg=new RegExp("^"+modelvalue+".*", "i");
			if(modellist[modelindex][i].match(modelreg))
			{
				if(tr==0)
				{
					tc++;
				}
				model2darray[tr][tc]=modellist[modelindex][i];
				modelid2darray[tr][tc]=modelidlist[modelindex][i];
				tr++;
				if(tr==trows)
				{
					tr=0;
				}
			}
		}

		var tabstr="<table id=\"model_tab\">";
		for(i=0;i<trows;i++)
		{
			tabstr+= "<tr>";
			for(j=0;j<tcols;j++)
			{
				if(model2darray[i][j])
				{
					tabstr+="<td nowrap=\"nowrap\" class=\"layer_td\" id=\""+ modelid2darray[i][j] +"\" onclick=\"getmodelvalue(this)\" onmouseover=\"no_highlight_model();getmodelrow_col(this);this.className='layer_td_hover';mouseovermodelvalue(this);\" onmouseout=\"removemodelrow_col();this.className='layer_td'\">"+ model2darray[i][j] +"</td>";
				}
			}
			tabstr+= "</tr>";
		}
		tabstr+="</table>";
		document.getElementById("model_subop").innerHTML = tabstr;
		highlight_model();
		var dv=document.getElementById("model_subop");
		document.getElementById("model_iframe").style.width=dv.clientWidth;
		document.getElementById("model_iframe").style.height=dv.clientHeight;
	}
}
function no_highlight_model()
{
	if(document.getElementById("model_tab").rows[modelrow].cells[modelcol])
		document.getElementById("model_tab").rows[modelrow].cells[modelcol].className="layer_td";
}
function highlight_model()
{
	if(document.getElementById("model_tab").rows[modelrow].cells[modelcol])
		document.getElementById("model_tab").rows[modelrow].cells[modelcol].className="layer_td_hover";
}

function getmodelrow_col(cur_cell)
{
	modelrow=cur_cell.parentNode.rowIndex;
	modelcol=cur_cell.cellIndex;
}
function removemodelrow_col()
{
	modelrow=0;
	modelcol=0;
}
function getmodelvalue(cur_cell)
{
    //alert('asdf');
	if(document.getElementById('model_tab') && document.getElementById('model_tab').rows[modelrow].cells[modelcol])
	{
		var cur_cell=document.getElementById('model_tab').rows[modelrow].cells[modelcol];
		document.search_listings.model_txt.value=cur_cell.innerHTML;
		document.search_listings.model.value=cur_cell.id;
	}
	closeResults("model_op");
	fun=0;
}
function mouseovermodelvalue(cur_cell)
{
    if(document.getElementById('model_tab') && document.getElementById('model_tab').rows[modelrow].cells[modelcol])
    {
        var cur_cell=document.getElementById('model_tab').rows[modelrow].cells[modelcol];
        document.search_listings.model_txt.value=cur_cell.innerHTML;
        document.search_listings.model.value=cur_cell.id;
    }

}
function reset_model()
{
    closeResults("model_op");
    document.search_listings.model_txt.value="";
    document.search_listings.model.value="";
    document.search_listings.year_from.value="";
    document.search_listings.year_to.value="";
}

function searchYearFrom()
{
	if(fun==4)
		return;
	// if left arrow
	if(keyval==37)
	{
		keyval=0;
		no_highlight_YearFrom();
		YearFromcol--;
		if(YearFromcol<0)
		{
			YearFromcol=document.getElementById("YearFrom_tab").rows[YearFromrow].cells.length-1;
			YearFromrow--;
			if(YearFromrow<0)
			{
				YearFromrow=document.getElementById("YearFrom_tab").rows.length-1;
			}
		}
		highlight_YearFrom();
	}
	// if up arrow is pressed then select one item above to current item in YearFrom list box.
	if(keyval==38)
	{
		keyval=0;
		no_highlight_YearFrom();
		YearFromrow--;
		if(YearFromrow<0)
		{
			YearFromrow=document.getElementById("YearFrom_tab").rows.length-1;
			YearFromcol--;
			if(YearFromcol<0)
			{
				YearFromcol=document.getElementById("YearFrom_tab").rows[YearFromrow].cells.length-1;
			}
		}
		highlight_YearFrom();
	}
	// if right arrow
	if(keyval==39)
	{
		keyval=0;
		no_highlight_YearFrom();
		YearFromcol++;
		if(document.getElementById("YearFrom_tab").rows[YearFromrow].cells.length<=YearFromcol)
		{
			YearFromcol=0;
			YearFromrow++;
			if(document.getElementById("YearFrom_tab").rows.length<=YearFromrow)
			{
				YearFromrow=0;
			}
		}
		highlight_YearFrom();
	}
	// if down arrow is pressed then select one item below to current item in YearFrom list box.
	else if(keyval==40)
	{
		keyval=0;
		no_highlight_YearFrom();
		YearFromrow++;
		if(document.getElementById("YearFrom_tab").rows.length<=YearFromrow)
		{
			YearFromrow=0;
			YearFromcol++;
			if(document.getElementById("YearFrom_tab").rows[YearFromrow].cells.length<=YearFromcol)
			{
				YearFromcol=0;
			}
		}
		highlight_YearFrom();
	}
	// if tab key is pressed then get selected value of YearFrom list box into YearFrom text box.
	else if(keyval==9)// && document.search_listings.YearFromlist.selectedIndex>=0)
	{
		keyval=0;
		getYearFromvalue(document.getElementById("YearFrom_tab").rows[YearFromrow].cells[YearFromcol]);
	}
	else if(keyval==13)// && document.search_listings.YearFromlist.selectedIndex>=0)
	{
		keyval=-1;
		getYearFromvalue(document.getElementById("YearFrom_tab").rows[YearFromrow].cells[YearFromcol]);
		document.search_listings.year_from.focus();
	}
	else
	{
		// get the YearFrom textbox value.
		YearFromvalue=document.search_listings.year_from.value;
		// show the YearFrom list box.
		displayResults("YearFrom_op");
		// put the filtered values of YearFrom from YearFromlist array into YearFrom list box.
		var YearFromcounter=0;
		var trows=0;
		var tcols=0;
		for(i=0;i<YearFromlist.length;i++)
		{
			YearFromreg=new RegExp("^"+YearFromvalue+".*", "i");
			if(YearFromlist[i].toString().match(YearFromreg))
			{
				YearFromcounter++;
			}
		}
		if(YearFromcounter>50)
		{
			trows=Math.ceil(YearFromcounter/5);
			tcols=5;
		}
		else
		{
			tcols=Math.ceil(YearFromcounter/10);
			trows=10;
		}
		trows=Math.ceil(YearFromcounter/tcols);
		var YearFrom2darray=new Array();
		for(i=0;i<trows;i++)
		{
			YearFrom2darray[i]=new Array();
		}
		var tr=0;
		var tc=-1;
		for(i=0;i<YearFromlist.length;i++)
		{
			YearFromreg=new RegExp("^"+YearFromvalue+".*", "i");
			if(YearFromlist[i].toString().match(YearFromreg))
			{
				if(tr==0)
				{
					tc++;
				}
				YearFrom2darray[tr][tc]=YearFromlist[i];
				tr++;
				if(tr==trows)
				{
					tr=0;
				}
			}
		}

		var tabstr="<table id=\"YearFrom_tab\">";
		for(i=0;i<trows;i++)
		{
			tabstr+= "<tr>";
			for(j=0;j<tcols;j++)
			{
				if(YearFrom2darray[i][j])
				{
					tabstr+="<td nowrap=\"nowrap\" class=\"layer_td\" onclick=\"getYearFromvalue(this)\" onmouseover=\"no_highlight_YearFrom();getYearFromrow_col(this);this.className='layer_td_hover';mouseoveryearfromvalue(this);\" onmouseout=\"removeYearFromrow_col();this.className='layer_td'\">"+ YearFrom2darray[i][j] +"</td>";
					//  id=\""+ YearFromid2darray[i][j] +"\"
				}
			}
			tabstr+= "</tr>";
		}
		tabstr+="</table>";
		document.getElementById("YearFrom_subop").innerHTML = tabstr;
		highlight_YearFrom();
		var dv=document.getElementById("YearFrom_subop");
		document.getElementById("YearFrom_iframe").style.width=dv.clientWidth;
		document.getElementById("YearFrom_iframe").style.height=dv.clientHeight;
		//YearFromrow=-1;
		//YearFromcol=-1;
	}
}
function no_highlight_YearFrom()
{
	if(document.getElementById("YearFrom_tab").rows[YearFromrow].cells[YearFromcol])
		document.getElementById("YearFrom_tab").rows[YearFromrow].cells[YearFromcol].className="layer_td";
}
function highlight_YearFrom()
{
	if(document.getElementById("YearFrom_tab").rows[YearFromrow].cells[YearFromcol])
		document.getElementById("YearFrom_tab").rows[YearFromrow].cells[YearFromcol].className="layer_td_hover";
}

function getYearFromrow_col(cur_cell)
{
	YearFromrow=cur_cell.parentNode.rowIndex;
	YearFromcol=cur_cell.cellIndex;
}
function removeYearFromrow_col()
{
	YearFromrow=0;
	YearFromcol=0;
}
function getYearFromvalue()
{
	if(document.getElementById('YearFrom_tab').rows.length!=0)
	{
		var cur_cell=document.getElementById('YearFrom_tab').rows[YearFromrow].cells[YearFromcol];
		document.search_listings.year_from.value=cur_cell.innerHTML;
	}
	closeResults("YearFrom_op");
	var yfromval=document.search_listings.year_from.value;
	var d = new Date();
	var cur_year=d.getFullYear();
	if(yfromval=="")
		return;
	if(!yfromval.match("[0-9]+"))
	{
		alert("Please provide year between 1900 and "+(cur_year+1).toString()+".");
		document.search_listings.year_from.value="";
		document.search_listings.year_from.focus();
		return;
	}

	if(yfromval.toString().length==2)
	{
		var newval=cur_year-(cur_year%100) + parseFloat(yfromval);
		if(newval>cur_year)
			newval=cur_year-100-(cur_year%100) + parseFloat(yfromval);
		document.search_listings.year_from.value=newval;
	}
	else if(yfromval<1900 || yfromval>(cur_year+1))
	{
		alert("Please provide year between 1900 and "+(cur_year+1).toString()+".");
		document.search_listings.year_from.value="";
		document.search_listings.year_from.focus();
	}
}
function mouseoveryearfromvalue(cur_cell)
{
    if(document.getElementById('YearFrom_tab').rows.length!=0)
    {
        var cur_cell=document.getElementById('YearFrom_tab').rows[YearFromrow].cells[YearFromcol];
        document.search_listings.year_from.value=cur_cell.innerHTML;
    }
}
function reset_yearfrom()
{
    closeResults("YearFrom_op");
    document.search_listings.year_from.value="";
    document.search_listings.year_to.value="";
}

//--------------------------------------------------------------------------------------------
function searchYearTo()
{
	if(fun==5)
		return;
	// if left arrow
	if(keyval==37)
	{
		keyval=0;
		no_highlight_YearTo();
		YearTocol--;
		if(YearTocol<0)
		{
			YearTocol=document.getElementById("YearTo_tab").rows[YearTorow].cells.length-1;
			YearTorow--;
			if(YearTorow<0)
			{
				YearTorow=document.getElementById("YearTo_tab").rows.length-1;
			}
		}
		highlight_YearTo();
	}
	// if up arrow is pressed then select one item above to current item in YearTo list box.
	if(keyval==38)
	{
		keyval=0;
		no_highlight_YearTo();
		YearTorow--;
		if(YearTorow<0)
		{
			YearTorow=document.getElementById("YearTo_tab").rows.length-1;
			YearTocol--;
			if(YearTocol<0)
			{
				YearTocol=document.getElementById("YearTo_tab").rows[YearTorow].cells.length-1;
			}
		}
		highlight_YearTo();
	}
	// if right arrow
	if(keyval==39)
	{
		keyval=0;
		no_highlight_YearTo();
		YearTocol++;
		if(document.getElementById("YearTo_tab").rows[YearTorow].cells.length<=YearTocol)
		{
			YearTocol=0;
			YearTorow++;
			if(document.getElementById("YearTo_tab").rows.length<=YearTorow)
			{
				YearTorow=0;
			}
		}
		highlight_YearTo();
	}
	// if down arrow is pressed then select one item below to current item in YearTo list box.
	else if(keyval==40)
	{
		keyval=0;
		no_highlight_YearTo();
		YearTorow++;
		if(document.getElementById("YearTo_tab").rows.length<=YearTorow)
		{
			YearTorow=0;
			YearTocol++;
			if(document.getElementById("YearTo_tab").rows[YearTorow].cells.length<=YearTocol)
			{
				YearTocol=0;
			}
		}
		highlight_YearTo();
	}
	// if tab key is pressed then get selected value of YearTo list box into YearTo text box.
	else if(keyval==9)// && document.search_listings.YearTolist.selectedIndex>=0)
	{
		keyval=0;
		getYearTovalue(document.getElementById("YearTo_tab").rows[YearTorow].cells[YearTocol]);
	}
	else if(keyval==13)// && document.search_listings.YearTolist.selectedIndex>=0)
	{
		keyval=-1;
		getYearTovalue(document.getElementById("YearTo_tab").rows[YearTorow].cells[YearTocol]);
		document.search_listings.year_to.focus();
	}
	else
	{
		// get the YearTo textbox value.
		YearTovalue=document.search_listings.year_to.value;
		// show the YearTo list box.
		displayResults("YearTo_op");
		// put the filtered values of YearTo from YearTolist array into YearTo list box.
		var YearTocounter=0;
		var trows=0;
		var tcols=0;
		var yfromvalue=document.search_listings.year_from.value;
		for(i=0;i<YearFromlist.length;i++)
		{
			YearToreg=new RegExp("^"+YearTovalue+".*", "i");
			if(YearFromlist[i].toString().match(YearToreg) && YearFromlist[i]>= parseInt(yfromvalue))
			{
				YearTocounter++;
			}
		}
		if(YearTocounter>50)
		{
			trows=Math.ceil(YearTocounter/5);
			tcols=5;
		}
		else
		{
			tcols=Math.ceil(YearTocounter/10);
			trows=10;
		}
		trows=Math.ceil(YearTocounter/tcols);
		var YearTo2darray=new Array();
		for(i=0;i<trows;i++)
		{
			YearTo2darray[i]=new Array();
		}
		var tr=0;
		var tc=-1;
		for(i=0;i<YearFromlist.length;i++)
		{
			YearToreg=new RegExp("^"+YearTovalue+".*", "i");
			if(YearFromlist[i].toString().match(YearToreg) && YearFromlist[i]>= parseInt(yfromvalue))
			{
				if(tr==0)
				{
					tc++;
				}
				YearTo2darray[tr][tc]=YearFromlist[i];
				tr++;
				if(tr==trows)
				{
					tr=0;
				}
			}
		}

		var tabstr="<table id=\"YearTo_tab\">";
		for(i=0;i<trows;i++)
		{
			tabstr+= "<tr>";
			for(j=0;j<tcols;j++)
			{
				if(YearTo2darray[i][j])
				{
					tabstr+="<td nowrap=\"nowrap\" class=\"layer_td\" onclick=\"getYearTovalue(this)\" onmouseover=\"no_highlight_YearTo();getYearTorow_col(this);this.className='layer_td_hover';mouseoveryeartovalue(this);\" onmouseout=\"removeYearTorow_col();this.className='layer_td'\">"+ YearTo2darray[i][j] +"</td>";
					//  id=\""+ YearToid2darray[i][j] +"\"
				}
			}
			tabstr+= "</tr>";
		}
		tabstr+="</table>";
		document.getElementById("YearTo_subop").innerHTML = tabstr;
		highlight_YearTo();
		var dv=document.getElementById("YearTo_subop");
		document.getElementById("YearTo_iframe").style.width=dv.clientWidth;
		document.getElementById("YearTo_iframe").style.height=dv.clientHeight;
	}
}
function no_highlight_YearTo()
{
	if(document.getElementById("YearTo_tab").rows[YearTorow].cells[YearTocol])
		document.getElementById("YearTo_tab").rows[YearTorow].cells[YearTocol].className="layer_td";
}
function highlight_YearTo()
{
	if(document.getElementById("YearTo_tab").rows[YearTorow].cells[YearTocol])
		document.getElementById("YearTo_tab").rows[YearTorow].cells[YearTocol].className="layer_td_hover";
}

function getYearTorow_col(cur_cell)
{
	YearTorow=cur_cell.parentNode.rowIndex;
	YearTocol=cur_cell.cellIndex;
}
function removeYearTorow_col()
{
	YearTorow=0;
	YearTocol=0;
}
function getYearTovalue()
{
	if(document.getElementById('YearTo_tab').rows.length!=0)
	{
		var cur_cell=document.getElementById('YearTo_tab').rows[YearTorow].cells[YearTocol];
		document.search_listings.year_to.value=cur_cell.innerHTML;
	}
	closeResults("YearTo_op");
	var ytoval=document.search_listings.year_to.value;
	var d = new Date();
	var cur_year=d.getFullYear();
	if(ytoval=="")
		return;
	if(!ytoval.match("[0-9]+"))
	{
		alert("Please provide year between 1900 and "+(cur_year+1).toString()+".");
		document.search_listings.year_to.value="";
		document.search_listings.year_to.focus();
		return;
	}

	if(ytoval.toString().length==2)
	{
		newval=cur_year-(cur_year%100) + parseFloat(ytoval);
		if(newval>cur_year)
			newval=cur_year-100-(cur_year%100) + parseFloat(ytoval);
		document.search_listings.year_to.value=newval;
	}
	else if(ytoval<1900 || ytoval>(cur_year+1))
	{
		alert("Please provide year between 1900 and "+(cur_year+1).toString()+".");
		document.search_listings.year_to.value="";
		document.search_listings.year_to.focus();
		return false;
	}
}
function mouseoveryeartovalue(cur_cell)
{
    if(document.getElementById('YearTo_tab').rows.length!=0)
    {
        var cur_cell=document.getElementById('YearTo_tab').rows[YearTorow].cells[YearTocol];
        document.search_listings.year_to.value=cur_cell.innerHTML;
    }
}
function reset_yearto()
{
    closeResults("YearTo_op");
    //document.search_listings.year_from.value="";
    document.search_listings.year_to.value="";
}


//--------------------------------------------------------------------------------------------
function getyearfrom()
{
	var yfromval=document.search_listings.year_from.value;
	var d = new Date();
	var cur_year=d.getFullYear();
	if(yfromval=="")
		return;
	if(!yfromval.match("[0-9]+"))
	{
		alert("Please provide year between 1900 and "+cur_year.toString()+".");
		document.search_listings.year_from.value="";
		document.search_listings.year_from.focus();
		return;
	}

	if(yfromval.toString().length==2)
	{
		var newval=cur_year-(cur_year%100) + parseFloat(yfromval);
		if(newval>cur_year)
			newval=cur_year-100-(cur_year%100) + parseFloat(yfromval);
		document.search_listings.year_from.value=newval;
	}
	else if(yfromval<1900 || yfromval>cur_year)
	{
		alert("Please provide year between 1900 and "+cur_year.toString()+".");
		document.search_listings.year_from.value="";
		document.search_listings.year_from.focus();
	}
}

function getyearto()
{
	var ytoval=document.search_listings.year_to.value;
	var d = new Date();
	var cur_year=d.getFullYear();
	if(ytoval=="")
		return;
	if(!ytoval.match("[0-9]+"))
	{
		alert("Please provide year between 1900 and "+cur_year.toString()+".");
		document.search_listings.year_to.value="";
		document.search_listings.year_to.focus();
		return;
	}

	if(ytoval.toString().length==2)
	{
		newval=cur_year-(cur_year%100) + parseFloat(ytoval);
		if(newval>cur_year)
			newval=cur_year-100-(cur_year%100) + parseFloat(ytoval);
		document.search_listings.year_to.value=newval;
	}
	else if(ytoval<1900 || ytoval>cur_year)
	{
		alert("Please provide year between 1900 and "+cur_year.toString()+".");
		document.search_listings.year_to.value="";
		document.search_listings.year_to.focus();
		return false;
	}
}

function hidemakemodel()
{
	closeResults("make_op");
	closeResults("model_op");
}
// on enter key press check form validation
// if enter kay is pressed on make or model list to choose item then do accordingly.
// otherwise post the form.
function validateform()
{
	if(document.getElementById("make_op").style.display == "block")
	{
		//getmakevalue(document.getElementById("make_tab").rows[makerow].cells[makecol]);
		//document.search_listings.make_txt.focus();
		return false;
	}
	else if(document.getElementById("model_op").style.display == "block")
	{
		//getmodelvalue(document.getElementById("model_tab").rows[modelrow].cells[modelcol]);
		//document.search_listings.model_txt.focus();
		return false;
	}
/*
	else if(document.getElementById("YearFrom_op").style.display == "block")
	{
		//getmakevalue(document.getElementById("make_tab").rows[makerow].cells[makecol]);
		//document.search_listings.make_txt.focus();
		return false;
	}
	else if(document.getElementById("YearTo_op").style.display == "block")
	{
		//getmodelvalue(document.getElementById("model_tab").rows[modelrow].cells[modelcol]);
		//document.search_listings.model_txt.focus();
		return false;
	}
*/
	else
		return true;
/*
	if(fun==3)
	{
		return false;
	}
	if(keyval==-1)
	{
		keyval=0;
		return false;
	}
	else
	{
		document.search_listings.submit();
	}
*/
}
/*---------------------- Code for Make, Model navigation End ----------------------------*/
