// Função acionada depois que todas as imagens são lidas
functionLoader = function() {
	inicializarPatrocinadores();
	inicializarPublicidade();
	FS.create();
};
// Funções da página
setPageEvents = function () {
	
};

/* Eventos da página */
window.onstopscroll = function() {
	// Posiciona o Alerta, quando aberto
	EMT.alinha();
	SA.refresh();
};

window.onload = function() {
	// Posiciona a seta e cria a "tween" para a mesma
	new tween('setaBusca');
	var newLeft = getPos('buscaProfdeSaude').x+((getSize('buscaProfdeSaude').x-13)/2);
	with ($('setaBusca').style) {
		top = topSetaBusca+'px';
		left = newLeft+'px';
		display = 'block';
	}
	// Seta os eventos
	setAllEvents();
	// Inicia o preloader das imagens
	loadNextImage();
	// ALPHA
	new alpha('molduraImagem');
	new alpha('fundoBranco');
	// Cria o evento da moldura
	document.onmousemove = function(e) {
		e = e || event;
		stopMoldura(e);
	};
	molduraAtiva = true;
	// Inicia o preloader das imagens
	loadSiteImages();
	changeComunidade();
	
};
window.onresize = function() {
	// Posiciona a seta em baixo do item atual
	var newLeft = getPos(arrBusca[btBuscaAtivo]).x+((getSize(arrBusca[btBuscaAtivo]).x-13)/2);
	$('setaBusca').stopTween();
	with ($('setaBusca').style) {
		top = topSetaBusca+'px';
		left = newLeft+'px';
		display = 'block';
	}
	// Posiciona o fundo branco e a janela de busca avançada
	if (tipoBusca == 2) {
		posicionaFundoBranco();
	}
	// Posiciona o Alerta, quando aberto
	EMT.alinha();
	SA.refresh();
};

validaCadastro = function() {
	/*if (radio('comunidade') == 0) {
		SA.open('No momento estamos disponibilizando cadastros apenas para profissionais de sa&uacute;de e pacientes!');
		return;
	}*/
	if (!$('email').value.isMail()) {
		formError('email', 'Voc&ecirc; deve digitar um e-mail v&aacute;lido!');
		return;
	}
	if (radio('comunidade') == 0) {
		if (!validaCnpj(filter2($('cpf').value,'0123456789'))) {
			formError('cpf', 'Voc&ecirc; deve digitar um cnpj v&aacute;lido!');
			return;
		}
	} else {
		if (!$('cpf').value.isCPF()) {
			formError('cpf', 'Voc&ecirc; deve digitar um cpf v&aacute;lido!');
			return;
		}
	}
	if (!radio('comunidade')) {
		formError('comunidade1', 'Voc&ecirc; deve escolher a comunidade!');
		return;
	}
	if (filter2($('cep').value,'0123456789').length != 8) {
		formError('cep', 'Voc&ecirc; deve digitar um cep v&aacute;lido!');
		return;
	}
	//if (radio('comunidade') != 0) {
		//EMT.show();
	//} else {
		$('form').submit();
	//}
};

validaLogin = function() {
	if (!$('email').value.isMail()) {
		formError('email', 'Voc&ecirc; deve digitar um e-mail v&aacute;lido!');
		return;
	}
	if ($('senha').value.length != 8) {
		formError('senha', 'Voc&ecirc; deve digitar a senha!');
		return;
	}
};

gerarImagem = function() {
	$('imgCode').src = 'imagens/blank.gif';
	if (radio('comunidade') == 0) {
	} else {
		$('imgCode').src = 'cpf/imagem.php?n='+nocache();
	}
	$('carac').value = '';
	focusTo('carac');
};

var maskCpfCnpj = '###.###.###-##';

