function abreImagem(link, width, height){
    var largura = width  + 25;
    var altura  = height + 25;

    var left = (screen.width - largura) / 2;
    var top  = (screen.height - altura) / 2;

    var propriedades = 'height='+altura+',width='+largura+',top='+top+',left='+left+',toolbar=no,copyhistory=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,maximize=no';

    window.open(link, 'Preview', propriedades);
}

function abreCadastro(){
	propriedades = 'height=355,width=787,top=0,left=50,resizable=no,scrollbars=yes';
	abre = window.open('../cadastro/index.php','cadastro',propriedades)
}

function abreContrato(id){
	propriedades = 'height=570,width=768,top=0,left=150,resizable=no,scrollbars=yes';
	abre = window.open('serv_contrato_print.php?idMateria=' + id,'contrato',propriedades)
}

function abreMapa(id){
	propriedades = 'height=570,width=668,top=0,left=150,resizable=no,scrollbars=yes';
	abre = window.open('vizualizar_mapa?idMateria=' + id,'mapa',propriedades)
}

function abreLink(lnk){
	propriedades = 'height=400,width=500,top=0,left=150,resizable=yes,scrollbars=yes';
	abre = window.open(lnk,'link',propriedades)
}

function ver(id){
	document.form.in_controle.value = id;
	document.form.it_acao.value = 'visualizar';
	document.form.action = "ver.php";
	document.form.submit();
}

function buscar(){
	document.form.action = "lista.php";
	document.form.submit();
}

/* bool verificaEmail(string enderecoDeEmail) */
function verificaEmail(mail){
	str = new String(mail);
	var arroba = str.indexOf('@',0);
	if (arroba > 0){ // Tem arroba
		var ponto = str.indexOf('.',arroba);
		if (ponto > 0){ // Tem ponto
			if (str.length > ponto){ // Tem algo depois do ponto
				if ((arroba + 1) != ponto){ // Tem algo entre a arroba e o ponto
					// É um E-mail !!!
					return true;
				}
				return false;
			}
			return false;
		}
		return false;
	}
	return false;
}

function verificaDadosNewsletter(){
	var erro_js = '';
	var foco = '';

	if (document.servicos.it_nome.value == "") {
		erro_js = erro_js + "O campo 'Nome' é de preenchimento obrigatório!\n";
		foco = 'it_nome';
	}

	if (document.servicos.it_email.value == ""){
		erro_js = erro_js + "O campo 'E-mail' é de preenchimento obrigatório!\n";
		if (foco == "") foco = 'it_email';
	} else {
		if (!verificaEmail(document.servicos.it_email.value)){
			erro_js = erro_js + "Digite um endereço de E-mail válido!\n";
			if (foco == "") foco = 'it_email';
		}
	}

	if (erro_js == '') {
		return true;
	} else {
		alert(erro_js);
		campo = eval('document.servicos.' + foco);
		campo.focus();
		return false;
	}
}

function mudaCorTd(lugar, cor){
	lugar.bgColor  = cor;
}

function validar_cpf( sender ){
	var conf=0
	var temp
	var temp1
	var cont
	var dg1
	var dg1_1
	var dg2
	var dg2_2
	var sValue
	var digits="0123456789"

	/*
	if (sender.value == ""){
		alert("Por Favor Preencha o Campo CPF.")
		sender.focus()
	}
	*/
	sValue = sender.value;

	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );

	for (var i = 0; i < sValue.length; i++) {
		temp1=temp
		temp=sValue.substring(i,i+1)

		if (temp==temp1){
			conf=conf+1
		}

		if (digits.indexOf(temp)==-1){
			alert("Por Favor, o Campo CPF deve ser preenchido apenas com números!")
      sender.focus()
      return
		}
	}

	if (conf==10){
		alert("Número do CPF Inválido")
		sender.focus()
		return
	}

	cont=0

	for (var i=0;i<sValue.length;i++){
		temp=sValue.substring(i,i+1)
		cont=cont+1
	}

	if (cont!=0){
		if (cont!=11){
			alert("Número do CPF Inválido")
			sender.focus()
			return
		}
    else {
			cont=1
			dg1=0
			dg2=0

			for (var i=0;i<sValue.length;i++){
				temp=Math.abs(sValue.substring(i,i+1))

				if (cont==1) {
					dg1=dg1+Math.abs(temp*10)
				}

				if (cont==2) {
					dg1=dg1+Math.abs(temp*9)
				}

				if (cont==3) {
					dg1=dg1+Math.abs(temp*8)
				}

				if (cont==4) {
					dg1=dg1+Math.abs(temp*7)
				}

				if (cont==5) {
					dg1=dg1+Math.abs(temp*6)
				}

				if (cont==6) {
					dg1=dg1+Math.abs(temp*5)
				}

				if (cont==7) {
					dg1=dg1+Math.abs(temp*4)
				}

				if (cont==8) {
					dg1=dg1+Math.abs(temp*3)
				}

				if (cont==9){
					dg1=dg1+Math.abs(temp*2)
				}

				if (cont==10) {
					dg1_1=temp
				}

				if (cont==11) {
					dg2_2=temp
				}

				cont=cont+1
			}

			dg1=11-(dg1-((Math.floor(dg1/11))*11))

			if (dg1==10 || dg1==11) {
				dg1=0
			}

			if (dg1!=dg1_1) {
				alert("Número do CPF Inválido")
				sender.focus()
				return
			}
			else {
				cont=1

				for (var i=0;i<sValue.length;i++) {
					temp=Math.abs(sValue.substring(i,i+1))

					if (cont==1){
						dg2=dg2+Math.abs(temp*11)
					}

					if (cont==2){
						dg2=dg2+Math.abs(temp*10)
					}

					if (cont==3){
						dg2=dg2+Math.abs(temp*9)
					}

					if (cont==4){
						dg2=dg2+Math.abs(temp*8)
					}

					if (cont==5){
						dg2=dg2+Math.abs(temp*7)
					}

					if (cont==6){
						dg2=dg2+Math.abs(temp*6)
					}

					if (cont==7){
						dg2=dg2+Math.abs(temp*5)
					}

					if (cont==8){
						dg2=dg2+Math.abs(temp*4)
					}

					if (cont==9){
						dg2=dg2+Math.abs(temp*3)
					}

					if (cont==10){
						 dg2=dg2+Math.abs(temp*2)
					}

					cont=cont+1
				}

				dg2=11-(dg2-((Math.floor(dg2/11))*11))

				if (dg2==10 || dg2==11){
        	dg2=0
				}

				if (dg2!=dg2_2) {
					alert("Número do CPF Inválido")
					sender.focus()
					return
				}
			}
		}
	}
}

