//#############################################################
//#               Script para validação de Dados              #
//# [Data]       : Cascavel, 06/05/2003                       #
//# [Autor]      : Eduardo Luiz Mizerkowski                   #
//# [Versão]     : v0.1.1                                     #
//#############################################################

function ES_validateForm() { //v3.0
	var i,p,q,z,x,nm,test,num,min,max,setFocus="",x,errors="",args=ES_validateForm.arguments;
	for (i=0; i<(args.length-3); i+=4)
	{ 
		test=args[i+2];
		mask=args[i+1];
		val=MM_findObj(args[i]);
		if (val)
		{ 
			nm=args[i+3];
			x=val;
			if (val.type == "select-one") 
			{
				if (val.selectedIndex == -1) 
				{
					val = ""
				}
				else 
				{
					val = val.options[val.selectedIndex].value
				}
			}
			else 
			{
				val=TrimJava(val.value)
			}
			if ((val) != "") 
			{
				if (test.indexOf("isEmail")!=-1) 
				{ 
					p=val.indexOf("@");
					z=val.indexOf(".");
					if ((p<1 || p==(val.length-1)) || (z<1 || z==(val.length-1))) 
					{
						errors+="# - "+nm+" não é válido.\n";
					}
				} 
				if (test.indexOf(">") != -1)
				{
					x.value = x.value.toUpperCase();
				}
				if (test.indexOf("isCNPJ") != -1)
				{
					if (!ChecaCNPJ (val))
					{
						errors+="# - "+nm+" não é válido.\n";
					}
				}
				if (test.indexOf("isCPF") != -1)
				{
					if (!ChecaCPF (val))
					{
						errors+="# - "+nm+" não é válido.\n";
					}
				}
				if (test.indexOf("isData") != -1)
				{
					if (!ValidaData(val, mask.toUpperCase()))
					{
						errors+="# - "+nm+" não é válida.\n";
					}
				}
				if (test.indexOf("isMoeda") != -1) 
				{
					if (!ValidaFloat(val, mask.charAt(0), mask.charAt(1), 7, 3, true)) 
					{
						errors+="# - "+nm+" deve ser numérico.\n";
					}
				}
				if (test.indexOf("isMoed2") != -1) 
				{
					if (!ValidaFloat(val, mask.charAt(0), mask.charAt(1), 3, 2, true)) 
					{
						errors+="# - "+nm+" deve ser numérico.\n";
					}
				}
				if (test.indexOf("isFloat") != -1) 
				{
					if (!ValidaFloat(val, mask.charAt(0), mask.charAt(1), 0, mask.charAt(2), false))
					{
						errors+="# - "+nm+" deve ser numérico.\n";
					}
				}
				if (test.indexOf("isQtde") != -1) 
				{
					if (!ValidaFloat(val, mask.charAt(0), mask.charAt(1), 7, 3, true))
					{
						errors+="# - "+nm+" deve ser numérico.\n";
					}
				}
				if (test.indexOf("isQtd2") != -1) 
				{
					if (!ValidaFloat(val, mask.charAt(0), mask.charAt(1), 21, 3, true))
					{
						errors+="# - "+nm+" deve ser numérico.\n";
					}
				}
				if (test.indexOf("isGenQtde") != -1)
				{
					if (!ValidaFloat(val, mask.charAt(0), mask.charAt(1), mask.charAt(2), mask.charAt(3), true))
					{
						errors+="# - "+nm+" deve ser numérico.\n";
					}
				}
				if (test.indexOf("isHorario") != -1)
				{
					if (!ValidaHora(val)) 
					{
						errors+="# - "+nm+" deve ser no formato hora.\n";
					}
				}
				if (test.indexOf("isText") != -1)
				{
					if (!isOnlyLetters(val)) 
					{
						errors+="# - "+nm+" deve ser no formato texto.\n";
					}
				}
				if (test.indexOf("isLong") != -1)
				{ 
					if (!checkInt(val))
					{
						errors+="# - "+nm+" deve ser numérico.\n";
					}
				}
				if (test.indexOf("LimitSize") != -1)
				{
					if (x.value.length > parseFloat(mask))
					{
						errors+="# - "+nm+" tamanho "+mask+" tamanho.\n";
					}
				}
				if (test.indexOf("isNum") != -1)
				{ 
					if (!checkInt(val))
					{
						errors+="# - "+nm+" deve ser numérico.\n";
					}
					else
					{
						if (!ValidaFloat(val, ',', '.', 7, 0, true))
						{
							errors+="# - "+nm+" deve ser numérico.\n";
						}

					}
				}
				if (test.indexOf("inRange") != -1)
				{
					if (checkVal(val)) {
						num = parseFloat(val);
						p=test.indexOf(":");
						min=test.substring(8,p); max=test.substring(p+1);
						if (num< min || max< num) 
						{
							errors+="# - "+nm+" range "+min+" e "+max+".\n";
						}
					}
					else {
						errors+="# - "+nm+" deve ser numérico.\n";
					}
				}
				if (test.indexOf("isPercent") != -1) 
				{
					if (!ValidaFloat(val, mask.charAt(0), mask.charAt(1), 3, 3, true))
					{
						errors+="# - "+nm+" deve ser numérico.\n";
					}
					else
					{
						val = parseFloat(DesFormataFloat(val,mask.charAt(0),mask.charAt(1)));
						if ((val < 0) || (val > 100))
							errors+="# - "+nm+" precisa estar entre 0 e 100.\n";
					}
				}
				if ((test.charAt(0) == "R") && 
					((test.indexOf("isMoeda") != -1) || (test.indexOf("isFloat") != -1) || 
					(test.indexOf("isQtde") != -1) || (test.indexOf("isQtd2") != -1)))
				{
					if (parseFloat(DesFormataFloat(val,mask.charAt(0),mask.charAt(1))) <= 0)
					{
						errors += "# - "+nm+" requerido.\n";
					}
				}
			}
			else if (test.charAt(0) == "R")
			{
				errors += ":: > "+nm+" requerido.\n";
			}
			if (x.type == "text")
			{
				x.value = x.value.replace("'",""); 
			}
		} 
		if ( (errors != "") && ( !setFocus ) )
		{
			setFocus=x;
		}
	}
	if (errors)
	{
	    var mensagem
		var mensagem2
		mensagem = "......................................................\n"+
		           ":: Os Seguintes erros foram encontrados:\n"+
				   "......................................................\n"+
				   "::\n"
				   
	    mensagem2 = "::\n"+
		            "......................................................\n"+
					"\t\t\tPowered by ViaBR.net\n"
		alert(mensagem+errors+mensagem2);
		if ((setFocus.type == "text") || (setFocus.type == "select-one") || (setFocus.type == "radio") || (setFocus.type == "checkbox"))
		{
			setFocus.focus();
		}
		if (setFocus.type == "text")
		{
			setFocus.select();
		}
	}  
	document.MM_returnValue = (errors == "");
}

