window.addEvent('domready', function() {
	if ($("frm_1")){
		var frmCad = new fValidator("frm_1");
		new iMask({   
			onInvalid: function(event, obj) {   
			if(!event.shift) obj.setStyles({"background-color":"#fcc", border:"1px outset #000"});   
			}   
		});
		if ($('btinserir'))	{
		$('btinserir').addEvent('click',function(e){
			event = new Event(e);
			var isValid = true;
			event.stop();
			event.preventDefault();
			event.stopPropagation(); 
			var frmCad = new fValidator("frm_1");
			frmCad.validations.each(function(array) {
				if (!$(array[0].id+"required_msg") && !$(array[0].id+"email_msg")) {
				if(frmCad._isChildType(array[0])) frmCad._validateChild(array[0], array[1]);
				else frmCad._validate(array[0], array[1]);
				if(array[0].cbErr > 0) isValid = false;
				} else { isValid = false; }
			}.bind(frmCad));
			var pagina = frmCad.form.action;
			if(isValid) {
				if (pagina.test('editar','i')) acAviso('Editando dados...');
				else if (pagina.test('ac02','i')) acAviso('Confirmando endereço...');	
				else acAviso('Cadastrando usuário...');	
				$("frm_1").set('send', {
					onComplete: function(response) { 
					//acBgAlert();
					resultado = response.split('=');
					actexto = resultado[0];
					if (String(actexto).substring(0,4) == "erro") {
						acError(actexto,resultado[1]);
					}
					else {
						//acBgAlert();
						if (pagina.test('Editar','i')) {
							acMsg('Edição realizada com sucesso!');
						}
						else if (pagina.test('ac02','i')) location.href = "pedido_passo2.asp";	
						else if (response.test('ok','i')) location.href = "index.asp";
					}
				}});
				$("frm_1").send();
			} else { $(document.body).scrollTo(0,0); }
		});}
	}
});
function acDelImg() {
	if ($('perfil').getProperty('src')!='fotos/sem-foto.jpg') {
	acAviso('Deleting photo...');
		var ajax = new Request({url:'imagem.asp?foto=foto&src=perfil&setor=deleteperfil',
			onSuccess: function(html) {	
				acBgAlert();
				if (html.contains('erro')) {
					acError(html,html);
				} else {
					$('perfil').setProperty('src','fotos/sem-foto.jpg')
					$('yexy').set('html','<a href="edAc">add photo</a>');
					$('yexy').getElements('a[href^=edAc]').addEvent('click',function(e){
						e.stop();
						acBoxImg('foto','perfil','perfil','');
					});

				}
			},
			onFailure: function(html) {acError('erro100','');}		
		}).send();
	}
}
function acBoxImg(foto,src,setor,arquivo) {
	acBgAlert();
	var acBord = new Element('img', {'src':'img/brd-top.gif', 'id':'brdAlert1'})
	.setStyles({height: 10,width: 300,position: 'absolute','z-index': 1000,top: (window.getCoordinates().bottom/2)-50,left: (window.getCoordinates().right/2)-150})
	.injectInside(document.body);
	var acBord2 = new Element('img', {'src':'img/brd-bot.gif', 'id':'brdAlert2'})
	.setStyles({height: 10,width: 300,position: 'absolute','z-index': 1000,top: (window.getCoordinates().bottom/2)+55,left: (window.getCoordinates().right/2)-150})
	.injectInside(document.body);
	var acBt = new Element('div', {'id':'bts'})
	.set("html","<center><img src='img/fechar.png' id='btsim' style='cursor: pointer;' /></center>")
	.setStyles({height: 20,width: 250,position: 'absolute','z-index': 1000,top: (window.getCoordinates().bottom/2)+75,left: (window.getCoordinates().right/2)-125})
	.injectInside(document.body);
	$('btsim').addEvent('click',function(){
		acBtSim();
	});
	var acBoxMsg = new IFrame({
	src: 'imagem.asp?foto='+foto+'&src='+src+'&setor='+setor+'&arquivo='+arquivo,
			id: 'aciframe',
			styles: {
				width: 300,
				height: 100,
				opacity: 1,
				border: 'none',
				background: '#fff',
				position: 'absolute',
				'z-index': 1000,
				align: 'center',
				top: (window.getCoordinates().bottom/2)-45,
				left: (window.getCoordinates().right/2)-150
			}
		})
		.injectInside(document.body);
}
function acBtSim(setor) {
	acBgAlert();
	$('btsim').destroy();
	$('brdAlert1').destroy();
	$('brdAlert2').destroy();
	$('aciframe').destroy();
	if (setor == 'editperfil') {
		$('yexy').set('html','photo: <a href="edAc">edit</a> | <a href="delAc">remove</a>');
		$('addfoto').getElements('a[href^=edAc]').addEvent('click',function(e){e.stop();acBoxImg('foto','perfil','editperfil','');});
		$('addfoto').getElements('a[href^=delAc]').addEvent('click',function(e){e.stop();acDelImg();});
	}
}