// JavaScript Document
/* 

OBS : ESSE ARQUIVO PRECISA DO JQUERY
---------------------------------------------------
* Classe FormValidate
* Propriedades: elements - Array de elements
*/
function FormValidate(elements) {	
	this.fields = elements;
	this.alerted = false;
}

function validate() {
	var isInvalid = false;
	for (var i = 0; i < this.fields.length; i++) {
		var element = this.fields[i];
		isInvalid = this.checkType(element);
		
		if (isInvalid) {
			this.showMessage(element);
			break;
		}
	}
	return !isInvalid;
}

function checkType(element) {
	var isInvalid = false;

	switch (element.type) 
	{ 
		case "text" : isInvalid = this.validateText(element); break;
		case "email" : isInvalid = this.validateEmail(element); break;
		case "phone" : break;
	}
	
	return isInvalid;
}

function showMessage(element) {
	if (!this.alerted) {
		alert("Por favor, preencha o campo "+ element.fieldName + " corretamente");
		element.input.focus();
		this.alerted = true;
	}
}

function validateText(element) {
	var isInvalid = false;
	
	if (element.input != null) {	
		var str = element.input.value;	
		
		if (jQuery.trim(str) == "") {
			isInvalid = true;
		}
	} else {
		alert("Erro\nElemento - "+element.fieldName);
	}

	return isInvalid;
}

function validateEmail(element) {
	var isInvalid = false;
	var str = element.input.value;
	
	if (!this.validateText(element)) {
		var objRegExp  = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
		isInvalid = !objRegExp.test(str);
	} else {
		isInvalid = true;	
	}
	return isInvalid;
}
/*
* Fim da Classe
---------------------------------------------------
*/

/* 
---------------------------------------------------
* Classe Field
* Propriedades: input - input para ser validado
*				fieldName - nome do campo
* 				type - tipo de validação:
					   text : Texto comum
					   email: E-mail
					   phone: Telefone
*/
function Field(input,fieldName,type) {
	this.input = input;
	this.fieldName = fieldName;
	this.type = type;
}
/*
Fim da Classe
---------------------------------------------------
*/

//Prototypes
FormValidate.prototype.validate = validate; 
FormValidate.prototype.checkType = checkType;
FormValidate.prototype.showMessage = showMessage;
FormValidate.prototype.validateText = validateText;
FormValidate.prototype.validate = validate;
FormValidate.prototype.validateEmail = validateEmail;