//we maken alleen maar gebruik van sessionID bij het aanroepen van w_eservice.asmx
//voor chatservice.asmx is deze niet nodig. Hier maken we geen gebruik van sessie informatie
//LETOP de w_eservice.asmx komt bij chatportal ook voor in de front. Anders heb je geen sessionid voor aanroep van chat.aspx en kun je de translations niet doen.
var sSessionID='';
var sHost='';
var TaalID;
var GroupID;
var NodeID;

function GetXMLHttpRequest() 
{
  if (window.XMLHttpRequest) 
  {
		return new XMLHttpRequest();
	}
	else 
	{
		var progIDs = ["MSXML2.XMLHTTP.3.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0",  "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
		for (var i = 0; i < progIDs.length; ++i) 
		{
			var progID = progIDs[i];
			try 
			{
				var x = new ActiveXObject(progID);
				return x;
			} 
			catch (e) 
			{

			}
		}
  }
	return null;
}

function CheckOnlineStatus2(sUrl, lTaalID, lGroupID, lNodeID)
{
  //Determine if there are operators which can accept new chats
  //If there are available then object will by shown otherwise object will be hidden.
  var i;
  var bResult = false;

  try
  {
    var xmlHttp = GetXMLHttpRequest();
    if ( xmlHttp == null)
    {
      //alert('Error');
    }
    else
    {
      xmlHttp.open ('POST', sUrl, false );
      xmlHttp.setRequestHeader('Host', sHost); 
      xmlHttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
      xmlHttp.setRequestHeader('SOAPAction', 'http://tempuri.org/OperatorAvailable2');
      xmlHttp.send('<?xml version=\"1.0\" encoding=\"utf-8\"?> '+
'<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> '+
'  <soap:Body> '+
'    <OperatorAvailable2 xmlns=\"http://tempuri.org/\"> '+
'      <lNodeID>'+lNodeID+'</lNodeID> '+
'      <lTaalID>'+lTaalID+'</lTaalID> '+
'      <lGroupID>'+lGroupID+'</lGroupID> '+
'      <lDepartmentID>0</lDepartmentID> '+
'      <lEmployeeID>0</lEmployeeID> '+
'    </OperatorAvailable2> '+
'  </soap:Body> '+
'</soap:Envelope> '); 

      if (xmlHttp.status == 200)
      {
        var xml = xmlHttp.responseXML;
        var xmlNode = xml.documentElement.getElementsByTagName("OperatorAvailable2Result")[0].firstChild;

        if (xmlNode.nodeValue=='true')
        {
          bResult = true;
        }
      }
      delete xmlHttp; 
    }
  }
  catch(e)
  {
    alert(e);
  }
  return bResult;
}

function Login()
{
  //Determine if there are operators which can accept new chats
  //If there are available then object will by shown otherwise object will be hidden.
  var i;
  var bResult = false;
  
  try
  {
    var xmlHttp = GetXMLHttpRequest();
    if ( xmlHttp == null)
    {
      //alert('Error');
    }
    else
    {
      var sEnvelope = '<?xml version=\"1.0\" encoding=\"utf-8\"?>'+
'<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">'+
'  <soap:Body>'+
'    <LoginAnonymous xmlns=\"http://tempuri.org/\">'+
'      <UserGroupID>'+GroupID+'</UserGroupID>'+
'      <TaalID>'+TaalID+'</TaalID>'+
'      <ProductList></ProductList>'+
'      <CategoryList></CategoryList>'+
'    </LoginAnonymous>'+
'  </soap:Body>'+
'</soap:Envelope>';
      
      xmlHttp.open ('POST', sUrl+sDevelop+'/('+sSessionID+')/w_eservice.asmx', false );
      xmlHttp.setRequestHeader('Host', sHost); 
      xmlHttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
      xmlHttp.setRequestHeader('SOAPAction', 'http://tempuri.org/LoginAnonymous');
      xmlHttp.send(sEnvelope); 

      if (xmlHttp.status == 200)
      {
        var xml = xmlHttp.responseXML;
        var xmlNode = xml.documentElement.getElementsByTagName("Successfull")[0].firstChild;

        if (xmlNode.nodeValue=='true')
        {
          bResult = true;
        }
      }
      delete xmlHttp; 
    }
  }
  catch(e)
  {
    alert(e);
  }
  return bResult;
}

function ShowChatbuttonPortal(sdummy, lTaalID, lGroupID, lNodeID)
{
  if (sHost=='')
  {
    var sTemp = new String(sUrl);
    sTemp = sTemp.replace('http://','');
    sTemp = sTemp.replace('https://','');
    if (sTemp.indexOf('/')>-1)
      sTemp = sTemp.substr(0, sTemp.indexOf('/'));
    sHost = sTemp;
  }
  //hier geen sDevelop gebruiken!!!
  if (CheckOnlineStatus2( sUrl+'/webservice/chatservice.asmx', lTaalID, lGroupID, lNodeID))
  {
    document.getElementById('divChatOnline').style.display  = 'block';
    document.getElementById('divChatOffline').style.display = 'none';
    TaalID = lTaalID;
    GroupID = lGroupID;
    NodeID = lNodeID;
  }
  else
  {
    document.getElementById('divChatOnline').style.display  = 'none';
    document.getElementById('divChatOffline').style.display = 'block';
  }
}

function CheckOnlineStatus(sUrl, sHost)
{
  //Determine if there are operators which can accept new chats
  //If there are available then object will by shown otherwise object will be hidden.
  var i;
  
  var bResult = false;
  
  try
  {
    var xmlHttp = GetXMLHttpRequest();
    if ( xmlHttp == null)
    {
      //alert('Error');
    }
    else
    {
      xmlHttp.open ('POST', sUrl, false );
      xmlHttp.setRequestHeader('Host', sHost); 
      xmlHttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
      xmlHttp.setRequestHeader('SOAPAction', 'http://tempuri.org/OperatorAvailable');
      xmlHttp.send('<?xml version=\"1.0\" encoding=\"utf-8\"?> ' +
                   '<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> ' +
                   '  <soap:Body> ' +
                   '    <OperatorAvailable xmlns=\"http://tempuri.org/\"> '+
                   '      <lNodeID>'+ document.getElementById('hdNodeID').value+'</lNodeID> '+
                   '      <lUserID>'+ document.getElementById('hdUserID').value+'</lUserID> '+
                   '      <lDepartmentID>0</lDepartmentID> '+
                   '      <lEmployeeID>0</lEmployeeID> '+
                   '    </OperatorAvailable> '+
                   '  </soap:Body> ' +
                   '</soap:Envelope>'); 
      
      if (xmlHttp.status == 200)
      {
        var xml = xmlHttp.responseXML;
        
        var xmlNode = xml.documentElement.getElementsByTagName("OperatorAvailableResult")[0].firstChild;
        
        if (xmlNode.nodeValue=='true')
        {
          bResult = true;
        }
      }
      delete xmlHttp; 
    }
  }
  catch(e)
  {
    alert(e);
  }
  return bResult;
}

function ShowChatbutton()
{
  var sUrl;
  sUrl = new String(this.location);
  
  sUrl = sUrl.substr(0, sUrl.lastIndexOf("/"));
  sUrl = sUrl.toLowerCase();
  sUrl = sUrl.replace('/front','');
  sUrl = sUrl.replace('/t5front', '');
  sUrl = sUrl.replace('s(', 'S(');  
  
  if (CheckOnlineStatus(sUrl+'/webservice/chatservice.asmx', sUrl))
    document.getElementById('divChatButton').style.display = 'block';
  else
    document.getElementById('divChatButton').style.display = 'none';
    
  setTimeout(ShowChatbutton, 30000);
}

function GetSessionID()
{
  try
  {
    var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
    xmldoc.async = false; 
   
    xmldoc.load(sUrl+sDevelop+'/w_eservice.asmx/GetSessionID');
    var xmlNode = xmldoc.documentElement.getElementsByTagName("SessionID")[0].firstChild;
    sResult = xmlNode.nodeValue;
    return sResult;
    delete xmldoc;
  }
  catch(e)
  {
    //misschien firefox
    var xmlHttp = GetXMLHttpRequest();
    if ( xmlHttp == null)
    {
      //alert('Error');
    }
    else
    {
      xmlHttp.open('GET', sUrl+sDevelop+'/w_eservice.asmx/GetSessionID', false);
      xmlHttp.send(null);
      var xmlNode = xmlHttp.responseXML.documentElement.getElementsByTagName("SessionID")[0].firstChild;
      sResult = xmlNode.nodeValue;
      return sResult;
      delete xmlHttp;
    }  
  }
}


try
{
  sSessionID = 'S(' + GetSessionID() + ')';
}
catch(e)
{
}
