/*
 (C) 2004-2007 Forex Practice
 fp_common.js
*/

// ---
function removeAllChilds(element)
{
	if (element != null)
	{
		while ( element.hasChildNodes() )
		{
			element.removeChild( element.lastChild );
		}
	}
}

// ---
function getIdentPrefix(depth)
{
	var str = "";
	var ident= depth;
	for ( ; ident > 0; --ident )
	{
		str = str + "\t";
	}
	return str;
}

function convertXmlToStr0(nodeSource, depth, isIdent)
{
	if (nodeSource == null)
		return "";

	var str= "";

	if (nodeSource.nodeName == "#document")
	{
		str= str + '<?xml version="1.0" ?>' + convertXmlToStr0(nodeSource.documentElement, depth, isIdent);
	}
	else if (nodeSource.nodeName == "#text")
	{
		str= str + nodeSource.nodeValue;
	}
	else if (nodeSource.nodeName == "#comment")
	{
		str= str + "<!-- " + nodeSource.nodeValue + " -->";
	}
	else if (nodeSource.nodeName == "#cdata-section")
	{
		str= str + "<![CDATA[" + nodeSource.nodeValue + "]]>";
	}
	else
	{
		if (isIdent)
		{
			str= str + "\n" + getIdentPrefix(depth);
		}

		str= str + "<" + nodeSource.nodeName;
		if ((!nodeSource.hasAttributes) || nodeSource.hasAttributes())
		{
			var attrs= nodeSource.attributes;
			if (attrs != null)
			{
				var i= 0;
				for ( ; i < attrs.length ; ++i )
				{
					var anattr= attrs.item(i);
					if ((anattr.nodeValue != null) && (anattr.nodeValue != ""))
					{
						str= str + ' ' + anattr.nodeName + '="' + anattr.nodeValue + '"';
					}
				}
			}
		}

		if (nodeSource.hasChildNodes())
		{
			str = str + ">";
			var children= nodeSource.childNodes;
			if (children != null)
			{
				var i= 0;
				for ( ; i < children.length ; ++i )
				{
					var achild= children.item(i);
					str = str + convertXmlToStr0(achild, depth+1, isIdent);
				}
			}
			
			str = str + "</" + nodeSource.nodeName + ">";
		}
		else
		{
			str = str + " />";
		}
	}

	return str;
}

function convertXmlToString(nodeSource)
{
	return convertXmlToStr0(nodeSource, 0, true);
}


// ---
// ---
function getXmlHttpObj()
{
	if ( typeof(XMLHttpRequest) != 'undefined' )
	{
		return new XMLHttpRequest();
	}

	var arrAX= ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
	var i= 0;
	for( ; i < arrAX.length ; ++i )
	{
		try
		{
			return new ActiveXObject( arrAX[i] );
		}
		catch (err)
		{
			//// do nothing here, try the next one
		}
	}
	return null;
}

// ---
function XmlRequest ()
{
	this.req= null;
	this.callback= null;
	this.context= null;

	// private
	this.processChange = function ()
	{
		if ((this.req) && (this.req.readyState == 4))
		{
			try
			{
				var statusCode= -1;
				var statusText= "";
				try
				{
					statusCode= this.req.status;
					statusText= this.req.statusText;
				}
				catch (exc1)
				{
					//// do nothing. This commonly happen when page is unloaded in Firefox and XML was not fully loaded yet
				}
				
				if (statusCode == 200)
				{
					if (this.callback.onXmlLoaded)
					{
						try
						{
							this.callback.onXmlLoaded(this);
						}
						catch (exc)
						{
							this.callback.onError(this, "Exc5: " + convertErrObjToStr(exc) );
						}
					}
				}
				else 
				{
					if (statusCode != -1)
					{
						if (this.callback.onError)
						{
							this.callback.onError(this, statusText + " (" + statusCode + ")" );
						}
					}
				}
			}
			catch (exc)
			{
				if (this.callback.onError)
				{
					this.callback.onError(this, "Exc1: " + convertErrObjToStr(exc) );
				}
			}
		}
	};
	
	//public
	this.loadAsync = function (url, callback, context, postdoc)
	{
		try
		{
			this.callback= callback;
			this.context= context;
			var myThis= this;

			var method= (postdoc == null) ? "GET" : "POST";
			
			this.req = getXmlHttpObj();
			if ( this.req != null )
			{
				if (this.req.overrideMimeType)
				{
					this.req.overrideMimeType('text/xml');
				}
				//new DebugHelper("XmlRequest").trace("validateOnParse: "+this.req.validateOnParse);
				if (this.req.validateOnParse)
				{
					//// supposedly this allow DOCTYPE part in XSLT file 
					this.req.validateOnParse= false;
				}

				this.req.onreadystatechange = function () { myThis.processChange(); };
				this.req.open(method, url, true);
				this.req.send(postdoc);
			}
			else
			{
				if (this.callback.onError)
				{
					this.callback.onError(this, "Browser does not support AJAX.");
				}
			}
		}
		catch (err)
		{
			if (this.callback.onError)
			{
				this.callback.onError(this, "Exc2: " + err );
			}
		}
	};
}

