
// Class //by Magia :)

function Validate(oForm)	
{
	// atributos de la clase
	this.Form = oForm;
	this.CantControles = oForm.elements.length;
	this.ResultValidation = true;
	this.RuleMenor = 0;
	this.RuleMayor = 0;
	this.MsgRequiredError = '';
	this.MsgUrlError = '';
	this.MsgEmailError = '';
	this.MsgisNumberError = '';
	this.MsgRuleMenorError = '';
	this.MsgRuleMayorError = '';
	this.MsgComboError= '';
	this.MsgRadioError = '';
	this.MsgFechaError = '';
	this.TypeErrorMsg = 0; //Tipo de mensaje, html o alert -- por default alert
	this.TypeValidation = true; // Tipo de validacion, por default en true = valida control a control -- en false = valida todo junto
	this.ControlValidations = new Array();
	this.FechaDesde='';
	this.MsgFormatoFechaError = '';
	
	//Metodos Privados de la clase
	this.ErrorMsg = function(Index,Action,MsgError,Version)
	{
		
		if (Version == 1) // Version con html
		{
			if (Action == 0)// el cero es action mostrar error
			{
				if (document.getElementById('e'+this.Form.elements[Index].name)) {
					//if(this.TypeValidation){
						this.ResultValidation = false;
					//}
					document.getElementById('e'+this.Form.elements[Index].name).style.display = '';
					document.getElementById('e'+this.Form.elements[Index].name).style.color = 'red';
					document.getElementById('e'+this.Form.elements[Index].name).innerHTML = MsgError;
					
				}	
			}
			else	
			{	
				if (document.getElementById('e'+this.Form.elements[Index].name))
					document.getElementById('e'+this.Form.elements[Index].name).style.display = 'none';	
			}	
		}else // version con alert
		{
			if (Action == 0)// el cero es action mostrar error
			{
				if(this.TypeValidation){
					this.ResultValidation = false;
				}
				alert(MsgError);
			}
		}
	}
	
	this.isFecha = function(ElementsIndex)
	{
		
		var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
		if (RegExPattern.test(this.Form.elements[ElementsIndex].value)== false){
			this.ErrorMsg(ElementsIndex,0,this.MsgFormatoFechaError,this.TypeErrorMsg);
			if (this.TypeValidation)
				this.Form.elements[ElementsIndex].focus();
			//this.ResultValidation = false;
		}
		else
		{
			this.ErrorMsg(ElementsIndex,1,this.MsgFormatoFechaError,this.TypeErrorMsg);
		}
	
	}
	
	this.isFechaDesde = function(ElementsIndex)
	{
		//checkeamos que sea de tipo text				
		if (this.Form.elements[ElementsIndex].type == "text"){
			this.FechaDesde = this.Form.elements[ElementsIndex].value;
		}	
	}
	
	this.isFechaHasta  = function(ElementsIndex)
	{
		//checkeamos que sea de tipo text				
		if (this.Form.elements[ElementsIndex].type == "text"){
			//validamos que fecha desde no sea mayor a fecha hasta
			var FechaDesde = new Array();
			var FechaHasta = new Array();
			
			FechaDesde = this.FechaDesde.split("/");
			FechaHasta = this.Form.elements[ElementsIndex].value.split("/")
			
			/*
			for(var i = FechaDesde.length - 1;i<0;i--)
			{
				if (parseInt(FechaDesde[i]) > parseInt(FechaHasta [i]))
				{
					this.ErrorMsg(ElementsIndex,0,this.MsgFechaError,this.TypeErrorMsg);
					if (this.TypeValidation)
						this.Form.elements[ElementsIndex].focus();	
				}
			}*/
			
			var anioDesde = parseInt(FechaDesde[2]);
			var anioHasta = parseInt(FechaHasta [2]);
			var mesDesde =  parseInt(FechaDesde[1]);
			if (mesDesde == 0)
			{
				mesDesde = parseInt(FechaDesde[1].substring(1,2));
			}
			var mesHasta =  parseInt(FechaHasta[1]);
			if (mesHasta == 0)
			{
				mesHasta = parseInt(FechaHasta[1].substring(1,2));
			}			
			var diaDesde =  parseInt(FechaDesde[0]);
			if (diaDesde == 0)
			{
				diaDesde = parseInt(FechaDesde[0].substring(1,2));
			}			
			var diaHasta =  parseInt(FechaHasta[0]);
			if (diaHasta == 0)
			{
				diaHasta = parseInt(FechaHasta[0].substring(1,2));
			}						
			
			if (anioDesde > anioHasta)
			{
				//error // el aņo de la fecha desde es mayor				
				this.ErrorMsg(ElementsIndex,0,this.MsgFechaError,this.TypeErrorMsg);
				if (this.TypeValidation)
					this.Form.elements[ElementsIndex].focus();				
			}else
			{
				if (anioDesde == anioHasta) // preguntamos si los aņos son iguales
				{
					if (mesDesde > mesHasta)
					{
						//error// los aņos son iguales pero el mes desde es mayor al hasta
						this.ErrorMsg(ElementsIndex,0,this.MsgFechaError,this.TypeErrorMsg);
						if (this.TypeValidation)
							this.Form.elements[ElementsIndex].focus();																
					}else
					{
						if (mesDesde == mesHasta) // preguntamos si los meses son iguales
						{
							if (diaDesde > diaHasta) // preguntamos si el dia desde es mayor al hasta
							{
								//error// los aņos son iguales,los meses son iguales pero el dia desde es mayor al dia hasta
								this.ErrorMsg(ElementsIndex,0,this.MsgFechaError,this.TypeErrorMsg);
								if (this.TypeValidation)
									this.Form.elements[ElementsIndex].focus();																								
							}
						}else
						{
							this.ErrorMsg(ElementsIndex,1,this.MsgFechaError,this.TypeErrorMsg);	
						}
					
					}
				}else
				{
					this.ErrorMsg(ElementsIndex,1,this.MsgFechaError,this.TypeErrorMsg);
				}
			}
			
		}	
	}		
	
	this.Required = function(ElementsIndex)
	{
		//checkeamos que sea de tipo text				
		if (this.Form.elements[ElementsIndex].type == "text" || this.Form.elements[ElementsIndex].type == "password" || this.Form.elements[ElementsIndex].type == "textarea"){
			//checkeamos que el control no este vacio
			if (this.Form.elements[ElementsIndex].value == "") {
				this.ErrorMsg(ElementsIndex,0,this.MsgRequiredError,this.TypeErrorMsg);
				if (this.TypeValidation)
					this.Form.elements[ElementsIndex].focus();
				//this.ResultValidation = false;
			}
			else {
				this.ErrorMsg(ElementsIndex,1,this.MsgRequiredError,this.TypeErrorMsg);
			}
		}	
	}
	this.url = function(ElementsIndex)
	{
			if (this.Form.elements[ElementsIndex].type == "text")
			{
				var url_match = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
				if (url_match.test(this.Form.elements[ElementsIndex].value)== false){
					this.ErrorMsg(ElementsIndex,0,this.MsgUrlError,this.TypeErrorMsg);
					if (this.TypeValidation)
						this.Form.elements[ElementsIndex].focus();
					//this.ResultValidation = false;
				}
				else
				{
					this.ErrorMsg(ElementsIndex,1,this.MsgUrlError,this.TypeErrorMsg);
				}
			}
	}
	this.isNumber = function(ElementsIndex)
	{
		if (this.Form.elements[ElementsIndex].type == "text")
		{
			if (isNaN(this.Form.elements[ElementsIndex].value))
			{
				this.ErrorMsg(ElementsIndex,0,this.MsgisNumberError,this.TypeErrorMsg);
				if (this.TypeValidation)
					this.Form.elements[ElementsIndex].focus();
				//this.ResultValidation = false;
			}
			else
			{
				this.ErrorMsg(ElementsIndex,1,this.MsgisNumberError,this.TypeErrorMsg);
			}
		}	
	}
	this.email = function(ElementsIndex)
	{
		if (this.Form.elements[ElementsIndex].type == "text")
		{
			var regex = new RegExp("^[0-9a-z\\._]+@[0-9a-z]+\\..+$","i");
			if (regex.test(this.Form.elements[ElementsIndex].value) == false){
				this.ErrorMsg(ElementsIndex,0,this.MsgEmailError,this.TypeErrorMsg);
				if (this.TypeValidation)
					this.Form.elements[ElementsIndex].focus();
				//this.ResultValidation = false;
			}
			else
			{
				this.ErrorMsg(ElementsIndex,1,this.MsgEmailError,this.TypeErrorMsg);
			}
		}	
	}
	this.isSelected = function(ElementsIndex)
	{
		if (this.Form.elements[ElementsIndex].type == "select-one")
		{
			if (this.Form.elements[ElementsIndex].selectedIndex == 0)
			{
				this.ErrorMsg(ElementsIndex,0,this.MsgComboError,this.TypeErrorMsg);
				if (this.TypeValidation)
					this.Form.elements[ElementsIndex].focus();
				//this.ResultValidation = false;
			}
			else
			{
				this.ErrorMsg(ElementsIndex,1,this.MsgComboError,this.TypeErrorMsg);
			}
		}	
	}
	this.ruleLessThan = function(ElementsIndex)
	{
	
		if (this.Form.elements[ElementsIndex].type == "text")
		{
			if (parseInt(this.Form.elements[ElementsIndex].value) >= parseInt(this.RuleMenor))
			{
				this.ErrorMsg(ElementsIndex,0,this.MsgRuleMenorError,this.TypeErrorMsg);
				if (this.TypeValidation)
					this.Form.elements[ElementsIndex].focus();
				//this.ResultValidation = false;
			}
			else
			{
				this.ErrorMsg(ElementsIndex,1,this.MsgRuleMenorError,this.TypeErrorMsg);
			}
		}	
	
	}
	this.ruleMoreThan = function(ElementsIndex)
	{
	
		if (this.Form.elements[ElementsIndex].type == "text")
		{
			if (parseInt(this.Form.elements[ElementsIndex].value) <= parseInt(this.RuleMayor))
			{
				this.ErrorMsg(ElementsIndex,0,this.MsgRuleMayorError,this.TypeErrorMsg);
				if (this.TypeValidation)
					this.Form.elements[ElementsIndex].focus();
				//this.ResultValidation = false;
			}
			else
			{
				this.ErrorMsg(ElementsIndex,1,this.MsgRuleMayorError,this.TypeErrorMsg);
			}
		}	
	
	}
	this.OneRadioSelected = function(ElementsIndex)
	{
		var RadioArray = new Array();
		var bValidar = false;
		RadioArray =document.getElementsByName(this.Form.elements[ElementsIndex].name)
		for (var x = 0; x < RadioArray.length;x++)
		{
			if (RadioArray[x].checked== true)
			{
				bValidar = true;		
			}
		}
		if (!bValidar)
		{
				this.ErrorMsg(ElementsIndex,0,this.MsgRadioError,this.TypeErrorMsg);
				if (this.TypeValidation)
					this.Form.elements[ElementsIndex].focus();
		}	
		else
		{
				this.ErrorMsg(ElementsIndex,1,this.MsgRadioError,this.TypeErrorMsg);
		}
	}
	
	//metodo de validacion
	this.Validation = function() 
	{
	for (var i = 0 ;i< this.CantControles;i++){
		this.ControlValidations= this.Form.elements[i].className.split(";");
		if (!this.TypeValidation)
			this.ResultValidation = true;
		for (var y=0;y<this.ControlValidations.length;y++) 
		{
		
			switch (this.ControlValidations[y]) 
			{
				case "required":
					if (this.ResultValidation){
						this.Required(i);
					}				
				break;
				case "url":
					if (this.ResultValidation){
						this.url(i);
					}
				break;
				case "isnumber":
					if (this.ResultValidation){
						this.isNumber(i);
					}
				break;
				case "email":
					if (this.ResultValidation){
						this.email(i);
					}
				break;			
				case "isselected":
					if (this.ResultValidation){
						this.isSelected(i);
					}
				break;
				case "rule<Than":
					if (this.ResultValidation){
						this.ruleLessThan(i);
					}	
				break;			
				case "rule>Than":
					if (this.ResultValidation){
						this.ruleMoreThan(i);
					}
				break;
				case "OneRadioSelected":
					if (this.ResultValidation){	
						this.OneRadioSelected(i);
					}
				break;
				case "isFechaDesde":
					if (this.ResultValidation){	
						this.isFechaDesde(i);
					}	
				break;
				case "isFechaHasta":
					if (this.ResultValidation){	
						this.isFechaHasta(i);
					}	
				break;
				case "isFecha":
					if (this.ResultValidation){	
						this.isFecha(i);
					}						
			}
		}
	}		
	}

}
