function AcceptChar(evento)
{
	if (navigator.appName == 'Microsoft Internet Explorer') {  
		key = evento.keyCode;
	}else{
		key = evento.which;
	}

	var fFound = false;
	if ((key >= 48 && key <= 57)) fFound = true
	if (fFound) return fFound
	if ((key >= 65 && key <= 90)) fFound = true
	if (fFound) return fFound
	if ((key >= 97 && key <= 122)) fFound = true
	return fFound
}

function mask(obj,e,mask )
{
   if ((obj.value.length >= obj.maxLength) || (obj.value.length >= mask.length)) return false;

   var strCheck = '#ADMYHMS';
   var numbers = '0123456789';
   var whichCode = (window.Event) ? e.which : e.keyCode;
   var key = String.fromCharCode(whichCode);
   var cChar = mask.charAt(obj.value.length);

   while ((obj.value.length < obj.maxLength) && (obj.value.length < mask.length))
   {
      if (strCheck.indexOf(cChar) < 0)
      {
         obj.value += cChar;
      }
      else
      {
         if (('#DMYHMS'.indexOf(cChar) >= 0) && (numbers.indexOf(key) < 0)) return false;
         break;
      }
      cChar = mask.charAt(obj.value.length);
   }
   return true;
}

/*******************************************************************/

function Valida_CPF (CPF) {return isCPF (CPF)}

function Valida_CNPJ(CNPJ)	{return isCNPF (CNPJ)}

function Valida_Data(Obj, FieldName, FieldFocus, fAlert){return isDate(Obj, FieldName, FieldFocus, fAlert)}

function Valida_Email (strString, FieldName, FieldFocus, fAlert){return isEmail(strString, FieldName, FieldFocus, fAlert)}

function Valida_String(FieldValue, FieldName, FieldFocus, fAlert) {return isString(FieldValue, FieldName, FieldFocus, fAlert)}

function Count_Char(FieldValue, FieldName, FieldLen, FieldFocus, fAlert) {return CountChar(FieldValue, FieldName, FieldLen, FieldFocus, fAlert)}

function Verifica_Char(str) {return isValidChar(str)}

function Valida_Combo(Field, FieldName, fAlert) {return isSelected(Field, FieldName, fAlert)}

/*******************************************************************/

function isCPF (CPF) {
		if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
			CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
			CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
			CPF == "88888888888" || CPF == "99999999999")
			return false;
		soma = 0;
		for (i=0; i < 9; i ++)
			soma += parseInt(CPF.charAt(i)) * (10 - i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11)
			resto = 0;
		if (resto != parseInt(CPF.charAt(9))) {
			return false;
		}
		soma = 0;
		for (i = 0; i < 10; i ++)
			soma += parseInt(CPF.charAt(i)) * (11 - i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11)
			resto = 0;
		if (resto != parseInt(CPF.charAt(10))) {
			return false;
		}
		return true;
	}

function isCNPJ( CNPJ )	{
		var d1 = 0;
		var d2 = 0;
		var fator = 14;

		if ((CNPJ == 11111111111111)||(CNPJ == 22222222222222)||(CNPJ == 33333333333333)||(CNPJ == 44444444444444)|| (CNPJ == 55555555555555)||(CNPJ == 66666666666666)||(CNPJ == 77777777777777)||(CNPJ == 88888888888888)|| (CNPJ == 99999999999999)||(CNPJ == 00000000000000))
		{
			return false;
		}

		for (i=12; i>0; i--)
		{
			if (i==4) 
			{
				fator=6;
			}
			d1 = d1 + (CNPJ.charAt(i-1) * (fator-i));
		} 

		d1 = d1 % 11;
		if (d1 == 0 || d1 == 1)
		{
			d1 = 0;
		}
		else
		{
			d1 = 11 - d1;
		}

		if (CNPJ.charAt(12) != d1)
		{
			return false;
		}

		fator = 15;

		for (i=13; i>0; i--)
		{
			if (i==5) 
			{
				fator=7;
			}
			d2 = d2 + (CNPJ.charAt(i-1) * (fator-i));
		} 

		d2 = d2 % 11;
		if (d2 == 0 || d2 == 1)
		{
			d2 = 0;
		}
		else
		{
			d2 = 11 - d2;
		}

		if (CNPJ.charAt(13) != d2)
		{
			return false;
		}

		return true;
	}

