var i = 0;
var flag = 0;

function menupull(n){
	if(flag == 0){
		document.getElementById(n).style.display="inline";
		document.getElementById("menu").style.zIndex="1";
		flag = 1;
	}else{
		document.getElementById(n).style.display="none";
		document.getElementById("menu").style.zIndex="0";
		flag = 0;
	}

}


function embeds(url,w,h){
document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+w+'" height="'+h+'">');
document.write('<param name="movie" value="'+url+'" />');
document.write('<param name="quality" value="high" />');
document.write('<param name="wmode" value="transparent" />');
document.write('<param name="menu" value="false" />');
document.write('<embed src="'+url+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'" wmode="transparent"></embed>');
document.write('</object>');
}

function embeds8(url,w,h){
document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+w+'" height="'+h+'">');
document.write('<param name="allowScriptAccess" value="sameDomain" />');
document.write('<param name="movie" value="'+url+'" />');
document.write('<param name="quality" value="high" />');
document.write('<param name="bgcolor" value="#ffffff" />');
document.write('<embed src="'+url+'" quality="high" width="'+w+'" height="'+h+'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
document.write('</object>');
}


// Utilizado para evitar de digitar: document.getElementById toda hora, tornando o processo mais prático
function gE(ID) {
	return document.getElementById(ID);
}

// Utilizado para evitar de digitar: document.getElementsByTagName toda hora, tornando o processo mais prático
function gEs(tag) {
	return document.getElementsByTagName(tag);
}

// Esta função instancia o objeto XMLHttpRequest
function openAjax() {
	var ajax;
	try {
		ajax = new XMLHttpRequest();
	} catch(ee) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				ajax = false;
			}
		}
	}
	return ajax;
}

//menu esquerda produto
flag=0;
function show_hide(id,aberto){
	if (document.getElementById("listaaplicacoes" + id) != null)
	{
		if(flag==0){
			if (document.getElementById("listaaplicacoes" + id).style.display == 'block' || aberto =='s'){	
				document.getElementById("listaaplicacoes" + id).style.display = 'none';
				flag = 1;
			}else{
				document.getElementById("listaaplicacoes" + id).style.display = 'block';	
			}
		}else{
			if (document.getElementById("listaaplicacoes" + id).style.display == 'block'){	
				document.getElementById("listaaplicacoes" + id).style.display = 'none';
			}else{
				document.getElementById("listaaplicacoes" + id).style.display = 'block';	
			}	
		}
	}	
}
//Espanhol e Ingles
var legflag = 0;
function legenda(lingua){
	if(legflag == 0){
		gE(lingua).style.display = 'block';
		legflag = 1;
	}else{
		gE(lingua).style.display = 'none';
		legflag = 0;
	}

}

// Chama a função loadFunctions ao carregar a página
window.onload = loadFunctions;

// Função que chama outras funções
function loadFunctions() {
	abrir();
}
//Album novidades
function abrir(){
	var linksBtn = gEs('a');
	for (var x = 0; x < linksBtn.length; x++) {
		var linkBtn = linksBtn[x];
		// Cria uma vairável - atributoRel - com o valor do atributo rel do link
		var atributoRel = new String(linkBtn.getAttribute('rel'));
		if (atributoRel.substring(0,5) == 'album') {
			// Ao clicar no botão será realizada uma ação
			linkBtn.onclick = function() {
			var ID = this.getAttribute('rel').split('-')[1];
			// Executa a função que cria o fundo sobre página
			exibirBgBody();
			// Cria um div - definida como boxCad - que armazenará o formulário de cadastro
			boxCad();			
			var ajax = openAjax();
			var recipiente = gE('boxCad');
			//ajax.open('GET', 'album.php?ajax=true&n='+gE('ID').value+'', true); 
			ajax.open('GET', 'album.php?ajax=true&n=' + ID, true); 
				ajax.onreadystatechange = function() {
					if (ajax.readyState == 1) {
						// Cria o efeito de loading
						loading(true);	
					} // if->readyState->1
					if (ajax.readyState == 4) {
						if (ajax.status == 200) {
							// Remove o efeito de loading
							loading(false);
							recipiente.innerHTML = ajax.responseText;
						} // if-status->200
					} // if->readyState->4
				} // ajax->onreadystatechange
				// Envia a requisição
				ajax.send(null);
				// Evita o reload da página
				return false;
			}
		}
	}
}
function fechar(){
		removerDivs();
}

