
function checkForm( Form, message )
{
	var i;
	try
	{
		for (i=0; i<Form.length; i++) 
		{
			switch(Form[i].validate)
			{
				case 'text':
					if(! checkField( Form[i], Form[i].label.toUpperCase(), 'text',  message) )
						return false;
					break;
				case 'email':
					if(! checkField( Form[i], Form[i].label.toUpperCase(), 'email', message) )
						return false;
					break;
				case 'cpf':
					if(! checkField( Form[i], Form[i].label.toUpperCase(), 'cpf', message) )
						return false;				
					break;
				case 'cnpj':
					if(! checkField( Form[i], Form[i].label.toUpperCase(), 'cnpj', message) )
						return false;				
					break;
				case 'date':
					if(! checkField( Form[i], Form[i].label.toUpperCase(), 'date', message) )
						return false;
					break;	
				case 'combo':
					if(! checkField( Form[i], Form[i].label.toUpperCase(), 'select', message))
						return false;
					break;	  	
			}
		}
	}
	catch(e)
	{
		alert(e);
		return false;
	}
	return true;
}


/** FUNÇÕES DE VALIDAÇÃO **/

//** funcao que valida um campo de determinado tipo.
function checkField( field, fieldName, type, message )
{
	var flag = true;
	//** valida de acordo com o tipo de campo.	
	switch( type )
	{
		case 'select':
			if( ! checkCombo( field ) )
				flag = false;
			break;
		case 'text':
			if( ! checkText( field.value ) )
				flag = false;
			break;	 
		case 'cnpj':
			if( ! checkCnpj( field.value ) )
				flag = false;			
			break;
		case 'email':
			if( ! checkEmail( field.value ) )
				flag = false;			
			break;
		case 'cpf':
			if( ! checkCpf( field.value ) )
				flag = false;
			break;
		case 'data':
			if( ! checkDate( field.value ) )
				flag = false;
			break;			
		case 'char':
			if( ! checkChar( field.value ) )
				flag = false;
			break;
		case 'length':
			if( ! checkLength( field.value ) )
				flag = false;
			break;
	}

	//** se a validacao falhar da uma mensagem e returna falso.
	if( ! flag )
	{
		if( message )
		{
			alert('O campo ' + fieldName + ' não está preenchido corretamente.');
			field.focus();	
		}
		return false;
	}
	else
		return true;
}


function checkCpf ( 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 checkCnpj( CNPJ )	
{
		var d1 = 0;
		var d2 = 0;
		var fator = 14;
		
		CNPJ = CNPJ.replace('.','');
		CNPJ = CNPJ.replace('.','');
		CNPJ = CNPJ.replace('/','');		
		CNPJ = CNPJ.replace('-','');			

		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 checkEmail( email )
{
	if (email.length == 0 || email.value == '')
		return false;
	else 
	{
		if (email.indexOf("@") == -1 || email.indexOf(".") == -1) 
			return false;
		if (email.length < 10)
			return false;
	}
	return true;
}

function checkCombo( oCombo ) {  
  if ( oCombo.selectedIndex == 0 )
    return false;
  else
		return true;
}

function checkText( text ) 
{
  if ( text == '' )
    return false;
  else 
		return true;
}

function checkDate( date )
{
	var dn1 = date.substr(0,2);
	var dn2 = date.substr(3,2);
	var dn3 = date.substr(6,4);

	if((date.indexOf("/",0) != 2) || (date.indexOf("/",3) != 5) || (date.charAt(7) == ""))
		return false;

	if((isNaN(dn1))||(isNaN(dn2))||(isNaN(dn3)))
		return false;

	if ((dn1 > 31) || (dn2 > 12))
		return false;
	
	return true;
}

function checkLength( text, min, max ) {
	if( min && min >= text.length )
		return false;
	if( max && text.length >= max )	
		return false;
	else	
		return true;
}

function checkChar( text ) {
  var textTemp = "/\'ÃãÕõÇç][!@#$%^&*()+|<>?=[];: ";
  var textTemp2 = '"';
  var textTemp3;
  if (text.indexOf(textTemp2) != -1)
    return false;
  for (var i = 0; i<textTemp.length; i++) 
  {
    textTemp3 = textTemp.substr(i,1);
    if (text.indexOf(textTemp3) != -1) 
			return false;
  }
  return true;
}

function AbrePg(theURL,winName,features) {
  var WIN;
  WIN = window.open(theURL,winName,features);
  WIN.focus();
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function Display() {
  var i,p,v,obj;
  var args = Display.arguments;
  for (i=0; i<(args.length-2); i+=3) 
    if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v;
  }
}

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;
}
