var dtValidAID = {
		defaultCss   : {/*valores por defecto, este codigo solo trabaja a nivel de formulario, no de hoja de estilo*/
						   backError    : '#FF6666',
						   backOk       : '#FFFFFF'
					   },
		defaultBloq  :
					   {
						   comillas     : {
						   					Patt: /[\x22\x27]/g, 
						   					TextRep: ''
						   				  },
						   dblSpace     : {
						   					Patt: /[\x20]{2,}/g, 
						   					TextRep: ' '
						   				  },
						   endSpace     : {
						   					Patt: /\x20+$/, 
						   					TextRep: ''
						   				  },
						   beginSpace   : {
						   					Patt: /^\x20+/, 
						   					TextRep: ''
						   				  },
						   spacial      : {
						   					Patt: /[\x21\x23\x5E\x7C]/g, 
							   				TextRep: ''
							   			  }
					   },
		txtUsr 		 : {
						   Texto        : '',
						   onTypeData   : /[0-9\b]/,
						   Val          : /^[0-9]+$/,
						   Format       : 'Sólo números',
						   Funcion      : dtValidNoEmpty
					   },
		txtPass		 : {
						   Texto        : '',
						   onTypeData   : /[a-zA-Z0-9\b]/,
						   Val          : /^[a-zA-Z0-9]+$/,
						   Format       : 'Sólo letras y números',
						   Funcion      : dtValidNoEmpty
					   },
	   txtCurso		 : {
						   Texto        : '',
						   onTypeData   : /[a-zA-Z0-9\b]/,
						   Val          : /^[1234]{1}[a-zA-Z]{1}$/,
						   Format       : '2A',
						   Funcion      : dtValidNoEmpty
					   },
		txtRut       : {
						   Texto        : '',
						   onTypeData   : /[0-9kK\x2D\b]/,
						   Val          : /^[123456789]{1,1}\d{6,7}-(k|K|\d)$/,
						   Format       : '12345678-0, debe ser un Rut válido',
						   Funcion      : dtValidRut
					   },
		txtNmSpace   : {
						   Texto        : '',
						   onTypeData   : /[\DA-Za-zÁÉÍÓÚáéíóúÑñ]/,
						   Val          : /^\D[A-Za-zÁÉÍÓÚáéíóúÑñ]+( \D[A-Za-zÁÉÍÓÚáéíóúÑñ]+)*$/,
						   Format       : 'Texto1 / Texto1 Texto2',
						   Funcion      : null
					   },
		txtDireccion : {
						   Texto        : '',
						   onTypeData   : /[\w\W\b]/,
						   Val          : /\w\W+/,
						   Format       : 'Aaaaaa 123',
						   Funcion      : dtValidNoEmpty
					   },
		txtFecha     : {
						   Texto        : '',
						   onTypeData   : /[\d\/\b]/,
						   Val          : /^\d{1,2}\/\d{1,2}\/\d{4,4}$/,
						   Format       : 'dd/mm/aaaa, debe ser una fecha válida',
						   Funcion      : dtValFecha
					   },
		txtFechHora  : {
						   Texto        : '',
						   onTypeData   : /[\d\/:\x20\b]/,
						   Val          : /^\d{1,2}\/\d{1,2}\/\d{4,4}\x20\d{1,2}:\d{1,2}(:\d{1,2})*$/,
						   Format       : 'dd/mm/aaaa, debe ser una fecha válida',
						   Funcion      : null
					   },
		txtFecha_    : {
						   Texto        : '',
						   onTypeData   : /[\d\x2D\b]/,
						   Val          : /^\d{1,2}-\d{1,2}-\d{4,4}$/,
						   Format       : 'dd-mm-aaaa hh:mm:ss, debe ser una fecha válida',
						   Funcion      : dtValFecha_
					   },
		txtFechHora_ : {
						   Texto        : '',
						   onTypeData   : /[\d\x2D:\x20\b]/,
						   Val          : /^\d{1,2}-\d{1,2}-\d{4,4}\x20\d{1,2}:\d{1,2}(:\d{1,2})*$/,
						   Format       : 'dd-mm-aaaa hh:mm:ss, debe ser una fecha válida',
						   Funcion      : null
					   },
		txtEmail     : {
						   Texto        : '',
						   onTypeData   : /[a-zA-Z0-9@_\.\x2D\b]/,
						   Val          : /^[a-zA-Z0-9]([a-zA-Z0-9]|_|\.|-)*[a-zA-Z0-9]*@[a-zA-Z0-9]([a-zA-Z0-9]|_|\.|-)*[a-zA-Z0-9]*\.[a-zA-Z]{2,3}$/,
						   Format       : 'tucorreo@dominio',
						   Funcion      : null
					   },
		txtFono      : {
						   Texto        : '',
						   onTypeData   : /[\d\b]/,
						   Val          : /^[123456789]{1,1}\d{5,6}$/,
						   Format       : '7654321 o 765432',
						   Funcion      : null
					   },
		txtCelular   : {
						   Texto        : '',
						   onTypeData   : /[\d\x2D\b]/,
						   Val          : /^0[6789]-\d{7,7}$/,
						   Format       : '09-1234567',
						   Funcion      : null
					   },
		txtFax       : {
						   Texto        : '',
						   onTypeData   : /[\d\b]/,
						   Val          : /^[123456789]\d{5,6}$/,
						   Format       : '7654321 o 765432 Segun corresponda',
						   Funcion      : null
					   },
		txtNum       : {
						   Texto        : '',
						   onTypeData   : /[\d\b]/,
						   Val          : /^\d+$/,
						   Format       : 'Sólo Números',
						   Funcion      : null
					   },
		txtAlfa      : {
						   Texto        : '',
						   onTypeData   : /[\w\W\b]/,
						   Val          : /\w\W+/,
						   Format       : 'Texto y Números',
						   Funcion      : dtValidNoEmpty
					   },
		txtAlfaNum   : {
						   Texto        : '',
						   onTypeData   : /[a-zA-Z0-9ÁÉÍÓÚáéíóúÑñ\b]/,
						   Val          : /^[a-zA-Z0-9ÁÉÍÓÚáéíóúÑñ]+$/,
						   Format       : 'Texto y Números',
						   Funcion      : dtValidNoEmpty
					   },
		txtTexto     : {
						   Texto        : '',
						   onTypeData   : /[^\x22\x27]/,
						   Val          : /^[^\x22\x27]+$/,
						   Format       : 'Texto y Números',
						   Funcion      : dtValidNoEmpty
					   },		
		slcTxt       : {
						   Texto        : '',
						   onTypeData   : null,
						   Val          : /^[a-zA-Z]+$/,
						   Format       : 'Selección obligatoria',
						   Funcion      : null
					   },
		slcNum       : {
						   Texto        : '',
						   onTypeData   : null,
						   Val          : /^[0-9]+$/,
						   Format       : 'Selección obligatoria',
						   Funcion      : null
					   },
		rdo			 : {
						   Texto        : '',
						   onTypeData   : null,
						   Val          : /^[a-zA-Z0-9]+$/,
						   Format       : 'Una única selección',
						   Funcion      : SelectRdo
					   },
		chk			 : {
						   Texto        : '',
						   onTypeData   : null,
						   Val          : /^[a-zA-Z0-9]+$/,
						   Format       : 'Debe seleccionar por lo menos una opción',
						   Funcion      : SelectChk
					   },
		aplicarVal   : function ( nf, c, txt, typ, tV )
					   {
						document.forms[ nf ].elements[ c ].value = this.CleanString( document.forms[ nf ].elements[ c ].value ); 
						var element = document.forms[ nf ].elements[ c ];
						var val = element.value;
						this[ typ ].Texto = txt;
						var vRdo = ( element.toString( ) == '[object NodeList]' || ( element.toString( ) == '[object]' && element.type == undefined ) ) ? true : val.match( this[ typ ].Val );
						if ( vRdo && this.execFuncion ( typ, element ) )
						{
							this.ElementTypeApplyStyle( element, 1 );
							if ( tV == 1 ) return true;
						}
						else
						{
							this.ElementTypeApplyStyle( element, 0 );
							if ( tV == 1 ) return false;
							else alert( 'Los datos ingresados del campo '+this[ typ ].Texto+' estan incorrectos.\nEj: '+this[ typ ].Format+'.-' );
						}
					   },
		valDt        : function ( evt, typ )
					   {
						x = ( document.all ) ? evt.keyCode : evt.which;
						c = String.fromCharCode( x );
						if ( c.match( this[ typ ].onTypeData ) || x == 0 ) return true;
						else return false;
					   }
};
dtValidAID.execFuncion = function ( tf, el )
{
	if ( this[ tf ].Funcion != null )
		return this[ tf ].Funcion.apply( null, [ el ] );
	else return true;
};
dtValidAID.CleanString = function ( v )
{
	v = ( v == undefined ) ? '' : v;
	for( Pat in this.defaultBloq )
		v = v.replace( this.defaultBloq[ Pat ].Patt, this.defaultBloq[ Pat ].TextRep );
	return v;
};
dtValidAID.ElementTypeApplyStyle = function ( el, te ) /*el: Elemento del formulario, te: tipo evento*/
{
	if ( typeof el ==  'object' && typeof te == 'number' )
	{
		switch( el.type )
		{
			case 'text':
				el.style.background = ( te == 1) ? this[ 'defaultCss' ].backOk : this[ 'defaultCss' ].backError;
				break;
			case 'password':
				el.style.background = ( te == 1) ? this[ 'defaultCss' ].backOk : this[ 'defaultCss' ].backError;
				break;
			case 'select-one':
				el.style.background = ( te == 1) ? this[ 'defaultCss' ].backOk : this[ 'defaultCss' ].backError;
				break;
			case 'textarea':
				el.style.background = ( te == 1) ? this[ 'defaultCss' ].backOk : this[ 'defaultCss' ].backError;
				break;
			default:
				break;
		}
	}
};
dtValidAID.existElementCss = function ( elemCss )
{
	var exElem = false;
	for ( i=0; i<document.styleSheets.length; i++)
	{
		var rules = ( document.styleSheets[ i ].cssRules ) ? document.styleSheets[ i ].cssRules : document.styleSheets[ i ].rules;
		for ( j=0; j<rules.length; j++ )
			if ( rules[ j ].selectorText == elemCss ) exElem = true;
	}
	return exElem;
};
function dtValid ( f, campo, texto, tipo, tipoV ) { dtValidAID.aplicarVal ( f, campo, texto, tipo, tipoV ); }
function dtTipoData ( ev, tipo ) { return dtValidAID.valDt ( ev, tipo ); }
function dtValidForm ( f, campo, texto, tipo, tipoV ) { return dtValidAID.aplicarVal ( f, campo, texto, tipo, tipoV ); }
function dtCleanString ( f, campo ) { document.forms[ f ].elements[ campo ].value = dtValidAID.CleanString( document.forms[ f ].elements[ campo ].value );}
