var mensagem  = new Array();
mensagem[1]   = new Array();
mensagem[2]   = new Array();

mensagem[1]['FormError'] = 'You forgot to fill the field {%field}!';
mensagem[2]['FormError'] = 'O campo {%field} é obrigatório!';

mensagem[1]['InvalidField'] = 'Invalid field {%field}!';
mensagem[2]['InvalidField'] = 'Campo {%field} inválido!';

mensagem[1]['DontMatch'] = 'The fields {%field_1} and {%field_2} don\'t match!';
mensagem[2]['DontMatch'] = 'Os campos {%field_1} e {%field_2} não batem!';

mensagem[1]['MinLength'] = 'The field {%field} must be at least {%number} characters!';
mensagem[2]['MinLength'] = 'O campo {%field} tem que ter pelo menos {%number} characters!';

function c_Forms() {
	
	this.Valida = function (form, divs) {
		this.setForm(form);
		this.setLang(default_id_lang);
		
		if (divs == null || divs == undefined) {		
			this.setModalDiv('DV_MSG');
			this.setModalDivAux('DV_alternada');
		} else {
			this.setModalDiv(divs[0]);
			this.setModalDivAux(divs[1]);
		}
		
		lang = this.getLang();
		ret  = new Array();
		temp = "";
		k = 0;
		z = 0;
		
		msgerro = this.getMensagem(lang,'FormError');
		
		for (x = 0; x < form.elements.length; x++) {
		this.ValidMinLength(form.elements[x]);
			if (form.elements[x].type != 'select-multiple' && (requerido = form.elements[x].getAttribute("required"))) {
				form.elements[x].value = form.elements[x].value.trim();
			}
		
			if ((requerido = form.elements[x].getAttribute("required")) && form.elements[x].value == "" || this.getExceptions(form.elements[x])) {
				temp = this.Assign(msgerro,'field',requerido);
				ret[z] = temp +"\n";
				
				if (k < 1) {
					form.elements[x].focus();
					k++;
				}
				
				z++;
			}
		}

		this.ValidCompares();
		this.ValidEmails();
		this.ValidDates();
		this.ValidUrls();
		
		if (ret.length >0) {
			this.ShowModal();
			if (this.modifica_flag) {
				this.getModificaFlag().value = '';
			}
			
			location = this.getAncora();
			return false;
		}
				
		return true;
	}
	
	this.ValidDates = function () {
		
	}
	
	this.ValidUrls = function () {
		if (this.urls) {
			msgerro = this.getMensagem(lang,'InvalidField');
			for (x = 0; x < this.getURLs(); x++) {
				if (this.getURLs(x).value != "" && !this.getURLs(x).value.validURL()) {
					requerido = (!this.getURLs(x).title) ? this.getURLs(x).getAttribute("required") : this.getURLs(x).title;
					temp = this.Assign(msgerro,'field',requerido);
					
					ret[z] = temp +"\n";
					
					if (k < 1) {
						this.getURLs(x).focus();
						k++;
					}
					
					z++;
				}
			}
		}
	}
	
	this.ValidMinLength = function (field) {
		minimo     = field.getAttribute("minlength");
		requerido_ = (!field.title) ? field.getAttribute("required") : field.title;
		if (minimo  != null && field.value != "" && minimo > field.value.length) {
			msgerro_ = this.getMensagem(lang,'MinLength');
			
			temp = this.Assign(msgerro_,'field',requerido_);
			temp = this.Assign(temp,'number',minimo);
			ret[z] = temp +"\n";
			
			if (k < 1) {
				field.focus();
				k++;
			}
			
			z++;
		}
	}
	
	this.ValidCompares = function () {
		if (this.comparafrom) {
			msgerro = this.getMensagem(lang,'DontMatch');
			for (x = 0; x < this.getComparaFrom(); x++) {
				if (this.getComparaFrom(x).value != this.getComparaTo(x).value) {
					requerido_1 = (!this.getComparaFrom(x).title) ? this.getComparaFrom(x).getAttribute('required') : this.getComparaFrom(x).title;
					requerido_2 = (!this.getComparaTo(x).title)   ? this.getComparaTo(x).getAttribute('required') : this.getComparaTo(x).title;
					temp = this.Assign(msgerro,'field_1',requerido_1);
					temp = this.Assign(temp,'field_2',requerido_2);
					
					ret[z] = temp +"\n";
					
					if (k < 1) {
						this.getComparaFrom(x).focus();
						k++;
					}
					
					z++;
				}
			}
		}
	}
	
	this.ValidEmails = function () {
		if (this.emails) {
			msgerro = this.getMensagem(lang,'InvalidField');
			for (x = 0; x < this.getEmails(); x++) {
				if (this.getEmails(x).value != "" && !this.getEmails(x).value.isEmail()) {
					requerido = (!this.getEmails(x).title) ? this.getEmails(x).getAttribute("required") : this.getEmails(x).title;
					temp = this.Assign(msgerro,'field',requerido);
					
					ret[z] = temp +"\n";
					
					if (k < 1) {
						this.getEmails(x).focus();
						k++;
					}
					
					z++;
				}
			}
		}
	}
	
	this.ValidExceptions = function () {
		if (this.exceptions) {
			msgerro = this.getMensagem(lang,'InvalidField');
			for (x = 0; x < this.getEmails(); x++) {
				if (this.getEmails(x).value != "" && !this.getEmails(x).value.isEmail()) {
					requerido = (!this.getEmails(x).title) ? this.getEmails(x).getAttribute("required") : this.getEmails(x).title;
					temp = this.Assign(msgerro,'field',requerido);
					
					ret[z] = temp +"\n";
					
					if (k < 1) {
						this.getEmails(x).focus();
						k++;
					}
					
					z++;
				}
			}
		}
	}
	
	this.ShowModal = function () {
	txterror = "";
		if (this.modal_div_aux) {				
			//this.getModalDivAux().style.visibility = 'visible';
			this.getModalDivAux().style.display = 'block';			
		} 
		
		txterror += "<div class='div_msg'>"
		txterror += "<div class='icon_alert'><span>Attention!</span></div><ul>";
		for (a in ret) {
			if (ret[a] == "______array") continue;	
			txterror += "<li class='gray_03_02'>" +unescape(ret[a])+ "</li>";
		}
		
		txterror += "</ul></div>"; //		
		this.getModalDiv().innerHTML = txterror;
		this.getModalDiv().style.display = 'block';
		this.getModalDiv().className = 'div_msg_container';
	}
	
	this.Assign = function (obj,texto,replace) {
		texto = "{%"+texto+"}";
		return obj.replace(texto,replace);
	}
	
	this.getLang = function () { return this.lang; }
	this.setLang = function (value) { this.lang = value; }
	
	this.getForm = function () { return this.form; }
	this.setForm = function (value) { this.form = value; }
	
	this.getComparaFrom = function (indice) {
		if (indice != null) {
			form = this.getForm();
			return form.elements[this.comparafrom[indice]];
		}
		
		return this.comparafrom.length;
	}
	this.setComparaFrom = function (value) {
		this.comparafrom = (this.comparafrom) ? this.comparafrom : new Array();
		len = this.comparafrom.length;
		this.comparafrom[len] = value;
	}
	
	this.getComparaTo = function (indice) {
		if (indice != null) {
			form = this.getForm();
			return form.elements[this.comparato[indice]];
		}
		
		return this.comparato.length;
	}
	this.setComparaTo = function (value) {
		this.comparato = (this.comparato) ? this.comparato : new Array();
		len = this.comparato.length;
		this.comparato[len] = value;
	}
	
	this.getDataFrom = function (indice) {
		if (indice != null) {
			form = this.getForm();
			return form.elements[this.datafrom[indice]];
		}
		
		return this.datafrom.length;
	}
	this.setDataFrom = function (value) {
		this.datafrom = (this.datafrom) ? this.datafrom : new Array();
		len = this.datafrom.length;
		this.datafrom[len] = value;
	}
	
	this.getDataTo = function (indice) {
		if (indice != null) {
			form = this.getForm();
			return form.elements[this.datato[indice]];
		}
		
		return this.datato.length;
	}
	this.setDataTo = function (value) {
		this.datato = (this.datato) ? this.datato : new Array();
		len = this.datato.length;
		this.datato[len] = value;
	}
	
	this.getEmails = function (indice) {
		if (indice != null) {
			form = this.getForm();
			return form.elements[this.emails[indice]];
		}
		
		return this.emails.length;
	}
	this.setEmails = function (value) {
		this.emails = (this.emails) ? this.emails : new Array();
		len = this.emails.length;
		this.emails[len] = value;
	}
	
	this.getURLs = function (indice) {
		if (indice != null) {
			form = this.getForm();
			return form.elements[this.urls[indice]];
		}
		
		return this.urls.length;
	}
	this.setURLs = function (value) {
		this.urls = (this.urls) ? this.urls : new Array();
		len = this.urls.length;
		this.urls[len] = value;
	}
	
	this.getExceptions = function (obj) {
		if (this.exceptions) {
			for (x = 0; x < this.exceptions.length; x++) {
				if (obj.value == this.exceptions[x]) {
					return true;
				}
			}
		}
		
		if (excecoes = obj.getAttribute('exceptions')) {
			if (excecoes == obj.value) {
				return true;
			}
		}
		
		return false;
	}
	
	this.setExceptions = function (value) {
		this.exceptions = (this.exceptions) ? this.exceptions : new Array();
		len = this.exceptions.length;
		this.exceptions[len] = value;
	}
	
	this.getMensagem = function (lang,indice) { return mensagem[lang][indice]; },
	this.setMensagem = function (lang,indice,valor) { mensagem[lang][indice] = valor; }
	
	this.getModalDiv = function () { return classObjetos.findObj(this.modal_div); }
	this.setModalDiv = function (valor) { this.modal_div = valor; }
	
	this.getModalDivAux = function () { return classObjetos.findObj(this.modal_div_aux); }
	this.setModalDivAux = function (value) { this.modal_div_aux = value; }
				
	this.getAncora = function () { return (this.ancora) ? this.ancora : '#'; }
	this.setAncora = function (value) { this.ancora = value; }
	
	this.getModificaFlag = function () { return this.getForm().elements[this.modifica_flag]; }
	this.setModificaFlag = function (value) { this.modifica_flag = value; }
}

classForm  = new c_Forms();
classForm2 = new c_Forms();
