﻿var portalPresentChecked = false;
var portalPresent = false;
var __GlobalTopmostW1ndow = null;
var SAPTop = null;
function getSAPTop()
{
  if (SAPTop != null) return SAPTop;
      var refDynamicTop = top;
      var testedFrame = window;
      try {
          while(testedFrame != top && testedFrame.parent != null) {
              try{
                  if(testedFrame.parent.EPCM != null) {
                      refDynamicTop = testedFrame.parent;
                  }
              } catch(ex1) {}
              testedFrame = testedFrame.parent;
          }
      } catch(ex2) {}
      SAPTop = refDynamicTop;
      return refDynamicTop;
}

// service call within same page: remote procedure call with IFrame; the IFrame will be created from scratch; Variable number of arguments
function xDKICallWithDomainRelaxing(aID,aURL,aReceiver,aSessionID,aLanguage)
{
    var i,aOldIFrame,aTempIFrame,aIFrameObj,aIFrameDoc,aIFrameForm,aOldIDiv,aTempIDiv,aIDivObj,aHTML1,aHTML2;
  globalArguments=arguments;
  if (!document.createElement) { return true } ;
  // Create IFrame always from scratch to avoid errors when calling multiple times with different fields
  aOldIFrame = xDKIElemId(aID);
  if (aOldIFrame) document.body.removeChild(aOldIFrame);
  // MSIE 5
  if (xDKIBrowser_IE5())
  {
     aOldIDiv = xDKIElemId('Div'+aID);
     if (aOldIDiv) document.body.removeChild(aOldIDiv);

     aTempIDiv = document.createElement('div');
     aTempIDiv.id = 'Div'+aID;
     aTempIDiv.style.display = "none";
     aHTML1 = '<iframe src="blank.html" id="'+aID+'" name="'+aID+'" frameBorder="0px" '+
              ' style="border:0px; background-color:white; position:absolute; left:0; top:0; width:0; height:0;">'+
              '</iframe>';
     aTempIDiv.innerHTML = aHTML1;
     aIDivObj = document.body.appendChild(aTempIDiv);

  }
  // Other Browser
  else
  {
     aTempIFrame = document.createElement('iframe');
     with (aTempIFrame) {src = "blank.html"; id = aID; name = aID; frameBorder = "0px";
                        with (style) { border = "0px"; backgroundColor = "white"; position = "absolute";
                                       left = 0; top = 0; width = 0; height = 0;  }
     }
  }
  aIFrameObj = document.body.appendChild(aTempIFrame);
}

function xDKICallFrameLoaded(frameWindow)
{
    var i,aOldIFrame,aTempIFrame,aIFrameObj,aIFrameDoc,aIFrameForm,aOldIDiv,aTempIDiv,aIDivObj,aHTML1,aHTML2;
    var aID,aURL,aReceiver,aSessionID,aLanguage;
    aID=globalArguments[0];
    aURL=globalArguments[1];
    aReceiver=globalArguments[2];
    aSessionID=globalArguments[3];
    aLanguage=globalArguments[4];
    if (xDKIBrowser_IE5())
    {
        aHTML2 = '<html><head/><body><form id="form'+aID+'" method="post" action="'+xDKIParseQuery(true,aURL)+'">'+
              '<input type="hidden" name="xDKI_Receiver" value="'+aReceiver+'"/>'+
              '<input type="hidden" name="xDKI_SessionID" value="'+aSessionID+'"/>'+
              '<input type="hidden" name="xDKI_Language" value="'+aLanguage+'"/>';
     if ((globalArguments.length == 6) && (typeof(globalArguments[5]) == 'object') && (globalArguments[5].constructor.toString().search('Array') != -1))
     {
        for (i=0; i<globalArguments[5].length; i=i+2)
            if ((globalArguments[5][i]!='xDKI_Receiver')&&(globalArguments[5][i]!='xDKI_SessionID')&&(globalArguments[5][i]!='xDKI_Language'))
               aHTML2 += '<input type="hidden" name="'+globalArguments[5][i]+'" value="'+globalArguments[5][i+1]+'"/>';
     }
     else
     {
        for (i=5; i<globalArguments.length; i+=2)
            aHTML2 += '<input type="hidden" name="'+globalArguments[i]+'" value="'+globalArguments[i+1]+'"/>';
     }
     aHTML2 += '</form></body></html>';
     with (document.frames[document.frames.length-1].document)
     {
          open();
          write(aHTML2);
          close();
          getElementById('form'+aID).submit();
     }
     return true;
  }
  else
    {
     aIFrameDoc = frameWindow.document;
     if (!aIFrameDoc) return true;
     // Form to post Receiver, SessionID, Language and Arguments
     aIFrameForm = aIFrameDoc.createElement("form");
     if (!aIFrameDoc.body) aIFrameDoc.appendChild(aIFrameDoc.createElement("body"));
     aIFrameDoc.body.appendChild(aIFrameForm);
     createHiddenFieldInDoc(aIFrameDoc,aIFrameForm,'xDKI_Receiver',aReceiver);
     createHiddenFieldInDoc(aIFrameDoc,aIFrameForm,'xDKI_SessionID',aSessionID);
     createHiddenFieldInDoc(aIFrameDoc,aIFrameForm,'xDKI_Language',aLanguage);
     if ((globalArguments.length == 6) && (typeof(globalArguments[5]) == 'object') && (globalArguments[5].constructor.toString().search('Array') != -1))
     {
        for (i=0; i<globalArguments[5].length; i=i+2)
            if ((globalArguments[5][i]!='xDKI_Receiver')&&(globalArguments[5][i]!='xDKI_SessionID')&&(globalArguments[5][i]!='xDKI_Language'))
               createHiddenFieldInDoc(aIFrameDoc,aIFrameForm,globalArguments[5][i],globalArguments[5][i+1]);
     }
     else
     {
        for (i=5; i<globalArguments.length; i+=2) createHiddenFieldInDoc(aIFrameDoc,aIFrameForm,globalArguments[i],globalArguments[i+1]);
     }
     aIFrameForm.method="post";
     aIFrameForm.action=xDKIParseQuery(true,aURL);
     aIFrameForm.submit();
     return true;
  }
globalArguments='';
}

