
//********************************** code for fairDetails strarts  ****************************************//

function getDetails(show_code)
{
	var text;
	var str;
	var xmlHttp = null

	document.getElementById("fairDetails").style.display="none";
	if (typeof window.ActiveXObject != 'undefined' ) 
	{ 
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	else 
	{ 
		xmlHttp = new XMLHttpRequest(); 
	}
	var url = fairDetailURL + "?show_code=" + show_code + "&sid="+Math.random();
	
	xmlHttp.open("GET", url, false);
	xmlHttp.send(null);
	if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") 
	{	
		document.getElementById("fairDetails").innerHTML=xmlHttp.responseText;
		setDivPosition();
		document.getElementById("fairDetails").style.display="block";
	}
	if(show_code == "")
	{
		document.getElementById("fairDetails").style.display="none";
	}
}

function setDivPosition()
{
	document.getElementById("fairDetails").style.left = event.clientX + 10;
	document.getElementById("fairDetails").style.top = event.clientY + 3;
}

function changePosition()
{
	if ( document.getElementById("fairDetails").style.display=="block" )
	{
		document.getElementById("fairDetails").style.left = event.clientX + 5;
		document.getElementById("fairDetails").style.top = event.clientY + 3;		
	}	
}

function hideDetails()
{
	document.getElementById("fairDetails").style.display="none";
}

function setRowColor(rowEle,colorCode,setCurrentRow)
{
	rowEle.style.backgroundColor = '#' + colorCode;
	if ( setCurrentRow == true )
	{
		currentRow = parseInt(rowEle.id);
	}
}

//********************************** code for fairList strarts  ****************************************//

function startNavigation(objfair,e)
{
	var totalRows;	
	if(document.getElementById("fairsearch_totalRows") != null )
	{
		totalRows = document.getElementById("fairsearch_totalRows").value;
	}
	else
	{
		totalRows = 0;
	}
	if ( objfair.value.search(pattern) < 0 && process == true && objfair.value != "" && objfair.value.length >= 3 && parseInt(totalRows) > 0 )
	{
		switch(e.keyCode)
		{
			case 9: // tab key
				if(currentRow != 0 )
				{			
					
					runSetRoutine(eval('document.getElementById("' + currentRow + '_row").innerText;'),objfair.id);
				}
				break;
			case 13: //enter key 
				if ( currentRow != 0 )
				{			
					runSetRoutine(eval('document.getElementById("' + currentRow + '_row").innerText;'),objfair.id);
				}
				event.returnValue = false;
				break;
			case 33: //page up key
				if ( parseInt(currentRow) == 1 )
				{
					eval('document.getElementById("' + totalRows + '_row").scrollIntoView(false);');
					eval('document.getElementById("' + totalRows + '_row").style.backgroundColor="#' + rowSelectedColor + '";');
					eval('document.getElementById("' + ( currentRow ) + '_row").style.backgroundColor="#' + oddRowColor + '";');
					currentRow = totalRows;
				}
				else if ( ( parseInt(currentRow) - 8 ) <= 8 )
				{
					eval('document.getElementById("1_row").scrollIntoView(false);');
					eval('document.getElementById("1_row").style.backgroundColor="#' + rowSelectedColor + '";');
					if ( currentRow != 0 )
					{
						if ( currentRow % 2 == 0 )
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),evenRowColor,false);
						}
						else
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),oddRowColor,false);
						}
					}
					currentRow = 1;
				}
				else if ( ( parseInt(currentRow) - 8 ) > 8 )
				{
					eval('document.getElementById("' + ( currentRow - 8 ) + '_row").scrollIntoView(false);');
					eval('document.getElementById("' + ( currentRow - 8 ) + '_row").style.backgroundColor="#' + rowSelectedColor + '";');
					
					if ( currentRow != 0 )
					{
						if ( currentRow % 2 == 0 )
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),evenRowColor,false);
						}
						else
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),oddRowColor,false);
						}
					}
					currentRow = currentRow - 8;
				}
				event.returnValue = false;
				break;
			case 34: //page down
				if ( parseInt(currentRow) == parseInt(totalRows) )
				{
					eval('document.getElementById("1_row").scrollIntoView(false);');
					eval('document.getElementById("1_row").style.backgroundColor="#' + rowSelectedColor + '";');					
					currentRow = 1;
				}
				else if ( ( parseInt(totalRows) - parseInt(currentRow) ) <= 8 )
				{
					eval('document.getElementById("' + ( parseInt(totalRows) ) + '_row").scrollIntoView(false);');
					eval('document.getElementById("' + ( parseInt(totalRows) ) + '_row").style.backgroundColor="#' + rowSelectedColor + '";');
					if ( currentRow != 0 )
					{
						if ( currentRow % 2 == 0 )
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),evenRowColor,false);
						}
						else
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),oddRowColor,false);
						}
					}
					currentRow = totalRows;
				}
				else if ( ( parseInt(totalRows) - parseInt(currentRow) ) > 8 )
				{
					eval('document.getElementById("' + ( parseInt(currentRow) + 8 ) + '_row").scrollIntoView(false);');
					eval('document.getElementById("' + ( parseInt(currentRow) + 8 ) + '_row").style.backgroundColor="#' + rowSelectedColor + '";');
					if ( currentRow != 0 )
					{
						if ( currentRow % 2 == 0 )
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),evenRowColor,false);
						}
						else
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),oddRowColor,false);
						}
					}
					currentRow = currentRow + 8;
				}
				event.returnValue = false;
				break;
			case 35: //end key
				eval('document.getElementById("' + ( parseInt(totalRows)) + '_row").scrollIntoView(false);');
				eval('document.getElementById("' + ( parseInt(totalRows)) + '_row").style.backgroundColor="#' + rowSelectedColor + '";');
				if ( currentRow != 0 )
				{
					if ( currentRow % 2 == 0 )
					{
						setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),evenRowColor,false);
					}
					else
					{
						setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),oddRowColor,false);
					}
				}
				currentRow = totalRows;
				event.returnValue = false;
				break;
			case 36: //home key
				eval('document.getElementById("' + ( parseInt(1)) + '_row").scrollIntoView(false);');
				eval('document.getElementById("' + ( parseInt(1)) + '_row").style.backgroundColor="#' + rowSelectedColor + '";');
				if ( currentRow != 0 )
				{
					if ( currentRow % 2 == 0 )
					{
						setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),evenRowColor,false);
					}
					else
					{
						setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),oddRowColor,false);
					}
				}
				currentRow = 1;
				event.returnValue = false;
				break;
			case 38: //up arrow key
				if ( parseInt(currentRow) == 1 )
				{
					eval('document.getElementById("' + totalRows + '_row").scrollIntoView(false);');
					eval('document.getElementById("' + totalRows + '_row").style.backgroundColor="#' + rowSelectedColor + '";');
					eval('document.getElementById("' + ( currentRow ) + '_row").style.backgroundColor="#' + oddRowColor + '";');
					currentRow = totalRows;
				}
				else if ( parseInt(currentRow) > parseInt(1) )
				{
					//highlight previous row
					eval('document.getElementById("' + ( currentRow - 1 ) + '_row").scrollIntoView(false);');
					eval('document.getElementById("' + ( currentRow - 1 ) + '_row").style.backgroundColor="#' + rowSelectedColor + '";');
					if ( currentRow != 0 )
					{
						if ( currentRow % 2 == 0 )
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),evenRowColor,false);
						}
						else
						{
							setRowColor(eval('document.getElementById("' + ( currentRow ) + '_row")'),oddRowColor,false);
						}
					}
					currentRow = currentRow - 1;
				}
				break;
			case 40: //down arrow key
				if ( parseInt(currentRow) == parseInt(totalRows) )
				{
					eval('document.getElementById("1_row").scrollIntoView(false);');
					eval('document.getElementById("1_row").style.backgroundColor="#' + rowSelectedColor + '";');
					
					if ( currentRow != 0 && currentRow != 1 ) //check for current row for first and last row.
					{
						if ( currentRow % 2 == 0 )
						{
							eval('document.getElementById("' + ( currentRow ) + '_row").style.backgroundColor="#' + evenRowColor + '";');
						}
						else
						{
							eval('document.getElementById("' + ( currentRow ) + '_row").style.backgroundColor="#' + oddRowColor + '";');
						}
					}
					currentRow = 1;
				}
				else if ( parseInt(currentRow) < parseInt(totalRows) )
				{
					//highlight next row
					eval('document.getElementById("' + ( currentRow + 1 ) + '_row").scrollIntoView(false);');
					eval('document.getElementById("' + ( currentRow + 1 ) + '_row").style.backgroundColor="#' + rowSelectedColor + '";');
					if ( currentRow != 0 ) //check for current row for first and last row.
					{
						if ( currentRow % 2 == 0 )
						{
							eval('document.getElementById("' + ( currentRow ) + '_row").style.backgroundColor="#' + evenRowColor + '";');
						}
						else
						{
							eval('document.getElementById("' + ( currentRow ) + '_row").style.backgroundColor="#' + oddRowColor + '";');
						}
					}
					currentRow = currentRow + 1;
				}				
				break;
		}
	}
}

