﻿var isIE	    = document.all;
var isIE7	    = isIE && window.XMLHttpRequest && window.ActiveXObject;
var isIE6	    = isIE && document.implementation;
var isgteIE6	= isIE7 || isIE6;
var isIE5	    = isIE && window.print && !isgteIE6;
var isIEDOM2	= isIE5 || isgteIE6;
var isIE4	    = isIE && !isIEDOM2 && navigator.cookieEnabled;
var isIE3	    = isIE && !isIE4 && !isIEDOM2;
var isNS	    = navigator.mimeTypes && !isIE;
var isNS3	    = isNS && !navigator.language;
var isNS4	    = document.layers;
var isNS6	    = document.getElementById && !isIE;
var isNS7	    = isNS6;
var isNS71	    = document.designMode;
var isNSDOM2	= isNS6;
var isDOM2	    = isIEDOM2 || isNSDOM2;

// -----------------------------------------------------------------
// Nome: allEve
// -----------------------------------------------------------------
function allEve(e)
{
	var ev = (window.event)? window.event: e;
	if (!ev || !ev.type) return false;

	var ME = ev;
	if (ME.type.indexOf('key') != -1)
	{
		if (isIE || ME.type.indexOf('keypress')!= -1)
		{
			ME.key = (ev.keyCode)? ev.keyCode: ((ev.charCode)? ev.charCode: ev.which);
		}
		else 
		{
		    ME.key = ev.charCode;
		}
		if(ME.key) ME.letter = String.fromCharCode(ME.key);
	}
	return ME;
}

// -----------------------------------------------------------------
// Nome: formataData
// Obs:  No input text adicione: onkeypress="return formataData(event, this)"
// -----------------------------------------------------------------
function formataData(evento, campo) 
{
    var e = allEve(evento);
    var tecla = e.key;

	// backspace
	if (tecla == 8)
	    campo.value = campo.value.substring(0, campo.value.length-1);
	    
    //tab
    if (tecla == 9)
        dummy;	    

	// somente números & barra
	if (tecla < 48 || tecla > 57)
	{
        try { e.returnValue = false; }
        catch ( dummy ) {}
        return false;
	}
	else
	{
		if(campo.value.length == 2 || campo.value.length == 5)
		{
		    campo.value = campo.value + '/';
        }
		else if (campo.value.length == 1 || campo.value.length == 4)
		{
			campo.value = campo.value + String.fromCharCode(tecla) + '/';
            try { e.returnValue = false; }
            catch ( dummy ) {}
            return false;
		}
	}
}

// -----------------------------------------------------------------
// Nome: validaData
// -----------------------------------------------------------------
function validaData(source, arguments)
{
    if (!ehData(arguments.Value))
    {   
        arguments.IsValid = false;
    }
}

// -----------------------------------------------------------------
// Nome: ehData
// -----------------------------------------------------------------
function ehData(data)
{
   if (data)
   {
      var arrData = data.split('/');
      var dia = arrData[0];
      var mes = arrData[1] - 1;
      var ano = arrData[2];

      objData = new Date(ano,mes,dia);

      if (ano != objData.getFullYear() || mes != objData.getMonth() || dia != objData.getDate())
      {
         return false;
      }
      
      if (ano < 1753)
      {
         return false;
      }
   }
   return true;
}