//Troca Foto
function trocafoto(foto,descricao){
		gE('fotomaior').innerHTML = "<img src='images/novidades/album/"+foto+"' /><br />"+descricao;
}

// Esta função elimina da página o fundo criado sobre o body e o boxCad;
function removerDivs() {
	var bgBody = gE('bgBody');
	var boxCad = gE('boxCad');
	bgBody.parentNode.removeChild(bgBody);
	if (boxCad) { // Por que ao clicar X (para deletar um registro) cria-se somente o encobridor e não o boxCad	
		boxCad.parentNode.removeChild(boxCad);
	}
}

// Validacao do formulario fale conosco e como comprar
// qualformulario = 1 Como comprar | 2 Fale Conosco
function validar(qualformulario){ 
	var flag = 0;
	var errors="";
		if(qualformulario == 1){ // Como Comprar
			if(gE('vis_nome').value == ''){
				gE('vis_nome').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Nome</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_nome').style.backgroundColor = '#FFFFFF';
			}
			if(gE('email').value == ''){
				gE('email').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>E-mail</span> não foi preenchido.</li>';
				topopg();
			}else{
				var checkemail = Verifica_Email('email', '');
				if(checkemail == ""){
					flag = flag+1;
					gE('email').style.backgroundColor = '#FFFFFF';
				}else{
					gE('email').style.backgroundColor = '#FDDBDC';
					errors += '<li><span class=\'destaque\'>'+ checkemail +'</span></li>';
					topopg();
				}
			}
			if(gE('ddd').value == ''){
				gE('ddd').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>DDD</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('ddd').style.backgroundColor = '#FFFFFF';
			}
			if(gE('telefone').value == ''){
				gE('telefone').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Telefone</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('telefone').style.backgroundColor = '#FFFFFF';
			}
			if(gE('mensagem').value == ''){
				gE('mensagem').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Mensagem</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('mensagem').style.backgroundColor = '#FFFFFF';
			}
			
			//verificacao do formulario Como comprar
			verificafinal(5, flag, errors);
			
		}
		if(qualformulario == 2){ // Fale Conosco
			if(gE('vis_nome').value == ''){
				gE('vis_nome').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Nome</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_nome').style.backgroundColor = '#FFFFFF';
			}
			if(gE('email').value == ''){
				gE('email').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>E-mail</span> não foi preenchido.</li>';
				topopg();
			}else{
				var checkemail = Verifica_Email('email', '');
				if(checkemail == ""){
					flag = flag+1;
					gE('email').style.backgroundColor = '#FFFFFF';
				}else{
					gE('email').style.backgroundColor = '#FDDBDC';
					errors += '<li><span class=\'destaque\'>'+ checkemail +'</span></li>';
					topopg();
				}
			}
			if(gE('ddd').value == ''){
				gE('ddd').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>DDD</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('ddd').style.backgroundColor = '#FFFFFF';
			}
			if(gE('telefone').value == ''){
				gE('telefone').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Telefone</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('telefone').style.backgroundColor = '#FFFFFF';
			}
			if(gE('mensagem').value == ''){
				gE('mensagem').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Mensagem</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('mensagem').style.backgroundColor = '#FFFFFF';
			}
			
			//verificacao do formulario Como comprar
			verificafinal(5, flag, errors);
		}
		if(qualformulario == 3){ // Novidades
			if(gE('vis_nome').value == ''){
				gE('vis_nome').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Nome</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_nome').style.backgroundColor = '#FFFFFF';
			}
			if(gE('vis_empresa').value == ''){
				gE('vis_empresa').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Empresa</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_empresa').style.backgroundColor = '#FFFFFF';
			}
			if(gE('vis_ramo').value == ''){
				gE('vis_ramo').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Ramo de Atividade</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_ramo').style.backgroundColor = '#FFFFFF';
			}
			if(gE('vis_cidade').value == ''){
				gE('vis_cidade').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Cidade</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_cidade').style.backgroundColor = '#FFFFFF';
			}
			if(gE('vis_estado').value == 'XX'){
				gE('vis_estado').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Estado</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_estado').style.backgroundColor = '#FFFFFF';
			}
			if(gE('email').value == ''){
				gE('email').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>E-mail</span> não foi preenchido.</li>';
				topopg();
			}else{
				var checkemail = Verifica_Email('email', '');
				if(checkemail == ""){
					flag = flag+1;
					gE('email').style.backgroundColor = '#FFFFFF';
				}else{
					gE('email').style.backgroundColor = '#FDDBDC';
					errors += '<li><span class=\'destaque\'>'+ checkemail +'</span></li>';
					topopg();
				}
			}
			
			//verificacao do formulario Novidades
			verificafinal(6, flag, errors);
		}
		if(qualformulario == 4){ // Assistencia
			if(gE('vis_nome').value == ''){
				gE('vis_nome').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Nome</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_nome').style.backgroundColor = '#FFFFFF';
			}
			if(gE('vis_empresa').value == ''){
				gE('vis_empresa').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Empresa</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_empresa').style.backgroundColor = '#FFFFFF';
			}
			if(gE('vis_ramo').value == ''){
				gE('vis_ramo').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Ramo de Atividade</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_ramo').style.backgroundColor = '#FFFFFF';
			}
			if(gE('vis_cidade').value == ''){
				gE('vis_cidade').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Cidade</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_cidade').style.backgroundColor = '#FFFFFF';
			}
			if(gE('vis_estado').value == 'XX'){
				gE('vis_estado').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Estado</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('vis_estado').style.backgroundColor = '#FFFFFF';
			}
			if(gE('email').value == ''){
				gE('email').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>E-mail</span> não foi preenchido.</li>';
				topopg();
			}else{
				var checkemail = Verifica_Email('email', '');
				if(checkemail == ""){
					flag = flag+1;
					gE('email').style.backgroundColor = '#FFFFFF';
				}else{
					gE('email').style.backgroundColor = '#FDDBDC';
					errors += '<li><span class=\'destaque\'>'+ checkemail +'</span></li>';
					topopg();
				}
			}
			if(gE('ddd').value == ''){
				gE('ddd').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>DDD</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('ddd').style.backgroundColor = '#FFFFFF';
			}
			if(gE('telefone').value == ''){
				gE('telefone').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Telefone</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('telefone').style.backgroundColor = '#FFFFFF';
			}
			if(gE('mensagem').value == ''){
				gE('mensagem').style.backgroundColor = '#FDDBDC';
				errors += '<li>O campo <span class=\'destaque\'>Mensagem</span> não foi preenchido.</li>';
				topopg();
			}else{
				flag = flag+1;
				gE('mensagem').style.backgroundColor = '#FFFFFF';
			}
			
			//verificacao do formulario Assistencia
			verificafinal(9, flag, errors);
		}

}

