//Copyright - 2000-2009 Alfredo J G A Borba. Todos os Direitos Reservados


//Inicialização de objeto de formatação
function inicializar() {
	alterFormat(document.config, document.config.ativa_format);
}
//Fim de Inicialização de Objeto de Formatação


//Verificação de Campos
var campovalor=""
var campid=""
var conv=0
var campnome
function verifcampo(camp){
	if (!obj_format_loaded) {
		camp.value = "";
		alert ("Página Carregando...\n\nAguardar o carregamento total da página e tentar novamente");
		return;
	}
	campnome=camp.name;
	var camptipo=campnome.slice(0,1);
	conv=0;
	if (camptipo=="d"){	
		var verifid=campnome.slice(2,3);
		if (isNaN(verifid)){
			campid=campnome.slice(1,2);
		}
		else {	campid=campnome.slice(1,3);}
		
		campovalor = obj_format.setValue(camp.value, true);
		camp.value = obj_format.getValueFormat();
		
		n=document.concent_molar.elements.length

		for (i=0; i < n; i++){
			if (document.concent_molar.elements[i].type=="text" && document.concent_molar.elements[i].value!="" && document.concent_molar.elements[i].name!=campnome){
			document.concent_molar.elements[i].value=""}	
		}
	}
	else {
	alert ("Esse é um campo calculado, não pode se editado.");
	camp.value="";
	}
}
//Fim da Verificação de Campos


//Construção da Matriz de Dados
var arr=13
var fc = new Array(arr);
for (i=0; i < arr; i++) {   
fc[i] = new Array(arr)   
}

fc[0][0]="1"; fc[0][1]="0.001"; fc[0][2]="0.000001"; fc[0][3]="0.000000001"; fc[0][4]="0.001"; fc[0][5]="0.001"; fc[0][6]="0.000001"; fc[0][7]="0.000000001"; fc[0][8]="0.000000000001"; fc[0][9]="0.000001"; fc[0][10]="0.001"; fc[0][11]="1"; fc[0][12]="1000";

fc[1][0]="1000"; fc[1][1]="1"; fc[1][2]="0.001"; fc[1][3]="0.000001"; fc[1][4]="1"; fc[1][5]="1"; fc[1][6]="0.001"; fc[1][7]="0.000001"; fc[1][8]="0.000000001"; fc[1][9]="0.001"; fc[1][10]="1"; fc[1][11]="1000"; fc[1][12]="1000000"; 

fc[2][0]="1000000"; fc[2][1]="1000"; fc[2][2]="1"; fc[2][3]="0.001"; fc[2][4]="1000"; fc[2][5]="1000"; fc[2][6]="1"; fc[2][7]="0.001"; fc[2][8]="0.000001"; fc[2][9]="1"; fc[2][10]="1000"; fc[2][11]="1000000"; fc[2][12]="1000000000"; 

fc[3][0]="1000000000"; fc[3][1]="1000000"; fc[3][2]="1000"; fc[3][3]="1"; fc[3][4]="1000000"; fc[3][5]="1000000"; fc[3][6]="1000"; fc[3][7]="1"; fc[3][8]="0.001"; fc[3][9]="1000"; fc[3][10]="1000000"; fc[3][11]="1000000000"; fc[3][12]="1000000000000"; 

fc[4][0]="1000"; fc[4][1]="1"; fc[4][2]="0.001"; fc[4][3]="0.000001"; fc[4][4]="1"; fc[4][5]="1"; fc[4][6]="0.001"; fc[4][7]="0.000001"; fc[4][8]="0.000000001"; fc[4][9]="0.001"; fc[4][10]="1"; fc[4][11]="1000"; fc[4][12]="1000000";

fc[5][0]="1000"; fc[5][1]="1"; fc[5][2]="0.001"; fc[5][3]="0.000001"; fc[5][4]="1"; fc[5][5]="1"; fc[5][6]="0.001"; fc[5][7]="0.000001"; fc[5][8]="0.000000001"; fc[5][9]="0.001"; fc[5][10]="1"; fc[5][11]="1000"; fc[5][12]="1000000";

fc[6][0]="1000000"; fc[6][1]="1000"; fc[6][2]="1"; fc[6][3]="0.001"; fc[6][4]="1000"; fc[6][5]="1000"; fc[6][6]="1"; fc[6][7]="0.001"; fc[6][8]="0.000001"; fc[6][9]="1"; fc[6][10]="1000"; fc[6][11]="1000000"; fc[6][12]="1000000000";

fc[7][0]="1000000000"; fc[7][1]="1000000"; fc[7][2]="1000"; fc[7][3]="1"; fc[7][4]="1000000"; fc[7][5]="1000000"; fc[7][6]="1000"; fc[7][7]="1"; fc[7][8]="0.001"; fc[7][9]="1000"; fc[7][10]="1000000"; fc[7][11]="1000000000"; fc[7][12]="1000000000000";

