<!-- 
/******************************************
/* Validação dos campos
/*****************************************/

// Validação de Formulários
MM_validateForm = function() { 
	var i, p, q, nm, test, num, min, max, errors = '', args = MM_validateForm.arguments;

	for (i = 2; i < (args.length-2); i += 3) { 
		erro = '';
		val = MM_findObj(args[i]);		
		val.className = args[0];		
		test = args[i+2]; 
		if (val) { 
			nm = args[i+1];
			if (test.indexOf('isCheckbox')!=-1) {
				if (! testaCheckbox(MM_findObj(args[i])))
					erro = '- ' + nm + ': você deve selecionar pelo menos uma opção.\n';
			} else {
				if ((val = val.value) != '') {
					if (test.indexOf('isEmail') != -1)
						if (! testaEmail(val))
							erro = '- ' + nm + ' deve conter um endereço de e-mail.\n';
					if (test.indexOf('isCPF') != -1)
						if (! testaCPF(val))
							erro = '- ' + nm + ' deve conter um CPF válido.\n';						
					if (test.indexOf('isCNPJ') != -1)
						if (! testaCNPJ(val))
							erro = '- ' + nm + ' deve conter um CNPJ válido.\n';
					if (test.indexOf('isData') != -1)
						if (! testaData(val))
							erro = '- ' + nm + ' deve conter uma data válida.\n';
					if (test.indexOf('isHora') != -1)
						if (! testaHora(val))
							erro = '- ' + nm + ' deve conter uma hora válida.\n';
					if (test.indexOf('isLink') != -1)
						if (! testaLink(val))
							erro = '- ' + nm + ' deve conter um link válido (com HTTP://).\n';
					if (test.indexOf('isNum') != -1)
						if (isNaN(val))
							erro = '- ' + nm + ' deve conter um número.\n';
					if (test.indexOf('isInt') != -1)
						if (! isInteger(val))
							erro = '- ' + nm + ' deve conter um número inteiro.\n';
					if (test.indexOf('isSeguro') != -1)
						if (! testaSeguro(val))
							erro = '- ' + nm + ' deve conter somente letras, _ e números.\n';
					if (test.indexOf('isLogin') != -1)
						if (! testaLogin(val))
							erro = '- ' + nm + ' deve conter de 4 a 30 caracteres e somente letras, números e os caracteres (._-).\n';
					if (test.indexOf('isSenha') != -1)
						if (! testaSenha(val))
							erro = '- ' + nm + ' deve conter de 4 a 30 caracteres e somente letras, números e os caracteres (@._-).\n';
				} else {
					if (test.charAt(0) == 'R') 
						erro = '- ' + nm + ' é requerido.\n';
				}
			}
		}
		if (erro != '') {
			errors += erro;
			val = MM_findObj(args[i]);			
			val.className = args[1];
		}
	}

	if (errors)
		alert('Erro:\n' + errors);

	document.MM_returnValue = (errors == '');
	return (errors == '');
}

MM_findObj = function(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;
};

// Verifica se é inteiro
isInteger = function(num){
	if (isNaN(num))
		return false;

	var a = parseInt(num),
		b = parseFloat(num);

	return (a == b); //  && num.indexOf('.') == -1
}

// Testa um login de usuário
testaLogin = function(str){
	var regex = /^[A-Za-z0-9._-]{4,30}$/; // login válido
	return regex.test(str);
}

// Testa uma senha de usuário
testaSenha = function(str){
	var regex = /^[A-Za-z0-9@._-]{4,30}$/; // senha válida
	return regex.test(str);
}


// Testa um grupo de CheckBox ou RadioButton
testaCheckbox = function(objChk){
	for (j=0; objChk[j]; j++)
		if (objChk[j].checked)
			return true;
	return false;
}

// Testa um e-mail
testaEmail = function(strEmail){
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // e-mail inválido
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // e-mail válido
	return (!reg1.test(strEmail) && reg2.test(strEmail));
}

// Testa um CPF
testaCPF = function(strCPF){
    var c = strCPF;
    if((c = c.replace(/[^\d]/g,"").split("")).length != 11) return false;
    if(new RegExp("^" + c[0] + "{11}$").test(c.join(""))) return false;
    for(var s = 10, n = 0, i = 0; s >= 2; n += c[i++] * s--);
    if(c[9] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    for(var s = 11, n = 0, i = 0; s >= 2; n += c[i++] * s--);
    if(c[10] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    return true;
};

// Testa um CNPJ
testaCNPJ = function(strCNPJ){
    var b = [6,5,4,3,2,9,8,7,6,5,4,3,2], c = strCNPJ;
    if((c = c.replace(/[^\d]/g,"").split("")).length != 14) return false;
    for(var i = 0, n = 0; i < 12; n += c[i] * b[++i]);
    if(c[12] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    for(var i = 0, n = 0; i <= 12; n += c[i] * b[i++]);
    if(c[13] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    return true;
};

// Testa uma Data
testaData = function(strData){
	if (strData.length > 10) return false;
	if (!isNaN(strData)) return false;
	if ((separador1=strData.indexOf("/"))==-1) return false;
	if ((separador2=strData.indexOf("/",separador1+1))==-1) return false;
	var dia = strData.substring(0,separador1);
	var mes = strData.substring(eval(separador1 + 1),separador2);
	var ano = strData.substring(eval(separador2 + 1),strData.length);
	if (mes<1&&mes>12) return false;
	if (dia<1&&dia>31) return false;
	if (mes==2)
		if (dia>29) 
			return false;
		else
			if (dia==29&&((ano%4)!=0)) return false;
	if ((mes == 4)||(mes == 6)||(mes == 9)||(mes == 11))
		if ((dia <= 0 ) && (dia > 30)) return false;
	if ((mes == 1)||(mes == 3)||(mes == 5)||(mes ==7)||(mes == 8)||(mes == 10)||(mes == 12))
		if ((dia <= 0) && (dia > 31)) return false;
	return true;
};

// Testa uma hora
testaHora = function(horario) {
	if (horario.indexOf(".") == -1)
		if (horario.indexOf(":") != -1){
			div    = horario.indexOf(":");
			tam    = horario.length;
			hora   = horario.substring(0,div);
			minuto = horario.substring(div+1,tam);
			msg    = "";
			if ((!isNaN(hora))&&(!isNaN(minuto))){
				if ((minuto>=60)||(minuto<0))
					return false;
				if ((hora>24)||(hora<0))
					return false;
				return true;
			}
		}
	return false;
};

// Testa um link
testaLink = function(strLink) {
   var reg1 = /http?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?/;
   return reg1.test(strLink);
};

// Testa se um valor é seguro 
testaSeguro = function(strSeguro){
	var reg1 = /(\%27)|(\')|(\-\-)|(\%23)|(#)/i;
	return !reg1.test(strSeguro);
};