function verificafinal(qtdaflag, flag, errors){
	if(flag == qtdaflag){
		gE('listaerro').style.display = 'none';
		document.formulario1.submit();
		alert("Enviar a mensagem.");
	}else{
		gE('listaerro').innerHTML = errors;
		gE('listaerro').style.display = 'block';
	}	
}

/* Ir para topo*/
function topopg()
{
	window.scrollBy(0,-10000);
}

//Se o parâmetro obrigatório for igual à zero, significa que elepode estar vazio, caso contrário, não 
function Verifica_Email(email, obrigatorio){
 	var errors ='';
 	var email = document.getElementById(email);
 	if((obrigatorio == 1) || (obrigatorio == 0 && email.value != "")){
 		//if( !email.value.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+)/gi) ){
 		
 		if(!email.value.match(/^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/)){

 			errors += "Informe um e-mail válido - Exemplo: usuario@dominio.com";
			document.getElementById("email").style.backgroundColor = '#FDDBDC';
 			//email.focus();
 			return errors;
 		}
		document.getElementById("email").style.backgroundColor = '#FFFFFF';
		return errors;
 	}
 }
 
function Verifica_Url(url, obrigatorio){
 
 	var url = document.getElementById(url);
 	if((obrigatorio == 1) || (obrigatorio == 0 && url.value != "")){
 		
 		if(!url.value.match(/http[s]?:\/\/\w[\.\w]+\w[\.\w]+\w[\w]$/)){

 			alert("Formato de Url inválida. Exemplo: http://dominio.com");
 			url.focus();
 			return false
 		}
 	}
 }
 
