﻿function openWindow(theURL, winName, features) {
	window.open(theURL, winName, features);
	return false;
}

function bloqEnter(objEvent) {
	var iKeyCode;
	iKeyCode = objEvent.keyCode;
	if (iKeyCode == 13)
		return false;
	return true;
}

function validaBeneficiario(tipo, campo, campo2) {
	var tip = document.getElementById(tipo).value;
	var busca = document.getElementById(campo).value;
	var data = document.getElementById(campo2).value;
		
	if (!validaVazioQtde(campo, 'Busca', 3)) {
		document.getElementById(campo).focus();
		return false;
	}else if (tip == "CPF") {
		if (!validaCpf(campo)){
			document.getElementById(campo).focus();
			return false;
			}
	} else if (tip == "MTN") {		
		if (!validaMatricula(campo)){
			document.getElementById(campo).focus();
			return false;
			}
	} else if (tip == "MTA") {
		if (busca.length < 10) {
			alert("Matrícula inválida!!!");
			document.getElementById(campo).focus();
			return false;
		}
	} else if (tip == "NPN") {
		if (data.length == "") {
			alert('Na busca por "Nome" ou "parte do Nome" é obrigatório preencher a Data de Nascimento');
			document.getElementById(campo2).focus();
			return false;
		} else {
			if (!validaData(campo2)){
				document.getElementById(campo2).focus();
				return false;
				}
		}
	}
	return true;
}

function mascaraBeneficiario(event, objeto, tipo, campo) {

	var tip = document.getElementById(tipo).value;

	document.getElementById(campo).maxLength = 100;

	if (tip == "CPF") {

		document.getElementById(campo).maxLength = 14;
		if (mascaraCPF(event, objeto)) {
			return true;
		}

	} else if (tip == "MTN") {

		document.getElementById(campo).maxLength = 21;
		if (mascaraMatricula(event, objeto)) {
			return true;
		}

	} else {
		return true;
	}
	return false;
}

function filtroBeneficiario(tipo, campo, campo2) {
	var tip = document.getElementById(tipo).value;
	var busca = document.getElementById(campo).value;	
	if (tip == "CPF") {
		if (busca.substring(3, 4) != '.') {
			document.getElementById(campo).value = "";
			document.getElementById(campo2).value = "";			
		}
	} else if (tip == "MTN") {
		if (busca.substring(4, 5) != '.') {
			document.getElementById(campo).value = "";
			document.getElementById(campo).value = "";			
		}
	} else {
		document.getElementById(campo2).value = "";
		if (busca.substring(4, 5) == '.' || busca.substring(3, 4) == '.') {
			document.getElementById(campo).value = "";			
		}
	}
}

function sendTab() {

	if (event.keyCode == 13) {

		var e = window.event.keyCode = 9;
		var key = (window.event) ? event.keyCode : e.which;

		if (window.event)
			key = event.keyCode;
		else
			key = e.which;

		if (navigator.appName == 'Microsoft Internet Explorer') {
			window.event.returnValue = 9;
		} else {
			e.preventDefault(9);
		}
	}
}

function sac() {
	var msg = "Telefones Assistência São Lucas: \n\n "
			+ "De segunda a sexta das 08:00hrs às 18:00hrs: \n\n"
			+ " S.A.C. \n " + "SAC Lins: (14)3533-4508  \n\n "
			+ "SAC Bauru: (14)3235-9895  \n\n "
			+ "SAC Marilia: (14)3311-4500 \n\n\n "
			+ "Em outros dias e horários: \n\n " + "Hospitais \n "
			+ "Hospital Lins: (14)3533-4401 \n\n "
			+ "Hospital Bauru: (14)3235-4500 \n\n";

	alert(msg);
	return false;
}

function mascaraMatricula(event, objeto) {
	var tecla = (window.event) ? event.keyCode : event.which;
	var campo = eval(objeto);
	if (tecla == 9 || tecla == 8 || (tecla >= 48 && tecla <= 57)) {
		if (tecla != 9
				&& tecla != 8
				&& (campo.value.length == 4 || campo.value.length == 9 || campo.value.length == 16)) {
			campo.value = campo.value + '.';
		}
		if (tecla != 9 && tecla != 8 && (campo.value.length == 19)) {
			campo.value = campo.value + '-';
		}
		return true;
	}
	return false;
}

function mascaraCPF(event, objeto) {
	var tecla = (window.event) ? event.keyCode : event.which;
	var campo = eval(objeto);

	if (tecla == 8 || (tecla >= 48 && tecla <= 57)) {

		if (tecla != 8 && (campo.value.length == 3 || campo.value.length == 7)) {
			campo.value = campo.value + '.';
		}

		if (tecla != 8 && (campo.value.length == 11)) {
			campo.value = campo.value + '-';
		}
		return true;
	}
	return false;
}

function mascaraData(event, objeto) {
	var tecla = (window.event) ? event.keyCode : event.which;
	var campo = eval(objeto);

		if (campo.value == '00/00/0000') {
			campo.value = "";
		}
		if (tecla == 8 || (tecla >= 48 && tecla <= 57)) {
			if (tecla != 8
					&& (campo.value.length == 2 || campo.value.length == 5)) {
				campo.value = campo.value + '/';
			}
			return true;
		} else {
			return false;
		}	
}

