var ExibeCampoErro = Class.create();
ExibeCampoErro.prototype = {
	
	initialize: function( objInput ){
		this.objInput = objInput;
		
		this.abaR = $( 'abar_' + this.objInput.id ) ? $( 'abar_' + this.objInput.id ) : null;
		this.abaL = $( 'abal_' + this.objInput.id ) ? $( 'abal_' + this.objInput.id ) : null;
		this.label = $( 'label_' + this.objInput.id ) ? $( 'label_' + this.objInput.id ) : null;
		
		this.borderOriginal = this.objInput.style.borderColor;
		this.corErro = '#d10202';
		
		this.addOnFocus();
	},
	
	addOnFocus: function(){
		var borderOrinal = this.borderOriginal;
		var objInput = this.objInput;
		
		var abaR = this.abaR;
		var abaL = this.abaL;
		var label = this.label;
		
		Event.observe( this.objInput, 'focus', function(){
			objInput.style.borderColor = borderOrinal;
		
			if( abaR ){
				abaR.src = 'templates/proximo_destino_includes/images/abar.gif';
			}
			
			if( abaL ){
				abaL.src = 'templates/proximo_destino_includes/images/abal.gif';
			}
			
			if( label ){
				label.style.border = '';
				label.style.backgroundColor = '';
			}
		});
	},
	
	go: function(){
		this.objInput.style.borderColor = this.corErro;
		
		if( this.abaR ){
			this.abaR.src = 'templates/proximo_destino_includes/images/abar2.gif';
		}
		
		if( this.abaL ){
			this.abaL.src = 'templates/proximo_destino_includes/images/abal2.gif';
		}
		
		if( this.label ){
			this.label.style.border = '1px solid #d10202';
			this.label.style.backgroundColor = '#d10202';
		}
		
		new Effect.ScrollTo( this.objInput.id, { offset: -45 } );	
	}
	
}

var Validacao = Class.create();
Validacao.prototype = {
	
	initialize: function( objInput, tipo ){		
		this.msgPadrao = 'Você precisa preencher todos os campos obrigatórios';
		this.objInput = objInput;
		this.exibirCampo = new ExibeCampoErro( objInput );
		this.tipo = tipo;		
	},
	
	erro: function( msg ){
		Aviso( msg )
		this.exibirCampo.go();
	},
	
	notNull: function( msg ){
		if( this.objInput.style.display == 'none' || this.objInput.disabled  ){
			return true;
		}
		
		if( ( this.objInput && this.objInput.value == '' || this.objInput.value == ' ' || this.objInput.value == 'Escolha um país' || this.objInput.value == 'Escolha um estado' || this.objInput.value == 'Selecione uma opção' || this.objInput.value == 'Escolha uma cidade' ) || this.objInput.value == 'nome' || this.objInput.value == 'null' || ( $( this.objInput.id + '_show' ) && $( this.objInput.id + '_show' ).value == '' ) ){
			this.erro( msg );
			return false;
		}
		
		return true;
	},
	
	cpf:function( msg ){
		if( this.objInput.style.display == 'none' ){
			return true;
		}

		var reCpf = /^([0-9]{3}\.){2}[0-9]{3}-[0-9]{2}$/; 
		if (reCpf.test(this.objInput.value)) {
			return true;
		} else{
			this.erro( msg );
			return false;
		}
	},
	
	cep:function( msg ){
		if( this.objInput.style.display == 'none' ){
			return true;
		}

		var reCep = /^[0-9]{5}-[0-9]{3}$/; 
		if (reCep.test(this.objInput.value)) {
			return true;
		} else{
			this.erro( msg );
			return false;
		}
	},
	
	data: function( msg ){
		if( this.objInput.style.display == 'none' ){
			return true;
		}

		var reDate = /^((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}$/; 
		if (reDate.test(this.objInput.value)) {
			return true;
		} else{
			this.erro( msg );
			return false;
		}
	},
	
	notEmpty: function( msg ){
		if( this.objInput.style.display == 'none' ){
			return true;
		}
	
		if( tinyMCE.getContent(this.objInput.id).toUpperCase() == '<P>&NBSP;</P>' || tinyMCE.getContent(this.objInput.id).toUpperCase() == '<BR>' || tinyMCE.getContent(this.objInput.id).toUpperCase() == '' || tinyMCE.getContent(this.objInput.id) == '' ){
			this.erro( msg );
			return false;
		}
		
		return true;
	},
	
	monetary: function( msg ){
		if( this.objInput.style.display == 'none' ){
			return true;
		}

		var er = /(\d)*([,.])\d{2}$/;
		
		if( this.objInput.value.search( er ) == -1 ){
			this.erro( msg );
			return false;
		}
		
		return true;
	},
	
	integer: function( msg ){
		if( this.objInput.style.display == 'none' ){
			return true;
		}

		var er = /^\d*$/;
		
		if( this.objInput.value.search( er ) == -1 || this.objInput.value == '' ){
			this.erro( msg );
			return false;
		}
		
		return true;
	},
	
	email: function( msg ){
		if( this.objInput.style.display == 'none' || this.objInput.disabled ){
			return true;
		}

		var er = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;

		if( this.objInput.value.search( er ) == -1 || this.objInput.value == ''){
			this.erro( msg );
			return false;
		}
		
		return true;
	},

	ddd: function( msg ){
		if( this.objInput.style.display == 'none' ){
			return true;
		}

		var er = /^\d*$/;
		
		if( this.objInput.value.search( er ) == -1 || this.objInput.value == '' ){
			this.erro( msg );
			return false;
		}
		
		return true;
	},

	telefone:function( msg ){
		if( this.objInput.style.display == 'none' ){
			return true;
		}

		var er = /^[0-9]{4}-[0-9]{4}$/;
		
		if( this.objInput.value.search( er ) == -1 ){
			this.erro( msg );
			return false;
		}
		
		return true;
	},
	
	validar: function(){
		var s = this.tipo.split( ':' );
		
		var tipo = s[0];
		var msg = s[1] ? s[1] : this.msgPadrao;
		tipo = tipo.replace( ' ', '' );
		tipo = tipo.toLowerCase();
		switch( tipo ){
			
			case 'notnull':
				return this.notNull( msg );
				break;
				
			case 'data':
				return this.data( msg );
				break;
				
			case 'cep':
				return this.cep( msg );
				break;
				
			case 'cpf':
				return this.cpf( msg );
				break;
			
			case 'notempty':
				return this.notEmpty( msg );
				break;
			
			case 'email':
				return this.email( msg );
				break;
			
			case 'ddd':
				return this.ddd( msg );
				break;
			
			case 'telefone':
				return this.telefone( msg );
				break;
			
			case 'integer':
				return this.integer( msg );
				break;
			
			case 'monetary':
				return this.monetary( msg );
				break;
		}
		
		return true;
	}
	
}


function Validar(e,objForm)
{
	/*
	* Pegando os elementos do formulario na ordem
	*/
	_validacao = Array();
	for(var n = 0 ; n < objForm.elements.length ; n++){
		var objInput = objForm.elements[n];
		if( objInput.getAttribute( 'validacao' ) ){
			var validacoes = objInput.getAttribute( 'validacao' ).split(',');
			
			for( var j = 0; j < validacoes.length; j++ ){
				var tpValidacao = validacoes[j];
				_validacao.push( new Validacao(  objInput, tpValidacao ) );
			}
		}
	}
	
	for( var j = 0; j < _validacao.length; j++ ){
		if( !_validacao[j].validar() ){
			Event.stop(e);
			return false;
		}
	}

	return true;
}
