﻿$(document).ready(function(){
	$('form').submit(function(){
		
		var pass = true;
		var message = "Please check your form: \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') + '[This field can not be empty]');
							message += valid[0] + " [This field can not be empty]\n";
							pass = false;
						}else{
							if(val != ''){
								var str = $(this).val();
								if(str.length < val){
									$(this).addClass('form-warning');
									$(this).attr('title', $(this).attr('title') + '[This field must be at least '+val+' characters]');
									message += valid[0] + " [This field must be at least "+val+" characters]\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') + '[This field can contain only numbers]');
							message += valid[0] + " [This field can contain only numbers]\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') + '[Enter a valid email address]');
							message += valid[0] + " [Enter a valid email address]\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') + '[The field does not match]');
								message += valid[0] + " [The field does not match]\n";
								pass = false;
							}
						}
						break;
					case 'novalid':
						if(val != ''){
							if($(this).val() == val){
								$(this).addClass('form-warning');
								$(this).attr('title', $(this).attr('title') + '[The field can not contain the data: ' + val + ']');
								message += valid[0] + " [The field can not contain the data: " + val + "]\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]);
			}
		});
	});
});
