/*
	@autor: Elvis Navarro Vega
	Grupo de Desarrollo Plataforma - Universidad de Pamplona.
	Pamplona (Norte de Santander). Colombia.
	Marzo 22 del 2006
	HMSValidaciones.js
	Version para PortalIG
*/



function HMSValidaciones () {
	this.HMSSoloNumeros = '1234567890';
	this.HMSSoloLetrasMinusculas = 'abcdefghijklmnopqrstuvwxyz';
	this.HMSSoloLetrasMayusculas = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_-';
	
	this.VerificarExistencia  = function ( _fuente , _comparacion , separador  ){
		var numero 		= 0 ;
		var palabras 	= new Array();
		var comparacion = _comparacion.toLowerCase();
		var fuente		= _fuente.toLowerCase();
		var palabras	= fuente.split(separador);
		var i			= 0;
		for ( i = 0 ; i < palabras.length ; i++ ){
			palabras [ i ]  =  palabras [ i ].replace(/^\s+|\s+$/g,'');//trim
			if ( comparacion.indexOf( palabras [ i ] ) != -1 ){
				numero++;
			}
		}
		if ( numero == palabras.length ){return true;}
		else{return false;}
	};
	
	this.BuscarNoExistencias  = function ( _fuente , _comparacion , separador ){
		var numero 		= 0 ;
		var palabras 	= new Array();
		var palabrasOriginales = new Array();
		var comparacion = _comparacion.toLowerCase();
		var fuente		= _fuente.toLowerCase();
		var palabras	= fuente.split(separador);
		var i			= 0;
		var palabrasOriginales		= _fuente.split(separador);
		var palabrasNoExistentes	= "";
		for ( i = 0 ; i < palabras.length ; i++ ){
			palabras [ i ]  =  palabras [ i ].replace(/^\s+|\s+$/g,'');//trim
			if ( comparacion.indexOf( palabras [ i ] ) == -1 ){
				if ( palabrasNoExistentes.length == 0 ){
					palabrasNoExistentes = palabrasNoExistentes + palabrasOriginales [ i ] ;
				} else{
					palabrasNoExistentes = palabrasNoExistentes + " ; " + palabrasOriginales [ i ]  ;					
				}
			}
		}
		return (palabrasNoExistentes);
	};
	
	
	this.CantidadPalabras = function ( _Param , separador , cantidad ){
		var numero		= 0 ;
		var palabras	= new Array();
		palabras 		= _Param.split(separador);
		var i			= 0 ;
		for ( i = 0 ; i < palabras.length ; i++ ){
			 if ( !this.EsVacio( palabras[ i ] )  ){
			 	numero ++ ;
			 }		
		}
		if ( numero >= cantidad ) {	return true;}
		else {return false;}
	};
	
	this.EsVacio = function ( _Param ) {
		if( _Param == null ) { return true; }
		for ( var i = 0; i < _Param.length; i ++) {
			if ( ( _Param.charAt ( i ) != ' ') && ( _Param.charAt ( i ) != '\t' ) && ( _Param.charAt ( i ) != '\n') && ( _Param.charAt ( i ) != '\r' ) ) {
				return false;
			}
		}
		return true;
	};
	
	
	this.EsNumeroFloat = function ( _Param ) { 
		return ( parseFloat ( _Param, 10) == ( Number ( _Param ) * 1 ) );
	};

	this.EsDigito = function ( _Param ) {
		if ( _Param.length > 1 ) { return false; }
		if ( this.HMSSoloNumeros.indexOf ( _Param ) != -1 ) { return true; }
		return false;
	};

	this.EsNumero = function ( _Param ) {
		for ( var i = 0; i < val.length; i ++) {
			if( !this.EsDigito ( _Param.charAt ( i ) ) ) 
				return false;
		}
		return true;
	};

	this.VerificarOpcionSeleccionada = function ( _formulario ) {
		if ( typeof ( _formulario ) == 'object' ) {
			for ( var i = 0; i < _formulario.elements.length; i ++ ) {
				var elemento = _formulario.elements [ i ];
				if ( elemento.type == 'radio' ){
					if ( elemento.checked )
						return (true);
				}		
			}
		}
		return (false);
	};
	
	

	this.VerificarOpcionSeleccionadaNombre = function ( _formulario, _nombre ) {
		if ( typeof ( _formulario ) == 'object' ) {
			for ( var i = 0; i < _formulario.elements.length; i ++ ) {
				var elemento = _formulario.elements [ i ];
				if ( elemento.type == 'radio' && elemento.name == _nombre ){
					if ( elemento.checked )
						return (true);
				}		
			}
		}
		return (false);
	};

	this.VerificarCheckSeleccionado = function ( _formulario ) {
		if ( typeof ( _formulario ) == 'object' ) {
			for ( var i = 0; i < _formulario.elements.length; i ++ ) {
				var elemento = _formulario.elements [ i ];
				if ( elemento.type == 'checkbox' ) {
					if ( elemento.checked )
						return (true);
				}		
			}
		}
		return (false);
	};

	this.VerificarCheckSeleccionadoNombre = function ( _formulario, _nombre ) {
		if ( typeof ( _formulario ) == 'object' ) {
			for ( var i = 0; i < _formulario.elements.length; i ++ ) {
				var elemento = _formulario.elements [ i ];
				if ( elemento.type == 'checkbox' && elemento.name == _nombre ) {
					if ( elemento.checked )
						return (true);
				}		
			}
		}
		return (false);
	};

	this.ContarCheckSeleccionados = function ( _formulario ) {
		var total = 0;
		if ( typeof ( _formulario ) == 'object' ) {
			for ( var i = 0; i < _formulario.elements.length; i ++ ) {
				var elemento = _formulario.elements [ i ];
				if ( elemento.type == 'checkbox' ){
					if ( elemento.checked )
						total = total + 1;		
				}		
			}
		}
		return (total);
	};

	this.compararFechas = function  ( _fechaIni, _fechaFin, _operacion ){
		comparar = false;
		var arrFechaIni = _fechaIni.split ( '-' );
		var strFechaIni = arrFechaIni [ 2 ] + arrFechaIni [ 1 ] + arrFechaIni [ 0 ];
		var valFechaIni = new Number( strFechaIni ); 
	
		var arrFechaFin = _fechaFin.split ( '-' );
		var strFechaFin = arrFechaFin [ 2 ] + arrFechaFin [ 1 ] + arrFechaFin [ 0 ];
		var valFechaFin = new Number( strFechaFin );

		switch ( _operacion ) {
			case '>=':
				//--> cuando fecha inicial es mayor o igual a fecha final
				if (valFechaIni >= valFechaFin) 
					comparar = true;
				break;	

			case '==':
				//--> cuando fecha inicial es igual a fecha final
				if (valFechaIni == valFechaFin) 
					comparar = true;
				break;	
		}
		return ( comparar );
	};

	this.EsLetra = function ( letra ) {	
		if ( this.HMSSoloLetrasMayusculas.indexOf( letra ) != -1 ) return true;
		if ( this.HMSSoloLetrasMinusculas.indexOf( letra ) != -1 ) return true;
		if ( this.HMSSoloNumeros.indexOf( letra ) != -1 ) return true;
		return false;
	};

	this.EsSoloLetra = function ( letra ) {	
		if ( this.HMSSoloLetrasMayusculas.indexOf( letra ) != -1 ) return true;
		if ( this.HMSSoloLetrasMinusculas.indexOf( letra ) != -1 ) return true;
		return false;
	};


	this.EsAlfabeto = function ( cadena ) {   
		var i;
		for ( i = 0; i < cadena.length; i++ ) {   
			var letra = cadena.charAt ( i );
			if ( !this.EsLetra ( letra ) )
				return false;
		}
		return true;
	};

	this.TamanoMaximo = function ( texto, tamano ) {
		if( texto.length > tamano )
			return false;
		else
			return true;
	};

	this.ValidarEmail = function  ( valor ) {
		if ( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test( valor ) )
			return ( true )
	  	else
			return ( false );
	};

	this.DeshabilitarBotones = function  ( _formulario, _bandera, _className ) {
		if ( typeof ( _formulario ) == 'object' ) {
			for ( var i = 0; i < _formulario.elements.length; i ++ ) {
				var elemento = _formulario.elements [ i ];
				if ( elemento.type == 'button' )
					this.DeshabilitarBoton ( elemento, _bandera, _className );
			}
		}
	};

	this.DeshabilitarBoton = function ( oBoton, _bandera, _className ) {
		if ( oBoton && oBoton.type == 'button' ) { 
			oBoton.className = _className;
			oBoton.disabled  = _bandera;
		}
	};

	this.GetObjeto = function ( oName ) {
		return ( document.getElementById ( oName ) );
	};

}

var HMSValidar = new HMSValidaciones ();



	