// Funcoes para validacao de formularios
var formRef;
var fieldName = new Array();
var fieldType = new Array();
var fieldErrorMsg = new Array();


function novaValidacao(strCampo, strTipo, strMensagem){

	fieldName[fieldName.length] 			= strCampo;
	fieldType[fieldType.length] 			= strTipo;
	fieldErrorMsg[fieldErrorMsg.length] 	= strMensagem;
	
}

function checkForm(formName){
	formRef = "document." + formName;
	count = 0;
	for(i=0;i<fieldName.length;i++){
		if(checkField(fieldName[i],fieldType[i])){
			count++;
		}
		else{
			if(fieldErrorMsg[i]){
				alert(fieldErrorMsg[i]);
				if(fieldName[i].indexOf(",") != -1)
				{
					target = fieldName[i].split(",");
					eval(formRef + "." + target[0]).focus();
					break;
				}
				if(eval(formRef + "." + fieldName[i])[0])
				{
					eval(formRef + "." + fieldName[i])[0].focus();
				}
				else
				{
					eval(formRef + "." + fieldName[i]).focus();
				}
			}
			return;
		}
	}
	if(count == fieldName.length){
		eval(formRef).submit();
	}
}

function checkField(field,type){
	switch(type){
		case 'notEmpty':
			if(isEmpty(field)){
				return false;
			}
			else{
				return true;
			}
		break;
		case 'Email':
			if(isMail(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'Number':
			if(isNumber(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'EmptyorNumber':
			if(isEmptyorNumber(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'Equal':
			if(isEqual(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		
		case 'Day':
			if(isDay(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'Month':
			if(isMonth(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'Year':
			if(isYear(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'Selected':
			if(isSelected(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'Checked':
			if(isChecked(field)){
				return true;
			}
			else{
				return false;
			}
		break;		
		case 'Length':
			if(isLength(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'CEP':
			if(isCEP(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'isDate':
			if(isDate(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'Pair':
			if(isPair(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'CPF':
			if(isCPF(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		case 'CNPJ':
			if(isCNPJ(field)){
				return true;
			}
			else{
				return false;
			}
		break;
		default:
			alert('Os tipos que podem ser validados são: notEmpty ; Email ; Number ; Day ; Month ; Year ; CEP ; Selected ; Checked ; Equal; Pair; EmptyorNumber; Length; CPF. Você selecionou:' + type);
			return false;
		break;
	}
}


function isEmpty(campo){
	if(eval(formRef+'.'+campo).value == ""){
		return true;
	}
	else{
		return false;
	}
}

function isNumber(campo){
	field = eval(formRef+'.'+campo).value;
	if(isNaN(field) || field.indexOf('e') != -1 || field == ""){
		return false;
	}
	else{
		return true;
	}
}

function isEmptyorNumber(campo){
	field = eval(formRef+'.'+campo).value;
	if(isNaN(field)){
		return false;
	}
	else{
		return true;
	}
}

function isDay(campo){
	field = eval(formRef+'.'+campo).value;
	intDay = field;
	if(isNaN(field) || field < 1 || field > 31 || field == ""){
		return false;
	}
	else{
		return true;
	}
}

function isMonth(campo){
	field = eval(formRef+'.'+campo).value;
	if(isNaN(field) || field < 1 || field > 12 || field == ""){
		return false;
	}
	else{
		if (intDay != '')
		{
			if (field == 2 && intDay > 28)
			{
				return false;
			}else{
				return true;
			}
		}else{
			return true;
		}
	}
}


function isLength(campo){
	fields = campo.split(",");
	field = eval(formRef+'.'+fields[0])
	if (field.value.length >= fields[1])
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isEqual(campo){
	fields = campo.split(",");
	value = eval(formRef+'.'+fields[0]).value;
	for(k=1;k<fields.length;k++)
	{
		if(eval(formRef+'.'+fields[k]).value != value)
		{
			return false;
		}
	}
	return true;
}


function isYear(campo){
	field = eval(formRef+'.'+campo).value;
	tempo = new Date();
	anoAtual = tempo.getYear();		
	if (anoAtual < 1900)
	{
		anoAtual += 1900;
	}
	if(isNaN(field) || field < 1900 || field == ""){
		return false;
	}
	else
	{
		return true;
	}
}
/*
function isLength(campo,num){
	field = eval(formRef+'.'+campo).value;
	if(field.length != num){
		return false;
	}
	else{
		return true;
	}
}
*/
function isSelected(campo){
	field_value = eval(formRef+"."+campo+"["+formRef+"."+campo+".selectedIndex"+"]"+".value");
	if(field_value == ""){
		return false;
	}
	else{
		return true;
	}
}

function isChecked(campo){
	field = eval(formRef+'.'+campo);
	selected = false;
	if (field[0])
	{
		for (x=0;x!=field.length;x++)
		{
			if(field[x].checked)
			{
				selected = true;
			}
		}	
	}
	else
	{
		if(field.checked)
		{
			selected = true;
		}	
	}
	
	return selected;
}


function isMail(campo){
	field = eval(formRef+'.'+campo).value;
	if(field.indexOf("@") != -1){
		mail_back = field.substring(field.indexOf("@")+1,field.length);
		if(mail_back.indexOf(".") > 0){
			mail_end = mail_back.substring(mail_back.lastIndexOf(".")+1,mail_back.length);
			if(mail_end.length != 2 && mail_end.length != 3){
				return false;
			}
			else{
				return true;
			}		
		}
		else{
			return false;
		}
	}
	else{
		return false;
	}
}

function isCEP(campo){
	field = eval(formRef+'.'+campo).value;
	if(field.indexOf("-") != -1){
	 	var begin = field.substring(0,field.indexOf("-"));
		var end = field.substring(field.indexOf("-") + 1, field.length);
		if(begin.length != 5 || end.length != 3 || isNaN(begin) || begin.indexOf('e') != -1 || isNaN(end) || end.indexOf('e') != -1){
			return false;
		}
		else{
			return true;
		}
	}
	else{
		if(field.length == 8 && isNumber(campo)){
			return true;
		}
		else{
			return false;
		}
	}
	
}
function isCPF(campo){
	var i;
	valor = eval(formRef+'.'+campo).value;
	primeiro=valor.substr(1,1);
	falso=true;
	size=valor.length;
	if (size!=11){
		return false;
	}
	size--;
	for (i=2; i<size-1; ++i){
		proximo=(valor.substr(i,1));
		if (primeiro!=proximo) {
			falso=false
		}
	}
	if (falso){
		return false;
	}

   	if(modulo(valor.substring(0,valor.length - 2)) + "" + modulo(valor.substring(0,valor.length - 1)) != valor.substring(valor.length - 2,valor.length)) {
   		return false;
   	}
   	return true;
}

function isPair(campo){
	field = eval(formRef+'.'+campo).value;
	if(isNaN(field) || field.indexOf('e') != -1 || field == "" || field%2 != 0){
		return false;
	}
	else{
		return true;
	}
}


function isDate(campo)
{
	arrCampos = eval(formRef+'.'+campo).value.split("/");
	dia=arrCampos[0];
	mes=arrCampos[1];
	ano=arrCampos[2];
  if ((mes >= 1)&&(mes <= 12)&&(ano > 0)) {
    if((dia >= 1)&&(dia <= DiasMes(mes,ano))) {
      return(true);
    } else {
      return(false); }
  } else {
    return(false); } 
}


 
function DiasMes(mes,ano) {
  if(mes == 2) {
    if(((ano % 4) != 0)||(((ano % 100) == 0) && ((ano % 400) != 0))) {
      return(28); 
    } else {
      return(29); }
  } else {
    if(mes < 8) {
      return(30 + (mes % 2));
    } else { 
      return(31 - (mes % 2)); } }
}

function modulo(str) {
   	soma=0;
   	ind=2;
   	for(pos=str.length-1;pos>-1;pos=pos-1) {
   		soma = soma + (parseInt(str.charAt(pos)) * ind);
   		ind++;
   		if(str.length>11) {
   			if(ind>9) ind=2;
   		}
	}
   	resto = soma - (Math.floor(soma / 11) * 11);
   	if(resto < 2) {
    	return 0
   	}
   	else {
   		return 11 - resto
   	}
}

function isNUMB(c)
	{
	if((cx=c.indexOf(","))!=-1)
		{		
		c = c.substring(0,cx)+"."+c.substring(cx+1);
		}
	if((parseFloat(c) / c != 1))
		{
		if(parseFloat(c) * c == 0)
			{
			return(1);
			}
		else
			{
			return(0);
			}
		}
	else
		{
		return(1);
		}
	}

function LIMP(c)
	{
	while((cx=c.indexOf("-"))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("/"))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(","))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("."))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("("))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(")"))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(" "))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	return(c);
	}
	
function isCNPJ(campo){
	valor = eval(formRef+'.'+campo).value;
	if(VerifyCNPJ(valor) == 1)
		{
		return true;
		}
	else
		{
		return false;
		}
}

function VerifyCNPJ(CNPJ)
	{
	CNPJ = LIMP(CNPJ);
	if(isNUMB(CNPJ) != 1)
		{
		return(0);
		}
	else
		{
		if(CNPJ == 0)
			{
			return(0);
			}
		else
			{
			g=CNPJ.length-2;
			if(RealTestaCNPJ(CNPJ,g) == 1)
				{
				g=CNPJ.length-1;
				if(RealTestaCNPJ(CNPJ,g) == 1)
					{	
					return(1);
					}
				else
					{
					return(0);
					}
				}
			else
				{
				return(0);
				}
			}
		}
	}
function RealTestaCNPJ(CNPJ,g)
	{
	var VerCNPJ=0;
	var ind=2;
	var tam;
	for(f=g;f>0;f--)
		{
		VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
		if(ind>8)
			{
			ind=2;
			}
		else
			{
			ind++;
			}
		}
		VerCNPJ%=11;
		if(VerCNPJ==0 || VerCNPJ==1)
			{
			VerCNPJ=0;
			}
		else
			{
			VerCNPJ=11-VerCNPJ;
			}
	if(VerCNPJ!=parseInt(CNPJ.charAt(g)))
		{
		return(0);
		}
	else
		{
		return(1);
		}
	}