function validar_cnpj( sender ) {
	var temp
	var cont
	var dg1
	var dg1_1
	var dg2
	var dg2_2
	var sValue
	var digits="0123456789"

	/*
	if (sender.value == "") {
		alert("Por Favor Preencha o Campo CNPJ.");
		sender.focus();
	}
	*/
	sValue = sender.value;

	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );

	for (var i=0;i<sValue.length;i++){
		temp=sValue.substring(i,i+1);

		if (digits.indexOf(temp)==-1){
			alert("Por Favor, o Campo CNPJ deve ser preenchido apenas com números!");
			sender.focus();
			return;
		}
	}

	cont=0;

	for (var i = 0; i < sValue.length; i++) {
		temp=sValue.substring(i,i+1);
		cont=cont+1;
	}

	if (cont!=0) {
		if (cont!=14) {
			alert("Número de CPNJ Inválido");
			sender.focus();
		}
		else {
			cont=1;
			dg1=0;
			dg2=0;

			for (var i=0;i<sValue.length;i++){
				temp = Math.abs( sValue.substring(i,i+1) );

				if (cont==1)
						dg1=dg1+Math.abs(temp*5);

				if (cont==2)
					dg1=dg1+Math.abs(temp*4);

				if (cont==3)
					dg1=dg1+Math.abs(temp*3);

				if (cont==4)
					dg1=dg1+Math.abs(temp*2);

				if (cont==5)
					dg1=dg1+Math.abs(temp*9);

				if (cont==6)
					dg1=dg1+Math.abs(temp*8);

				if (cont==7)
					dg1=dg1+Math.abs(temp*7);

				if (cont==8)
					dg1=dg1+Math.abs(temp*6);

				if (cont==9)
					dg1=dg1+Math.abs(temp*5);

				if (cont==10)
					dg1=dg1+Math.abs(temp*4);

				if (cont==11)
					dg1=dg1+Math.abs(temp*3);

				if (cont==12)
					dg1=dg1+Math.abs(temp*2);

				if (cont==13)
					dg1_1=temp;

				if (cont==14)
					dg2_2=temp;

				cont=cont+1;
			}

			dg1=11-(dg1-((Math.floor(dg1/11))*11))

			if (dg1==10 || dg1==11)
				dg1=0;

			if (dg1!=dg1_1){
				alert("Número de CPNJ Inválido");
				sender.focus();
			}
			else {
				cont=1;

				for (var i=0;i<sValue.length;i++){
					temp=Math.abs(sValue.substring(i,i+1));

					if (cont==1)
						dg2=dg2+Math.abs(temp*6);
					if (cont==2)
						dg2=dg2+Math.abs(temp*5);
					if (cont==3)
						dg2=dg2+Math.abs(temp*4);
					if (cont==4)
						dg2=dg2+Math.abs(temp*3);
					if (cont==5)
						dg2=dg2+Math.abs(temp*2);
					if (cont==6)
						dg2=dg2+Math.abs(temp*9);
					if (cont==7)
						dg2=dg2+Math.abs(temp*8);
					if (cont==8)
						dg2=dg2+Math.abs(temp*7);
					if (cont==9)
						dg2=dg2+Math.abs(temp*6);
					if (cont==10)
						dg2=dg2+Math.abs(temp*5);
					if (cont==11)
						dg2=dg2+Math.abs(temp*4);
					if (cont==12)
						dg2=dg2+Math.abs(temp*3);
					if (cont==13)
						dg2=dg2+Math.abs(temp*2);

					cont=cont+1;
				}

				dg2=11-(dg2-((Math.floor(dg2/11))*11));

				if (dg2==10 || dg2==11)
					dg2=0;

				if (dg2!=dg2_2) {
					alert("Número de CPNJ Inválido");
					sender.focus();
				}
			}
		}
	}
}

function is_number() {
	// funcao que valida apenas numeros e virgulas. Usar o retorno no evento onKeyPress dos textField.
	if ( !(((window.event.keyCode > 47) &&
					(window.event.keyCode < 58)) ||
				 (window.event.keyCode == 44) ) )
	window.event.keyCode = 0;
}

function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	}

	sValue = objForm[strField].value;

	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;

	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;

	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++;
		} else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}

		i++;
	}

	objForm[strField].value = sCod;

	if (nTecla != 8) { // backspace
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); // números de 0 a 9
		} else { // qualquer caracter...
			return true;
		}
	} else {
		return true;
	}
}