	// JavaScript Document
	// Validador.js
	/* 
		Dependencias: 
		Opcionales: 
		Corre en: ie6, ie7
		Versión: 2.0		
		Autor: rlopez
	*/	

	function Validador() {

		// GLOBALES	
		this.errores = new Array();
		
		this.formato = function(error) {
			return "<li class='pt-list_errores'>".concat("<b>", error.campo, "</b>", ": ", error.error, "</li>"); 
		}
		
		this.vaciar = function() {
			this.errores = new Array();
		}
		
		this.hayErrores = function() {
			return (this.errores.length == 0) ? false : true;
		}
		
		this.getErrores = function() {
			var str = "<ul class='pt-list'>";
			for(var i=0;i<this.errores.length;i++) {
				str += this.formato(this.errores[i]);
			}
			str += "</ul>";
			return str;
		}

		this.mostrar = function(ancho, alto) {
			var w = new Ventana("/resources/html/msgValidacion.html");
			if(arguments.length != 0) {
				w.w = ancho;
				w.h = alto;
			} else {
				w.w = 445;
				w.h = 285;
			}
			w.abrir();
			return w;
		}

		this.correo = function (ctrl) {
			var error = "";
			var b = true;
			if (ctrl.value != "") {
			    var emailFilter = /^.+@.+\..{2,3}$/;
			    if (!(emailFilter.test(ctrl.value))) { 
			       error = "You must a insert a valid email.";
			       this.setError(ctrl, error);
				   b = false;
			    }
			    else {
					//test email for illegal characters
					var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
			        if (ctrl.value.match(illegalChars)) {
						error = "You must a insert a valid email.";
						this.setError(ctrl, error);
						b = false;
			        }
			    }		    
			}
			return b;    
		}
		
		this.telefono = function (ctrl) {
			var error = "";
			var b = true;
			if(ctrl.value != "") {
				var strValidChars = "0123456789-() ";
			    var strChar;
				for (i = 0; i < ctrl.value.length; i++) {
			      	strChar = ctrl.value.charAt(i);
			      	if (strValidChars.indexOf(strChar) == -1) {
						error = "The phone number has not valid characters.";
						this.setError(ctrl, error);
						b = false;
						break;
			      	}
			   	}	    
			}
			return b;
		}
		
		this.requerido = function (ctrl) {
			var error = "";
			var b = true;
			if (ctrl.value.length == 0) {
				error = "This field is required.";
				this.setError(ctrl, "This field is required.");
				b = false;
			} else {
				var val = false;			
				for(var i=0;i<ctrl.value.length;i++) {
					if (ctrl.value.charAt(i) != " ") {
						val = true;
						break;	
					}
				}
				if(!val) {
					b = false;
					error = "This field is required.";
					this.setError(ctrl, "This field is required.");
				}
			}
			return b;
		}
	
		this.moneda = function (ctrl, allowNegative, allowDecimals) {
			var error = "";
			var b = true;
		    if(ctrl.value != "") {
				var i = 0;
			    var strValidChars = "0123456789.";
			    var strChar;
			    		   
				if(allowNegative) {
					strValidChars += "-";
					if (ctrl.value.substring(1, ctrl.value.length).indexOf("-") != -1) {
						alert(ctrl.value.substring(1, ctrl.value.length));
						error = "Debe ingresar solo valores numéricos.";
						this.setError(ctrl, error);
						b = false;
						return b;
					}
				}
				
				if(allowDecimals) {
					var cantDot = 0;
					if(ctrl.value.charAt(0) == ".") {
		 				error = "Debe ingresar solo valores numéricos.";
						this.setError(ctrl, error);
						b = false;
						return b;
					}				
					for (i = 0; i < ctrl.value.length; i++) {
			    	  	if (ctrl.value.charAt(i) == ".") { cantDot++; }
					}					
					if(cantDot > 1) {
		 				error = "Debe ingresar solo valores numéricos.";
						this.setError(ctrl, error);
						b = false;
						return b;
					}
				}
		   
				for (i = 0; i < ctrl.value.length; i++) {
			      	strChar = ctrl.value.charAt(i);
			      	if (strValidChars.indexOf(strChar) == -1) {								
			 			error = "Debe ingresar solo valores numéricos.";
						this.setError(ctrl, error);
						b = false;
			 	     	return b;
			      	}
			   	}		   	
			}
		    return b;
		}

		this.intervalo = function (ctrl, initialValue, finalValue) {
			var error = "";
			var b = true;
			if (ctrl.value != "") {
			    if ((ctrl.value < initialValue) || (ctrl.value > finalValue)) {
			       error = "El valor estar dentro del intervalo [" + initialValue + " - " + finalValue + "].";
			       this.setError(ctrl, error);
				   b = false;
			    }		    
			}
			return b;
		}
		
		this.mayorIgualA = function (ctrl, numero) {
			var error = "";
			var b = true;
		  	if(ctrl.value != "") {
			  	if (ctrl.value <= numero) {
					error = "Debe ingresar un valor mayor &oacute; igual a "+ numero+".";
					this.setError(ctrl, error);
					b = false;
				}
			}			
			return b;	 
		}		

		this.combo = function (ctrl, value) {
			if(value == undefined) value = -1; // Seleccione:
			var error = "";
			var b = true;
		    if (ctrl.value == value) {
		    	error = "Debe seleccionar un elemento.";
		    	this.setError(ctrl, error); 
				b = false;
		    } 		    
			return b;
		}		
		
		this.tamano = function (ctrl, initSize, finalSize) {
			var error = "";		
			var b = true;
			if (ctrl.value != "") {
				if(initSize == 0 && (ctrl.value > finalSize)) {
			        error = "La cantidad de d&iacute;gitos debe ser menor a " + finalValue + ".";
			        this.setError(ctrl, error);
					b = false;
			    } else if(initSize == finalSize && (ctrl.value.length != finalSize)) {
			    	error = "Debe tener una longitud de " + initSize + " d&iacute;gito(s).";
	  		    	this.setError(ctrl, error);
					b = false;					
			    } else if ((ctrl.value < initSize) || (ctrl.value > finalSize) && initSize != finalSize) {
				    error = "La cantidad de d&iacute;gitos debe estar comprendida entre los valores [" + initSize + " - " + finalSize + "].";
			        this.setError(ctrl, error);
					b = false;
				} else if (finalSize == 0 && (ctrl.value < initSize)) {
			        error = "La cantidad de d&iacute;gitos debe ser mayor a " + initValue + ".";
			        this.setError(ctrl, error);
					b = false;
				}
			}
			return b;
		}
	
		this.RIF = function(ctrl)	{	
			var str = ctrl.value;
			var strChar;
			var strValidChars = "0123456789";
			var esValido = false;
			var error = "";
			var b = true;
			if(ctrl.value != "") {
				if(ctrl.value.length == 10) {
					if(strValidChars.indexOf(str.charAt(0)) == -1) {
						for (var i=1;i<str.length;i++) {				
							strChar = str.charAt(i);
							if (strValidChars.indexOf(strChar) == -1) {
								error = "Debe ingresar un formato de RIF v&aacute;lido ej: J123456789.";
								this.setError(ctrl, error); 
								b = false;
								break;
							}
						}
					} else {
						error = "Debe ingresar un formato RIF v&aacute;lido ej: J123456789.";
						this.setError(ctrl, error); 
						b = false;
					}
				} else {
						error = "El RIF debe contener 10 caracteres.";
						this.setError(ctrl, error);
						b = false;
				}
			}
			return b;
		}

		this.lista = function(ctrl, hasSelect) {
			var error = "";
			var b = true;
			if(hasSelect) {
				if (ctrl.selectedIndex == 0 || ctrl.selectedIndex == -1) {
					error = "Debe seleccionar un elemento.";
					this.setError(ctrl, error); 
					b = false;
				} 
			} else {
				if (ctrl.selectedIndex == -1) {
					error = "Debe seleccionar al menos un elemento.";
					this.setError(ctrl, error); 
					b = false;
				} 		
			}	    
			return b;
		}
	
		this.blancos = function(ctrl) {	
			var error = "";
			var b = true;
		    if(ctrl.value != "") {	  
				if(ctrl.value.indexOf(" ") > -1) {
					error = "Espacios en blanco no permitidos.";
					this.setError(ctrl, error);
					b = false;
				}
			}
			return b;
		}

		this.nombreArchivo = function (ctrl) {
			var error = "";		
			var b = true;			
			if(ctrl.value != "") {
				error = checkSpace(ctrl);
				if(error.length == 0) {	
					var iChars = "\\/:|<>?*";				
					for(var i = 0; i < ctrl.value.length; i++) {
						if (iChars.indexOf(ctrl.value.charAt(i)) != -1) {
							error = "\\ / : | < > ? * caracteres no permitidos.";
							this.setError(ctrl, error);
							b = false;
							break;
						}
					}
				}		    
			}
			return b;    
		}
	
		this.fecha = function(ctrl) {			
			var b = true;
			var error = "";
			var fechaStr = ctrl.value;
			if(fechaStr != "") {
				var dia = fechaStr.substr(0,2);
				var mes = fechaStr.substr(3,2);
				var ano = fechaStr.substr(6,4);
		
				var date = mes + "/" + dia + "/" + ano;
				var dt = new Date(date);
				
				if(dt.getDate() != dia) {
					b = false;
				} else if(dt.getMonth() != (mes - 1)) {
					b = false;
				} else if(dt.getFullYear() != ano) {
					b = false;
				}				
				
				if(!b) {
					error = "Debe ingresar una fecha válida.";
					this.setError(ctrl, error);
				} else { // Verificación del ańo
					if(ano < 1900 || ano > ((new Date().getFullYear()) + 100)) {
						error = "Debe ingresar una fecha válida.";
						this.setError(ctrl, error);
						b = false;
					}
				}
			}
			return b;
		}

		this.rangoFecha = function (ctrlDesde, ctrlHasta) {
			var error = "";
			var b = true;
		    if (ctrlDesde.value != "" && ctrlHasta.value != "" && convertirFechaAInt(ctrlHasta.value) < convertirFechaAInt(ctrlDesde.value)) {	  
				error = label(ctrlHasta) + " debe ser mayor que " + label(ctrlDesde);
				//this.setErrorArray([ctrlDesde, ctrlHasta], error);
				b = false;
			}
			return b;
		}
		
		this.autoCompletar = function (ctrl) {
			var error = "";
			var b = true;
			if (ctrl.getObjeto() == null) {
				if(ctrl.esVisible()) error = "Debe seleccionar un elemento.";
				else error = "Debe buscar un elemento.";
				b = false;
				this.setError(ctrl.input, error);
			}
			return b;
		}
		
		this.horas = function (ctrl, militar) {			
			if(militar == undefined) militar = false;
			var error = "";
			var b = true;
			var hora = ctrl.value.split(":"); 
			hora[0] = parseInt(hora[0]);
			hora[1] = parseInt(hora[1]);
			if(militar) {
				if (hora[0] > 23 || hora[1] > 59) {
					error = "Debe ingresar una hora valida.";
					b = false;
					this.setError(ctrl, error);
				}
			} else {
				if ((hora[0] < 1 || hora[0] > 12) || hora[1] > 59) {
					error = "Debe ingresar una hora valida.";
					b = false;
					this.setError(ctrl, error);
				}
			}
			return b;
		}

		this.agregar = function(error) {
			this.errores.push(error);			
		}

		// almacena los errores en un arreglo		
		this.setError = function(ctrl, error) {
			try	{
				this.errores.push({
					campo: ctrl.title,
					error: error				 
				});			
			} catch(e) {
				alert("It's not possible create an Error to: " + ctrl.id + "\nError type: " + e.message);
			}
		}
	
	} // End Validador	