var gCurrentRowIndex = -1;


function dceFindControlByName(tdElement, controlName)
{
	var element;
	var childNodes = tdElement.childNodes;
	for(var i = 0; i < childNodes.length; i++)
	{
		if(childNodes[i].name == controlName) return childNodes[i];
	}
	return null;
}
function HidDiv()
{

document.getElementById("autocomplete").style.Visibility="hidden";
}
function dceFindControlInRow(trElement, controlName) 
{
	var element;
	if(trElement==null)
		return null;
	var cellCount = trElement.cells.length;
	for(var i = 0; i < cellCount; i++)
	{
		element = dceFindControlByName(trElement.cells[i], controlName);
		if(element) return element;
	}
	return null;
}


//	function to move the records up or down for the appropriate key pressed(Up or Down)
function moveRecord(tablename)
{

	var keyCode = window.event.keyCode;
	if(keyCode == 38) moveUp(tablename);
	if(keyCode == 40) moveDown(tablename);
}
//	Function to move the record

function moveUp(tableName)
{
	var tableElement = document.getElementById(tableName);
	var tBody = tableElement.tBodies[0];
	if(gCurrentRowIndex > 0 && tBody.rows.length > 0)
	{	
		gCurrentRowIndex--;
		document.getElementById("autocomplete").scrollTop=document.getElementById("autocomplete").scrollTop-10;
		changeRowColor(tableElement, gCurrentRowIndex);
	}
}

// Function to move down the record

function moveDown(tableName)
{
	var tableElement = document.getElementById(tableName);
	var tBody = tableElement.tBodies[0];
	if(gCurrentRowIndex < (tBody.rows.length -1) && tBody.rows.length > 0)// check current row
	{ 
		gCurrentRowIndex++;
		document.getElementById("autocomplete").scrollTop=document.getElementById("autocomplete").scrollTop+10;
		changeRowColor(tableElement, gCurrentRowIndex);
		
		//alert("MouseDown");
	
		
	
	}
}

//	function change color for the table for a particular row
function changeRowColor(tableElement, rowIndex) 
{
	var tBody = tableElement.tBodies[0];
	resetRowStyle(tableElement);
	if(gCurrentRowIndex>-1)
	
	tBody.rows[gCurrentRowIndex].className = "tableheader";//	 change the selected row color
	
}

//	Reset the color
function resetRowStyle(tableElement)
{
	var tBody = tableElement.tBodies[0];
	for(var i = 0; i < tBody.rows.length; i++) 
	{
		tBody.rows[i].className = "tablecontent4"
	}
}

//	Search for an item
//	function will be called when item code or name keypress on order list table
function HideDiv(divid) 
{
	if (document.layers) document.layers[divid].visibility="hide";
	else 
	{
		document.getElementById(divid).style.visibility="hidden";
		var nodes=document.body.getElementsByTagName("select");
		alert(nodes.length);
	for(var i=0;i<nodes.length;i++)
	{
	//alert(document.getElementById(nodes[i].getAttribute("id")).style.Visibility);
	document.getElementById(nodes[i].getAttribute("id")).style.display="block";
	}
	}
}
function ShowDiv(divid) 
{
   
   if (document.layers) document.layers[divid].visibility="show";
   
   else
   {

    document.getElementById(divid).style.visibility="visible";
   		
    }
 //  document.getElementById("drpProjectType").style.visibility="visible";


}
function searchItem(object, cellIndex,tablename,controlname) {

	var keyCode = window.event.keyCode;
	//alert(keyCode);
	if(keyCode != 38 && keyCode != 40 && keyCode != 13)
	{
		mdsSearch(object.value, tablename, cellIndex)
	}
	if(keyCode == 13) {	
//alert("enter is prees");
	//	When enter is pressed
		var destRow = object.parentElement.parentElement;
		mdsItemSelected(tablename, destRow,controlname);
	}
}


