function widget_submit()
{
    //var make = document.getElementById('make').value;
    //var model = document.getElementById('model').value;

    //if (make != '' && model != '')
    //{
        document.getElementById('lemonfree_widget_form').submit();
    //}
}

/*---------------------- 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")
	}
	if(obj_str=="model_op")
	{
		closeResults("make_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();
	}
    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;

function searchMakes(pos)
{
	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.getElementById('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.getElementById('make_txt').focus();
	}
	else
	{
		// get the make textbox value.
		makevalue=document.getElementById('make_txt').value;
/*
		if(makevalue=="") //"Start typing your make")
		{
			document.getElementById("make_iframe").style.width="0px";
			document.getElementById("make_iframe").style.height="0px";
			document.getElementById("make_subop").innerHTML = "<div class=\"inittext\">Start typing your make</div>";
			displayResults("make_op");
			return;
			//makevalue="";
		}
*/
		// 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'\" 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;

		if (pos > 0)
        {
            //var position_adjust = dv.clientWidth - 125;
            var position_adjust = dv.clientWidth - pos;
            document.getElementById("make_op").style.right = position_adjust+"px";
        }
	}
}
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.getElementById('make_txt').value=cur_cell.innerHTML;
		document.getElementById('make').value=cur_cell.id;
	}
	closeResults("make_op");
	document.getElementById('model_txt').value="";
	document.getElementById('model').value="";
}
function searchModels(pos)
{
	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.getElementById('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.getElementById('model_txt').focus();
	}
	else
	{
		// get the model textbox value.
		modelvalue=document.getElementById('model_txt').value;
/*
		if(modelvalue=="")
		{
			document.getElementById("model_iframe").style.width="0px";
			document.getElementById("model_iframe").style.height="0px";
			document.getElementById("model_subop").innerHTML = "<div class=\"inittext\">Start typing your model</div>";
			displayResults("model_op");
			return;
			//makevalue="";
		}
*/
		// 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.getElementById('make').value;
        if (modelindex == '')
        {
            for (var z=0; z<makelist.length; z++)
            {
                if (makelist[z] == document.getElementById('make_txt').value)
                {
                    modelindex = makeidlist[z];
                    document.getElementById('make').value = modelindex;
                    break;
                }
            }
        }

		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=\"getmodelrow_col(this);this.className='layer_td_hover'\" 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;
		if (pos > 0)
	    {
			//var position_adjust = dv.clientWidth - 125;
            var position_adjust = dv.clientWidth - pos;
			document.getElementById("model_op").style.right = position_adjust+"px";
		}
	}
}
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)
{
	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.getElementById('model_txt').value=cur_cell.innerHTML;
		document.getElementById('model').value=cur_cell.id;
	}
	closeResults("model_op");
	fun=0;
}
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
		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 ----------------------------*/
