// *****************************************************************************
//
// SOAP Api
//
// Author: Anders Dreyer
//         Enzym ApS
//
// *****************************************************************************


//=====================================
// API functions
//=====================================

// Build Package from Metod and Array of elements
function BuildToExecute(sMethod, sArray) {
	var oPackage = new Package();
	oPackage.InsertElement("soap:Envelope");
	oPackage.SetAttribute("xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");
	oPackage.InsertElement("soap:Body");
	oPackage.InsertElement(sMethod);
	oPackage.SetAttribute("xmlns", "http://primecargo.dk/oss/");
	if (sArray != null) {
		for(var n=0; n < sArray.length-1; n+=2) {
			if (sArray[n] != "") {
				oPackage.AddSoapElement(sArray[n], null, sArray[n+1]);
			}
		}
	}
	return Execute(sMethod, oPackage.InnerXml());
}

// Execute SOAP call
function Execute(sMethod, sXml) {
	var request = new ActiveXObject("microsoft.xmlhttp");
	request.open("POST", "/WebService/oss.asmx", false);
	request.setRequestHeader("SOAPAction", "\"http://primecargo.dk/oss/"+sMethod+"\"");
	request.setRequestHeader("Content-Type", 'text/xml; charset="UTF-8"');
	request.send(sXml);
	var oHandler = new ResponseHandler(request.status, request.responseXml, sMethod);
	return oHandler.TakeCareOfBusiness();
}

//=====================================
// Response Handler
//=====================================
function ResponseHandler(status, xml, method) {
	this.status = status;
	this.xml = xml;
	this.method = method;
	this.error = null;
	this.message = "";
}

ResponseHandler.prototype.TakeCareOfBusiness = function() {
	if (this.status==200 || this.status==500) {
		if (this.xml.selectSingleNode("//faultcode")!=null) {
			this.error = this.xml.selectSingleNode("//faultstring").text;
		} else if (this.status==500) {
			this.error = "Not logged in. If this problem continues, please contact your system administrator.";
		} else {
			this.message = this.xml.selectSingleNode("//"+this.method+"Result").text;
		}
	} else {
		this.error = "SOAP Request error occurred, status:\n "+this.status;
	}
	
	if (this.error!=null) {
		return this.error;
	}
	if (this.message == "") {
		this.message = "ok"; // SOAP call has apparently been completed successfully, but had no returnvalue.
	}
	return this.message; // return plain text
}


//=====================================
// Packages
//=====================================
function Package() {
  this.xml = LoadSoapXml("<oss/>");
  this.node = this.xml.childNodes[0];
}

Package.prototype.AddSoapElement = function(sName, vAttributes, sValue) {
  var node = this.AddXmlElement(sName, this.node);
  node.text = sValue;
  if (vAttributes != null) {
    for(var n=0; n < vAttributes.length-1; n+=2) {
      if (vAttributes[n] != "") {
        node.setAttribute(vAttributes[n], vAttributes[n+1]);
      }
    }
  }
}

Package.prototype.GetElementValue = function(sName) {
	var sOut = "";
	var node = this.node.selectSingleNode(sName);
	if (node!=null) {
		sOut = node.text;
	}
	return sOut;
}

Package.prototype.AddXmlElement = function(sName, node) {
  var element = node.ownerDocument.createElement(sName);
  node.appendChild(element);
  return element;
}

Package.prototype.InsertElement = function(sName) {
	this.node = this.AddXmlElement(sName, this.node);
}

Package.prototype.SetAttribute = function(sName, sValue) {
	this.node.setAttribute(sName, sValue);
}

Package.prototype.InnerXml = function() {
  var node = this.xml.selectSingleNode("oss");
  if (node != null && node.childNodes.length > 0) {
    return node.childNodes[0].xml;
  }
  return "";
}

//=====================================
// XML functions
//=====================================

function LoadSoapXml(sXml) {
	var xml = new ActiveXObject("MSXML.DOMDocument");
	xml.async = false;
	xml.loadXML(sXml);
	if (xml.parseError.errorCode == 0) {
		return xml;
	}
	alert("function LoadXml error:"+xml.parseError.reason);
	return null;
}