/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */

var nationalCharactersActiveElement = "";
var nationalCharactersActiveObjectID = "";

componentNationalCharacters = function(name)
{
	this.componentElement = componentElement;
	this.componentElement(name);
	
	this.font = {};
};

componentNationalCharacters.prototype = new componentElement();
componentNationalCharacters.prototype.type = "componentNationalCharacters";

componentNationalCharacters.prototype.onCreate = function(obj)
{
	this.resourcesStatus = "online";
	this.resourcesURL = resourcesURL;
	
	if (PageInterface.GetValue("state") != psDesigning && lms && lms.name != "lmsOffline") 
	{
		this.resourcesStatus = lms.resourcesStatus();
		if (this.disableOnlineOfflineMix) this.resourcesURL = resourcesURL;
		else this.resourcesURL = lms.resourcesURL();
	};

	this.setImageSrc(this.resourcesURL + this.buttonSrc, this.findContentObj());
	
	this.imagesNameArr = [	this.buttonSrc, this.buttonStandardSrc ];
	this.getPreloadImagesArr();	
	
	this.font = getProjectFont(this.allowGF, this.fontFamily, this.fontSize, this.fontColor );
	
	if (is.moz)	this.mosBUG();
	
	this.setInnerHTML(this);
	this.captureMouseEvents();
};

componentNationalCharacters.prototype.mosBUG = function(image)
{
	var popup = document.getElementById(this.id+'_PopupDiv');
	
	if(popup)
	{
		popup.style.width = this.w-4;
		popup.style.height = this.h-this.popupY-4;
	}
}

componentNationalCharacters.prototype.setButtonImage = function(image)
{
	this.setImageSrc(this.resourcesURL + image, this.findContentObj());
}

componentNationalCharacters.prototype.addSign = function(text)
{
	if (page.doc.forms[nationalCharactersActiveElement])
	{
		page.doc.forms[nationalCharactersActiveElement][0].focus();
		page.doc.forms[nationalCharactersActiveElement][0].value += text;
	}else if (nationalCharactersActiveObjectID != '') {
		var obj = document.getElementById(nationalCharactersActiveObjectID);
		obj.focus();
		obj.value += text;		
	}else{
		showMessage("Sign can be inserted only in last active input field.");
	}
};

componentNationalCharacters.prototype.chooseLetters = function(letters)
{
	var tableLetters = "";
	tableLetters += "<table cellspacing=0 cellpadding=0 border=0 style=\" width: 100%; background-color: #FFFFFF; \" ><tr>";

	var width = this.w-4;// 4 - popup border;
	var modulo = Math.floor((width - 5) / 26);

	for (var j = 0; j < letters.length; j++)
	{
		if ((j % modulo) == 0) tableLetters += "<tr>";

		tableLetters += "<td class=\""+this.id+"sign\" onClick=\"page.all['"+this.id+"'].addSign('"+letters.charAt(j)+"')\" onMouseOver=\"this.className='"+this.id+"signOn'\" onMouseOut=\"this.className='"+this.id+"sign'\"><font style=\"font-family:"+this.font.family+"; font-size:"+getFontSize(this.font.size, "px")+"px; color:" + this.font.color + ";\">"+letters.charAt(j)+"</font></td>";

					
		if ((j % modulo) == modulo-1) tableLetters += "</tr>";
			
	}
	if ((j % modulo) != modulo-1) tableLetters += "</tr>";

	tableLetters += "</table>";
	
	if(document.getElementById(this.id+'_lettersDiv'))
	{
		document.getElementById(this.id+'_lettersDiv').innerHTML = tableLetters;
	}
};

componentNationalCharacters.prototype.componentNationalCharactersShow = componentNationalCharacters.prototype.show;
componentNationalCharacters.prototype.show = function() 
{
	this.componentNationalCharactersShow();
	
	if (PageInterface.GetValue("state") == psDesigning) {
		var popup = document.getElementById(this.id+'_PopupDiv');
		
		if(popup)
		{
			popup.style.display = "";
		}
	};
}

componentNationalCharacters.prototype.setInnerHTML = function(e)
{
	var width = this.w-4;// 4 - popup border;
	var text = e.list;
	
	var select = "";
	select += "<select onChange=\"page.all['"+this.id+"'].chooseLetters(this.value);\" name=\""+this.id+"selectNC\"> ";

	var tableLetters = "";
	tableLetters += "<table cellspacing=0 cellpadding=0 border=0 style=\" width: 100%; background-color: #FFFFFF; \" ><tr>";

	text = text.replace(/^(<.*>)(.*)(<\/.*>)$/,'$2');

	var arrOpt = text.split("||");
	var arrDel = null;
	var arrLet = null;
	var modulo = Math.floor((width-5) / 26);

	e.option = "";
	e.tableLetterCell = "";

	for (var i = 0; i < arrOpt.length; i++) {
		arrDel = (arrOpt[i]).split(",");
		select += '<option id="opt'+i+this.id+'" value="'+arrDel[1]+'">' + arrDel[0] + '</option>';

		if (i == 0)
		{
			for (var j = 0; j < arrDel[1].length; j++)
			{
				if ((j % modulo) == 0) tableLetters += "<tr>";
				
				tableLetters += "<td class=\""+this.id+"sign\" onClick=\"pc."+this.id+".addSign('"+arrDel[1].charAt(j)+"')\" onMouseOver=\"this.className='"+this.id+"signOn'\" onMouseOut=\"this.className='"+this.id+"sign'\">";
				tableLetters += "<font style=\"font-family:"+this.font.family+"; font-size:"+getFontSize(this.font.size, "px")+"px; color:" + this.font.color + ";\">";
				tableLetters += arrDel[1].charAt(j);
				tableLetters += "</font></td>";
				
				if ((j % modulo) == modulo-1) tableLetters += "</tr>";
			}
			if ((j % modulo) != modulo-1) tableLetters += "</tr>";
		}
	}
	select += "</select>";
	tableLetters += "</table>";
	

	if(document.getElementById(this.id+'_selectDiv'))
	{
		document.getElementById(this.id+'_selectDiv').innerHTML = select;
	}
	if(document.getElementById(this.id+'_lettersDiv'))
	{
		document.getElementById(this.id+'_lettersDiv').innerHTML = tableLetters;
	}
};

componentNationalCharacters.prototype.setElement = function(elementNb, id, objID) 
{
	nationalCharactersActiveElement = elementNb;
	nationalCharactersActiveObjectID = objID;	
};

componentNationalCharacters.prototype.addhandlers = function(f, e, objArr)
{	

	
	for(var i = 0; i < f.length; i++) 
	{
		if ( f[i][0].tagName == "INPUT" || f[i][0].tagName == "TEXTAREA")
		{
			f[i][0].onclick = new Function("pc."+this.id+".setElement('"+i+"','"+this.id+"', '')");
		}
	};
	if (objArr.length > 0) {
		for(var j=0; j < objArr.length; j++) 
		{
			objArr[j].onclick = new Function("pc."+this.id+".setElement('"+eval(i+j)+"','"+this.id+"', '"+objArr[j].id+"')");
		};
	};
};

componentNationalCharacters.prototype.buttonClick = function(e)
{
	var popup = document.getElementById(this.id+'_PopupDiv');
	
	if(popup)
	{
		if (popup.style.display == "")
		{
			popup.style.display = "none";
		}else{
			popup.style.display = "";
		};
	}
};

componentNationalCharacters.prototype.onAfterLoad = function(obj)
{
	this.setInnerHTML(this);	
	var componentsArr = document.getElementsByName('myInputId');
	this.addhandlers(window.document.forms, this, componentsArr);
};
