window.addEvent('domready', function() {
	var local = location.hash;
	if (local) acMostra(location.hash.replace('#',''));
});
function acMsg(tit,msg) {
	acBgAlert();
	var acBoxMsg = new Element('div', {'id':'acalertbox'})
		.set('html','<h1>'+tit+'</h1><div class="alertMsg">'+msg+'</div><br/><input type="button" id="acalertsim" value="Confirmar" />')
		.setStyles({
			width: 400,
			align: 'center',
			position: 'fixed',
			'z-index': '1000',
			opacity: 0,
			color: '#000',
			left: (window.getCoordinates().right/2)-205
		})
		.injectInside(document.body);
		$('acalertbox').setStyle('top',(window.getCoordinates().bottom/2)-($('acalertbox').getSize().y/2)-5)
		var myFx = new Fx.Tween('acalertbox');
		myFx.start('opacity', '0', '1');
		if (!$('acalert')) acBgAlert();
		$('acalertsim').addEvent('click',function(){acBgAlert();});
		$('acalert').addEvent('click',function(){acBgAlert()});
}

function acMostra(id) {$$("#conteudo div").setStyle('display','none');$(id).setStyle('display','block');}
function acEnviaEmail(e) {
	var event = new Event(e);
	var frm = new acForm("frm_contato",true,"#434343","#FF0000");
	if (frm.error) {
		event.stop();
		event.preventDefault();
		event.stopPropagation(); 
	}
}

var acForm = new Class({
	initialize: function(form,msg,cor01,cor02){
		if ($('erroForm')) $('erroForm').destroy();
		if ($('erroForm2')) $('erroForm2').destroy();
		if ($('erroForm3')) $('erroForm3').destroy();
		var error = false;
		var status;
		$(form).getElements('*[class*=acObg]').each(function(el){
			status = true;
			if (el.getProperty('type') == 'checkbox' && !el.getProperty('checked')) status = false;
			else if (!el.get('value')) status = false;
			else if(el.getProperty('acTipo') == 'email' && !el.get('value').match(/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/gi)){
				if (msg) {
					var acBoxMsg = new Element('div', {'id':'erroForm2'})
					.set("html","E-mail inválido. Favor tentar mais uma vez.")
					.inject($(form),'before');
				}
				status = false;
				this.error = true;
			}
			else if(el.getProperty('acTipo') == 'confirma_senha' && $('senha').get('value') != el.get('value')){
				var acBoxMsg = new Element('div', {'id':'erroForm2'})
				.set("html","As senhas digitadas não conferem. Favor tentar mais uma vez.")
				.inject($(form),'before');
				status = false;
				this.error = true;
				$(document.body).scrollTo(0,0);
			}
			else if (el.getProperty('acTipo') == 'cpf'){
				 var cpf = el.get('value');
				 var cpferro = false;
                 if (cpf.length < 11) cpferro = true;
                 else {
					 var nonNumbers = /\D/;
		             if (nonNumbers.test(cpf)) cpferro = true;
			         if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") cpferro = true;
				}
				if (!cpferro) {
					var a = [];
					var b = new Number;
					var c = 11;
					for (i=0; i<11; i++){
						a[i] = cpf.charAt(i);
						if (i < 9) b += (a[i] * --c);
		             }
			         if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
					 b = 0;
		             c = 11;
				     for (y=0; y<10; y++) b += (a[y] * c--);
		             if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
				     if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
                       cpferro = true;
		             }
				} 
               if (cpferro) { 
					 var acBoxMsg = new Element('div', {'id':'erroForm3'})
					.set("html","CPF inválido. Favor tentar mais uma vez.")
					.inject($(form),'before');
					status = false;
					this.error = true;
				}
			}
			if (!status) {
				error = true;
				if (el.getPrevious()) el.getPrevious().setStyle('color',cor02);
				else if (el.getNext()) el.getNext().setStyle('color',cor02);
			} else {
				if (el.getPrevious()) el.getPrevious().setStyle('color',cor01);
				else if (el.getNext()) el.getNext().setStyle('color',cor01);
				el.set('value',el.get('value').clean());
			}
		});
		if (error) {
			if (msg) {
				$(document.body).scrollTo(0,0);
				var acBoxMsg = new Element('div', {'id':'erroForm'})
				.set("html","Por favor, complete os dados que faltam.")
				.inject($(form),'before');
			}
			this.error = true;
		} 
		else this.error = false;
	}
});

function acAviso(msg) {
	acBgAlert();
	var acBoxMsg = new Element('div', {'id':'acalertbox'})
		.set("html",msg+"<br/><img src='imagens/load.gif' />")
		.setStyles({
			height: 60,
			width: 400,
			align: 'center',
			position: 'absolute',
			opacity: 1,
			color: '#000',
			top: (window.getCoordinates().bottom/2)-35,
			left: (window.getCoordinates().right/2)-205
		})
		.injectInside(document.body);
}
function acBgAlert() {
	if (Browser.Engine.name == 'trident' && Browser.Engine.version < 5) ie6 = true;
	else ie6 = false;
	
	var formtags = $$('select','textarea');
	if ($('acalert')) {
		$(document.body).getElements('*[id^=acalert]').destroy();
		if (ie6 && formtags) formtags.setStyle('visibility', 'visible');
	}
	else {
		if (formtags.length > 0 && ie6) formtags.setStyle('visibility', 'hidden');
		var acBox = new Element('div', {"id":"acalert"})
			.setStyles({
				opacity: 0.8,
				background: '#fff',
				position: 'fixed',
				top: 0,
				left: 0,
				'z-index': '800',
				width: window.getScrollSize().x,
				height: window.getScrollSize().y
			})
			.injectInside(document.body);
	}
}
function acPop(acurl,x,y) {if ($('aciframe')) $('aciframe').destroy();acBgAlert();var acBoxMsg = new IFrame({src: acurl,id: 'aciframe',styles: {width: x,height: y,opacity: 1,border: '5px solid #BBCCFF',background: '#fff',position: 'fixed',align: 'center','z-index': 1000,top: (window.getCoordinates().bottom/2) - (y/2),left: (window.getCoordinates().right/2)-(x/2)}}).injectInside(document.body);}
function acBtClose() {
	if ($('aciframe')) $('aciframe').dispose();
	if ($('acalertbox')) $('acalertbox').dispose();
	if ($('acalert')) acBgAlert();
	if ($('recebe_conteudo')) {
		var query = location.search;
		var local = location.pathname;
		var request = new Request({ 
			url: local+'?acao=atualiza&'+query, 
			onRequest: function() { acAviso('Atualizando dados...');},
			onComplete: function(response) { $('recebe_conteudo').set('html',response); acNovoEditavel(); if ($('acalert')) acBgAlert();} 
		}).send(); 	
	}
}
