function buscar(send, e){
	var form = document.busca;
	if(send){
		if(form.q.value!="" && form.q.value!="buscar") form.submit();
	}else{
		if(form.q.value=="buscar"){ 
			form.q.value="";
		}else{
			if(form.q.value.length==0) 
				form.q.value="buscar";
		}
	}
}

function aumentaTamanhoFonte() {
	var titulo = document.getElementById("titulo");
	var subtitulo = document.getElementById("subtitulo");
	var texto = document.getElementById("texto");
	
	if(titulo.style.fontSize) {
		var s = parseInt(titulo.style.fontSize.replace("px",""));
	} else {
		var s = 18;
	}
	s += 2;
	titulo.style.fontSize = s+"px";
	
	if(subtitulo.style.fontSize) {
		var s = parseInt(subtitulo.style.fontSize.replace("px",""));
	} else {
		var s = 12;
	}
	s += 2;
	subtitulo.style.fontSize = s+"px";
	
	if(texto.style.fontSize) {
		var s = parseInt(texto.style.fontSize.replace("px",""));
	} else {
		var s = 12;
	}
	s += 2;
	texto.style.fontSize = s+"px";
}

function diminuiTamanhoFonte() {
	var titulo = document.getElementById("titulo");
	var subtitulo = document.getElementById("subtitulo");
	var texto = document.getElementById("texto");
	
	if(titulo.style.fontSize) {
		var s = parseInt(titulo.style.fontSize.replace("px",""));
	} else {
		var s = 18;
	}
	s -= 2;
	titulo.style.fontSize = s+"px";
	
	if(subtitulo.style.fontSize) {
		var s = parseInt(subtitulo.style.fontSize.replace("px",""));
	} else {
		var s = 12;
	}
	s -= 2;
	subtitulo.style.fontSize = s+"px";
	
	if(texto.style.fontSize) {
		var s = parseInt(texto.style.fontSize.replace("px",""));
	} else {
		var s = 12;
	}
	s -= 2;
	texto.style.fontSize = s+"px";
}

function cadastraAssociado(form) {
	var url = createQueryStringAllcheck(form);
	url += "&sid=allcheck_intranet&command=associado&action=save";
	alert(url);
	ChamaPaginaArray(url, 'botao');
}

function validarCadastro1() {
	if( document.getElementById("botao").innerHTML == "OK" ) {
		ChamaPaginaArray("ajax/logar.jsp?login=" + document.getElementById('email').value + "&senha=" + document.getElementById('senha').value + "&comprando=true", "botao", "validarCadastro2()");
	}
}

function validarCadastro2() {
	if( document.getElementById('compra').value == "true" ) {
		location.href="carrinhoFinalizado.jsp";
	} else {
		location.href="index.jsp";
	}
}

function atualizaCliente(form) {
	var url = createQueryString(form);
	url += "&sid=Vanitte&command=cliente&action=atualiza";
	ChamaPaginaArray(url, 'valida');
}

function compraCreditos(form) {
	if(validaCampos(form)){	
		var url = createQueryString(form);
		url += "&sid=allcheck_intranet&command=creditos&action=compra_creditos";
		ChamaPaginaArray(url, 'valida', "recarregaMiolo()");
	}
}

function validaCampos(form){
	var ok = true;
	var pass1 = "";
	var pass2 = "";
	var aux = 1;
	for(var i = 0; i < form.length; i++){
		try{
			if(form[i].type != "button" && form[i].type != "submit" && form[i].type != "radio" && form[i].type != "checkbox"){
				if(form[i].value == null || form[i].value == undefined || form[i].value == ""){
					document.getElementById("v" + form[i].name).innerHTML = "Preencha o campo corretamente.";
					ok = false;
				}else{
					document.getElementById("v" + form[i].name).innerHTML = "";
					if(form[i].type == "password" && aux == 1){
						pass1 = form[i].value; aux++;
					}else if(form[i].type == "password"){
						if(pass1 != form[i].value){
							document.getElementById("v" + form[i].name).innerHTML = "As senhas não conferem.";
							ok = false;
						}
					}else if(form[i].name == "email"){
						ok = validaEmail(form[i].value);
						if(ok == false)
							document.getElementById("v" + form[i].name).innerHTML = "Email escrito incorretamente.";
					}
				}
			}
		}catch(e){}
	}
	return ok;
}
function validaEmail(email){
	var atpos=email.indexOf("@");
	var dotpos=email.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
	  return false;
	return true;
}

