
FD.Help = new Object();
FD.Help.window = null;
FD.Help.html = '';
FD.Help.domain = '';
FD.Help.lang = 2;
FD.Help.js = false;

FD.Help.Title = '';
FD.Help.CloseText = '';
FD.Help.ClickHere = '';
FD.Help.ShowType = 0;
FD.Help.IdForm = 0;
FD.Help.Type = 0;

FD.Help.Items = new Array();

/*
loadFile( '/scripts/unitip.js' ) ;
if ( FD.Browser.ie && FD.Browser.VersionNr < 7 )
loadFile( '/scripts/unitpngfix.js' ) ;
loadFile( '/scripts/tooltip/unitip.css?v1' ) ;
*/

FD.Help.Close = function() {
	var lClosed = false;
	try {
		if (window.helpWindow) { window.helpWindow.close(); lClosed = true; }
	} catch (e) {
		window.helpWindow = null;
		throw e;
	}
	window.helpWindow = null;
	return lClosed;
}

FD.Help.Init = function(cpTitle, cpCloseText, cpClickHere, npShow, npIdForm, npType) {
	this.Title = cpTitle;
	this.CloseText = cpCloseText;
	this.ClickHere = cpClickHere;
	this.ShowType = npShow;
	this.IdForm = npIdForm;
	this.Type = npType;

	this.ajax = typeof AJAX != 'undefined' && AJAX.checkEnabled();
	this.js = !this.ajax && this.Type == 0;
	try {
		this.domain = FD.Form.Info.domain;
	} catch (e) {
		this.domain = '';
		//this.js		= false ;
		//this.Type	= 1 ;
	}
	if (this.domain != 'undefined') this.domain = '';
	if (this.js) {
		try {
			FD.Script.loadFile(this.domain + '/help/js/2/' + this.IdForm + '?r=' + escape((typeof __GenID__ != 'undefined' ? __GenID__ : FD.Utils.Random())));
		} catch (e) {
			this.js = false;
			this.Type = 1;
		}
	}
}

FD.Help.Add = function(npHelpId, cpTitle, cpText) {
	if (arguments.length > 1) {
		var obj = { filled: true, id: npHelpId, title: unescape(cpTitle), text: unescape(cpText) };
		if (obj.title.length > 50) obj.title = obj.title.substr(0, 47) + '...';
	} else {
		var obj = { filled: false, id: npHelpId };
	}
	this.Items.push(obj);
}

FD.Help.Set = function(npHelpId, cpTitle, cpText) {
	var obj = this.Get(npHelpId);
	if (!obj) this.Add(npHelpId, cpTitle, cpText);
	else {
		obj.title = unescape(cpTitle);
		if (obj.title.length > 50) obj.title = obj.title.substr(0, 47) + '...';
		obj.text = unescape(cpText);
	}
}

FD.Help.Get = function(vpHelpId) {
	var obj;
	for (var i = 0; i < this.Items.length; i++) {
		if (this.Items[i].id == vpHelpId) obj = this.Items[i];
	}
	if (!obj) {
		obj = { filled: false, id: vpHelpId };
		this.Items.push(obj);
	}
	if (!obj.filled && !this.js && this.ajax) {
		if (typeof vpHelpId == 'string') { // formdesk backoffice
			var data = {
				url: '/help/xml/3/' + escape(obj.id) + '/' + top.Global.Id_Lang + '?r=' + escape(FD.Utils.Random()),
				async: false,
				verb: 'GET'
			};
			var req = AJAX.sendRequest(data);
			if (req.status == 200) {
				try {
					var oData = req.responseXML, node = oData.selectNodes('//result').item(0);
				} catch (e) { return true; }
				if (parseIntEx(AJAX.getNodeValue(node, 'code', '')) == 0) {
					node = node.selectSingleNode('value/helper/help');
					obj.text = AJAX.getNodeValue(node, 'text');
					obj.filled = true;
				}
			}
		} else { // form
			var data = {
				url: this.domain + '/help/xml/1/' + this.IdForm + '/' + obj.id + '?r=' + escape((typeof __GenID__ != 'undefined' ? __GenID__ : FD.Utils.Random())),
				async: false,
				verb: 'GET'
			};
			var req = AJAX.sendRequest(data);
			if (req.status == 200) {
				try {
					var oData = req.responseXML, node = oData.selectNodes('//result').item(0);
				} catch (e) { return true; }
				if (Number.int(AJAX.getNodeValue(node, 'code', '')) == 0) {
					node = node.selectSingleNode('value/helper/help[@id=' + obj.id + ']');
					obj.title = AJAX.getNodeValue(node, 'title');
					obj.text = AJAX.getNodeValue(node, 'text');
					obj.filled = true;
				}
			}
		}
	}
	return obj;
}

