function chkSubmit(arRules,frm){
	//Valida campos
	ValidForm = ValidaCampos(arRules);
	if(ValidForm == true){
		//alert('Formulário OK!');
		document.getElementById(frm).submit();
		return true;
	}else{
		alert(ValidForm);
		return false;
	}
}

function ValidaCampos(arRules){
	var Errors = 0;
	var sMsgErro = 'Os seguintes campos não foram preenchidos ou estão incorretos:\n';
	for (rules = 0; rules < arRules.length; rules++){
		if(document.getElementById(arRules[rules][0])){
			sRule = arRules[rules][1];
			switch (sRule){
				case "empty":
					if(isEmpty(document.getElementById(arRules[rules][0]).value)){ Errors++;
						sMsgErro += '- ' + arRules[rules][2] + '\n';
					}
					break;
				case "email":
					if(!isEmail(document.getElementById(arRules[rules][0]).value)){ Errors++;
						sMsgErro += '- ' + arRules[rules][2] + '\n';}
					break;
				case "date":
					if(!isDate(document.getElementById(arRules[rules][0]).value)){ Errors++;
						sMsgErro += '- ' + arRules[rules][3] + '\n';}
					break;
				case "datefmt":
					if(!isDateFmt(document.getElementById(arRules[rules][0]).value,arRules[rules][2])){ Errors++;
						sMsgErro += '- ' + arRules[rules][3] + '\n';}
					break;
				case "selected":
					if(!isSelected(document.getElementById(arRules[rules][0]))){Errors++;
						sMsgErro += '- ' + arRules[rules][2] + '\n';}
					break;
				case "radio":
					if(!isCheckedRadio(document.getElementById(arRules[rules][0]), arRules[rules][2])){Errors++;
						sMsgErro += '- ' + arRules[rules][3] + '\n';}
					break;
				case "checked":
					if(!isChecked(document.getElementById(arRules[rules][0]))){Errors++;
						sMsgErro += '- ' + arRules[rules][2] + '\n';}
					break;
				default:
					alert('Erro!\n\nRegra de validação inválida:\n' + arRules[rules][1]);
			}
		}else{
			alert('Erro!\n\nObjeto inválido nas regras de validação:\n' + arRules[rules][0])
		}
	}
	if (Errors > 0){/*alert(Errors);*/ return sMsgErro;}
	else{return true;}
}

function isEmail(text){
	var arroba = "@",ponto = ".",posponto = 0,posarroba = 0;
	if (text =="") return false;
	for (var indice = 0; indice < text.length; indice++){if (text.charAt(indice) == arroba){posarroba = indice;break;}}
	for (var indice = posarroba; indice < text.length; indice++){if (text.charAt(indice) == ponto) {posponto = indice;break;}}
	if (posponto == 0 || posarroba == 0) return false;
	if (posponto == (posarroba + 1)) return false;
	if ((posponto + 1) == text.length) return false;
	return true;
}


function isDate (strData){
	var arrData = strData.split("/");
	for (var i in arrData) if (isNaN(arrData[i])) return false;
	if (arrData.length!=3) return false;
	digitosAno = arrData[2].length;
	if (digitosAno==4) return (Number(arrData[0])<=31 && Number(arrData[1])<=12 && Number(arrData[2])>=1900 && Number(arrData[2])<=2005);
	else if (digitosAno==2) return (Number(arrData[0])<=31 && Number(arrData[1])<=12 && Number(arrData[2])>=1 && Number(arrData[2])<=99);
	else return false;
}

function isDateFmt( data, format ){
	var tdate, ad, rd, dm, tmpstr, pDay, pMonth, pYear;
	format = format.toUpperCase();
	pDay = format.indexOf('D');
	pMonth = format.indexOf('M');
	pYear = format.indexOf('Y');
	dm = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	data = data.toString().replace(/\-/gi, "/");
	ad = data.split('/');
	if (ad.length != format.length) return false;
	if((ad[pMonth]>=1 && ad[pMonth]<=12)==false) return false;
	if(Math.floor(ad[pYear]/4)*4==ad[pYear]) dm[1] = 29;
	if(ad[pYear].toString().length != 4) return false;
	if((ad[pDay]>=1 && ad[pDay]<=dm[ad[pMonth]-1])==false) return false;
	for(i=0; i<ad.length; i++){if ((isEmpty(ad[i])) || (!isNumber(ad[i])) || (parseInt(ad[i])<0)) return false;}
	return true;
}

function isEmpty(text){
   var enter1 = "\n",enter2 = "\r",espaco = " ",tab = "\t";
   if (text =="") return true;
   for (var indice = 0; indice < text.length; indice++){if (text.charAt(indice) != espaco && text.charAt(indice) != tab &&text.charAt(indice) != enter1 && text.charAt(indice) != enter2 )return false;}
   return true;
}

function isSelected(obj){
	if(obj.options[obj.selectedIndex].text == ''){
		return false;
	}else{
		return true;
	}
}

function isChecked(obj){
	if(obj.checked != true){
		return false;
	}else{
		return true;
	}
}

function isCheckedRadio(obj,grp){
	var Checks = 0;
	//alert(obj);

	chkGroup = obj.name;
	chkForm = obj.form;
	for(elem=0;elem < chkForm.elements.length;elem++){
		if(chkForm.elements[elem].name == grp){
			if(chkForm.elements[elem].checked == true){
				Checks++;
			}
		}
	}
	if(Checks > 0){
		return true;
	}else{
		return false;
	}
	
}

function isNumber(numero){
   var CaractereInvalido = false;
   for (i=0; i < numero.length; i++){var Caractere = numero.charAt(i);if(Caractere != "." && Caractere != "," && Caractere != "-"){if (isNaN(parseInt(Caractere))) CaractereInvalido = true;}}
   return !CaractereInvalido;
}


//Auxiliares

function ReplaceX( texto, procurar, novo ){
   len = procurar.length;
   pos = texto.indexOf(procurar);
   while (pos > -1){parte1 = texto.substring(0, pos);parte2 = texto.substring(pos + len , texto.length);texto = parte1 + novo + parte2;pos = texto.indexOf(procurar);}
   return texto;
}

function splitstr( str, strdiv ){
   var vetret, vetpos, ocorr, i, i2, ini, fim;
   ocorr = 0;
   for (i=0; i < str.length; i++) if ( str.charAt(i) == strdiv) ocorr++;
   vetret = new Array(ocorr);
   vetpos = new Array(ocorr);
   i2=0;
   for (i=0; i < str.length; i++)if( str.charAt(i) == strdiv){vetpos[i2]=i;i2++;}
   i2=0; ini=0; fim=0;
   for (i=0; i<ocorr+1; i++){if(i == vetpos.length)fim = str.length;elsefim = vetpos[i];vetret[i]=str.substring(ini, fim);ini = vetpos[i]+1;}
   return vetret;
}

function joinDates(d,m,y,x){
	current_date = new Date();
	current_year = current_date.getYear();
	//if(document.getElementById(y).value > 30){
	//	ano = '19' + document.getElementById(y).value;
	//}else{
	//	ano = '20' + document.getElementById(y).value;
	//}
	ano = document.getElementById(y).value;
	newDate = document.getElementById(d).value + '/' + document.getElementById(m).value + '/' + ano;
	document.getElementById(x).value = newDate;
}