changeComunidade = function() {
	try{
		if (radio('comunidade') == 0) {
			maskCpfCnpj = '##.###.###/####-##';
			$('titCpfCnpj').innerHTML = 'CNPJ';
			$('cpf').setAttribute('maxlength', 18);
			$('cpf').maxLength = 18;			
			$('TPessoa').innerHTML = '<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center">'+
				                	 '	<tr align="center">'+ 
				                  	 '		<td style="width:50%;text-align:center;height:24px;vertical-align:middle;"><input style="margin-left:14px;width:16px;height:16px;" type="radio" name="tipPessoa" id="tipPessoa0" value="0" onclick="changeTPessoa();" /></td>'+
				                  	 '		<td style="width:50%;text-align:center;height:24px;vertical-align:middle;"><input style="margin-left:14px;width:16px;height:16px;" type="radio" name="tipPessoa" id="tipPessoa1" value="0" onclick="changeTPessoa();" checked="checked";/></td>'+
				                	 '	</tr>'+
				                	 '	<tr align="center">'+ 
				                  	 '		<td style="width:50%;text-align:center;height:24px;vertical-align:middle;color:#AA6F4A">Pessoa F&iacute;sica</td>'+
				                  	 '		<td style="width:50%;text-align:center;height:24px;vertical-align:middle;color:#AA6F4A">Pessoa  Jur&iacute;dica</td>'+
				                	 '	</tr>'+
				              		 '</table>';			
			$('SelImagem').innerHTML = '<img alt="" src="imagens/loginUsuarioSelecioneCadastro1.gif" />';			
			$('ImgSelect').style.backgroundImage = 'url(imagens/loginUsuarioFundoNaoSouCadastrado1.gif)';
		} else {
			maskCpfCnpj = '###.###.###-##';
			$('titCpfCnpj').innerHTML = 'CPF';
			$('cpf').setAttribute('maxlength', 14);
			$('cpf').maxLength = 14;			
			$('TPessoa').innerHTML = '';			
			$('SelImagem').innerHTML = '<img alt="" src="imagens/loginUsuarioSelecioneCadastro.gif" />';			
			$('ImgSelect').style.backgroundImage = 'url(imagens/loginUsuarioFundoNaoSouCadastrado.gif)';
		}
		$('cpf').value = '';
		focusTo('cpf');
	}catch(e){}
};

