// JavaScript Document

function janela(url, width, height){
	window.open(url,"Predilecta","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,menubar=no,width="+width+",height="+height);
}

function janelaRep(url, width, height){
	window.open(url,"Predilecta","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,menubar=no,width="+width+",height="+height);
}

function acertaIFrame(){
	var tamanho = document.body.offsetHeight;
	parent.document.getElementById("conteudo").style.height = tamanho;
}

function mudaRodape(rodId,iframe){
	rodapes = new Array("supRodape.jpg","rodape_2.jpg","rodape_verprod.jpg");
	if(!iframe){
		document.images.rodape.src = "../imagens/" + rodapes[rodId];
	}else{
		parent.document.images.rodape.src = "../imagens/" + rodapes[rodId];
	}
}

function verificaHash(){
	var hash = window.location.hash.split('#');
	if(hash.length > 1){
		hash = hash[1];
		abreLink(hash,document.getElementById('conteudo'));
	}
}

function buscaContatos(tipo,lingua){
	var dep = new Array("Departamento", "Department", "Departamento");
	var rep = new Array("Representante", "Representation", "Representante");
	var ind = 0;
	if(lingua == "_en"){
		ind = 1;
	}else if(lingua == "_es"){
		ind = 2;
	}
	limpaListBox(document.getElementById('cbModo'));
	var url = '';
	if(tipo == 'P'){
		document.getElementById('modo').innerHTML = dep[ind];
		document.getElementById('seta').style.visibility = 'hidden';
		url = "scr_contatos.php?op=P";		
	}else if(tipo == 'R'){
		document.getElementById('modo').innerHTML = rep[ind];
		document.getElementById('seta').style.visibility = 'visible';
		url = "scr_contatos.php?op=R";
	}
	processaURL(url,mostraContatos);
}

function mostraContatos(){
	var xml = getRespostaXML();
	var lBox = document.getElementById('cbModo');
	if(xml != null){
		var tipo = xml.getElementsByTagName('contatos')[0].getAttribute('tipo');
		if(tipo == 'P'){
			var contatos = xml.getElementsByTagName('contato');
			for(i = 0; i < contatos.length; i++){
				var nome = contatos[i].getElementsByTagName('nome')[0].firstChild.data;
				var id = contatos[i].getElementsByTagName('id')[0].firstChild.data;
				var email = contatos[i].getElementsByTagName('email')[0].firstChild.data;				
				inserirOpcao(lBox, email, nome, false, id,null);
			}
		}else if(tipo == 'R'){		
			var lng = xml.getElementsByTagName('contatos')[0].getAttribute('lng');
			if(lng == '_pt'){
				inserirGrupo(lBox,'Brasil','gpBrasil');
				inserirGrupo(lBox,'Exterior','gpExterior');
			}else{
				inserirGrupo(lBox,'Exterior','gpExterior');
				inserirGrupo(lBox,'Brasil','gpBrasil');
			}
			var contatos = xml.getElementsByTagName('brasil')[0].getElementsByTagName('contato');
			for(i = 0; i < contatos.length; i++){
				var nome = contatos[i].getElementsByTagName('nome')[0].firstChild.data;
				var id = contatos[i].getElementsByTagName('id')[0].firstChild.data;
				var email = contatos[i].getElementsByTagName('email')[0].firstChild.data;				
//				var opt = new Option;
				inserirEmGrupo(obterOpcao(email, nome, false, id),'gpBrasil');
//				opt = obterOpcao(email, nome, false, id);				
			}			
			var contatos = xml.getElementsByTagName('exterior')[0].getElementsByTagName('contato');			
			for(i = 0; i < contatos.length; i++){
				var nome = contatos[i].getElementsByTagName('nome')[0].firstChild.data;
				var id = contatos[i].getElementsByTagName('id')[0].firstChild.data;
				var email = contatos[i].getElementsByTagName('email')[0].firstChild.data;				
//				var opt = new Option;
				inserirEmGrupo(obterOpcao(email, nome, false, id),'gpExterior');
			}
		}
	}
	//alert(lBox.innerHTML);
}

