var currentDomName = "";
function onLoadWindow()
{
  document.getElementById("popupFooter").innerHTML = document.getElementById("popupFooter").innerHTML + "Enter-Set Value <b class=\"seperator\">||</b> Esc- Cancel <b  class=\"seperator\">||</b> UP & DOWN arrow - Navigation";
  var table = document.getElementById("popupTable"); 
  var tableLength = table.rows.length;
  if(table.rows.length == 0)
  {
    try
    {
      table.getElementsByTagName("td")[0].style.width = "300px";
      table.getElementsByTagName("td")[0].style.height = "250px";
      table.getElementsByTagName("td")[0].innerHTML = "<center>NO DATA FOUND</center>";
    }
    catch(err)
    {}
  }
  try
  {
  var table = document.getElementById("popupTable");
  for( i = 0 ;i<table.rows.length ; i++)
  {
    table.rows[i].className = "deSelectedRow";
      for(j = 2; j<table.rows[i].cells.length ; j++)
      {
	table.rows[i].children[j].children[0].className = "popupFieldDisable";
      }
  }
  var currentfield = table.rows[1].children[0].children[0];
  currentfield.checked = true;
  var currentfield = table.rows[1].children[1].children[0];
  focusRow(table,currentfield);
  currentfield.focus();
  
  
  }
  catch(err){}
  
}  

document.onkeydown = onKeypressEvent;

function onKeypressEvent(e)
{
  var keyID = (window.event) ? event.keyCode : e.keyCode;  

  switch (keyID)  
  {  
    //Esc Key
    case 27:
	  var loc = window.location.href;
	  var pos = loc.indexOf("FIELDNAME=");
	  var pos1 = loc.indexOf("&OBJ_NAME");
	  fieldId = loc.substring(pos+10,pos1);
	  parent.closePopWindow(fieldId,false);
	  return false;
	  break;
  }
  return true;
}
function onLoadCalculator()
{
  document.getElementById("popupFooter").innerHTML = "Esc- Exit";
}

