/*
	Function allows to replace ^ with argument passed through fonction, either : 
	- By order 1st ^ encountered  replaced with 1st argument and 2nd ^ replaced with 2nd argument &c.
	- By number all ^1 occurences encountered replaced with 1st argument and all ^3 with 3rd argument 
*/
Error.prototype.include=function()	{
	var sReturn=this.description;
	for(var __a=0;__a<arguments.length;__a++) {
		var rCaret=new RegExp("\\^"+(__a+1),"g");
		sReturn=rCaret.test(sReturn)?sReturn.replace(rCaret,arguments[__a]):sReturn.replace(/\^(?!\d)/,arguments[__a]);
	}
	return sReturn;
}

Error.prototype.toString=function()	{
	return this.description;
}

function getCookie(sCookie) {
	var sValue="";
	var aCookies=document.cookie.split(';');
	for(var __c=0;__c<aCookies.length;c__++) {
		var oCookie=aCookies[__c].split(/=/);
		if(oCookie[0]==sCookie) {
			sValue=oCookie[1];
			break;
		}
	}
	return sValue;
}
function webDictionary(sFilename) {
	//get language 
	var sLanguage=getCookie("__language")?getCookie("__language"):"en";
	this.sDictionary=(sFilename)?sFilename:document.location.href.replace(/^(.*)\/admin\/.*$/,"$1/lib/dictionary."+sLanguage+".xml");
	this.XML=new ActiveXObject("Microsoft.XMLDOM");
	this.XML.async=false;
	this.XML.load(this.sDictionary);
	this.sModuleName=document.location.href.replace(/^.*\/([^\/\?]+).*$/,"$1");
	this.oModule=this.XML.selectSingleNode("/dictionary/modules/module[@name='"+this.sModuleName+"']")
	this.oElements=this.XML.selectSingleNode("/dictionary/elements");
	
	//get element by name ...
	this.getElementByName=function(sName) {
		var oElement=this.oModule.selectSingleNode("element[@name='"+sName+"']");
		if(!oElement)	oElement=this.oElements.selectSingleNode("element[@name='"+sName+"']");
		return oElement?oElement:null;
	}

	//get text by id ...
	this.getTextById=function(sId) {
		var oText=this.oModule.selectSingleNode("text[@id='"+sId+"']");
		if(!oText)	oText=this.oElements.selectSingleNode("text[@id='"+sId+"']");
		return oText?oText.firstChild:null;
	}

	/*
	get messages, return error message object ... 
	create error message array 
		aMessage[number]=value;
	*/
	this.getMessages=function()	{
		var cMessages=oDict.XML.getElementsByTagName("message");
		var __aMessage=new Object();
		for(var __m=0;__m<cMessages.length;__m++)	{
			if(cMessages[__m].firstChild)	__aMessage[cMessages[__m].getAttribute("id")]=new Error(cMessages[__m].getAttribute("id"),cMessages[__m].firstChild.text.replace("\\n","\n"));
		}
		return __aMessage;
	}
	
	this.setTitle= function() {
		//update title ...if specified 
		var oTitle=this.oModule?this.oModule.selectSingleNode("title"):null;
		if(oTitle)	window.document.title=oTitle.text;
	}

	//translate module ... 
	this.translate=function() {
		if(this.oModule)	{

			//translate texts ... 
			var cSpans=document.getElementsByTagName("SPAN")
			for(var __s=0;__s<cSpans.length;__s++) {
				//ignore elements without id ...
				if(!cSpans[__s].getAttribute("id"))	continue;
				var oText=this.getTextById(cSpans[__s].id);
				if(oText)	cSpans[__s].innerText=oText.nodeValue;
			}
	
			var cDivs=document.getElementsByTagName("DIV")
			for(var __d=0;__d<cDivs.length;__d++) {
				//ignore elements without id ...
				if(!cDivs[__d].getAttribute("id"))	continue;
				//ignore non-translated elements ...
				var oText=this.getTextById(cDivs[__d].getAttribute("id"));
				if(!oText)	continue;
				if(oText.nodeType==4) {				//CDATA with HTML content ... 
					cDivs[__d].innerHTML=oText.nodeValue;
				} else {
					var cNodes=cDivs[__d].childNodes;
					for(var __n=0;__n<cNodes.length;__n++) {
						if(cNodes[__n].nodeType==3) { //text ... 
							cNodes[__n].nodeValue=oText.nodeValue;
							break;
						}
					}
				}
			}
			
			//translate labels 
			var cLabels=document.getElementsByTagName("label");
			for(var __l=0;__l<cLabels.length;__l++) {
				var oElement=this.getElementByName(cLabels[__l].getAttribute("htmlFor"));
				if(oElement && oElement.getElementsByTagName("label").length>0) {
					var cNodes=cLabels[__l].childNodes;
					for(var __n=0;__n<cNodes.length;__n++) {
						if(cNodes[__n].nodeType==3) { //text ... 
							cNodes[__n].nodeValue=oElement.getElementsByTagName("label")[0].firstChild.nodeValue;
							break;
						}
					}
					var oTitle=oElement.getElementsByTagName("title")[0];
					if(oTitle && oTitle.firstChild)	cLabels[__l].setAttribute("title",oTitle.firstChild.nodeValue);
				}
			}
	
			//translate input elements
			var cInputs=document.forms[0].elements;
			for(var __i=0;__i<cInputs.length;__i++) {
				var oElement=this.getElementByName(cInputs[__i].getAttribute("id"));
				if(oElement) {
					var oTitle=oElement.getElementsByTagName("title")[0];
					if(oTitle)	cInputs[__i].setAttribute("title",oTitle.firstChild.nodeValue);
					//update help value ...
					var oHelp=oElement.getElementsByTagName("help")[0];
					if(oHelp)	cInputs[__i].setAttribute("help",oTitle.firstChild.nodeValue);
					//update default value ...
					if(oElement.getAttribute("default"))	cInputs[__i].value=oElement.getAttribute("default");
					//update default select options ...
					if(oElement.getAttribute("type")=="select" && /^select/i.test(cInputs[__i].type)) {
						var cDefOptions=oElement.getElementsByTagName("option");
						var cOptions=cInputs[__i].options;
						for(var __p=0;__p<cDefOptions.length;__p++) {
							for(var __pp=0;__pp<cOptions.length;__pp++) {
								if(cDefOptions[__p].getAttribute("value")==cOptions[__pp].value)	{
									cOptions[__pp].text=cDefOptions[__p].firstChild.nodeValue;
									break;
								}
							}
						}
					}
				}
			}
			
			//translate image elements
			var cImages=document.getElementsByTagName("img");
			for(var __i=0;__i<cImages.length;__i++) {
				var oElement=this.getElementByName(cImages[__i].getAttribute("id"));
				if(oElement) {
					if(oElement.getAttribute("src"))	cImages[__i].setAttribute("src",oElement.getAttribute("src"));
					var oTitle=oElement.getElementsByTagName("title")[0];
					if(oTitle)	cImages[__i].setAttribute("title",oTitle.firstChild.nodeValue);
				}
			}
		}
	}
}

//create dictionary object
if(!window.oDict) {
	var oDict=new webDictionary();
	//set messages ...
	var aMessage=oDict.getMessages();
	//translate title before page load ...
	oDict.setTitle();
}
