var eLearnWindow;
var eLearnMode        = 0;
if(document.cookie && document.cookie.toString().search(/eLearn=1/) !=  -1){
  eLearnMode          = 1;
}













function wFireELearn(object, action, type, data){

  if(eLearnMode != 0){
    
    var url = "/weblication/grid/scripts/wELearn.php?action=triggerActivity&repository=&area="+object+"&eLearnAction="+action+"&type="+type+"&parameter="+data;

    getUrlELearn(url, wFireELearnResponse);
  }
}









function wFireELearnResponse(strELearn){
  
  if(eLearnMode != 0){
    var strElearnMode = strELearn.toString().substr(0, 1);
    
    if(strElearnMode == 1 || strElearnMode == 2){
      var url = strELearn.toString().substr(2)+'/wPages/index.php?action=wShowMessage';
      
      if(ie){
        var eLearnWindowHeight   = screen.availHeight - 20;
        var eLearnWindowLeft    = screen.availWidth - 410;
      }
      else{
        var eLearnWindowHeight   = screen.height;
        var eLearnWindowLeft    = screen.width - 200;
      }
      
      eLearnWindow   = window.open(url, "eLearnWindow", "width=400px, height="+eLearnWindowHeight+"px, left="+eLearnWindowLeft+"px, top=0px, resizable=yes")
      
      wPosELearn(1);
      
    }
    else if(strElearnMode == 3){

    }
  }
}









function getXPathExpression(node){
  if(eLearnMode != 0){
    var strReturn = '';
    if(node.parentNode){
      var nodeParent = node.parentNode;
      if(nodeParent.hasChildNodes()){
        var j = 0;
        for(var i = 0; i < nodeParent.childNodes.length; i++){
          if(nodeParent.childNodes[i].nodeType == 1){
            if(nodeParent.childNodes[i].nodeName == node.nodeName){  
              if(nodeParent.childNodes[i] === node){
                strReturn += getXPathExpression(nodeParent)+'|'+node.nodeName+'['+j+']';
              }
              j++;
            }
          }
        }
      }
    }
    else {
      strReturn += '|'+node.nodeName;
    }
    return strReturn;
  }
}









function wCheckPosELearn(){
  if(eLearnMode != 0){
    if(window.name != 'eLearnWindow'){
      var url = "/weblication/grid/scripts/wELearn.php?action=checkELearnMode";
      
      getUrlELearn(url, wPosELearn);
    }
  }
}

function wPosELearn(eLearnModeTxt){
  if(eLearnMode != 0){
    if(eLearnModeTxt == 1){
      var url = '/eLearn/wELearn/wPages/index.php?action=wShowMessage';
      if(eLearnWindow.closed != true){
        if(ie){
          var eLearnWindowHeight   = screen.availHeight - 20;
          var eLearnWindowLeft    = screen.availWidth - 410;
        }
        else{
          var eLearnWindowHeight   = screen.height;
          var eLearnWindowLeft    = screen.width - 200;
        }
      
        eLearnWindow   = window.open(url, "eLearnWindow", "width=400px, height="+eLearnWindowHeight+"px, left="+eLearnWindowLeft+"px, top=0px, resizable=yes")
      
        if(ie){
          var winWidth   = document.body.clientWidth;
          var winHeight  = document.body.clientHeight+285;
        }
        else{
          var winWidth   = window.outerWidth;
          var winHeight  = window.outerHeight;
        }
        
        if(winHeight > screen.height){
          var windowHeight = screen.height;
        }
        else{
          var windowHeight = winHeight;
        }
        if(ie){
          if(ieVers >= 7){
            var eLearnWidth = screen.width - eLearnWindow.document.body.clientWidth - 10;
          }
          else{
            var eLearnWidth = eLearnWindow.document.body.clientWidth - 170;
          }
        }
        else{  
          var eLearnWidth = screen.width - eLearnWindow.outerWidth;
        }
        
        if(winWidth > eLearnWidth){
          window.resizeTo(eLearnWidth, windowHeight);
        }
        
        var posX = window.screenX;
        var posY = window.screenY;
          
        if(window.screenX < 0){
          posX = 0;
        }
        else if(window.screenX > screen.width){
          posX = 0;
        }
        else if(window.screenX + winWidth > eLearnWidth){
          posX = screen.width - winWidth - eLearnWidth;
        }
          
        if(window.screenY < 0){
          posY = 0;
        }
        else if(window.screenY > screen.height){
          posY = 0;
        }
        
        window.moveTo(posX, posY);
      }
    }
  }
}











function getUrlELearn(url, functionResult){

  var requestObjectELearn = new wRequestObjectELearn(url, functionResult);
}











function wRequestObjectELearn(url, functionResult){

  if(typeof functionResult == 'function'){

      var id              = resultFunctions.length;

  
      this.request        = null;
  
      if(document.all){
        this.request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else{
        this.request = new XMLHttpRequest();
      }
  
      resultFunctions.push(functionResult);

  
      requests.push(this.request);
      this.request.open('GET', url, true);
  
      this.request.onreadystatechange = function(){
        if(requests){
          var requestObjectELearn = requests[id];
          if(requestObjectELearn.readyState == 4){
            if(requestObjectELearn.status){
              if(requestObjectELearn.status == '200'){
                resultFunctions[id](requestObjectELearn.responseText);
              }
            }
          }
        }
      }
  
      this.request.send('');
    }

}