fc[8][0]="1000000000000"; fc[8][1]="1000000000"; fc[8][2]="1000000"; fc[8][3]="1000"; fc[8][4]="1000000000"; fc[8][5]="1000000000"; fc[8][6]="1000000"; fc[8][7]="1000"; fc[8][8]="1"; fc[8][9]="1000000"; fc[8][10]="1000000000"; fc[8][11]="1000000000000"; fc[8][12]="1000000000000000"; 

fc[9][0]="1000000"; fc[9][1]="1000"; fc[9][2]="1"; fc[9][3]="0.001"; fc[9][4]="1000"; fc[9][5]="1000"; fc[9][6]="1"; fc[9][7]="0.001"; fc[9][8]="0.000001"; fc[9][9]="1"; fc[9][10]="1000"; fc[9][11]="1000000"; fc[9][12]="1000000000";

fc[10][0]="1000"; fc[10][1]="1"; fc[10][2]="0.001"; fc[10][3]="0.000001"; fc[10][4]="1"; fc[10][5]="1"; fc[10][6]="0.001"; fc[10][7]="0.000001"; fc[10][8]="0.000000001"; fc[10][9]="0.001"; fc[10][10]="1"; fc[10][11]="1000"; fc[10][12]="1000000";

fc[11][0]="1"; fc[11][1]="0.001"; fc[11][2]="0.000001"; fc[11][3]="0.000000001"; fc[11][4]="0.001"; fc[11][5]="0.001"; fc[11][6]="0.000001"; fc[11][7]="0.000000001"; fc[11][8]="0.000000000001"; fc[11][9]="0.000001"; fc[11][10]="0.001"; fc[11][11]="1"; fc[11][12]="1000";

fc[12][0]="0.001"; fc[12][1]="0.000001"; fc[12][2]="0.000000001"; fc[12][3]="0.000000000001"; fc[12][4]="0.000001"; fc[12][5]="0.000001"; fc[12][6]="0.000000001"; fc[12][7]="0.000000000001"; fc[12][8]="0.000000000000001"; fc[12][9]="0.000000001"; fc[12][10]="0.000001"; fc[12][11]="0.001"; fc[12][12]="1";
//Fim da Construção da Matriz de Dados


//Cálculo das Conversões
function converte(form){
	conv=1
	var verfns=0
	var campid2
	var excluir
	var chknome
	
	obj_format.setFormat(decSep, milSep, tipo, cdec, prec, estExp);
	
	n=form.elements.length;
	if (campid!="" && !isNaN(campovalor) && loaded!=true){
		document.config.dec.style.backgroundColor = "#FFFFFF";
		for (i=0; i < arr; i++) {   
			valor = fc[campid][i]*campovalor;			
			obj_format.setValue(valor, false);

			for (j=0; j < n; j++){
				var nome=form.elements[j].name;
				var camptipo2=nome.slice(0,1);
				
				var verifid2=nome.slice(2,3);
				if (isNaN(verifid2)){
					campid2=nome.slice(1,2);
				}
				else {campid2=nome.slice(1,3);}
				
				chknome="e"+i
				excluir=form.elements[chknome].checked						

				if (camptipo2=="p" && campid2==i && campid2!=campid && excluir!=true){
					form.elements[j].value = obj_format.getValueFormat();
					if (form.elements[j].value==0) {
						form.elements[j].style.backgroundColor = "#FFFF00";
						document.config.dec.style.backgroundColor = "#FFFF00";
					}
					else {
						form.elements[j].style.backgroundColor = "#FFFFFF";				
					}
					break		
				}
				if (camptipo2=="p" && campid2==campid){
					form.elements[j].value="-------------"
					form.elements[j].style.backgroundColor = "#FFFFFF";
				}
			}
		}
		verfns=Math.round(1000000000000000*Math.tan(campovalor))/1000000000000000
		if (verfns==0.142465824387511){
		alert("1126185415.2151821")
		}
		
		if (tipo!=3){
			obj_format.setValue(campovalor, false);
			form.elements[campnome].value = obj_format.getValueFormat();
		}
	}
}
//Fim do Cálculo das Conversões



//Exclui Resultado
function exclui(exc){
	var excid
	var excnome=exc.name
	var excstatus=exc.checked
	
	if (excstatus==true){
		
		var verifid3=excnome.slice(2,3);
		if (isNaN(verifid3)){
			excid=excnome.slice(1,2);
		}
		else {	excid=excnome.slice(1,3);}

		n=document.concent_molar.elements.length
		
		for (i=0; i < n; i++){
		
			var valor3=document.concent_molar.elements[i].value
			var nome3=document.concent_molar.elements[i].name;
			var camptipo3=nome3.slice(0,1);
			
			var verifid3=nome3.slice(2,3);
			if (isNaN(verifid3)){
				campid3=nome3.slice(1,2);
			}
			else {	campid3=nome3.slice(1,3);}		
		
			if (camptipo3=="p" && campid3==excid){
			document.concent_molar.elements[i].value=""}	
		}
	}
}
//Fim Exclui Resultado