changeTPessoa = function() {
	try{
		if ($('tipPessoa1').checked) {
			maskCpfCnpj = '##.###.###/####-##';
			$('titCpfCnpj').innerHTML = 'CNPJ';
			$('cpf').setAttribute('maxlength', 18);
			$('cpf').maxLength = 18;
		} else if ($('tipPessoa0').checked) {
			maskCpfCnpj = '###.###.###-##';
			$('titCpfCnpj').innerHTML = 'CPF';
			$('cpf').setAttribute('maxlength', 14);
			$('cpf').maxLength = 14;
		}		
		$('cpf').value = '';
		focusTo('cpf');
	}catch(e){}
};
/*
var EMT = {
	'tweencreate': 0,
	'aberto': 0,
	'podeClicar': 1,
	'show': function() {
		$('confirmarCPF').style.display = "block";	
		if(EMT.tweencreate == 0){
			new tween('confirmarCPF');
			EMT.tweencreate = 1;
		}
		EMT.aberto = 1;
		EMT.alinha();
		bgSite.show();	
		gerarImagem();
	},
	'alinha': function(){
		if(EMT.aberto == 0)return;
		var SC = getScroll();
		var VS = getDocVisibleSize();
		var SZ = getSize('confirmarCPF');
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-SZ.y)/2;	
		$('confirmarCPF').tweenTo((newY+SC.y), (newX+SC.x), 'easeoutexpo', 0.3);		
	},
	'hide': function(){
		EMT.aberto = 0;
		var SC = getScroll();
		var VS = getDocVisibleSize();
		var SZ = getSize('confirmarCPF');	
		
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-SZ.y)/2;			
		$('confirmarCPF').tweenTo(-((newY+SC.y)+50), -((newX+SC.x)+50), 'easeoutexpo', 1,function(){
			$('confirmarCPF').style.top = "-1000px";
			$('confirmarCPF').style.lef = "-1000px";		
			$('confirmarCPF').style.display = "none";	
			EMT.aberto = 0;
			EMT.btOK = ''
		});	
		bgSite.hide();	
	},
	'onClick':function(){
		if (EMT.podeClicar == 0) return;
		if($('carac').value.length != 4){
			SA.onClick = function(){
				focusTo('carac');
			};
			SA.open('Digite os 4 caracteres!');
			return false;
		}
		var xml = new xmlConnection();
		if (xml.create()) {
			xml.setMethod('POST');
			xml.add('carac',$('carac').value);
			if (radio('comunidade') == 0) {
				//xml.add('cpf',filter2($('cpf').value,'0123456789'));
				//xml.setURL('cnpj/cpfXML.php');
				//xml.setChildName('razao');
				//xml.onComplete = function() {
					//if (this.getDataByName(0) != '') {
						//SA.onClick = function(){};
						//$('nome').value = this.getDataByName(0);
						//$('form').submit();
						//EMT.hide();
					//} else {
						//SA.onClose = function(){
							//setAlpha('confirmarCPF.envia', 100);
							//setAlpha('confirmarCPF.fecha', 100);
							//EMT.podeClicar = 1;
							//gerarImagem();
						//};
						//SA.open('Os caracteres informados estão incorretos!');
					//}
				//}; 
			} else {
				xml.add('cpf',$('cpf').value);
				xml.setURL('cpf/cpfXML.php');
				xml.setChildName('erro');
				xml.onComplete = function() {
					if (this.getDataByName(0) == 1) {
						SA.onClick = function(){};
						this.setChildName('nome');
						$('nome').value = this.getDataByName(0);
						$('form').submit();
						EMT.hide();
					} else if (this.getDataByName(0) == 2) {
						SA.onClose = function(){
							setAlpha('confirmarCPF.envia', 100);
							setAlpha('confirmarCPF.fecha', 100);
							EMT.podeClicar = 1;
							EMT.hide();
						};
						SA.open('Sistema indisponível das 3h às 7h.');
					} else {
						SA.onClose = function(){
							setAlpha('confirmarCPF.envia', 100);
							setAlpha('confirmarCPF.fecha', 100);
							EMT.podeClicar = 1;
							gerarImagem();
						};
						SA.open('Os caracteres informados est&atilde;o incorretos!');
					}
				};
			}
			xml.execute();
			setAlpha('confirmarCPF.envia', 50);
			setAlpha('confirmarCPF.fecha', 50);
			EMT.podeClicar = 0;
		}
	}
}*/

validaCnpj = function(cnpj) {
	var i;
	var c = cnpj.substr(0,12);
	var dv = cnpj.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++) d1 += c.charAt(11-i)*(2+(i % 8));
	if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1) return false;
	d1 *= 2;
	for (i = 0; i < 12; i++) d1 += c.charAt(11-i)*(2+((i+1) % 8));
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1) return false;
	return true;
};

login2 = function(){
	if($('usuario').value.length == 0){
		SA.onClick = function(){
			$('usuario').focus();
		}
		SA.open('Digite seu login!');
		return false;
	}
	if($('senha').value.length == 0){
		SA.onClick = function(){
			$('senha').focus();
		}
		SA.open('Digite sua senha!');
		return false;
	}	
	if($('senha').value.length < 6){
		SA.onClick = function(){
			$('senha').focus();
		}
		SA.open('Sua senha cont&eacute;m menos que 6 caracteres!');
		return false;
	}		
	var xml = new xmlConnection();
	if (xml.create()) {
		xml.setMethod('POST');
		xml.setURL('loginXML.php');
		xml.add('login',$('usuario').value);
		xml.add('senha',$('senha').value);
		xml.setChildName('l');
		xml.onComplete = function() {	
			if(this.getAttByName('status',0) == 0){
				SA.open("Usu&aacute;rio ou senha incorreto!");
			}else if(this.getAttByName('status',0) == 2){
				SA.open("Voc&ecirc; n&atilde;o ativou sua conta por e-mail!");
			}else if(this.getAttByName('status',0) == 1){
				location = arrPaineis[this.getAttByName('redir',0)];
			}
			$("Login.bt").src = "imagens/loginOk.gif";
		};
		$("Login.bt").src = "imagens/botaoOKcarregando.gif";
		xml.execute();
	}
};