// ---
function XmlIslandLoadXslt ()
{
	this.doc= null;
	this.reqxsl= new XmlRequest ();
	this.element= null;

	this.callback= null;
	this.context= null;

	// interface of XmlDefualtLoaded
	this.onXmlLoaded = function (xmlreq)
	{
		try
		{
			removeAllChilds(this.element);
			
			if (document.implementation && document.implementation.createDocument)
			{
				var xsltProcessor = new XSLTProcessor();
				xsltProcessor.importStylesheet( this.reqxsl.req.responseXML );
				var resultDocument = xsltProcessor.transformToFragment( this.doc, document );
				this.element.appendChild(resultDocument);
				
				this.callback.onXmlLoaded(this);
			}
			else if (window.ActiveXObject)
			{
				var xml = createEmptyIeDocument ();
				var xsl = createEmptyIeDocument ();
				
				var text= "";
				if (this.doc.documentElement.xml)
				{
					text= this.doc.documentElement.xml;
					//alert( "XML: " + text );
				}
				else
				{
					//// this is HTML-DOM (as oppose to XML-DOM)
	
					var xmldoc= createEmptyDocument();
					var nodeRoot= doImportNode( this.doc.documentElement, xmldoc, true);
					xmldoc.appendChild(nodeRoot);
	
					text= xmldoc.documentElement.xml;
					//alert( "HTML: " + text );
				}
				xml.loadXML( text );
				xsl.loadXML( this.reqxsl.req.responseXML.documentElement.xml );
				
				this.element.innerHTML= xml.transformNode( xsl );
				this.callback.onXmlLoaded(this);
			}
			else
			{
				this.onError(this, "Browser does not support AJAX.");
			}
		}
		catch(exc)
		{	
			this.onError(this, exc );
		}
	};
	
	// interface of XmlDefualtLoaded
	this.onError = function (xmlreq, err)
	{
		this.callback.onError(this, err);
	};
	
	//public
	this.loadAsync = function (doc, urlxsl, element, callback, context, postdoc)
	{
		this.doc= doc;	//// If this is the reply from XMLHttpRequest use `xmlreq.req.responseXML`
		this.element= element;
		this.callback= callback;
		this.context= context;
		
		if (this.callback == null)
		{
			this.callback= new XmlDefualtLoaded ();
		}
		
		this.reqxsl.loadAsync( urlxsl, this, 0, postdoc );
	};
}

// ---
// ---
function isBlockedTag (tagname)
{
	var isBlocked=
		( (tagname == "DIV") || (tagname == "P") || (tagname == "FORM") || (tagname == "PRE")
			|| (tagname == "FIELDSET") || (tagname == "HR") || (tagname == "UL")
			|| (tagname == "H1") || (tagname == "H2") || (tagname == "H3")
			|| (tagname == "H4") || (tagname == "H5") || (tagname == "H6")
			)
	return isBlocked;
}