//Search Filter added
function searchFilterName(object, cellIndex,tablename,controlname,srchFilter) {
	var keyCode = window.event.keyCode;
	alert(keyCode);
	if(keyCode != 38 && keyCode != 40 && keyCode != 13)
	{
		mdsSearch(object.value, tablename, cellIndex)
	}
	if(keyCode == 13) {	
		var destRow = object.parentElement.parentElement;
		mdsItemSelected(tablename, destRow,controlname);
	}
}
// End of Search Filter

function searchItemforlist(object, cellIndex,tablename,controlname) {
	var keyCode = window.event.keyCode;
	//alert(keyCode);
	if(keyCode != 38 && keyCode != 40 && keyCode != 13)
	{
		mdsSearch(object.value, tablename, cellIndex)
	}
	if(keyCode == 13) {	//	When enter is pressed
		var destRow = object.parentElement.parentElement;
		mdsItemSelectedforlist(tablename, destRow,controlname);
	}
}

function mdsSearch(strFind, tableName, cellIndex) {
	//	Get the table to find should happend
	var tableElement = document.getElementById(tableName);
	var tBody = tableElement.tBodies[0];
	//resetRowStyle(tableElement);
	for(var i = 0; i < tBody.rows.length; i++) {
		var cellText = tBody.rows[i].cells[cellIndex].innerText.toUpperCase();
		if(cellText.indexOf(strFind.toUpperCase()) == 0) {
			gCurrentRowIndex = i; // change global variable to track the current index
			changeRowColor(tableElement, gCurrentRowIndex);
			return true;
		}
	}

	gCurrentRowIndex = -1;	//	Reset because key code does not match
	//tableElement.tHead.scrollIntoView()
	return false;
}

function mdsItemSelected(srcTableName, destRow,controlname) {
	var tableElement = document.getElementById(srcTableName);
	if(tableElement!=null)
	var tBody = tableElement.tBodies[0];
	//alert(":"+gCurrentRowIndex);
	if(gCurrentRowIndex > -1)
	{
	
		var srcRow = tBody.rows[gCurrentRowIndex];
		if(srcRow==null)
			return;
		var str=dceFindControlInRow(srcRow,"hidKeyword").value;
		document.getElementById(controlname).value =
				dceFindControlInRow(srcRow,"hidKeyword").value;
		gCurrentRowIndex=-1;
		var IDValue=dceFindControlInRow(srcRow,"hidKeyword1").value;
		HideDiv("autocomplete");
		SetID(IDValue);
	}
}
function mdsItemSelectedforlist(srcTableName, destRow,controlname) {
	var tableElement = document.getElementById(srcTableName);
	if(tableElement!=null)
	var tBody = tableElement.tBodies[0];
	//alert(":"+gCurrentRowIndex);
	if(gCurrentRowIndex > -1)
	{
		var srcRow = tBody.rows[gCurrentRowIndex];
		if(srcRow==null)
			return;
		var str=dceFindControlInRow(srcRow,"hidKeyword").value;
		//alert(document.getElementById(controlname).options.length);
		for(var k=0;k<document.getElementById(controlname).options.length;k++)
{
if(document.getElementById(controlname).options[k].value==str)
{
alert("User already selected");

return false;
}
}
		document.getElementById(controlname).options[document.getElementById(controlname).options.length] =
			new Option(dceFindControlInRow(srcRow,"hidKeyword").value,dceFindControlInRow(srcRow,"hidKeyword").value);
		gCurrentRowIndex=-1;
		
		HideDiv("autocomplete");
		//callAjax();
	}
}
function HideDiv(divid) 
{

//document.getElementById("drpProjectType").style.visibility ="visible";
   if (document.layers) document.layers[divid].visibility="hide";
   
  else
  {
   document.getElementById(divid).style.visibility="hidden";
  
   }
  
}//

function replaceAll(oldStr,findStr,repStr)
{
  var srchNdx = 0;      
  var newStr = ""; 
  while (oldStr.indexOf(findStr,srchNdx) != -1) 
  {
    newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
    newStr += repStr;
    srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);
  return newStr;
}



