var helpXHTTPs = new Array();
var helpKeys = new Array();
var helpTexts = new Array();

var helpLang = "en";
var helpDiv = null;
var helpHeader = "";
var helpFooter = "";
var helpXhttp = null;
var helpLookupID = "AD";

function helpMakeXML(slang, topic)
{
	return	"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
			"<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/\">" +
			"  <soap:Body>" +
			"    <loadHelpText xmlns=\"http://www.AddressDoctor.com/Lookup/DQC\">" +
			"      <slang>" + slang + "</slang>" +
			"      <topic>" + topic + "</topic>" +
			"      <lookupid>" + helpLookupID + "</lookupid>" +
			"    </loadHelpText>" +
			"  </soap:Body>" +
			"</soap:Envelope>";
}

function helpFinishFooter()
{
	if (helpXhttp.readyState != 4) return;
	if (window.XMLHttpRequest)
	{
		xdoc = helpXhttp.responseXML; 
	}
	else
	{
		xdoc = new ActiveXObject("Msxml2.DOMDocument");
		xdoc.loadXML(helpXhttp.responseXML.xml);
	}
	helpFooter = helpGetXMLElement(xdoc, "Text", 0);
}

function helpFinishHeader()
{
	if (helpXhttp.readyState != 4) return;
	if (window.XMLHttpRequest)
	{
		xdoc = helpXhttp.responseXML;
	}
	else
	{
		xdoc = new ActiveXObject("Msxml2.DOMDocument");
		xdoc.loadXML(helpXhttp.responseXML.xml);
	}
	helpHeader = helpGetXMLElement(xdoc, "Text", 0);

	if (window.XMLHttpRequest)
	{
		helpXhttp = new XMLHttpRequest;
	}
	else
	{
		helpXhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	helpXhttp.onreadystatechange = helpFinishFooter;
	helpXhttp.open("POST", location.protocol + "//" + location.host + "/Lookup/DQC.asmx", true);
	helpXhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
	helpXhttp.setRequestHeader("SOAPAction", "http://www.AddressDoctor.com/Lookup/DQC/loadHelpText");
	helpXhttp.send(helpMakeXML(helpLang, "helpFooter"));
}

function helpInitHelp(lookupid, slang, div)
{
	helpLookupID = lookupid;
	helpLang = slang;
	helpDiv = div;

	if (window.XMLHttpRequest)
	{
		helpXhttp = new XMLHttpRequest;
	}
	else
	{
		helpXhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	helpXhttp.onreadystatechange = helpFinishHeader;
	helpXhttp.open("POST", location.protocol + "//" + location.host + "/Lookup/DQC.asmx", true);
	helpXhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
	helpXhttp.setRequestHeader("SOAPAction", "http://www.AddressDoctor.com/Lookup/DQC/loadHelpText");
	helpXhttp.send(helpMakeXML(helpLang, "helpHeader"));
}

function helpTest(el, ev)
{
	var divstyle = document.getElementById(helpDiv).style;
	divstyle.visibility = "visible";
	divstyle.display = "block";
	/*
	divstyle.left = ev.clientX + 16;
	divstyle.top = ev.clientY + 16;
	*/
	var x = 16;
	var y = 16;
	var pl = el;
	while (pl)
	{
		x += pl.offsetLeft;
		y += pl.offsetTop;
		pl = pl.offsetParent;
	}
	divstyle.left = x;
	divstyle.top = y;
	window.status = document.body.clientWidth + " / " + document.body.clientHeight;
}
function helpGetText(topic)
{
	document.getElementById(helpDiv).style.visibility = "visible";
	document.getElementById(helpDiv).style.display = "block";
	switch(helpLang)
	{
	case "de":
		document.getElementById(helpDiv).innerHTML = helpHeader + "<p>Lade Hilfetext. Bitte warten...</p>" + helpFooter;
		break;
	case "fr":
		document.getElementById(helpDiv).innerHTML = helpHeader + "<p>Loading help text. Please wait...</p>" + helpFooter;
		break;
	case "it":
		document.getElementById(helpDiv).innerHTML = helpHeader + "<p>Loading help text. Please wait...</p>" + helpFooter;
		break;
	default:
		document.getElementById(helpDiv).innerHTML = helpHeader + "<p>Loading help text. Please wait...</p>" + helpFooter;
	}
	x = document.body.clientWidth - 10;
	y = document.body.clientHeight - 10;
	if (window.XMLHttpRequest)
	{
		x -= 15;
		y -= 20;
	}
	if (helpLang == "ar") // arabic is aligned to the right side of the window
	{
		x -= 800;
		y -= 300;
		if (x < 20 ) x = 20;
		if (y < 20 ) y = 20;
		if (y > 300) y = 300;
	}
	else // all other countries
	{
		x -= 300;
		y -= 300;
		if (x > 400 && y > 300)
		{
			x = 400;
			y = 300;
		}
	}
	document.getElementById(helpDiv).style.left = x;
	document.getElementById(helpDiv).style.top = y;
	// Have we already loaded the text?	
	
	for (var i=0; i < helpKeys.length; i++)
	{
		if (helpKeys[i] == topic)
		{
			document.getElementById(helpDiv).innerHTML = helpHeader +  helpTexts[i] + helpFooter;
			return;
		}
	}
	
	// We must load the text
	if (window.XMLHttpRequest)
	{
		xhttp = new XMLHttpRequest;
	}
	else
	{
		xhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	helpXHTTPs.push(xhttp);
	xhttp.onreadystatechange = helpLoadTextFinished;
	try
	{
		xhttp.open("POST", location.protocol + "//" + location.host + "/Lookup/DQC.asmx", true);
	}
	catch (ex)
	{
		alert(ex.message);
		return;
	}
	xhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
	xhttp.setRequestHeader("SOAPAction", "http://www.AddressDoctor.com/Lookup/DQC/loadHelpText");
	xhttp.send(helpMakeXML(helpLang, topic));
}

function helpGetXMLElement(xdoc, tag, pos)
{
	try
	{
		return xdoc.getElementsByTagName(tag).item(pos).firstChild.data;
	}
	catch (e)
	{
		return "";
	}
}

function helpLoadTextFinished()
{
	var xhttp = null;
	var div = null;
	
	for (var i=0; i < helpXHTTPs.length; i++)
	{
		if (helpXHTTPs[i].readyState == 4)
		{
			xhttp = helpXHTTPs[i];
			break;
		}
	}

	if (!xhttp) return;
	if (window.XMLHttpRequest)
	{
		xdoc = xhttp.responseXML;
	}
	else
	{	
		xdoc = new ActiveXObject("Msxml2.DOMDocument");
		xdoc.loadXML(xhttp.responseXML.xml);
	}
	topic = helpGetXMLElement(xdoc, "Topic", 0);
	text = helpGetXMLElement(xdoc, "Text", 0);

	helpKeys.push(topic);
	helpTexts.push(text);
	
	document.getElementById(helpDiv).innerHTML = helpHeader + text + helpFooter;
		
	helpXHTTPs.splice(i, 1);
}