function isDate(Obj, FieldName, FieldFocus, fAlert){
	var dn1 = Obj.substr(0,2);
	var dn2 = Obj.substr(3,2);
	var dn3 = Obj.substr(6,4);

	if((Obj.indexOf("/",0) != 2) || (Obj.indexOf("/",3) != 5) || (Obj.charAt(7) == "")) {
		if (fAlert) alert('O campo ' + FieldName + ' não está preenchido corretamente.');
		if (fAlert) FieldFocus.focus();
		return false;
	}

	if((isNaN(dn1))||(isNaN(dn2))||(isNaN(dn3))) {
		if (fAlert) alert('O campo ' + FieldName + ' não está preenchido corretamente.');
		if (fAlert) FieldFocus.focus();
		return false;
	}

	if ((dn1 > 31) || (dn2 > 12)) {
		if (fAlert) alert('O campo ' + FieldName + ' não está preenchido corretamente.');
		if (fAlert) FieldFocus.focus();
		return false;
	}	
	
	return true;
}

function isEmail (strString, FieldName, FieldFocus, fAlert){
	if (strString.length == 0 || strString.value == '') {
		if (fAlert) alert('O campo ' + FieldName + ' não está preenchido corretamente.');
		if (fAlert) FieldFocus.focus();
		return false;
	}
	else {
		if (strString.indexOf("@") == -1 || strString.indexOf(".") == -1) {
			if (fAlert) alert('O campo ' + FieldName + ' não está preenchido corretamente.');
			if (fAlert) FieldFocus.focus();
			return false;
		}
		if (strString.length < 10) {
			if (fAlert) alert('O campo ' + FieldName + ' não está preenchido corretamente.');
			if (fAlert) FieldFocus.focus();
			return false;
		}
	}
	return true;
}

function isString(FieldValue, FieldName, FieldFocus, fAlert) {
  if (FieldValue == '') {
    if (fAlert) alert('O campo ' + FieldName + ' não está preenchido corretamente.');
    if (fAlert) FieldFocus.focus();
    return false;
  }
  return true;
}

function CountChar(FieldValue, FieldName, FieldLen, FieldFocus, fAlert)
{
	if (FieldValue.length > parseInt(FieldLen))
  {
    if (fAlert) alert('O campo ' + FieldName + ' aceita um máximo de ' + FieldLen + ' caracteres.');
	if (fAlert) FieldFocus.focus();
    return false;
  }
  return true;
}

function isValidChar(str) {
  var strTemp = "/\'ÃãÕõÇç][!@#$%^&*()+|<>?-=[];: ";
  var strTemp2 = '"';
  var strTemp3;
  if (str.indexOf(strTemp2) != -1)
    return false;
  for (var i = 0; i<strTemp.length; i++) {
    strTemp3 = strTemp.substr(i,1);
    if (str.indexOf(strTemp3) != -1) return false;
  }
  return true;
}

function openPopUp(theURL,winName,features)
{
	try
	{
		var WIN;
		WIN = window.open(theURL,winName,features);
		WIN.focus();
	}
	catch(e)
	{
		alert('Uma nova janela não pode ser aberta, tente novamente.\nVerifique se utiliza anti-popup ou utilize a tecla CTRL.')
	}
}

function AbrePg(theURL,winName,features){openPopUp(theURL,winName,features)}

function isSelected(Field, FieldName, fAlert)
{
	if (Field.type.toLowerCase() == ("select-multiple"))
	{
		if (Field.selectedIndex == -1)
		{
			if (fAlert) alert('O campo ' + FieldName + ' não está preenchido corretamente.');
			if (fAlert) Field.focus();
			return false;
		}
	}
	else
		if (Field.selectedIndex == 0)
		{
			if (fAlert) alert('O campo ' + FieldName + ' não está preenchido corretamente.');
			if (fAlert) Field.focus();
			return false;
		}
	return true;
}