/********************************************************************************
 Descrição : Modulo de Validações Genéricas para Formulários.
 Autor     : Victor Barros
 Data      : 12/12/2007
 ****************************** Modificações *************************************
 Descrição : Permitir o uso de "(aspas) no atributo mascara.
 Autor     : Victor Barros
 Data      : 12/12/2007
 ****************************** Modificações *************************************
 Descrição : Colocar validação com funções padrões.
 Autor     : Victor Barros
 Data      : 12/12/2007
 ****************************** Modificações *************************************
 Descrição : Colocar validação de Radion e Select.
 Autor     : Victor Barros
 Data      : 12/12/2007
********************************************************************************/
var strMsgErroPrincipal = null;
function ValidaForm(frm) {
	var intQtdInput = frm.elements.length;
	var alertMsg = "";
	
	for (i=0; i < intQtdInput; i++) {
		var objCampo = frm.elements[i];
		if (objCampo.id == null) {
			alert('Atenção Desenvolvedor \n INPUT sem id.' + objCampo.name);
			return false;
		}
		if (objCampo.type == "text" || objCampo.type == "textarea" || objCampo.type == "password") {
			/************* Aqui começa a validação *************/
			if (objCampo.value == "") {
				if (objCampo.attributes["required"] != null) {
					if (objCampo.attributes["required"].value == "true") {
						// Se o campo required estiver = true então o campo não pode estar vazio.
						var strRequiredMSG = objCampo.attributes["requiredmsg"].value;
						alertMsg += ("\t - " + strRequiredMSG + "\n");
					}
				}
			} else {
				if (objCampo.attributes["mascara"] != null && objCampo.attributes["errormsg"] != null) {
					// Se houver uma mascara e uma mensagem de erro, o campo é validado.
					if (objCampo.attributes["mascara"].value.search('^FNC#') != -1) {
						var strMascara = objCampo.attributes["mascara"].value.replace('FNC#', '');
						strMascara = eval(strMascara);
					}
					else { var strMascara = objCampo.attributes["mascara"].value; }
					var strErrorMSG = objCampo.attributes["errormsg"].value;
					var erroBoolean = false;
					//alert("mascara:" + strMascara + "\n MsgErro:" + strErrorMSG);
					switch (strMascara) 
					{
						case "true" :
							break;
							
						case "false":
							alertMsg += ("\t - " + strErrorMSG + "\n");
							break;
							
						default :
							if (objCampo.value.search(strMascara) == -1)
								alertMsg += ("\t - " + strErrorMSG + "\n");
					}//switch
				}//if
			}//else
		}//If(text|textarea|password)
		else {
			if (objCampo.type == "radio"){
				/*var checked = false;
				alert(objCampo.checked);
				for (var j=0; j < objCampo.length; j++){
					alert(objCampo[j].checked);
					if (objCampo[j].checked) { checked = true; }
				}
				if (checked && objCampo[0].attributes["required"].value != Null) { 
					alertMsg += ("\t - " + objCampo.attributes["requiredmsg"].value + "\n");
				}*/
			} else {
				if (objCampo.type == "select-one"){
					if (objCampo.options[objCampo.selectedIndex].value == ""){
						if (objCampo.attributes["required"] != null) {
							if (objCampo.attributes["required"].value == "true")
								alertMsg += ("\t - " + objCampo.attributes["requiredmsg"].value + "\n");	
						}
					}
				}
			}
		}
	}//for
	if(alertMsg.length>0) {
		if (strMsgErroPrincipal != null) {
			alert(strMsgErroPrincipal + "\n\n" + alertMsg);
			return false;
		}
		else {
			alert("Foram detectados os seguintes erros:\n\n" + alertMsg);
			return false;
		}
	}
	else
		return true;
}
function SomenteNumeros() { return "^\\d+$"; }
function SomenteLetras() { return "^[\\D]+$"; }
function Data() { return "^((0?[1-9]|[12]\\d)\\/(0?[1-9]|1[0-2])|30\\/(0?[13-9]|1[0-2])|31\\/(0?[13578]|1[02]))\\/(19|20)?\\d{2}$"; }
function Email() { return "^[A-Za-z0-9]+([._+]{1}[a-zA-Z0-9]+)*[@]{1}[A-Za-z0-9]+([._]{1}[a-zA-Z0-9]+)*"; }
function Telefone(pais) {
	switch (pais){
		case "USA" || "CANADA":
			return "^[\\d]{10}$";
			break;
		default:
			return "^[\\d]{4}-?[\d]{4}$";
			break;
	}
}
function Cep(country) {
	switch (country) {
		case "Australia" || "Austria" || "Belgium" || "Netherlands" || "Denmark" || "Switzerland":
			return "^\\d{4}$";
			break;
		case "France" || "Espana" || "spain" || "Germany" || "United States" || "USA" || "Thailand" || "Sweden" || "Puerto Rico" || "Mexico" || "Italy":
			return "^\\d{5}$";
			break;
		case "Malaysia" || "India" || "China" || "Russia" || "Chile" || "Singapore":
			return "^\\d{6}$";
			break;
		case "Japan":
			return "^\\d{7}$";
			break;
		case "Great Britain":
			return "^[a-zA-Z][\\da-zA-Z][\\da-zA-Z][\\da-zA-Z][\\da-zA-Z][a-zA-Z]?[a-zA-Z]?$";
			break;
		case "Canada":
			return "^[0-9a-zA-Z]\\d[0-9a-zA-Z] \\d[0-9a-zA-Z]\\d$";
			break;
		default:
			return "^\\d{5}[-]?\\d{3}$";
			break;
	}
}










