lines = new Array(0); // linhas utilizadas para a tradução do idioma.
linguagem = ""; // Qual a linguagem que está sendo utilizada
var labels = new Array(); // Array que irá conter todos as linguaguens
var id = null; // variavel que irá representar o campo idioma da tela
var linguagemId = new Array(0); // Array contendo as linhas iniciais de cada idioma
var labelLetra = new Array(0); // Array contendo a linha de início das palavras.
var inicioLinguagem = 0; // variavel contendo o inicio do idioma atual dentro da array de idiomas
var inicioLetraLinguagem = 0; // Código de inicio da letra daquela linguagem
var finalLetraLinguagem  = 0; // Código do final da letra daquela linguagem

// constantes
var LINGUAGEM = 0; // Código da linguagem
var CODIGO = 1; // Código do Código
var LABEL = 2; // código do Label
var MENSAGEM_ERRO = 3; // mensagem de erro caso não seja preenchdo o campo
var TOOLTIP = 4; // Tool Tip que deve aparecer
var NOME_CAMPO = 5; // nome do campo existente na tela

// pega todos os labels existentes na tela
function getLabels(){
        
	var auxLabels = document.getElementsByTagName("span");
	var contador = 0;
	for(index =0; index < auxLabels.length; index++){
		if(auxLabels[index].name != undefined){
			labels[contador++] = auxLabels[index].name;
		}
	}
}

// atualiza o índice de unício da pesquisa de acordo com a linguagem escolhida
function updateLanguageIndex(){
	for(var index = 0; index < linguagemId.length; index++){
		if(linguagemId[index][LINGUAGEM].toUpperCase() == linguagem.toUpperCase()){
			inicioLinguagem = linguagemId[index][1];
			break;
		}
	}

}

//  verifica se o campo linguagem existe, caso exista joga na variavel
function verifyFormLanguage(){
	if(id ==null){
		id = document.getElementById("idioma");
		if(id == undefined || id == null || id.value == undefined){
			id = document.getElementsByName("idioma");
			if(id.length > 0){
				id = id[0];
			}
		}
	}
}

// Verifica se a linguagem (variavel local) está setada com algum valor
// Caso  negativo, pega a linguagem e atualiza o indice inicial
function verifyLanguageExists(){
	if(linguagem == ""){
		verifyFormLanguage();
		linguagem = id.value;
		updateLanguageIndex();
	}
	if(labelLetra.length ==0){
		var inicio = "";
		var final = "";
		var linguagemInicio = "";
		var contador = Number(0);
		linguagemInicio = lines[0][LINGUAGEM].toUpperCase();
		var indexLines = 0;
		for(indexLines = 0; indexLines < lines.length; indexLines++){
			if(linguagemInicio == lines[indexLines][LINGUAGEM].toUpperCase()){
				inicio = lines[indexLines][CODIGO].toUpperCase().charAt(0);
				if(inicio != final){
					labelLetra[contador++] = new Array(lines[indexLines][CODIGO].toUpperCase().charAt(0), indexLines);
				}
				final = inicio;
			}else{
				break;
			}
		}
		labelLetra[contador++] = new Array("Final", indexLines);
	}
}

// Altera a linguagem da página
function changeLabelLanguage(idioma){
	verifyFormLanguage();
	id.value = idioma;
	linguagem = idioma;
	updateLanguageIndex();
	showAllLabels();
}

// pega o valor e retorna a valor inicial e final do array da letra.
function updateStartEndArray(valor){
	for(var index = 0; index < labelLetra.length; index++){
		if(labelLetra[index][0] == valor.toUpperCase().charAt(0)){
			inicioLetraLinguagem = labelLetra[index][1] + inicioLinguagem;
			finalLetraLinguagem = labelLetra[(index+1)][1] + inicioLinguagem;
			break;
		}
	}
}

// pega o valor de um label específico, de acordo com a linha passada.
function getLabel(codLabel){
	verifyLanguageExists();
	updateStartEndArray(codLabel);
	for(var indexLines = inicioLetraLinguagem; indexLines < finalLetraLinguagem; indexLines++){
			if(lines[indexLines][CODIGO]  == codLabel){
				return lines[indexLines][LABEL];
			}
	}
}

function updateWMTT(e) {
	try{
		x = (document.all) ? window.event.x + document.body.scrollLeft : ((e == undefined) ? 0 : e.pageX);
		y = (document.all) ? window.event.y + document.body.scrollTop  : ((e == undefined) ? 0 : e.pageY);
		if (wmtt != null) {
			wmtt.style.left = (x + 15) + "px";
			wmtt.style.top 	= (y + 15) + "px";
		}
	}
	catch(ex)
	{
		
	}
}

document.write("<div class=\"tooltip\" id=\"tooltip\"></div>");
document.write("<style type=\"text/css\">");
document.write(".tooltip {");
document.write("	position: absolute;");
document.write("	font-size: 12px;");
document.write("	font-family: Verdana, Arial, Helvetica, sans-serif;");
document.write("	font-style: italic;");
document.write("	display: none;");
document.write("	background-color: #FFFFCC;");
document.write("	border-style: solid;");
document.write("	border-top-width: 1px;");
document.write("	border-right-width: 1px;");
document.write("	border-bottom-width: 1px;");
document.write("	border-left-width: 1px;");
document.write("	border-top-color: #000000;");
document.write("	border-right-color: #000000;");
document.write("	border-bottom-color: #000000;");
document.write("	border-left-color: #000000;");
document.write("	z-index:100;");
document.write("}");
document.write("</style>");
wmtt = null;

