﻿//	**************************************************************************
//	Jena - Javascript Library (Services)
//	Requires	jena.Xml
//				jena.Form
//
//	Written by Robert Swiston
//
//	Copyright 2010, RomiDesigns - All Rights Reserver
//
//	No part of this script may be reproducted or altered without the
//	expressed consent of RomiDesigns or its representative(s)
//	**************************************************************************

//	**************************************************************************
//	the global constants used in an asmx SOAP Enveloper
//	**************************************************************************
var _soapEnvelope = "<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/\">";
var _soapStart = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + _soapEnvelope + "<soap:Body><";
var _soapEnd = "</soap:Body></soap:Envelope>";

//	**************************************************************************
//	The Root jenaSoap method
//	Creates and sends a soap request to an asmx service, receives and parses
//	the response from the service returning the error and the xml result
//	**************************************************************************
function jenaSoap(pHost, pUrl, pAction, pRequest, pParameters, pAsync, pOnComplete)
{
	this.action = pAction || "";
	this.async = pAsync || false;
	this.onComplete = pOnComplete;
	this.header = "";
	this.host = pHost || "";
	this.parameters = pParameters;
	this.request = pRequest || "";
	this.response = "";
	this.url = pUrl || "";
	
	//Compile the request into XML-Complient SOAP Envelope
	this.compileRequest = function()
	{
		var ParameterString = "";
		var HeaderElements = this.request.replace("<", "").replace("/>", "").split(/ xmlns/);
		this.header = HeaderElements[0];
		
		if (this.parameters != null)
		{
			for (var Key in this.parameters)
			{
				ParameterString += "<" + Key + ">" + this.parameters[Key] + "</" + Key + ">";
			}
		};
		
		return _soapStart + this.request + (ParameterString != "" ? ">" + ParameterString + "</" + this.header + ">" : "/>") + _soapEnd;			
	};
	//Create the request from the Host and return the compile request soap envelope
	this.createRequest = function()
	{
		if (!this.host.indexOf("http")) this.host = "http://" + this.host;
		
		return this.compileRequest();
	};
	//Parse the response from the returned soap response
	this.parseResponse = function()
	{
		var Xml = new jXml(this.response),
			ResultNode = Xml.node("//" + this.header + "Result");
			
		var Result = ResultNode != null ? ResultNode.child(0) : null;
		var ErrorNode = ResultNode != null ? ResultNode.node("Error") : null;
		var Error = (ErrorNode != null) ? ErrorNode.value : "";
		
		return {result:Result, error:Error, parent:ResultNode};
	};
	//Send the soap request asynchronously
	this.send = function(pRequest)
	{
		this.response = new jForm().submitPage(this.host + this.url, false, pRequest, "text/xml; charset=utf-8", {SOAPAction:this.action});
	};
	//Send the soa[ request non-asynchronously
	this.sendAsync = function(pRequest, pFunction)
	{
		this.response = new jForm().submitPage(this.host + this.url, true, pRequest, "text/xml; charset=utf-8", {SOAPAction:this.action}, pFunction);
	};
	
	//Create and send the request if all necessary parameters have already been submitted
	if (this.url !== "" && this.action !== "" && this.request !== "")
	{
		var Request = this.createRequest();
		
		if (!this.async) this.send(Request); else this.sendAsync(Request, this.onComplete);
	}
	
	return this;
};

//	**************************************************************************
//	The Root jenaServices method
//	**************************************************************************
function jenaServices(pHost, pBase, pUrl)
{
	this.base = pBase;
	this.host = pHost;
	this.url = pUrl;
	
	//Create a new soap service request and return the parsed response;
	this.call = function(pAction, pAttributes)
	{
		var ActionRequest = this.getActionAndRequest(pAction);
		
		return new jenaSoap(this.host, this.url, ActionRequest.action, ActionRequest.request, pAttributes).parseResponse();
		
	};
	//Create a valid soap action and request based on a value and base
	this.getActionAndRequest = function(pValue, pBase)
	{
		pBase = pBase || this.base;
		
		var Action = pBase + pValue;
		var Request = pValue + " xmlns=\"" + pBase + "\"";
		
		return {action:Action, request:Request};
	};
	
	return this;
};

jServices = function(pHost, pBase, pUrl)
{
	return new jenaServices(pHost, pBase, pUrl);
};