(function($) {

	$.fn.jaivalidate = function(options){


		var opts = $.extend({}, $.fn.jaivalidate.defaults, options);
		opts._control=this;

		return this.each(function() {
		
			var retorno=1;
			var iClave=0;
			var arrClave = new Array();

			$("div.validateError",opts._control).remove();
			
			$(":submit",opts._control).attr("disabled", "disabled");
			
			$("input, select, textarea", opts._control).each(function(){
				//alert($(this));
				//alert($(this).attr("name"));
				
				if($(this).val().length == 0){
					if($(this).attr("required")=="yes"){
						$(this).parent().append("<div class='validateError'>Debe diligenciar este campo</div>");
						retorno=0;
					}
				}else{
					if($(this).attr("format")=="email"){
						
						var str=$(this).val();			
						var at="@"
						var dot="."
						var lat=str.indexOf(at)
						var lstr=str.length
						var ldot=str.indexOf(dot)
						if (str.indexOf(at)==-1){
							$(this).parent().append("<div class='validateError'>Debe diligenciar un email valido</div>");
							retorno=0;
						}else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
							$(this).parent().append("<div class='validateError'>Debe diligenciar un email valido</div>");
							retorno=0;
						}else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
							$(this).parent().append("<div class='validateError'>Debe diligenciar un email valido</div>");
							retorno=0;
						}else if (str.indexOf(at,(lat+1))!=-1){
							$(this).parent().append("<div class='validateError'>Debe diligenciar un email valido</div>");
							retorno=0;
						}else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
							$(this).parent().append("<div class='validateError'>Debe diligenciar un email valido</div>");
							retorno=0;
						}else if (str.indexOf(dot,(lat+2))==-1){
							$(this).parent().append("<div class='validateError'>Debe diligenciar un email valido</div>");
							retorno=0;
						}else if (str.indexOf(" ")!=-1){
							$(this).parent().append("<div class='validateError'>Debe diligenciar un email valido</div>");
							retorno=0;
						}
							
					}
					if($(this).attr("format")=="password"){
						//alert("ppp");
						arrClave[iClave]=$(this).val();
						if(arrClave.length==2){
							if(arrClave[0]!=arrClave[1]){
								$(this).parent().append("<div class='validateError'>Las claves deben coincidir</div>");
								retorno=0;
							}
						}
						iClave++;
					}
					if($(this).attr("format")=="date"){
	
						//calculo la fecha de hoy
						fInicial=new Date();
						fInicial.setYear(fInicial.getYear()-14);
						fInicial.setHours(0);
						fInicial.setMinutes(0);
						fInicial.setSeconds(0);
						
						var array_fecha = $(this).val().split("-")
						var ano = parseInt(array_fecha[0],10);
						var mes = parseInt(array_fecha[1],10);
						var dia = parseInt(array_fecha[2],10);
						var mifecha = new Date(ano,mes-1,dia);
						//si el array no tiene tres partes, la fecha es incorrecta
						if (array_fecha.length!=3){
							$(this).parent().append("<div class='validateError'>Fecha no valida</div>");
							retorno=0;
						}else if(isNaN(ano)){
							$(this).parent().append("<div class='validateError'>Fecha no valida</div>");
							retorno=0;
						}else if(isNaN(mes) || (mes > 12)){
							$(this).parent().append("<div class='validateError'>Fecha no valida</div>");
							retorno=0;
						}else if(isNaN(dia)){
							$(this).parent().append("<div class='validateError'>Fecha no valida</div>");
							retorno=0;
						}
						
						/*else if(Math.floor(mifecha)>Math.floor(fInicial)){
							$(this).parent().append("<div class='validateError'>Edad m&iacute;nima para participar es de 14 a&ntilde;os</div>");
							retorno=0;
						}*/
						
					}
					
					if($(this).val().length<$(this).attr("minlength")){
						$(this).parent().append("<div class='validateError'>Este campo debe tener al menos "+$(this).attr("minlength")+" carateres</div>");
						retorno=0;
					}
					
					if($(this).attr("function")){
						eval($(this).attr("function")+"('#"+$(this).attr("id")+"')");
					}
					
				}
			
			
			});
			
			//if(retorno==false)
			$(":submit",opts._control).removeAttr("disabled");
			
			$(opts._control).attr("valid",retorno);
						
		});	
		
	};




	$.fn.jaivalidate.defaults = {
		_control: '',
		_action: '',
		_params: '',
		callback: ''
	}

})(jQuery);