﻿//This is the constructor object for the object oriented webservice class.
function wsObj() 
{
    this.http = null;           // xml http interface object pointer
    this.ContinueJsCode=null;	// holds jscript code that is executed after loading an album
    this.CancelJsCode=null;		// holds jscript code that is executed after cancelling a load
    this.Host = document.domain;	// host location
    this.strRequest = "";
    this.functionName = "";
    this.nameSpace = "http://webservice.lifepics.com";
    this.callbackFunction = null;
    
    this.addParam = AddXMLParameter;
    this.goodResponse = GotGoodResponse;
    this.wrapXML = WrapXML;
    this.createRequest = CreateSOAPRequestXML;
    this.submitRequest = SubmitSOAPRequest;
    this.getHTTPObj = GetXMLHTTP;
    
    this.sendRequest = function() { 
                            this.wrapXML(this.functionName, "xmlns='" + this.nameSpace + "'", this.strRequest); 
                            this.createRequest(this.strRequest); 
                            this.submitRequest(this.strRequest, this.nameSpace + "/" + this.functionName, this.callbackFunction)
                       };
}

// builds an xml string that are the parameters for calling a soap method
function AddXMLParameter(name, value)
{
	var xmlstring;
	
	// if the value is null, generate a self-closing tag, otherwise do the normal thing
	if (value == null) xmlstring = "<" + name + "/>\r\n";
	else xmlstring = "<" + name + ">" + value + "</" + name + ">\r\n";
	
	this.strRequest += xmlstring;
}

// wrap given xml with given tag + attributes
function WrapXML(tag, attributes, xml)
{
	var xmlstring;
	var space = (attributes == "") ? "" : " ";
	
	xmlstring = "<" + tag + space + attributes + ">\r\n" + xml + "</" + tag + ">\r\n";
	
	this.strRequest = xmlstring;
}

// makes a SOAP 1.1 version request XML string ready for sending
function CreateSOAPRequestXML(method_and_parameters)
{
	var soapstring = '<?xml version="1.0" encoding="utf-8" ?>\r\n' + 
	'<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/">\r\n' + 
	'<soap:Body>\r\n' +
	method_and_parameters + 
	"</soap:Body>\r\n" +
	"</soap:Envelope>\r\n";
	
	this.strRequest = soapstring;
}

function GetXMLHTTP()
{
	if (this.http == null)
	{
		// non - ie first
		if (window.XMLHttpRequest) {
		    this.http = new XMLHttpRequest();
		}
		// non - ie second
		if (!this.http && window.createRequest) this.http = window.createRequest();

		// now try ie if needed
		if (!this.http && window.ActiveXObject)
		{
			try	{ this.http = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e)
			{
				try	{ this.http = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch (e){}
			}
		}
	}
}

// when the ready state of response is "loaded" status checking is then done
// if the http status is 200 return true, else return false
// if the status is an error show an error alert message
function GotGoodResponse()
{
	var success = false;
	
	// 4 = loaded
	if (this.http.readyState == 4)
	{	
	    // set the mouse cursor back to the default
	    document.body.style.cursor="default";
	    
		// 200 = ok
		if (this.http.status == 200) {
		    LP_Success = true;
		    success = true;
		}
		else
		{
			alert("HTTP Error - " + this.http.status + " (" + this.http.statusText + ")");
			LP_Success=false;
			setTimeout(this.CancelJsCode, 100);
		}
	}
	
	return success;
}

// post a request to LPWebService, version 1.1
function SubmitSOAPRequest(request, soapaction, callback)
{
	// get the xml http object
	this.getHTTPObj();
	
	if (this.http != null)
	{
		// set up the header
		this.http.open("POST", location.protocol + "//" + this.Host + "/net/LPWS/LPWebService.asmx", true);
		
		// set the callback
		this.http.onreadystatechange = callback;
		
		// set the header
		this.http.setRequestHeader("Host", this.Host);
		this.http.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		this.http.setRequestHeader("Content-Length", request.length+"");
		this.http.setRequestHeader("SOAPAction", soapaction);
		
		// call the method
		this.http.send(request);
		
		// set the mouse cursor to wait
		document.body.style.cursor="wait";
	}
}
