
//retorna a tecla pressionada - Internet Explorer e Netscape
function getkey(e)
{
	if (window.event)
	   key = window.event.keyCode;
	else
	   key = e.which;

	return key;
}

function isNum(key)
{
	if ( key == 0 || key == 8 ) //tab, backspace
		return true;

	if ( key == 32 ) //space
		return false;

	if ( key<48  ||  key>57  )
		return false;
}

function blurCEP(CEP)
{
	//alert(CEP);
	if ( CEP.length == 8 )
		document.form_cadastro.cnr_cep.blur();
	else
		return false;
}


function checkCEP (CEP) {
    if (CEP.length != 8 )
    	return false;
    return true;
}

function isValidEmail(str) {
   return (str.indexOf(".") > 0) && (str.indexOf("@") > 0);

}


/*Função loadIframe - efetua um reload no conteúdo do iframe para consultar o CEP*/
function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;
	//alert(url);
    return false;
  }
  else return true;
}


function valida_recuperasenha(form) {
   with(form)
   {

    if (editnrcpf.value == '') {
            alert("Deve-se preencher o número do CPF com dígito verificador e sem traço separador.");
            editnrcpf.focus();
            return false;
       }

    if (!(validacpf(editnrcpf.value)))
       { alert("O CPF informado é inválido.");
         editnrcpf.focus();
         return false;
       }

    if (!isValidEmail(editemail.value)) {
            alert("O E-MAIL informado está incompleto.");
            editemail.focus();
            return false;
       }

     btn_recuperasenha.value = "Recuperando ...";
     btn_recuperasenha.disabled = true;
     //submit();
   }
}


function valida_cpf(form) {

   with(form)
   {
    if (editnrcpf.value == '') {
            alert("Deve-se preencher o número do CPF com dígito verificador e sem traço separador.");
            editnrcpf.focus();
            return false;
       }

    if (!(validacpf(editnrcpf.value)))
       { alert("O CPF informado é inválido.");
         editnrcpf.focus();
         return false;
       }

    btn_verificacao.value = "Iniciar Inscrição";
    btn_verificacao.disabled = true;
   }
}

function valida_orientador(form) {


   with(form)
   {

			if ( trim(editcpforientador.value).length != 11 )
			{
				alert("Informe o CPF DO ORIENTADOR com 11 dígitos. Se necessário informe zeros à esquerda.");
                editcpforientador.focus();
				return false;
			}

			if ( !validacpf(editcpforientador.value) )
			{
				alert("O CPF DO ORIENTADOR é inválido.");
                editcpforientador.focus();
				return false;
			}



        if ( trim(editnmorientador.value) == "" ) {

		        alert("O NOME DO ORIENTADOR deve ser preenchido.");
		        editnmorientador.focus();
		        return false;
		}
		    
    	if ( !editemailorientador.value == "" ) {
                if (!isValidEmail(editemailorientador.value)) {
                alert("O E-MAIL informado está incompleto.");
                editemailorientador.focus();
                return false;
                }
    		}
    		else
    		{
                alert("O E-MAIL deve ser informado.");
                editemailorientador.focus();
                return false;
    		}

   }
}


function valida_login(form) {
   with(form)
   {

    if (editnrcpf.value == '') {
            alert("Deve-se preencher o número do CPF com dígito verificador e sem traço separador.");
            editnrcpf.focus();
            return false;
       }


    if (editsenha.value == '') {
            alert("Informe a SENHA para acesso.");
            editsenha.focus();
            return false;
       }

     btn_login.value = "Entrar";
     btn_login.disabled = true;
     //submit();
   }
}



function submitquestionario() {
   document.form_questionario.btn_confirmarquestionario.value = "Enviando ...";
   document.form_questionario.btn_confirmarquestionario.disabled = true;

   document.form_questionario.submit();
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this;
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("O formato correto para a data é: dd/mm/aaaa.");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Por favor, entre com um mês válido.");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Por favor, entre com um dia válido.");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Por favor, entre com 4 dígitos para ano entre "+minYear+" e "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Por favor, entre com uma data válida.");
		return false;
	}
return true;
}


function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

/*
function contador(str) {
  contents = str;
  contents = contents.replace(/<(.+?)>/g, "");//apagar HTML tags
  contents = contents.replace(/&nbsp;/g, "*");//contar nbsp; como um único keystroke
  document.forms[0].EditContador.value = contents.length;
}
*/

function validacpf(cpf_p){
	//alert(cpf_p);
	var i;
	s = cpf_p;
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
   var arrayInvalidos;

   arrayInvalidos = ["11111111111","22222222222","33333333333",
                     "44444444444","55555555555","66666666666","77777777777",
                     "88888888888","99999999999","00000000000"];

   for (var i = 0; i < arrayInvalidos.length; i++){
     if (arrayInvalidos[i] == cpf_p){
		return false;
	   }
   }

	for (i = 0; i < 9; i++) {
		d1 += c.charAt(i)*(10-i);
	}


	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 < 9; i++) {
		d1 += c.charAt(i)*(11-i);
	}

	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1) {
		return false;
	}
   return true;
}

