var Forms = {
	_msg:null,
	_form:null,
	_response:null,

	email : function(mail){
		var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
			if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
			if(er.test(mail.value)){
				return true;
			}
    }else{
			return false;
    }
	},

	cpf : function(cpf){
		var c = cpf;
		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;
	},

	cnpj : function(cnpj){
		var b = [6,5,4,3,2,9,8,7,6,5,4,3,2], c = cnpj;
		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;
	},

	inputs : function(form){
		var inputs = form.getElementsByTagName('input');
		var i = inputs.length;
		var erMail = new RegExp(/email+|e-mail+/);
		var erCnpj = new RegExp(/cnpj+|CNPJ+/);
		while(i--){
			if(inputs[i].type == 'text'){
				if(inputs[i].title != '' && inputs[i].value == ''){
					this._msg.push("O campo "+inputs[i].title+" é obrigatório") ;
				}else{
					// verifica se é um campo email, para fazer a validação
					if(erMail.test(inputs[i].name)){
						// email valido, title != null, value != null
						if((this.email(inputs[i].value) && inputs[i].title != '' && inputs[i].value != '') || (inputs[i].title == '' && inputs[i].value == '') || (this.email(inputs[i].value) && inputs[i].title == '' && inputs[i].value != '')){
						}else{
							this._msg.push("O campo "+inputs[i].title+" é inválido");
						}
					// Verifica se é um cnpj
					}else if (erCnpj.test(inputs[i].name)){
						if(this.cnpj(inputs[i].value)){
						}else{
							this._msg.push("O campo "+inputs[i].title+" é inválido");
						}
					}
				}
			}// if text
		}
	},
	
	textareas : function(form){
		var textareas = form.getElementsByTagName('textarea');
		var i = textareas.length;
		while(i--){
			if(textareas[i].title != '' && textareas[i].value == ''){
				this._msg.push("O campo "+textareas[i].title+" é obrigatório");
			}
		}
		return this._msg;
	},
	
	selects : function(form){
		var selects = form.getElementsByTagName('select');
		var i = selects.length;
		while(i--){
			if(selects[i].title != '' && selects[i].value == ''){
				this._msg.push("O campo "+selects[i].title+" é obrigatório");
			}
		}
		return this._msg;
	},

	submit : function(form_name, response){
		this._form = document.getElementById(form_name);
		this._msg = new Array();
		this._response = document.getElementById(response);
		this.inputs(this._form);
		this.textareas(this._form);
		this.selects(this._form);
		if(this._msg.length == 0){
			this._msg = null;
			$("#"+form_name+" .submit").fadeOut(500);
			this._response.innerHTML = "Enviando...";
			$(this._response).fadeIn(500);
			var dados = $(this._form).serialize();
			var resposta = eval($.ajax({
				url:'?id=/forms.php',
				cache:false,
				async:false,
				data:dados
			}).responseText);
			if(resposta.retorno == true){
				$(this._response).animate({
					backgroundColor:"#DAFEDB"
				},500);
				this._response.innerHTML = resposta.msg.join("<br />");
			}else{
				$(this._response).animate({
					backgroundColor:"#FFD9D9"
				},500);
				this._response.innerHTML = resposta.msg.join("<br />");
			}
		}else{
			this._response.innerHTML = unescape(this._msg.join("<br />"));
			$(this._response).fadeIn(500);
			this._msg = null;
		}
	},
	
	submitNormal : function(form_name, response){
		this._form = document.getElementById(form_name);
		this._response = document.getElementById(response);
		this._msg = new Array();
		this.inputs(this._form);
		this.textareas(this._form);
		this.selects(this._form);
		if(this._msg.length == 0){
			this._msg = null;
			this._form.submit();
		}else{
			this._response.innerHTML = unescape(this._msg.join("<br />"));
			$(this._response).fadeIn(500);
			this._msg = null;
		}
	}

};