//
// Biblioteca que checa Data
// Pre-requisitos: Deve estar inserida juntamente com a biblioteca basico.js
// Autor: Aristides Andrade 23/02/2000
// Ultima Atualização: 23/02/2000
//

function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}


function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}


function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}



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 isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function warnInvalid (theField, s)
{  
    theField.focus()
    theField.select()
    alert(s)
    return false
}

function checkYear (theField, emptyOK)
{   if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear(theField.value, false)) 
       return warnInvalid (theField, iYear);
    else return true;
}



function checkMonth (theField, emptyOK)
{   if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isMonth(theField.value, false)) 
       return warnInvalid (theField, iMonth);
    else return true;
}



function checkDay (theField, emptyOK)
{   if (checkDay.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isDay(theField.value, false)) 
       return warnInvalid (theField, iDay);
    else return true;
}



function checkDate (year, month, day, labelString, Datafield, OKtoOmitDay)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkDate.arguments.length == 5) OKtoOmitDay = false;
    if (!isYear(year)) return warnInvalid (Datafield, iYear);
    if (!isMonth(month)) return warnInvalid (Datafield, iMonth);
    if ( (OKtoOmitDay == true) && isEmpty(day) ) return true;
    else if (!isDay(day)) 
       return warnInvalid (Datafield, iDay);
    if (isDate (year, month, day))
       return true;
    alert (iDatePrefix + labelString + iDateSuffix)
    return false
}

function DigitoVerificador(numero,cpf)
{
	var soma
	var comprimento
	var fator
	var digito
	
	comprimento = numero.length
	soma = 0
	
	for (var i = 1; i <= comprimento; i++) 
	{
		if (cpf) {
			fator = i + 1
		}
		else {
			fator = 2 + ((i-1)%8) 
		}
		
		soma = soma + parseInt(numero.charAt(comprimento-i))*fator
		
	}
	
	digito = ((10*soma)%11)%10;
	return digito 
}


function CompletaNumero (numero,cpf)
{
	var Saux
	Saux = numero
	for  (var i = 1; i <=2; i++) 
	{
		Saux = Saux + DigitoVerificador(Saux, cpf)
	}
	return Saux 
}


function verificanumero(cpfcgcfield, cpf)
{
	var tamanho
	var numero
	var numerosemdv
	
	
	if (cpf) {
		tamanho = 11
	}
	else 
	{
		tamanho = 14
	}
	
	numero = cpfcgcfield.value
	numerosemdv = numero.substring(0,numero.length - 2)
	if (numero==CompletaNumero(numerosemdv,cpf) ) 
	{
		  return true;
	}
	else
	{
		return warnInvalid(cpfcgcfield, icpfcgc);
	}
	
}

function checkCPFCGC(cpfcgcfield)
{
	var s
	if (!isInteger(cpfcgcfield.value)) {
		return warnInvalid (cpfcgcfield,icpfcgc)
	}
	s = cpfcgcfield.value
	if ((s.length != 11) && (s.length !=14)) {
		return warnInvalid(cpfcgcfield, icpfcgc)
	}
	if (s.length == 11) {
	        return verificanumero(cpfcgcfield, true)
	}
	else {
		return verificanumero(cpfcgcfield, false)
	}
	        
}

function formatcgccpf(s)
{
    if (s.length == 11) 
    {
    	return  s.substring(0,3) + "." + s.substring(3,6) + "." + s.substring(6,9) + "-" + s.substring(9,11)
    }
    else
    {
	return  s.substring(0,2) + "." + s.substring(2,5) + "." + s.substring(5,8) + "/" + s.substring(8,12) + "-" + s.substring(12,14)    
    }
}
	
function desformatcgccpf(s)
{
    var n,c
    n = ""
    for (i = 0; i < s.length; i++)
    {   
            c = s.charAt(i);
    
            if (isDigit(c)) 
            {
		n = n + c    
	    }
    }
    return n
    
}
function Datacompleta(s)
{
	var x,t
	
	t = s.substring(s.indexOf("/",3) + 1,s.length)
	
	if (t.length == 2) 
	{
	    x = "20" + t	
	}
	else
	{
	    x = t	
	}
	
	t = s.substring(s.indexOf("/") + 1,s.indexOf("/",3))
	if (t.length == 1)
	{
	    x = x + "0" + t	
	}
	else
	{
	    x = x + t	
	}
	t = s.substring(0,s.indexOf("/")) 
	if (t.length == 1)
	{
	    x = x + "0" + t	
	}
	else
	{
	    x = x + t	
	}
	return x
	
}
