﻿$(document).ready(function() {
		
	$('form').submit(function(){
		
		var pass = true;
		var message = "Por favor verique su formulario: \n\n";
		var cform = $(this);
		
		$(this).find("input[id*='|'], select[id*='|'], textarea[id*='|']").each(function(){
			var valid = $(this).attr('id').split('|');
			$(this).attr('title','');
			
			for(i = 1; i < valid.length; i++){
				var val = '';
				
				if(valid[i].search('=') != -1){
					var sep = valid[i].split('=');
					valid[i] = sep[0];
					val = sep[1];
				}
				switch(valid[i]){
					case 'text':
						if($(this).val() == ''){
							$(this).addClass('form-warning');
							$(this).attr('title', $(this).attr('title') + '[Este campo no puede quedar vacio]');
							message += valid[0] + " [Este campo no puede quedar vacío]\n";
							pass = false;
						}else{
							if(val != ''){
								var str = $(this).val();
								if(str.length < val){
									$(this).addClass('form-warning');
									$(this).attr('title', $(this).attr('title') + '[Este campo debe tener al menos '+val+' caracteres]');
									message += valid[0] + " [Este campo debe tener al menos "+val+" caracteres]\n";
									pass = false;
								}
							}
						}
						break;
					case 'number':
						var filter = /^[0-9]+$/;
						
						if(!filter.test($(this).val())){
							$(this).addClass('form-warning');
							$(this).attr('title', $(this).attr('title') + '[Este campo solo puede contener números]');
							message += valid[0] + " [Este campo sólo puede contener números]\n";
							pass = false;
						}
						break;
					case 'email':
						var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
						
						if(!filter.test($(this).val())){
							$(this).addClass('form-warning');
							$(this).attr('title', $(this).attr('title') + '[Escriba una direccion de email valida]');
							message += valid[0] + " [Escriba una dirección de email válida]\n";
							pass = false;
						}
						break;
					case 'match':
						if(val != ''){
							if($(this).val() != $(cform).find("*[name="+val+"]").val()){
								$(this).addClass('form-warning');
								$(this).attr('title', $(this).attr('title') + '[El campo no coincide]');
								message += valid[0] + " [El campo no coincide]\n";
								pass = false;
							}
						}
						break;
					case 'novalid':
						if(val != ''){
							if($(this).val() == val){
								$(this).addClass('form-warning');
								$(this).attr('title', $(this).attr('title') + '[El campo no puede contener el valor: ' + val + ']');
								message += valid[0] + " [El campo no puede contener el valor: " + val + "]\n";
								pass = false;
							}
						}
						break;
					case 'selvalid':
						if(val != ''){
							if($(this).val() == val){
								$(this).addClass('form-warning');
								$(this).attr('title', $(this).attr('title') + '[Selecione un valor: ' + val + ' válido]');
								message += valid[0] + " [Selecione un valor: " + val + " válido]\n";
								pass = false;
							}
						}
						break;
					 case 'compair':
						if(val != ''){
							if($(this).val() != val){
								$(this).addClass('form-warning');
								$(this).attr('title', $(this).attr('title') + '[El campo no coincide]');
								message += valid[0] + " [El campo no coincide]\n";
								pass = false;
							}
						}
						break;	
					default:
						break;
				}
			}
			
			if($(this).attr('title') == ''){
				$(this).removeClass('form-warning');
			}
		});
		
		if( ! pass){
			alert(message);
		}else{
			return true;
		}
		
		return false;
	});
	
	// || - Campos con Valores por Defecto - || //
	
	$('input.default').each(function(){
		var params = ($(this).val()).split('=');
		var cform = $(this).parent('form');
		var obj = $(cform).find('input[name='+params[0]+']');
		
		$(obj).focus(function(){
			if($(this).val() == params[1]){
				$(this).val('');
			}
		});
		
		$(obj).blur(function(){
			if($(this).val() == ''){
				$(this).val(params[1]);
			}
		});
	});
	
});

