/*
var ACTIVEX_PREFIX_XML = ["Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument.2.6", "Msxml2.DOMDocument", "Microsoft.XMLDOM"];
var ACTIVEX_PREFIX_XMLHTTP = ["Microsoft.XMLHTTP", "Msxml2.ServerXMLHTTP"];

  var Browser_Name = navigator.appName;
  var Browser_Version = parseInt(navigator.appVersion);
  //Document Handler
  var documentHandler = null;
  var xslHandler = null;
  
  //alert(Browser_Name  + " " + Browser_Version + " " + navigator.userAgent);

// return the first not-null ActiveX object from the list
function getActiveX(arrObjects){
  //alert("here");
  for(var i = 0; i < arrObjects.length; i++ ){
    try
    {
      //alert(i + ": " + arrObjects[i]);
      var result = new ActiveXObject(arrObjects[i]);
      if( result != null )
        return result;
    } catch (e)
    {
      //alert(e);
    }
  }
  return null;
}


// Reset the source doc
function prepareDocumentHandler()
{
  try
  {
    
    if (
         (navigator.userAgent.indexOf("Mozilla") >= 0 )&&
         (navigator.userAgent.indexOf("MSIE") < 0  )
       )
    {
      //alert("trying for Mozilla");
      documentHandler = document.implementation.createDocument("", "", null);
    }else if (navigator.userAgent.indexOf("MSIE") >= 0  )
    {
      //alert("trying for ie");
      documentHandler = getActiveX(ACTIVEX_PREFIX_XML);
    }  
    if ( documentHandler == null )
    {
      alert("Can not Create document Handler unknown browser");
    }
  } catch(Exception) 
  {
    alert(Exception);
    alert("Can not Create document Handler");
  }
}

//Load a document from the specified URL.
function load(URLString)
{
  try
  {
    prepareDocumentHandler();
    documentHandler.async = false;
    //alert(URLString);
    documentHandler.load(URLString);
  }  catch (Exception)
  {
    alert(Exception);
  };
}

// Reset the source doc
function prepareXSLHandler()
{
  try
  {
    if (
         (navigator.userAgent.indexOf("Mozilla") >= 0 )&&
         (navigator.userAgent.indexOf("MSIE") < 0  )
       )
    {
      //alert("trying for Mozilla");
      xslHandler = document.implementation.createDocument("", "", null);
    }else if (navigator.userAgent.indexOf("MSIE") >= 0  )
    {
      //alert("trying for ie");
      xslHandler = getActiveX(ACTIVEX_PREFIX_XML);
    }  
    if ( xslHandler == null )
    {
      alert("Can not Create document Handler unknown browser");
    }
  } catch(Exception) 
  {
    alert(Exception);
    alert("Can not Create document Handler");
  }
}

//Load a document from the specified URL.
function loadXSL(URLString)
{
  try
  {
    prepareXSLHandler();
    xslHandler.async = false;
    xslHandler.load(URLString);
  }  catch (Exception)
  {
    alert(Exception);
  };
}
//New Function
function CallXML(theXSL, theXML, paramName, paramValue)
 {
  var xsltTree = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0");
  xsltTree.async = false;
  xsltTree.load(theXSL)

  var srcTree = new ActiveXObject("Msxml2.DOMDocument.4.0");
  srcTree.async = false;
  srcTree.load(theXML);

  var xsltThread = new ActiveXObject("Msxml2.XSLTemplate.4.0");
  xsltThread.stylesheet = xsltTree;
  var xsltProc = xsltThread.createProcessor();
  xsltProc.input = srcTree;
  if (paramName){
    xsltProc.addParameter(paramName, paramValue);
  }
  xsltProc.transform();
  return xsltProc.output;
 } 

 //transform xml with XSLT and returns transformed content  
 function transformContent(url, xslURL, parameter, paramValue){
   load(url);
   loadXSL(xslURL);
   //loadXSL("http://smellyfrog/web2/testxsl.xsl");
   var result = null;
      if (navigator.userAgent.indexOf("MSIE") >= 0  )
      {
        //alert("IE");
      try
      {
        result = CallXML(xslURL,url, parameter, paramValue);
      }catch (Exception)
      {
      //alert(Exception);
      };
        //result = documentHandler.transformNode(xslHandler);
      }
      else {
        //alert("Mozilla");
        try{
        var processor = new XSLTProcessor();
        var docResult;
        //alert((new XMLSerializer()).serializeToString(xslHandler));
        //alert((new XMLSerializer()).serializeToString(documentHandler));
        processor.importStylesheet(xslHandler);
	if (parameter){
	  processor.setParameter(null, parameter, paramValue);
	}
        docResult = processor.transformToDocument(documentHandler);
        result = (new XMLSerializer()).serializeToString(docResult);        
        }catch(Exception){
        alert("ERROR: \n" + Exception);
        }
      }
      //alert(result);
   return result;
 }*/
//-------------------------------------------------
/**
 * Javascript used to send a soap request to a webservice 
**/
  var ACTIVEX_PREFIX_XML = ["Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument.2.6", "Msxml2.DOMDocument", "Microsoft.XMLDOM"];
  var ACTIVEX_PREFIX_XMLHTTP = ["Microsoft.XMLHTTP", "Msxml2.ServerXMLHTTP"];
  var respdocumentHandler = null;
  var documentHandler = null;
  var httpHandler = null;
  //alert("pullobjects loaded");
  // Reset the doc handler
