// some globals
var http=null;				// xml http interface object pointer
var ContinueJsCode=null;	// holds jscript code that is executed after loading an album
var CancelJsCode=null;		// holds jscript code that is executed after cancelling a load
var Host = document.domain;	// host location

// 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";
	
	return 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";
	
	return 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";
	
	return soapstring;
}

function GetXMLHTTP()
{
	if (http == null)
	{
		// non - ie first
		if (window.XMLHttpRequest) {
		    http = new XMLHttpRequest();
		}
		// non - ie second
		if (!http && window.createRequest) http = window.createRequest();

		// now try ie if needed
		if (!http && window.ActiveXObject)
		{
			try	{ http = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e)
			{
				try	{ 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 (http.readyState == 4)
	{	
		// 200 = ok
		if (http.status == 200) {
		    LP_Success = true;
		    success = true;
		}
		else
		{
			alert("HTTP Error - " + http.status + " (" + http.statusText + ")");
			LP_Success=false;
			setTimeout(CancelJsCode, 100);
		}
	}
	
	return success;
}

// post a request to Life Pics Web Service, version 1.1
function SubmitSOAPRequest(request, soapaction, callback)
{
	// get the xml http object
	GetXMLHTTP();
	
	if (http != null)
	{
		// set up the header
		http.open("POST", location.protocol + "//" + Host + "/net/LPWS/LPWebService.asmx", true);
		
		// set the callback
		http.onreadystatechange = callback;
		
		// set the header
		http.setRequestHeader("Host", Host);
		http.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		http.setRequestHeader("Content-Length", request.length+"");
		http.setRequestHeader("SOAPAction", soapaction);
		
		// call the method
		http.send(request);
	}
}