function runSetRoutine(value,objName)
{
	eval('document.getElementById("' + objName + '").value=value.split("[")[0]');
	if( typeof(document.getElementById("txtshowcode")) == 'object' )
	{
		try
		{
			document.getElementById("txtshowcode").value=value.split("[")[1].replace("]","");
		}
		catch(e)
		{
			//do nothing
		}
	}
	document.getElementById("tdContents").style.height="25px";
	document.getElementById("fairList").style.display="none";
	document.getElementById("fairDetails").style.display="none";
	eval('document.getElementById("' + objName + '").focus();');
}

function processKey(objRef)
{
	if( objRef.value.charAt(objRef.length).search(pattern) > 0 )
	{
		process = false;
	}
	else
	{
		process = true;
	}
}

function populateFairList(value,objRef)
{
	var text;
	var str;
	var xmlHttp = null

	value = value.replace(pattern,'');
	text = value;
	text = text.replace(pattern,'');
	
	if(text.length > 100 )
	{
		alert("How can you remember this long name while I can't even search for that.  Try something valid.");
		return;
	}
	else
	{	
		if ( text != "" && text.length >= 3 && process == true )
		{
			document.getElementById("fairList").style.display="none";
			document.getElementById("fairDetails").style.display="none";
			
			if (typeof window.ActiveXObject != 'undefined' ) 
			{ 
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			} 
			else 
			{ 
				xmlHttp = new XMLHttpRequest(); 
			}
			
			var url = fairListURL + "?objectName=" + objRef.id + "&text=" + text + "&sid="+Math.random();
			
			xmlHttp.open("GET", url, false);
			xmlHttp.send(null);
			
			if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") 
			{
				if (xmlHttp.responseText != "" )
				{
					document.getElementById("fairList").innerHTML=xmlHttp.responseText;
					currentRow = 0;
					
					if ( document.getElementById("tableContents").offsetHeight < document.getElementById("fairList").offsetHeight)
					{
						document.getElementById("tdContents").style.height=document.getElementById("tableContents").offsetHeight+25;
					}
					else
					{
						document.getElementById("tdContents").style.height=document.getElementById("fairList").offsetHeight+25;
					}
					
					document.getElementById("fairList").style.display="block";				
					
					if( parseInt(document.getElementById("fairList").style.left) != parseInt(document.getElementById("fairList").offsetLeft) )
					{
						document.getElementById("fairList").style.left=document.getElementById("fairList").offsetLeft;
					}
				}
			}
			
			if (xmlHttp.responseText == "" )
			{
				document.getElementById("tdContents").style.height="25px";
			}
		}
		
		if(text == "" || text.length < 3 )
		{
			document.getElementById("fairList").style.display="none";
			document.getElementById("tdContents").style.height=objRef.offsetHeight;
			document.getElementById("fairDetails").style.display="none";
		}
	}
}