function prepareDocumentHandler()
{
var theHandler = null;
  try
  {

    if (
         (navigator.userAgent.indexOf("Mozilla") >= 0 )&&
         (navigator.userAgent.indexOf("MSIE") < 0  )
       )
    {
      //alert("trying for Mozilla");
      theHandler = document.implementation.createDocument("", "", null);
    }else if (navigator.userAgent.indexOf("MSIE") >= 0  )
    {
      //alert("trying for ie");
      theHandler = getActiveX(ACTIVEX_PREFIX_XML);
    }
    if ( theHandler == null )
    {
      alert("Can not Create document Handler unknown browser");
    }
  } catch(Exception)
  {
    alert(Exception);
    alert("Can not Create document Handler");
  }
  return theHandler;
}

// Reset the request handler
function prepareHTTPHandler()
{
  try
  {

    if (
         (navigator.userAgent.indexOf("Mozilla") >= 0 )&&
         (navigator.userAgent.indexOf("MSIE") < 0  )
       )
    {
      httpHandler = new XMLHttpRequest();
      //alert(httpHandler);
    }else if (navigator.userAgent.indexOf("MSIE") >= 0  )
    {
      httpHandler = getActiveX(ACTIVEX_PREFIX_XMLHTTP);
    }
    if ( httpHandler == null )
    {
      alert("Can not Create HTTP Handler unknown browser");
    }
  } catch(Exception)
  {
    alert(Exception);
    alert("Can not Create document Handler");
  }
}


  // return the first not-null ActiveX object from the list
  function getActiveX(arrObjects){
      //alert("here");
      for(var i = 0; i < arrObjects.length; i++ ){
        try
        {
          var result = new ActiveXObject(arrObjects[i]);
          if( result != null )
            return result;
        } catch (e)
        {
          //alert(e);
        }
      }
      return null;
  }
    function getPureResponseText(serviceName, textToApure){
      var returnStr = textToApure;

      var separ1 = textToApure.split("<" + serviceName + ">");
      if (separ1.length>1){
          var separ2 = separ1[1].split("</" + serviceName + ">");
          if (separ2.length>1){
              returnStr = separ2[0];
              var re = /&lt;/g;
              returnStr = returnStr.replace(re ,"<");
              re = /&gt;/g;
              returnStr = returnStr.replace(re ,">");
              re = /&amp;/g;
              returnStr = returnStr.replace(re ,"&");
          }
      }
      return returnStr;
    }
   
   function getElementPosition(sender) {
    var offsetTrail = sender;
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
  }

//
  function displayResultText(httpHandler, divId){
    var returnText = httpHandler.responseText;
    var divDisplay = document.getElementById(divId);
    divDisplay.innerHTML = getPureResponseText("GetCoaTraceResult", returnText);
  }

//Main function to call
function callWebpage(urlStr, divId){
  var returnText = "";
  prepareHTTPHandler();
  documentHandler = prepareDocumentHandler();
  respdocumentHandler = prepareDocumentHandler();
  httpHandler.open("GET", urlStr, true);

  httpHandler.setRequestHeader("HTTP", "GET");
  httpHandler.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
  //httpHandler.setRequestHeader("Content-Length", "" + soapMsg.Length);

  var isLoaded = false;
  
    if (
         (navigator.userAgent.indexOf("Mozilla") >= 0 )&&
         (navigator.userAgent.indexOf("MSIE") < 0  )
       )
    {
        isLoaded = documentHandler.load(urlStr);
    }else{
        isLoaded = documentHandler.loadXML(urlStr);
    }

          if (isLoaded) {
              httpHandler.send(urlStr);
              httpHandler.onreadystatechange = function() {displayResultText(httpHandler, divId);};
              //returnText = httpHandler.responseText;
          }/*else{
            returnText = "Error calling webservice";
          }*/
  //return returnText;
  return isLoaded;
}

//Main function to call
function callWebService(urlStr, soapMsg, soapAction){
  var returnText = "";
  prepareHTTPHandler();
  documentHandler = prepareDocumentHandler();
  respdocumentHandler = prepareDocumentHandler();
  httpHandler.open("POST", urlStr, true);

  httpHandler.setRequestHeader("SOAPAction", soapAction);
  httpHandler.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
  httpHandler.setRequestHeader("Content-Length", "" + soapMsg.Length);

  var isLoaded = false;
  
    if (
         (navigator.userAgent.indexOf("Mozilla") >= 0 )&&
         (navigator.userAgent.indexOf("MSIE") < 0  )
       )
    {
        isLoaded = documentHandler.load(soapMsg);
    }else{
        isLoaded = documentHandler.loadXML(soapMsg);
    }

          if (isLoaded) {
              httpHandler.send(soapMsg);
              httpHandler.onreadystatechange = function() {displayResultText(httpHandler);};
              //returnText = httpHandler.responseText;
          }/*else{
            returnText = "Error calling webservice";
          }*/
  //return returnText;
  return isLoaded;
}

