/*
Função para validar formulario baseada na função de validação do dreamweaver
parametros: MM_validateForm(['campoformulario'],['texto de aviso'],['parametro de validação'])
parametro de validação :  
R -> campo text
C -> campo menu/list
CK -> campo checkbox ou radiobutton
RCFP -> campo text com CPF (não esquecer de usar o arquivo cpf.js)
RisEmail -> campo text com e-mail
RisNum -> campo text númerico
ex: onClick=(MM_validateForm('nome','Nome','R','email','E-mail','RisEmail','cpf','C.P.F.','RCPF','sexo','Sexo','CK'));
*/

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 MM_validateForm() { //v4.0
	var i, j, p, q, nm, test, num, min, max, errors='', args=MM_validateForm.arguments;
	for (i=0; i<(args.length-2); i+=3) { 
		test=args[i+2]; val=MM_findObj(args[i]);
		
		if (val) {
			nm=val.name; 
			if (test.substring(0,7) == "inRange") test = "RisNum"
			switch (test) {
				case "H" : //Campo para validação de hora
					if (val.value=="") errors = '- '+args[i+1]+' é necessário.\n';
					if (val.value > 23) errors = '- '+args[i+1]+' incorreto.\n';	
				break;
				
				case "M" : //Campo para validação de minutos
					if (val.value=="") errors = '- '+args[i+1]+' é necessário.\n'; 
					if (val.value > 59) errors = '- '+args[i+1]+' incorreto.\n';	
				break;
				
				case "R" ://Qualquer tipo de campo requerido
					if(val.value=="") errors = '- '+args[i+1]+' é necessário.\n';
				break;

				case "C" ://campo menu/list
					if(val.value=="0") errors = '- '+args[i+1]+' é necessário.\n';
				break;
				
				case "CK" ://campo checkbox ou radiobutton
					var tam, tipo;
					tam = val.length;	
					tipo = false;
					if (typeof(tam)=='undefined') {
						if (eval("document.form1." + args[i] + ".checked")) tipo = true;
						if (!tipo) errors = '- '+args[i+1]+' é necessário.\n';
					}
					else {
						for(j=0;j<tam;j++) {
							if (eval("document.form1." + args[i] + "[" + j +"].checked")) {
								tipo = true;
								nm = args[i] + "";
							}
						}
						if (!tipo) 	{			
							errors = '- '+args[i+1]+' é necessário.\n';
							nm = args[i] + "[0]";
						}
					}
				break;
				
				case "RCPF" ://campo text com CPF
					if(val.value=="") errors = '- '+args[i+1]+' é necessário.\n';
					else if (!CPF(val.value)) errors = '- O número do '+args[i+1]+' está incorreto favor confirmar.\n';
				break;

				
				case "RisEmail" ://campo text com e-mail
					val=val.value
					if (val.length != 0) if (val.indexOf('@') == -1) errors+='- '+args[i+1]+' deve conter um endereço de e-mail.\n';
				break;
				
				case "RisNum" ://campo text númerico
					val=val.value
					if (val!="") {
						if (isNaN(val)) errors+='- '+args[i+1]+' deve conter números.\n';
						test = args[i+2];
						if (test.indexOf('inRange') != -1) { 
							p=test.indexOf(':');
							min=test.substring(8,p); 
							max=test.substring(p+1);
							if (parseInt(val)<parseInt(min) || parseInt(max)<parseInt(val)) errors+='- '+args[i+1]+' deve conter números entre '+min+' e '+max+'.\n';
						}
					}
				break;
			}
		}
		
		if (errors) {
			alert('Erro:\n'+errors);
			if(test!= 'C' && test!= 'CK') eval("form1." + nm + ".value='';");
			eval("document.form1." + nm + ".focus();");
			return false;
		}
	} 
	return true;
}
