// JavaScript Document

function isValidDate(fld){	
	var s = document.getElementById(fld);
	s.style.background='#FFFFFF';
	dia = parseInt(s.value.substring(0,2),10); 
	mes = parseInt(s.value.substring(3,5),10); 
	ano = parseInt(s.value.substring(6,10),10); 
	situacao = true; 
	// verifica o dia valido para cada mes 
	if((dia < 1)||(dia < 1 || dia > 30) && (mes == 4 || mes == 6 || mes == 9 || mes == 11 ) || dia > 31){
		situacao = false;
	} 
	// verifica se o mes e valido 
	if(mes < 1 || mes > 12 ){ 
		situacao = false; 
	} 
	// verifica se e ano bissexto 
	if(mes == 2 && ( dia < 1 || dia > 29 || ( dia > 28 && (parseInt(ano / 4, 10) != ano / 4)))) { 
		situacao = false; 
	} 
	if(s.value == "") { 
		situacao = false; 
	}
	if(s.value.length < 10){
		situacao = false; 
	}
	if(situacao == false) { 
		alert('Data Invalida');
		s.focus();
		s.style.background='#FF0000';
		return false;
	}
	return true;
}

function isValidEmail(obj) {
	var fld = document.getElementById(obj);
   	var erro = new String;
    var tfld = trim(fld.value);
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	
	fld.style.background='#FFFFFF';
   
    if (fld.value == "") {
        erro = "Você não informou o e-mail";
    } else if (!emailFilter.test(tfld)) {
        erro = "Por favor informe um e-mail válido";
    } else if (fld.value.match(illegalChars)) {
        erro = "O e-mail possui caracteres inválidos";
    }
		
	if (erro.length > 0){
		alert(erro);
		fld.focus();
		fld.style.background='#FF0000';
		return false;	
	}else{
		return true;
	}
}


function isValidCPF(obj) {
	var fld = document.getElementById(obj);
	fld.style.background='#FFFFFF';
	var cpf = fld.value;
	cpf = cpf.replace (".","");
	cpf = cpf.replace (".","");
	cpf = cpf.replace ("-","");
	var erro = new String;
	if(cpf.length < 11 || cpf.length > 11){
		erro += "Sao necessarios 11 digitos para verificacao do CPF! \n";
	}
	var nonNumbers = /\D/;
	if(nonNumbers.test(cpf)){
		erro += "A verificacao de CPF suporta apenas numeros! \n";
	}
	if(cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
		erro += "Numero de CPF invalido!";
	}
	var a = [];
	var b = new Number;
	var c = 11;
	for(i=0; i<11; i++){
		a[i] = cpf.charAt(i);
		if(i < 9){
			b += (a[i] * --c);
		}
	}
	if((x = b % 11) < 2){
		a[9] = 0;
	}else{
		a[9] = 11-x;
	}
	b = 0;
	c = 11;
	for(y=0; y<10; y++){
		b += (a[y] * c--);
	}
	if((x = b % 11) < 2){
		a[10] = 0;
	}else{
		a[10] = 11-x;
	}
	if((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
		erro +="Digito verificador com problema!";
	}
	
	if (erro.length > 0){
		alert(erro);
		fld.focus();
		fld.style.background='#FF0000';
		return false;	
	}else{
		return true;
	}
	
}

function isValidPass(p,c){
	p = document.getElementById(p);
	c = document.getElementById(c);
	p.style.background='#FFFFFF';
	c.style.background='#FFFFFF';
	if(p.value.length >= 6){
		if(p.value == c.value){
			return true;
		}else{
			alert("O campo CONFIRMA SENHA não confere.");
			c.focus();
			c.style.background='#FF0000';
			return false;
		}
	}else{
		alert("A senha deve conter no minimo 6 caracteres.");
		p.focus();
		p.style.background='#FF0000';
		return false;
	}
}

function isValidField(c){
	for(var i = 0; i < c.length; i++){
		var field = document.getElementById(c[i]);
		var label = document.getElementById('l'+c[i]);
		if(field.type == 'text' || field.type == 'textarea' || field.type == 'select-one' || field.type == 'password'){
			field.style.background='#FFFFFF';
			if(field.value.length <= 0){
				alert('O campo '+label.title+' deve ser preenchido.');
				field.focus();
				field.style.background='#FF0000';
				return false;
			}
		}
	}
	return true;
}