function checkEvent( field,e )   
{   
  var table = document.getElementById("popupTable");
  var tableLength = table.rows.length;

  var keyID = (window.event) ? event.keyCode : e.keyCode; 
//  alert("keyID :"+keyID);
  if(keyID == "" || keyID == null || keyID == "undefined")
  {
	  keyID = 13;
  }
 // alert("keyID@@@ :"+keyID);
  switch (keyID)  
  {  
    //Down Key
    case 40:
	var curRowId = field.parentNode.parentNode.rowIndex;
 	
	if(curRowId == (tableLength - 1))
	{
	  currentfield = table.rows[0].children[0].children[0];
	  currentfield.checked = true;
	  currentfield = table.rows[0].children[1].children[0];
	  focusRow(table,currentfield);
	  currentfield.focus();
	}
	else
	{
	  var currentfield = table.rows[curRowId+1].children[0].children[0];
	  currentfield.checked = true;
	  currentfield = table.rows[curRowId+1].children[1].children[0];
	  focusRow(table,currentfield);
	  currentfield.focus();
	}
	return false;
    break;  
    //TAB key
    case 9:
	var curRowId = field.parentNode.parentNode.rowIndex;
	if(e.shiftKey)
	{
	  if(curRowId == 0)
	  {
	    currentfield = table.rows[tableLength-1].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[tableLength-1].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	  else
	  {
	    var currentfield = table.rows[curRowId-1].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[curRowId-1].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	}
	else
	{
	  if(curRowId == (tableLength - 1))
	  {
	    currentfield = table.rows[0].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[0].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	  else
	  {
	    var currentfield = table.rows[curRowId+1].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[curRowId+1].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	}
	return false;
    break;  
    //Up key
    case 38:  
	  var curRowId = field.parentNode.parentNode.rowIndex;
	  if(curRowId == 0)
	  {
	    currentfield = table.rows[tableLength-1].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[tableLength-1].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	  else
	  {
	    var currentfield = table.rows[curRowId-1].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[curRowId-1].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	  return false;
      break;  
    //Page Down
    case 34:
	  var curRowId = field.parentNode.parentNode.rowIndex;
	  if((parseInt(curRowId)+20) < tableLength )
	  {
	    currentfield = table.rows[curRowId+20].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[curRowId+20].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	  else
	  {
	    currentfield = table.rows[tableLength-1].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[tableLength-1].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	  return false;
	  break;
    //Page UP
    case 33:  
	  var curRowId = field.parentNode.parentNode.rowIndex;
	  
	  if((parseInt(curRowId)-20) > 0 )
	  {
	    currentfield = table.rows[curRowId-20].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[curRowId-20].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	  else
	  {
	    currentfield = table.rows[0].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[0].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  }
	  return false;
	  break;
    //Home Key
    case 36:
	    currentfield = table.rows[0].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[0].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  return false;
      break;
    //End Key
    case 35:
	    currentfield = table.rows[tableLength-1].children[0].children[0];
	    currentfield.checked = true;
	    currentfield = table.rows[tableLength-1].children[1].children[0];
	    focusRow(table,currentfield);
	    currentfield.focus();
	  return false;
      break;
    //Esc Key
    case 27:  
	  onKeypressEvent(e);
	  return false;
      break;  
    //Enter Key
    case 13: 
	  var loc = window.location.href;
	  var pos = loc.indexOf("FIELDNAME=");
	  var pos1 = loc.indexOf("&OBJ_NAME");
	  fieldId = loc.substring(pos+10,pos1);
	  domName = fieldId.substring(0,fieldId.lastIndexOf("."));
	  tableRowField = field.parentNode.parentNode;
	  parent.setPopValue(tableRowField,domName,fieldId);
	  parent.closePopWindow(fieldId,true);
	  return false;
      break;
  }
  return false;
}
function onFocusFun(field)
{
    var table = document.getElementById("popupTable");
    var tableLength = table.rows.length;
    var curRowId = field.parentNode.parentNode.rowIndex;
    currentfield = table.rows[curRowId].children[0].children[0];
    currentfield.checked = true;
    currentfield = table.rows[curRowId].children[1].children[0];
    focusRow(table,currentfield);
    table.rows[curRowId].children[0].style.textAlign = "right";
    currentfield.focus();
    currentfield.select();
    return false;
}
function focusRow(table,currentfield)
{
  currentfield.parentNode.parentNode.className = "selectRow";
  rowIndex1 = currentfield.parentNode.parentNode.rowIndex;
  for(j = 1; j<table.rows.length ; j++)
  {
    table.rows[j].children[0].style.textAlign = "left";
  }
    for(j = 2; j<table.rows[rowIndex1].cells.length ; j++)
    {
      currentfield.parentNode.parentNode.children[j].children[0].className = "selectRowFieldDisable";
    }
   currentfield.parentNode.parentNode.children[0].children[0].style.textAlign = "left";
}
function onblurFunction(field)
{
  var table = document.getElementById("popupTable");
  field.parentNode.parentNode.className = "deSelectedRow";
  rowIndex1 = field.parentNode.parentNode.rowIndex;
    for(j = 2; j<table.rows[rowIndex1].cells.length ; j++)
    {
      field.parentNode.parentNode.children[j].children[0].className = "popupFieldDisable";
    }
}
function checkCalPopUpEvent(field,e)
{

  var table = document.getElementById("popupTable");
  var tableLength = table.rows.length;

  var keyID = (window.event) ? event.keyCode : e.keyCode;  

  switch (keyID)  
  {  
    //Down Key
    case 40:
        var curRowId = field.parentNode.parentNode.rowIndex;
        if(curRowId == (tableLength - 1))
        {
          currentfield = table.rows[0].children[1].children[0];
          currentfield.focus();
        }
        else
        {
          currentfield = table.rows[curRowId+1].children[1].children[0];
          currentfield.focus();
        }
        return false;
    break;  
    //TAB key
    case 9:
        var curRowId = field.parentNode.parentNode.rowIndex;
        if(e.shiftKey)
        {
          if(curRowId == 0)
          {
            currentfield = table.rows[tableLength-1].children[1].children[0];
            currentfield.focus();
          }
          else
          {
            currentfield = table.rows[curRowId-1].children[1].children[0];
            currentfield.focus();
          }
        }
        else
        {
          if(curRowId == (tableLength - 1))
          {
            currentfield = table.rows[0].children[1].children[0];
            currentfield.focus();
          }
          else
          {
            currentfield = table.rows[curRowId+1].children[1].children[0];
            currentfield.focus();
          }
        }
        return false;
    break;  
    //Up key
    case 38:  
          var curRowId = field.parentNode.parentNode.rowIndex;
          if(curRowId == 0)
          {
            currentfield = table.rows[tableLength-1].children[1].children[0];
            currentfield.focus();
          }
          else
          {
            currentfield = table.rows[curRowId-1].children[1].children[0];
            currentfield.focus();
          }
          return false;
      break;  
    //Esc Key
    case 27:  
          onKeypressEvent(e);
          return false;
      break;  
  }
  return true;  
}
function getUrlVars(domName,field) {
    var vars = "";
    var hashes = document.getElementById("paramURL").value.slice(1).split('&');
    for(var i = 0; i < hashes.length; i++) 
    {
      popInputField = document.popupform.getElementsByTagName("input");
      hash = hashes[i].split('=');
      for(cnt = 0; cnt < popInputField.length ; cnt++)
      {
        if(popInputField[cnt].name.indexOf("Detail") == -1)
        {
          if(domName + "."+popInputField[cnt].name == hash[0])
          {
            hash[1] = popInputField[cnt].value;
          }
        }
        else
        {
          if(popInputField[cnt].name == hash[0])
          {
            hash[1] = popInputField[cnt].value;
          }
        }
        if(hash[0].indexOf("FOCUSED_COL") != -1)
	{
	    hash[1] = domName+"."+field.name;
	}
        if(hash[0].indexOf("ACTION") != -1)
	{
	    hash[1] = "post_item_change";
	}
      } 
      vars +=hash[0]+"="+hash[1]+"&";
    }
  return vars;
 
  }
function onServerCallFun(field)
{
   
  var loc = window.location.href; 
 
  var pos = loc.indexOf("FIELDNAME=");
  var pos1 = loc.indexOf("&OBJ_NAME");
  fieldId = loc.substring(pos+10,pos1);
  if(field != null && (field.getAttribute("isservercallonchange") != null && field.getAttribute("isservercallonchange") == "true")) // CHECK FIELD IS SET FOR ITEM CHANGE OR NOT
    {
      
       createActionEventElement(field);
        if(document.getElementById("ACTION") != null)
        {
          document.getElementById("ACTION").value = "post_item_change";
        }
        if(document.getElementById("FOCUSED_COL") != null)
        {
	  if(field.name.indexOf("Detail") == -1)
	  {
	       document.getElementById("FOCUSED_COL").value = "Detail2.1."+field.name;
	  }
	  else
	  {
	    document.getElementById("FOCUSED_COL").value = field.name;
	  }
	  	  
        }
        if(document.getElementById("forcedSave") != null)
        {
          document.getElementById("forcedSave").value = "false";
	  
        }
        
    }
  domName = fieldId.substring(0,fieldId.lastIndexOf("."));  
  currentDomName = domName;
  keyString  = getUrlVars(domName,field);
  xmlhttpPost("/ibase/E12SingleTranEditorServlet", keyString,""); 
      
}

function xmlhttpPost(strURL,formname,responsemsg) 
{
    var xmlHttpReq = false;
    var self = this;
    // Xhr per Mozilla/Safari/Ie7
    if (window.XMLHttpRequest) 
    {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // per tutte le altre versioni di IE
    else if (window.ActiveXObject) 
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open("POST", strURL, true);
    self.xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    self.xmlHttpReq.onreadystatechange = function() 
    {
        if (self.xmlHttpReq.readyState == 4) 
        {
            setValueInPopUp(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(keyString);
}

function createActionEventElement(field)
{
  if(document.getElementById("ACTION") == null)
  {
    var element = document.createElement("input");
    element.type = "hidden";
    element.name = "ACTION";
    element.value = "post_item_change";
    element.setAttribute("id", "ACTION");
    element.setAttribute("readonly", "readonly");
    document.popupform.appendChild(element);  
  }
  if(document.getElementById("FOCUSED_COL") == null)
  {
    var element = document.createElement("input");
    element.type = "hidden";
    element.name = "FOCUSED_COL";
    if(field != null)
    {
      element.value = field.name;
    }
    else
    {
      element.value = "";
    }
    element.setAttribute("id", "FOCUSED_COL");
    element.setAttribute("readonly", "readonly");
    document.popupform.appendChild(element);  
  }
  if(document.getElementById("forcedSave") == null)
  {
    var element = document.createElement("input");
    element.type = "hidden";
    element.name = "forcedSave";
    element.value = "false"
    element.setAttribute("id", "forcedSave");
    element.setAttribute("readonly", "readonly");   
    document.popupform.appendChild(element);  
  }
  }

function setValueInPopUp(xmlStr)
{
  if (window.DOMParser)
    {
      parser=new DOMParser();
      xmlDoc=parser.parseFromString(xmlStr,"text/xml");
    }
  else // Internet Explorer
    {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async=false;
      xmlDoc.loadXML(xmlStr);
    }
    if(xmlDoc.getElementsByTagName("Root") != null ) 
    {
      var x = xmlDoc.getElementsByTagName("Root")[0].childNodes;
      if(x != null)
      {
        for ( i = 1 ; i < x.length ; i++)
        {
          
          detailElement = x[i];
          if(detailElement.nodeName.indexOf("Detail1") != -1)
          {
              detailChild = detailElement.childNodes;
              for( j = 1 ;j < detailChild.length ; j++)
              {
                if(detailChild[j] != null)
                {
                  fieldName = detailChild[j].nodeName;
                  if(document.getElementById("Detail1.1." + fieldName) != null)
                  {
                    try
                    {
                      document.getElementById("Detail1.1." + fieldName).value = detailChild[j].childNodes[0].nodeValue;
                      if(detailChild[j].getAttribute("protect") == "1" || detailChild[j].getAttribute("PROTECT") == "1")
                    {
                      document.getElementById("Detail1.1." + fieldName).setAttribute("readonly","true");
                      document.getElementById("Detail1.1." + fieldName).setAttribute("tabIndex", "-1");
                    }
                    }
                    catch(err){}
                  }
                }
              }
          }
            else
            {
              detailChild = detailElement.childNodes;
              for( j = 0 ;j < detailChild.length ; j++)
              {
                if(detailChild[j] != null)
                {
                    if(document.getElementById(detailChild[j].nodeName) != null)
                    {
                      try{
                        document.getElementById(detailChild[j].nodeName).value = detailChild[j].childNodes[0].nodeValue;
                        if(detailChild[j].getAttribute("protect") == "1" || detailChild[j].getAttribute("PROTECT") == "1")
                        {
                          document.getElementById(detailChild[j].nodeName).setAttribute("readonly","true");
                          document.getElementById(detailChild[j].nodeName).setAttribute("tabIndex", "-1");
                        }
                      }
                      catch(err){}
                    }
                }
              }
            }
          }
        }
      }
}