function Trim(str) {
	return str.replace(/^\s+|\s+$/g, "");
}

function JanelaCentralizada(mypage, myname, w, h, scroll, pos) {
	var win = null;

	if (pos == "random") {
		LeftPosition = (screen.width) ? Math.floor(Math.random()
				* (screen.width - w)) : 100;
		TopPosition = (screen.height) ? Math.floor(Math.random()
				* ((screen.height - h) - 75)) : 100;
	}
	if (pos == "center") {
		LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
		TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
	} else if ((pos != "center" && pos != "random") || pos == null) {
		LeftPosition = 0;
		TopPosition = 20;
	}
	settings = 'width='
			+ w
			+ ',height='
			+ h
			+ ',top='
			+ TopPosition
			+ ',left='
			+ LeftPosition
			+ ',scrollbars='
			+ scroll
			+ ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win = window.open(mypage, myname, settings);
}

function validaVazio(campo, titulo) {
	var dado = document.getElementById(campo).value;
	if (dado == "") {
		alert("O campo " + titulo + " não pode ser vazio!");
		return false;
	}
	return true;
}

function validaVazioQtde(campo, titulo, qtde) {
	var filtro = document.getElementById(campo).value;

	if (filtro == "") {
		alert("O campo " + titulo + " não pode ser vazio!");
		return false;
	} else if (filtro.length < qtde) {
		alert("Digite no mínimo " + qtde + " letras... !!! ");
		return false;
	}
	return true;
}

function validaCpf(campo) {
	var i;
	var x = document.getElementById(campo).value;
	if (x == "000.000.000-00" || x == "111.111.111-11" || x == "222.222.222-22"
			|| x == "333.333.333-33" || x == "444.444.444-44"
			|| x == "555.555.555-55" || x == "666.666.666-66"
			|| x == "777.777.777-77" || x == "888.888.888-88"
			|| x == "999.999.999-99") {
		alert("C.P.F Inválido!");
		return false;
	}

	s = x.replace('.', '').replace('.', '').replace('.', '').replace('-', '');

	var c = s.substr(0, 9);
	var dv = s.substr(9, 2);
	var d1 = 0;
	for (i = 0; i < 9; i++) {
		d1 += c.charAt(i) * (10 - i);
	}
	if (d1 == 0) {
		alert("CPF Inválido!");
		return false;
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9)
		d1 = 0;
	if (dv.charAt(0) != d1) {
		alert("CPF Inválido!");
		return false;
	}
	d1 *= 2;
	for (i = 0; i < 9; i++) {
		d1 += c.charAt(i) * (11 - i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9)
		d1 = 0;
	if (dv.charAt(1) != d1) {
		alert("CPF Inválido!");
		return false;
	}

	return true;
}

function validaData(campo) {
	var data = document.getElementById(campo).value;
	var dia = (document.getElementById(campo)).value.substring(0, 2);
	var mes = (document.getElementById(campo)).value.substring(3, 5);
	var ano = (document.getElementById(campo)).value.substring(6, 10);
	var i;

	if (data.length != 10) {
		alert("Data Inválida!");
		return false;
	}
	if (data.length > 0 && data.length < 10 || data == "00/00/0000"
			|| data == "11/11/1111" || data == "22/22/2222"
			|| data == "33/33/3333" || data == "44/44/4444"
			|| data == "55/55/5555" || data == "66/66/6666"
			|| data == "77/77/7777" || data == "88/88/8888"
			|| data == "99/99/9999") {
		alert("Data Inválida!");
		return false;
	}
	if (dia < "01" || dia > "31") {
		alert("Dia inválido!");
		return false;
	}

	if (mes < "01" || mes > "12") {
		alert("Mês inválido!");
		return false;
	}
	if (ano.length != 4) {
		alert("O ano deve ser com 4 dígitos. DD/MM/AAAA !!!");
		return false;
	}
	return true;
}

function validaMatricula(mat) {

	var campo = document.getElementById(mat).value;

	if (campo.length != 21) {
		alert("Matrícula Inválida!");
		return false;
	}

	if (campo.length > 0 && campo.length < 21) {
		alert("Matrícula Inválida!");
		return false;
	} else {
		if (campo == "0000.0000.000000.00-0"
				|| campo == "1111.1111.111111.11-1"
				|| campo == "2222.2222.222222.22-2"
				|| campo == "3333.3333.333333.33-3"
				|| campo == "4444.4444.444444.44-4"
				|| campo == "5555.5555.555555.55-5"
				|| campo == "6666.6666.666666.66-6"
				|| campo == "7777.7777.777777.77-7"
				|| campo == "8888.8888.888888.88-8"
				|| campo == "9999.9999.999999.99-9") {
			alert("Matrícula Inválida!");
			return false;
		}
	}
	return true;
}

function validaVazioQtdeNaoObrigatorio(campo, titulo, qtde) {
	var filtro = document.getElementById(campo).value;

	if (filtro.length > 0 && filtro.length < 4) {
		alert("Digite no mínimo " + qtde + " letras no campo " + titulo + "! ");
		return false;	
	}
	return true;
}

function validaExtrato(campo, campo2) {
	
	if (!validaMatricula(campo)) {
		document.getElementById(campo).focus();
		return false;
	}
	if (!validaCpf(campo2)) {
		document.getElementById(campo2).focus();
			return false;
	}	
	return true;
}