function ReturnValueToService(aName, aValue)// changes a hidden field and submit the form
{
  with (document.forms['Form-A1'])
  {
       elements[aName].value = aValue;
       submit();
  }
}
function ChangeHiddenFields(aName, aValue)// changes a hidden field without submit
{
  with (document.forms['Form-A1'])
  {
       elements[aName].value = aValue;
  }
}

function findPosX(obj) // this functions return the absolute position of an object.
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}
function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}
function SetCurrentNode(NodeId)
{
  if (document.forms['Form-A1']) with (document.forms['Form-A1']) // otherwise error in printform, when clicking
  {
       elements['CurrentNode'].value = NodeId;
       elements['ListViewService'].value = 'ListView';
       elements['ViewPart'].value = '1';
       submit();
  }
}

function adjustHeightPropertySearch(PropertySearchHeight)
{
    PSIFrame=document.getElementById('ListViewTable');
    if (PropertySearchHeight == 0) PropertySearchHeight = 580;
    PSIFrame.style.height=(PropertySearchHeight+20);
    adjustHeightinPortal();
}
// Test if Portal is present. If so include festo_portal.js else include portal_emu.js //
function checkPortalPresent() {
  if ( portalPresentChecked ) {
      return portalPresent;
  }
  portalPresentChecked = 1;
  var myParent, mySaveParent;
  try
  {
    myParent = window.self;
    mySaveParent = null;
  }
  catch(e)
  {
    myParent=null;
  }
  var i = 0;
  while ((myParent != mySaveParent) && ( i < 1000 ) ) {
    var isolatedWorkArea = myParent.document.getElementById('isolatedWorkArea');
    var innerPage = myParent.document.getElementById('divChangeContent');
    innerPage = null;
    if ( (isolatedWorkArea != null) || ( innerPage != null ) ) {
      document.write('<script type="text\/javascript" language="JavaScript" src="javascript\/xdki_portal.js"><\/script>');
      document.write('<script type="text\/javascript" language="JavaScript" src="javascript\/epcfproxy.js"><\/script>');
      portalPresent = true;
      return portalPresent;
    }
    mySaveParent = myParent;
    myParent = myParent.parent;
    i++;
  }
  portalPresent = false;
  document.write('<script type="text\/javascript" language="JavaScript" src="javascript\/portal_emu.js"><\/script>');
  return portalPresent;
}
function callServiceWithWaitMessage(ElementID,Target,Options,xPos,yPos,Message,MessageWidth,MessageHeight,aRelative)
{
    function hideWaitDiv()
    {
        try
        { 
          var aEl = findElementById(ElementID)
      aEl.removeChild(WaitDiv);
          aEl.visibility="visible";
      }catch(e){ }    
    }
  if (pnf == "true") {
    TryLoadingStyleSheet();
  }
  var docwin = findElementsFrameById(ElementID);
  if (!docwin) docwin = self;
    var WaitDiv = docwin.document.createElement("DIV");
    var WaitImage=docwin.document.createElement("IMG");
    WaitImage.src=getxDKILocation()+"images/wait_mov.gif";
    var br=docwin.document.createElement("BR");
    var WaitTable;
    var WaitTCell;
    var WaitTRow;
    if (isNaN(parseInt(MessageHeight)))     
     MessageHeight=140
    else
     MessageHeight=parseInt(MessageHeight);    
     
    if (isNaN(parseInt(MessageHeight)))
    MessageWidth=300
  else          
    MessageWidth=parseInt(MessageWidth);     
    
    
    try {
    try { 
      findElementById(ElementID).appendChild(WaitDiv);
    } catch (err) { }  
      
      WaitDiv.id="WaitIFrame";
    if ((aRelative == undefined) || (aRelative == false) ) {
        WaitDiv.style.position="absolute";   
        WaitDiv.style.left=xPos-300;
        WaitDiv.style.top=yPos+5;
        WaitDiv.style.zIndex=0;
    }
      WaitDiv.className="allbordergreybg";
      WaitDiv.style.padding="0px";
      WaitDiv.align="center";
      WaitDiv.vAlign="middle";
      WaitDiv.style.height=MessageHeight+2+"px";
      WaitDiv.style.width=MessageWidth+2+"px";
      WaitTable=docwin.document.createElement("TABLE");
    if (pnf == "true") {
      WaitTable.className="festoreset";
    }
      WaitTable.style.height="100%";
      WaitTable.style.padding="20px";
      WaitTRow=WaitTable.insertRow(-1);
      WaitTCell=WaitTRow.insertCell(-1);
    if (pnf == "true") {
      WaitTRow.className="festoreset";
      WaitTCell.className="festoreset";
    }
      WaitTCell.align="center";
      WaitTCell.vAlign="middle";
    if (pnf == "true") {
        WaitTCell.innerHTML="<h2 class=\"festoreset\">"+Message+"</h2>";
    } else {
        WaitTCell.innerHTML="<h1>"+Message+"</h1>";
    }
      WaitTRow=WaitTable.insertRow(-1);   
      WaitTCell=WaitTRow.insertCell(-1);
    if (pnf == "true") {
      WaitTRow.classNname="festoreset";
      WaitTCell.className="festoreset";
    }
      WaitTCell.align="center";
      WaitTCell.vAlign="middle";
      WaitTCell.appendChild(WaitImage);
      WaitDiv.appendChild(WaitTable);
    } catch (err) {
  }
    if (window.frameElement)
    {
        adjustHeightinPortal();
    }
  var aElement = findElementById(Target);
  if (!aRelative) {
    try {
      if (aElement.onload == undefined) {
          try
          {
          aElement.attachEvent('onload',hideWaitDiv);//for IE
           } catch(e) { // FF
              try {     
                aElement.onload=hideWaitDiv; //for Firefox
              } catch (err) { }
          }  
      }
    } catch (e) {};
  }
    callService(ElementID,Target,Options);
    try
    {
        callServiceWithWaitMessageCallBack();
    }
    catch(e)
    {
      // Dummy....
    }
 
}