function chkdate(pStrDate, format_us) 
{
	var strDate = pStrDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Janeiro";
	strMonthArray[1] = "Fevereiro";
	strMonthArray[2] = "Março";
	strMonthArray[3] = "Abril";
	strMonthArray[4] = "Maio";
	strMonthArray[5] = "Junho";
	strMonthArray[6] = "Julho";
	strMonthArray[7] = "Agosto";
	strMonthArray[8] = "Setembro";
	strMonthArray[9] = "Outubro";
	strMonthArray[10] = "Novembro";
	strMonthArray[11] = "Dezembro";
	if (strDate.length < 1) 
	{
		return false;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
	{
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
		{
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) 
			{
				err = 1;
				return false;
			}
			else 
			{
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}
	if (booFound == false) 
	{
		if (strDate.length>5) 
		{
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
		else
		{
			return false;
		}
	}
	if (strYear.length == 2) 
	{
		strYear = "20" + strYear;
	}
	if (format_us)
	{
		temp = strDay;
		strDay = strMonth;
		strMonth = temp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday))
	{
		err = 2;
		return false;
	}

	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) 
	{
		for (i = 0;i<12;i++) 
		{
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
			{
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) 
		{
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) 
	{
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) 
	{
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
	{
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
	{
		err = 7;
		return false;
	}
	if (intMonth == 2) 
	{
		if (intday < 1) 
		{
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) 
		{
			if (intday > 29) 
			{
				err = 9;
				return false;
			}
		}
		else 
		{
			if (intday > 28) 
			{
				err = 10;
				return false;
			}
		}
	}
	// intday + "/" + strMonthArray[intMonth-1] + "/" + strYear;
	return true;
}