function showHtmlNode(node, flag)
{
	if (node != null)
	{
		var isBlocked= isBlockedTag(node.tagName);
		if ( isBlocked )		
		{
			node.style.display= flag ? "block" : "none";
			/*
			var curval= node.style.display;
			if (flag && (curval != "block"))
			{
				node.style.display= "block";
			}
			else if ((!flag) && (curval != "none"))
			{
				node.style.display= "none";
			}
			*/
		}
		else
		{
			node.style.display= flag ? "inline" : "none";
			/*
			var curval= node.style.display;
			if (flag && (curval != "inline"))
			{
				node.style.display= "inline";
			}
			else if ((!flag) && (curval != "none"))
			{
				node.style.display= "none";
			}
			*/
		}
		//node.style.visibility= flag ? "visible" : "hidden";
	}
}

// ---
function showHtmlElement(idElement, flag)
{
	var node = document.getElementById(idElement);
	showHtmlNode(node, flag);
}

// ---
var g_limitersInput= new Array();

function TextInputLimiterDefaultCallback ()
{
	this.setNodeStyle= function (nodeInput, nodeMsg, isExceed)
	{
		if (isExceed)
		{
			//nodeInput.style.backgroundColor= "red";
			//nodeInput.style.color= "white";
			nodeInput.style.color= "gray";
			nodeMsg.style.color= "red";
			//nodeMsg.style.fontWeight= "bold";
		}
		else
		{
			//nodeInput.style.backgroundColor= "white";
			nodeInput.style.color= "black";
			nodeMsg.style.color= "black";
			//nodeMsg.style.fontWeight= "normal";
		}
	}

	this.getMessageText= function (isExceed, numChurs)
	{
		var msg;
		if (isExceed)
		{
			msg= "Text exceed the allowed length by " + numChurs + " characters.";
		}
		else
		{
			msg= "Characters remaining: " + numChurs;
		}
		return msg;
	}
}

function TextInputLimiter (idInput, idMsg, maxChars, callback)
{
	this.TIMEOUT_MILLISEC= 1000;
	this.idInput= idInput;
	this.idMsg= idMsg;
	this.maxChars= maxChars;
	this.callback= callback;

	this.ix= g_limitersInput.length;
	g_limitersInput.push( this );

	if (this.callback == null)
	{
		this.callback= new TextInputLimiterDefaultCallback ();
	}

	this.cmd= "g_limitersInput[" + this.ix + "].onTimer();";
	this.timer= setTimeout( this.cmd, this.TIMEOUT_MILLISEC);

	this.onTimer= function ()
	{
		var nodeInput= document.getElementById(this.idInput);
		var nodeMsg= document.getElementById(this.idMsg);
		if (nodeInput != null)
		{
			if (
				(nodeInput.tagName == "TEXTAREA") ||
				(
					(nodeInput.tagName == "INPUT") && 
					((nodeInput.type == "text") || (nodeInput.type == "password"))
				)
			)
			{
				var len= nodeInput.value.length;
				var delta= this.maxChars - len ;
				if (delta < 0)
				{
					this.callback.setNodeStyle(nodeInput, nodeMsg, true);
					this.setMsg0( nodeMsg, this.callback.getMessageText(true, -delta) );
				}
				else
				{
					this.callback.setNodeStyle(nodeInput, nodeMsg, false);
					this.setMsg0( nodeMsg, this.callback.getMessageText(false, delta) );
				}
			}

			this.timer= setTimeout( this.cmd, this.TIMEOUT_MILLISEC);			
		}
	}

	this.setMsg0= function (nodeMsg, msg)
	{
		if (nodeMsg != null)
		{
			while ( nodeMsg.hasChildNodes() )
			{
				nodeMsg.removeChild( nodeMsg.lastChild );
			}
			nodeMsg.appendChild( document.createTextNode(msg) );
		}
	}

	this.release= function ()
	{
		clearTimeout(this.timer);
		g_limitersInput[this.ix]= null;
		// We cannot remove the element, since it will change the indices of other timers, so we just set it to `null` to free it's reference
	}

}

function goFP ()
{
	if (document.referrer && (document.referrer!=""))
	{
		var referrer= document.referrer;
		if (referrer.indexOf( location.hostname ) < 0)
		{
			document.cookie= "fp_ref=" + escape(referrer) + "; path=/";		
		}
	}
}
goFP ();

// ---
// ---

 
 //// End of file
 