function recarregaMiolo(){
	ChamaPaginaArray('/allcheck_intranet/aviseme/pagina_associado.jsp','miolo');
}

function salvaComentario(form, rate, id) {
	var url = createQueryString(form, "cadastraComentario.jsp");
	url += "&starId="+rate+"&idProduto="+id;
	ChamaPaginaArray(url, 'mensagem');
}

function ativar() {
    $('#overlay').fadeIn('fast',function(){
        $('#box').animate({'top':'160px'},500);
    });
}
function desativar() {
	$('#box').animate({'top':'-1000px'},500,function(){
		$('#overlay').fadeOut('fast');
	});
}

function aumentaBox() {
	var box = document.getElementById('box');
	box.style.left = '20%';
	box.style.right = '20%';
}
function diminuiBox() {
	var box = document.getElementById('box');
	box.style.left = '30%';
	box.style.right = '30%';
}

function ValidarCPF(Objcpf) {
	var cpf;
	var digito = new Array(11);
	var digito2 = new Array(11);
	var total_numero_d10 = 0;
	var total_numero_d11 = 0;
	var valor1;
	var valor2;

	cpf = Objcpf.value;
//	Retira os pontos (".") e traços ("-") do CPF
	cpf = cpf.replace(".","");
	cpf = cpf.replace(".","");
	cpf = cpf.replace(".","");
	cpf = cpf.replace("-","");
	if (cpf=="") {
//		O usuário deixou o campo em banco
		alert("Digite um C.P.F para validar");
	}else if (cpf.length!=11) {
//		O cpf informado não possui 11 dígitos
		alert("O CPF digitado é inválido !\nNão possui 11 dígitos");
	}else if (isNaN(cpf)) {
		alert("O CPF informado não é um número válido");
	}else{
		/* Nessa parte iremos fazer a verificação completa do CPF */
//		Atribui valor às posições 10 e 11 do array (dígitos verificadores)
		digito[10] = cpf.charAt(9);
		digito[11] = cpf.charAt(10);

//		Faz uma array com os números de 10 a 2
		for (i=11;i>=2;i--) {
			digito2[i] = i;
		}
//		Verifica os dígitos informados no CPF
		for (i=1;i<=9;i++) {
			digito[i] = cpf.charAt(i-1);
			total_numero_d10 += parseInt(digito[i])*parseInt(digito2[11-i]);
		}
		valor1 = total_numero_d10 % 11;
		if (valor1<2) {
			valor1 = 0;
		}else{
			valor1 = 11 - valor1;
		}
		for (i=1;i<=9;i++) {
			total_numero_d11 += parseInt(digito[i])*(11-i+1);
		}

		total_numero_d11 += valor1*2;
		valor2 = total_numero_d11 % 11;
		if (valor2<2) {
			valor2 = 0;
		}else {
			valor2 = 11 - valor2;
		}
		
	    if(cpf == "11111111111" || cpf == "22222222222" || cpf ==
			"33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf ==
			"66666666666" || cpf == "77777777777" || cpf == "88888888888"  || cpf == "99999999999" || cpf ==
			"00000000000" ){
	    	
	    	valor1 = "99999999";
	    }
		
//	    alert( digito[10] == valor1 );
//	    alert( digito[11] == valor2 );
		
//		Verifica se os dígitos conferem
		if (digito[10]==valor1 && digito[11]==valor2) {
//			O CPF é válido
	    	return true;
		}else{
			alert("O CPF informado é inválido !");
			formCadastra.cpf.focus();
//			throw "O CPF informado é inválido !";
			return false;
		}
	}
}

function enviarAssociado() {
	try { 
		if( $('#formCadastra').validate().form() ) {
			if( ValidarCPF(formCadastra.cpf) ) {
				formCadastra.submit();
			} 
		}
	} catch(e) {
	}	
}