FD.Help.Show = function(npHelpId) {

	var obj = this.Get(npHelpId);
	if (!obj) return;
	if (this.ajax || this.Type == 0) {
		var cHTML = '<html><head><title>' + this.Title + '</title>' +
				'<style type="text/css">.txt { font-family:arial,verdana;font-size:10pt;width:' + (FD.Browser.ie ? '100%' : '99%') + ';height:98%;overflow:scroll;padding:0px;border-style:inset;border-width:2px }</style>' +
				'</head><body style="margin:0px" onload=""><table cellpadding="0" cellspacing="0" style="width:100%;height:97%"><tr><td><form style="height:98%">' +
				'<table border=0 width=100% height=100% cellspacing=0 cellpadding=0>' +
				'<tr><td valign=top>' +
				'<table border=0 width=100% cellspacing=0 cellpadding=4 bgcolor=#c0c0c0 style="border-style:outset;border-width:2px">' +
				'<tr><td><font face="Arial, Verdana" size=3 color=#0000BB><b>' + obj.title + '</b></font></td></tr></table></td></tr>' +
				'<tr><td valign=top height=100%>' +
				'<table border=0 style="width:100%;height:100%" cellpadding=0 cellspacing=0><tr><td valign=top align=left>' +
				'<div class=txt ><div style="padding:4px">' + obj.text + '</div></div></td></tr></table></td></tr>';
		if (this.CloseText != '')
			cHTML = cHTML + '<tr><td valign="middle" align="center" style="padding:4px">' +
				'<input type=button value="' + this.CloseText + '" onclick="try{if(!window.opener.FD.Help.Close()) self.close();}catch(e){self.close();}"></td></tr>';
		cHTML = cHTML + '</table></form></td></tr></table></body></html>';

		this.html = cHTML;
		this.window = OpenWindow(null, '/empty.htm', 410, 310, 'yes', 'no', true, 'fdhelp');
		this.window.focus();

		// for safari a timeout is necessary
		window.setTimeout(FD.Help.writeHTML, 100);
	} else {
		this.window = OpenWindow(null, this.domain + '/help/window/1/' + this.IdForm + '/' + obj.id, 410, 310, 'yes', 'no', true, 'fdhelp');
		this.window.focus();
	}
}

FD.Help.writeHTML = function() {
	var self = FD.Help;
	self.window.document.open('text/html', 'replace');
	self.window.document.write(self.html);
	self.window.document.close();
}

FD.Help.MouseOver = function(vpHelpId, el) {
	obj = this.Get(vpHelpId);
	if (!obj) return true;
	var cTitle = ((this.ShowType > 0 || Browser.ns4) ? '' : obj.title);
	var cText = ((this.ShowType > 0 || Browser.ns4) ? this.ClickHere : obj.text);
	//	el = ( typeof el == 'string' ? $( el ) : ( el || $( 'help' + obj.id ) ) ) ;
	//	el.onmouseout = function() {hide();};
	//	build( el, obj.text ) ;
	window.dlb(window, 'help' + obj.id, cText, cTitle);
	return true;
}