function vFale(form, lingua){
	var vMsg = '';
	var nome = form.txtNome.value;
	var uf = form.cbUF.value;
	var estado = form.txtEstado.value;
	var pais = form.txtPais.value;
	var email = form.txtEmail.value;
	var fone = form.txtFone.value;
	var msg = form.txtMsg.value;
	var cidade = form.txtCidade.value;
	
	var ind = 0;
	if(lingua == "_en"){
		ind = 1;
	}else if(lingua == "_es"){
		ind = 2;
	}
	
	var msgs = new Array(new Array(' - Digite o nome\n',
								   ' - Digite a cidade\n',
								   ' - Digite o país\n',
								   ' - Digite o telefone\n',
								   ' - Escolha o estado\n',
								   ' - Digite o email\n',
								   ' - E-mail Inválido\n',
								   ' - Digite a mensagem\n',
								   ' - Digite o estado\n'
								   ),
						 new Array(' - Type your name\n',
								   ' - Type your city\n',
								   ' - Type your country\n',
								   ' - Type your phone number\n',
								   ' - Choose a state\n',
								   ' - Type your email\n',
								   ' - Invalid e-mail\n',
								   ' - Type your message\n',
								   ' - Type your state\n'
								   ),
						 new Array(' - Escribe su nombre\n',
								   ' - Escribe su ciudad\n',
								   ' - Escribe su país\n',
								   ' - Escribe su número de teléfono\n',
								   ' - Escoge un estado\n',
								   ' - Escribe su email\n',
								   ' - El email e inválido\n',
								   ' - Escribe su mensaje\n',
								   ' - Escribe su estado\n'
								   )
						 );
	
	if(nome == ''){
		vMsg += msgs[ind][0];
	}
	
	if(cidade == ''){
		vMsg += msgs[ind][1];
	}
	
	if(pais == ''){
		vMsg += msgs[ind][2];
	}
	
	if(fone == ''){
		vMsg += msgs[ind][3];
	}
	
	if(uf == 'N/A'){
		vMsg += msgs[ind][4];
	}else if(uf == 'fora' && estado == ''){
		vMsg += msgs[ind][8];
	}
	
	if(email == ''){
		vMsg += msgs[ind][5];
	}else{
		var arroba = false;
		var ponto = false;
		var caracter = false;
		for(var i=0; i < email.length; i++){
			if(email.charAt(i) == "@") arroba = true;
			if(email.charAt(i) == ".") ponto = true;
			if(email.charAt(i) != " ") caracter = true;
	  }
		if (ponto == false || arroba == false || caracter == false){
			vMsg += msgs[ind][6];
		}			
	}
	
	if(msg == ''){
		vMsg += msgs[ind][7];
	}
	
	if(vMsg == ''){
		return true;
	}else{
		alert(vMsg);
		return false;
	}
}

function enviaFale(lingua){
	document.getElementById('resposta').style.display = 'block';
	var val = new Array("Aguarde, validando...", 
						"Wait, validating...",
						"La espera, validando..."
						);
	var env = new Array("Aguarde, enviando...",
						"Wait, sending...",
						"La espera, mandando...");
	var ind = 0;
	if(lingua == "_en"){
		ind = 1;
	}else if(lingua == "_es"){
		ind = 2;
	}
	var antigo = document.getElementById('resposta').innerHTML;
	document.getElementById('resposta').innerHTML = val[ind];
	var form = document.frmFale;
	if(vFale(form,lingua)){
		document.getElementById('resposta').innerHTML = env[ind];
		processaForm(form,mostraResposta);
		//form.submit();
	}else{
		document.getElementById('resposta').innerHTML = antigo;
	}
}

function enviaSac(lingua){
	document.getElementById('resposta').style.display = 'block';
	var val = new Array("Aguarde, validando...", 
						"Wait, validating...",
						"La espera, validando..."
						);
	var env = new Array("Aguarde, enviando...",
						"Wait, sending...",
						"La espera, mandando...");
	var ind = 0;
	if(lingua == "_en"){
		ind = 1;
	}else if(lingua == "_es"){
		ind = 2;
	}
	var antigo = document.getElementById('resposta').innerHTML;
	document.getElementById('resposta').innerHTML = val[ind];
	var form = document.frmSac;
	if(vFale(form, lingua)){
		document.getElementById('resposta').innerHTML = env[ind];
		processaForm(form,mostraResposta);
	}else{
		document.getElementById('resposta').innerHTML = antigo;
	}
}

function mostraResposta(){
	xml = getRespostaXML();
	if(xml != null){
		resp = xml.getElementsByTagName('resultado')[0].firstChild.data;
		flag = xml.getElementsByTagName('flag')[0].firstChild.data;
		document.getElementById('resposta').innerHTML = resp;
		if(flag == '1'){
			document.getElementById('formDiv').style.display = 'none';
			acertaIFrame();
		}
	}
}

function buscaRepresentante(parTipo,parValor){
	url = "scrRepresentantes.php?parTipo=" + parTipo + "&parValor=" + parValor;
	incluiPagina(url,"reps","<center>Buscando...</center>");
}

function selecionaMail(id){
	window.opener.document.getElementById(id).selected = "selected";
	this.close();
}

function verEstado(estado){
	if(estado == 'fora'){
		document.getElementById('outroEstado').style.display = 'block';
	}else{
		document.getElementById('outroEstado').style.display = 'none';	
	}
}