function callServiceBlankTarget(ElementID,Options)
{
    var MyArray;
    var OptionsArray=new Array;
    var ValuesArray=new Array;
    // Allow Options to be an array
    if (isArray(Options)) {
        MyArray = Options;
    } else {
        MyArray = Options.split(",");
    }
    
    var Counter;
    Counter=0
    for (i=0; i<MyArray.length; i++)
    {
        if (i%2==0)
        {
            OptionsArray[Counter]=MyArray[i];
        }
        if (i%2==1)
        {
            ValuesArray[Counter]=MyArray[i];
            Counter++;
        }
    }
    var aForm=document.createElement("FORM");
    if (document.getElementById(ElementID))
    document.getElementById(ElementID).appendChild(aForm);
    else
    document.body.appendChild(aForm);
  aForm.target="_blank";
    var ArrayOfHiddenFields=new Array;
    var OptionsCount=OptionsArray.length;
    for (i=0; i<OptionsCount; i++)
    {
        var hiddenField=document.createElement("INPUT");
        hiddenField.type="hidden";
        hiddenField.name=OptionsArray[i];
        hiddenField.value=ValuesArray[i];
        aForm.appendChild(hiddenField);
    }
    aForm.method="post";
    aForm.setAttribute('target','_blank');
    aForm.action="xDKI.asp";
    aForm.submit();
    if (document.getElementById(ElementID))
    document.getElementById(ElementID).removeChild(aForm);
    else
    document.body.removeChild(aForm);
}