function Verificacep(cep){
	var cep = document.getElementById(cep).value;
	
		//alert(cep);
		cep = cep.replace(/-/,"");
		cep = cep.replace(/,/,"");
		cep = cep.replace(/\./,"");
		
			if (isNaN(cep)){
					alert(cep+ ' não é um formato válido. Exemplo: 12345678 ou 12345-678');
				}else{
					if(cep.length!=8){
						alert(cep+ ' não é um formato válido. Exemplo: 12345678 ou 12345-678');	
						}else{
							document.getElementById("cep").value = cep;	
						} 
				}
			
		
		return true;
}

// Cria a div denominada como boxCad, a qual conterá o album
function boxCad() {
	// Cria um 'container' que comportará o album
	var objBody = gEs('body').item(0);
	var sizesPage = getPageSize();
	var boxCad = document.createElement('div');
	boxCad.setAttribute('id','boxCad');
	var wPage = arrayPageSize[0]; // Largura total da página
	var hPage = arrayPageSize[1]; // tamanho total da página
	boxCad.style.height = (wPage / 2) + 'px'; // metada da altura da página
	boxCad.style.marginTop = -(wPage / 4) + 'px'; // 1 quarto da largura
	objBody.insertBefore(boxCad, objBody.lastChild);
}

// Utilizado para criar o fundo sobre a página (wiewport), body.
function exibirBgBody() {
	// Seleciona a tag body. item(0) por que só existe uma tag body
	var tagBody = gEs('body').item(0);
	// Pega os tamanhos atuais da página, como largura, altura, ...
	var sizesPage = getPageSize();
	// Vamos criar uma tag div
	var bgBody = document.createElement('div');
	// Setar o atributo ID a div criada
	bgBody.setAttribute('id','bgBody');
	// Essa div terá o tamanho exato da página
	bgBody.style.height = arrayPageSize[1] + 'px';
	// Essa div terá a largura exata da página
	bgBody.style.width = arrayPageSize[0] + 'px';
	// Evita criar a div novamente
	if (!gE('bgBody')) {
		tagBody.insertBefore(bgBody, tagBody.firstChild);
	}	
}

// Utilizado para criar o efeito de loading
function loading(opt) {
	if (opt == true) {
		// A tag que receberá a img de loading
		var refer = gE('bgBody');
		// O tamanho da referida tag
		var referHeight = refer.offsetHeight;
		// Dizemos que os elementos dentro dela será alinhado ao centro
		refer.style.textAlign = 'center';
		// Criamos uma imagem, img.
		var img = document.createElement('img');
		// Informamos o caminho da img
		img.setAttribute('src','images/loading.gif');
		// Setamos um atributo ID na img criada
		img.setAttribute('id','loading');
		// Definimos seu tamanho
		img.setAttribute('width','30');
		// Dizemos que o margin-top será a metada do tamanho da div
		img.style.marginTop = (referHeight /2) + 'px';
		// Evita que seja criada duas ou mais img de loading
		if (!document.getElementById('loading')) {
			// Insere a img na tag informada na variável refer
			refer.insertBefore(img, refer.firstChild);
		}
	} else if (opt == false) {
		// Referenciamos a img de login através de seu ID
		var imgLoading = gE('loading');
		// Removemos a img de loading
		if (imgLoading) {
			imgLoading.parentNode.removeChild(imgLoading);
		}
	}
}

/* Funções de terceiros */
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 

}