function showWMTT(mensagem) {
	wmtt = document.getElementById("tooltip");
	if(mensagem != ""){
		wmtt.innerHTML= mensagem;
		wmtt.style.display = "block";
	}
}

function hideWMTT() {
	wmtt.style.display = "none";
}

document.onmousemove = updateWMTT;

// atualiza todos os labels existentes na tela.
// e prepara o tool tip
function showAllLabels(){
	verifyLanguageExists();
	getLabels();
   
	for(var index = 0; index < labels.length; index++){
		updateStartEndArray(labels[index]);
		for(var indexLines = inicioLetraLinguagem; indexLines < finalLetraLinguagem; indexLines++){
			if(lines[indexLines][CODIGO]  == labels[index] && lines[indexLines][LINGUAGEM] == linguagem){
				label = document.getElementsByName(labels[index]);
				for(i = 0; i < label.length; i++){
					if(label[i].tagName.toUpperCase() == "SPAN")
						label[i].innerHTML= lines[indexLines][LABEL];
						label[i].onmouseover = new Function("showWMTT('" + lines[indexLines][TOOLTIP] + "')");
						label[i].onmouseout = new Function("hideWMTT()");
//						alert(label[i].onmouseover);
						//label[i].onmouseout = new function(){hideWMTT()};
				}
			}
		}
	}
}

function showAllLabelsByFieldName(){
	verifyLanguageExists();
	getLabels();
	for(var index = 0; index < labels.length; index++){
		updateStartEndArray(labels[index]);
		for(var indexLines = 0; indexLines < lines.length; indexLines++){
			if(lines[indexLines][NOME_CAMPO] == labels[index] && lines[indexLines][LINGUAGEM] == linguagem){
				label = document.getElementsByName(labels[index]);
				for(i = 0; i < label.length; i++){
					if(label[i].tagName.toUpperCase() == "SPAN")
						label[i].innerHTML= lines[indexLines][LABEL];
						label[i].onmouseover = new Function("showWMTT('" + lines[indexLines][TOOLTIP] + "')");
						label[i].onmouseout = new Function("hideWMTT()");
//						alert(label[i].onmouseover);
						//label[i].onmouseout = new function(){hideWMTT()};
				}
			}
		}
	}
}

function validateForm(formulario){
	var mensagem = "";
	resposta = "";

	if (formulario.tagName.toLowerCase() == "form"){
		for(var index = 0; index < labels.length; index++){
			updateStartEndArray(labels[index]);
			for(var indexLines = inicioLetraLinguagem; indexLines < finalLetraLinguagem; indexLines++){
				if(lines[indexLines][CODIGO]  == labels[index]){
					campo = lines[indexLines];
					resposta += campo +  "\n";
					if(campo != undefined){
						if(formulario.elements[campo[NOME_CAMPO]] != undefined){
							//alert("Kina\n" + NOME_CAMPO + "=" + campo[NOME_CAMPO] + "\n" + formulario.elements[campo[NOME_CAMPO]] +"\n" +formulario.elements[campo[NOME_CAMPO]].name);
							if(formulario.elements[campo[NOME_CAMPO]].length == undefined){
								valor = getValue(formulario.elements[campo[NOME_CAMPO]]);
								if(valor == "" && campo[MENSAGEM_ERRO].length > 0){
									mensagem += campo[MENSAGEM_ERRO] +"\n";
								}
							}else for(idx =0; idx < formulario.elements[campo[NOME_CAMPO]].length; idx++){
								valor = getValue(formulario.elements[campo[NOME_CAMPO]][idx]);
								if(valor == "" && campo[MENSAGEM_ERRO].length > 0){
									mensagem += campo[MENSAGEM_ERRO] +"\n";
								}
							}
						}
					}
				}
			}
		}
	} else {
		for(var index = 0; index < labels.length; index++){
			updateStartEndArray(labels[index]);
			for(var indexLines = inicioLetraLinguagem; indexLines < finalLetraLinguagem; indexLines++){
				if(lines[indexLines][CODIGO]  == labels[index]){
					campo = lines[indexLines];
					resposta += campo +  "\n";
					if(campo != undefined){
						if(formulario.all[campo[NOME_CAMPO]] != undefined){
							//alert("Kina\n" + NOME_CAMPO + "=" + campo[NOME_CAMPO] + "\n" + formulario.all[campo[NOME_CAMPO]] +"\n" + formulario.all[campo[NOME_CAMPO]].name);
							if(formulario.all[campo[NOME_CAMPO]].length == undefined){
								valor = getValue(formulario.all[campo[NOME_CAMPO]]);
								if(valor == "" && campo[MENSAGEM_ERRO].length > 0){
									mensagem += campo[MENSAGEM_ERRO] +"\n";
								}
							}else for(idx =0; idx < formulario.all[campo[NOME_CAMPO]].length; idx++){
								valor = getValue(formulario.all[campo[NOME_CAMPO]][idx]);
								if(valor == "" && campo[MENSAGEM_ERRO].length > 0){
									mensagem += campo[MENSAGEM_ERRO] +"\n";
								}
							}
						}
					}
				}
			}
		}
	}
	if(mensagem.length > 0 ){
		alert(mensagem);
		return false;
	}
	return true;
}

// retorna a valor do campo, de acordo como o tipo dele
function getValue(val){
	var retorno = null;
	if(val.tagName != undefined){
		switch(val.tagName.toUpperCase()){
			case "SELECT":
				retorno = val[val.selectedIndex].value;
				break;
			case "INPUT":
			case "TEXTAREA":
				retorno = val.value;
				break;
		}
	}
	return retorno;
}