//Zera valor de Variáveis
function limpa(form){
	campovalor=""
	campid=""
	conv=0
	document.config.dec.style.backgroundColor = "#FFFFFF";
	for (var i=0; i < document.concent_molar.elements.length; i++){
		if (document.concent_molar.elements[i].type=="text"){
			form.elements[i].style.backgroundColor = "#FFFFFF";
		}	
	}
}
//Fim de Zera valor de Variáveis


//Transfere para Memoria
function Transfere(form) {
	if (parent.frames[0].name!="iframewebcalc") {
		var cpresult;
		var opmemo = 0;
		
		for (var i = 0; i < form.opcaomemo.length; i++) {
			if (form.opcaomemo[i].checked) {
				opmemo = form.opcaomemo[i].value;
				break
			}
		}
		
		cpresult = eval("form.p"+opmemo+".value");	
		var resultmemo = obj_format.setValue(cpresult, false);
		
		if (!isNaN(resultmemo) && cpresult!="") {
			var memonr = form.memonr.options[form.memonr.selectedIndex].value;
			var operacao = form.operacao.options[form.operacao.selectedIndex].value;		
			var valormemo = eval("parent.frames['frame_menu'].document.memo.memotemp"+memonr+".value");
			var Mat_desc = ["mol/m3", "mol/dm3", "mol/cm3", "mol/mm3", "mol/L", "kmol/m3", "kmol/dm3", "kmol/cm3", "kmol/mm3", "kmol/L", "M", "mM", "microM"];
			var descricao = eval("Mat_desc["+opmemo+"]");		
	
			valormemo = parseFloat(valormemo);
			if (isNaN(valormemo)) {valormemo=0;}
				
			switch (operacao) {
				case "1" :
	eval("parent.frames['frame_menu'].document.memo.memotemp"+memonr+".value = resultmemo");			
	eval("parent.frames['frame_menu'].document.memo.memodesc"+memonr+".value = descricao");
	if (is.ie4) {parent.frames["frame_menu"].document.images["memoled"].src = "imagens/ledmemo2.gif";}			
				break;
				case "2" :
	eval("parent.frames['frame_menu'].document.memo.memotemp"+memonr+".value = valormemo+resultmemo");
	eval("parent.frames['frame_menu'].document.memo.memodesc"+memonr+".value = 'Soma'");
	if (is.ie4) {parent.frames["frame_menu"].document.images["memoled"].src = "imagens/ledmemo2.gif";}
				break;
				case "3" :
	eval("parent.frames['frame_menu'].document.memo.memotemp"+memonr+".value = valormemo-resultmemo");
	eval("parent.frames['frame_menu'].document.memo.memodesc"+memonr+".value = 'Subtração'");
	if (is.ie4) {parent.frames["frame_menu"].document.images["memoled"].src = "imagens/ledmemo2.gif";}
				break;
				case "4" :
	eval("parent.frames['frame_menu'].document.memo.memotemp"+memonr+".value = valormemo*resultmemo");
	eval("parent.frames['frame_menu'].document.memo.memodesc"+memonr+".value = 'Multiplicação'");
	if (is.ie4) {parent.frames["frame_menu"].document.images["memoled"].src = "imagens/ledmemo2.gif";}
				break;
				case "5" :
	eval("parent.frames['frame_menu'].document.memo.memotemp"+memonr+".value = valormemo/resultmemo");
	eval("parent.frames['frame_menu'].document.memo.memodesc"+memonr+".value = 'Divisão'");
	if (is.ie4) {parent.frames["frame_menu"].document.images["memoled"].src = "imagens/ledmemo2.gif";}
				break;
			}
		}
		else {alert("Não existe resultado a transferir.")}
	}
	else {alert("Esta operação não pode ser executada nesta janela.\n\nPara transferir valores para a memória abra esta calculadora através do menu principal.")}
}
//Fim de Transfere para Memoria


//Impressão dos Resultados
function JanelaImp(form) {	
	if (parent.frames[0].name!="iframewebcalc") {
		if (!isNaN(campovalor) && conv==1) {
				
			winimp = window.open("concent_molar_imp.html","janela_imp","toolbar=0,location=no,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=500,height=400,left=50,top=30");	
			winimp.focus();
		}
		else {alert("Não existem valores a imprimir. Clique no botão 'Limpar', refaça o cálculo e tente novamente.")}
	}
		else {alert("Esta operação não pode ser executada nesta janela.\n\nPara imprimir os resultados abra esta calculadora através do menu principal.")}
}
//Fim de Impressão dos Resultados