function callService(ElementID,Target,Options)
{
    var MyArray;
    var OptionsArray=new Array;
    var ValuesArray=new Array;
    // Allow Options to be an array
    if (isArray(Options)) {
        MyArray = Options;
    } else {
        MyArray = Options.split(",");
    }
    
    var Counter;
    Counter=0
    for (i=0; i<MyArray.length; i++)
    {
        if (i%2==0)
        {
            OptionsArray[Counter]=MyArray[i];
        }
        if (i%2==1)
        {
            ValuesArray[Counter]=MyArray[i];
            Counter++;
        }
    }
    var aForm=document.createElement("FORM");
    if (document.getElementById(ElementID))
    document.getElementById(ElementID).appendChild(aForm);
    else
    document.body.appendChild(aForm);
    var ArrayOfHiddenFields=new Array;
    var OptionsCount=OptionsArray.length;
    for (i=0; i<OptionsCount; i++)
    {
        var hiddenField=document.createElement("INPUT");
        hiddenField.type="hidden";
        hiddenField.name=OptionsArray[i];
        hiddenField.value=ValuesArray[i];
        aForm.appendChild(hiddenField);
    }
    aForm.method="post";
    aForm.setAttribute('target',Target);
    aForm.action="xDKI.asp";
    aForm.submit(); 
  
  try {
      if (!(document.getElementsByName(Target).item(0))&& (!(document.getElementById(Target))));
      {
          newWindow=window.open("",Target);
          newWindow.focus();
      }
    if (document.getElementById(ElementID))
      document.getElementById(ElementID).removeChild(aForm);
    else
      document.body.removeChild(aForm);
  } catch (e) { 
    // this does happen in IE/ //
  };
  
}
function globalSearchElement(IFrame,ElementId) //this function searchs for this element in all IFrames and returns the IFrame
{
     var i;
     var result;
     result="";
    if ((IFrame.document.getElementsByName(ElementId).length > 0)||(IFrame.document.getElementById(ElementId)))
    {
        result=IFrame;
    }
    else
    {
      for(i=0;i<IFrame.length;i++)
       {
           try
           {
              if ((IFrame.frames[i].document.getElementsByName(ElementId).length > 0)||(IFrame.frames[i].document.getElementById(ElementId))||(IFrame.frames[i].name==ElementId)||(IFrame.frames[i].id==ElementId))
             {
                     result=IFrame.frames[i];
                     break;
                 } else {
                     result = globalSearchElement(IFrame.frames[i],ElementId)
                     if (result!="")break;
                 }
             }
             catch(e){}
       }
    }
     return result;
}
function openSmallWindow(xSize,ySize,WindowURL)
{
  var delClassAdviceWindow;
  delClassAdviceWindow=window.open(WindowURL,"newWindow", "width="+xSize+",height="+ySize+",left=0,top=0,status=no,toolbar=no,location=no");
  return delClassAdviceWindow;
}
  
function isFunction(a)
{
    return typeof a == 'function';
}
function isObject(a)
{
    return (typeof a == 'object' && !!a) || isFunction(a);
}
function isArray(a)
{
    return isObject(a) && a.constructor == Array;
}
// works like getEelementById() but also in all IFrames on the page //
// pity that it does not work on IE...... //
function findElementById(aId) {
  var ares = findFrameAndElementById(aId);
  return ares[1];
}
function findElementsFrameById(aId) {
  var ares = findFrameAndElementById(aId);
  return ares[0];
}
function findFrameAndElementById(aId) {
  function lookForElement(_aWindow,aId,aIt) {
    if (aIt.length > 20) { 
//      xDKIDS('Break drawn');
      return;                                                   // the 'break' for browsers performing badly
    }
//    try { xDKIDS('Lei le '+aIt+'   >'+_aWindow.location+'<  '+aId); } catch (e) {};
    var aThisIsIt = null;
    try {
      aThisIsIt = _aWindow.contentDocument.getElementById(aId);  // its an IFrame
    } catch (err) { 
//      xDKIDS('  =  '+err);
      try {
        aThisIsIt = _aWindow.document.getElementById(aId);      // its a 'normal' window / frame
      } catch (err) {  
//        xDKIDS('  -  '+err); 
      }
    }
    if (aThisIsIt != undefined) { 
//      xDKIDS('Found '+aThisIsIt);
      return [_aWindow,aThisIsIt];                                        // we found it => return the Element
    }
    var aElements;
    if (document.frames) {
      aElements = _aWindow.document.frames;
    } else {
      aElements = _aWindow.frames; 
    }
//    xDKIDS('  Found '+aElements.length);
    aIt += '['+aElements.length+']'; 
    for (var i = 0; i<aElements.length ; i++)    {
      try {
        aThisIsIt = lookForElement(aElements[i],aId,aIt+'/'+i);
      } catch (e) { }
      if (aThisIsIt != undefined) {
        return aThisIsIt;    
      }
    }     
    return aThisIsIt;
  }
//  xDKIDS('Start with '+aId);
  try {
    return lookForElement(top,aId,'root');
  } catch (e) { alert(e); };
}
/////////////////////////////////////////////////////////////
function getxDKILocation() {
  if (pnf=="false") {
    return window.location.protocol+'//'+window.location.host+'/xDKI/';
  } else {
    return window.location.protocol+'//'+window.location.host+document.location.pathname.replace(/xDKI.asp/g,"");
  }
}
function deletePx(aString) {
  return parseInt(aString.replace(/px/g,""));  
}
