var Clase64 = {
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input,limit) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Clase64._replacement_encode(input);
		input = Clase64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
		}
		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}
		
		output = Clase64._utf8_decode(output);
		output = Clase64._replacement_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	},
	
	// private method for replacement encode
	_replacement_encode : function(texto){
		var strcode="";
		var char='';
		var charcode='';
		for(i=0;i<texto.length;i++)
		{
			char=texto.charCodeAt(i);
			charcode=String.fromCharCode(char+3);
			strcode=strcode+charcode;
		}
		return strcode;
	},
	
	// private method for replacement decode
	_replacement_decode : function(texto)
	{
		var strdecode="";
		var char='';
		var charcode='';
		for(i=0;i<texto.length;i++)
		{
			char=texto.charCodeAt(i);
			charcode=String.fromCharCode(char-3);
			strdecode=strdecode+charcode;
		}
		return strdecode;
	}
}

var ClaseMD5 = {
	/*********/
	//	hexcase	- 	formato de salida de hexadecimal 	0 - Bajas		1 - Altas
	//	chrsz	-	bits por caracter de entrada 		8 - ASCII		16 - Unicode
	/*********/
	hexcase : 0,
	chrsz   : 8,
	string_md5 : "time-gee-m3",
	
	txt2md5 : function(texto)
	{
		return this.hex_md5(this.string_md5 + texto);
	},
	
	hex_md5 : function(s)
	{
		return ClaseMD5.binl2hex(ClaseMD5.core_md5(ClaseMD5.str2binl(s), s.length * this.chrsz));
	},

	binl2hex : function(binarray)
	{
		var hex_tab = this.hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
		var str = "";
		for(var i = 0; i < binarray.length * 4; i++)
		{
			str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
		}
		return str;
	},
	
	core_md5 : function(x, len)
	{
		x[len >> 5] |= 0x80 << ((len) % 32);
		x[(((len + 64) >>> 9) << 4) + 14] = len;

		var a =  1732584193;
		var b = -271733879;
		var c = -1732584194;
		var d =  271733878;

		for(var i = 0; i < x.length; i += 16)
		{
			var olda = a;
			var oldb = b;
			var oldc = c;
			var oldd = d;

			a = ClaseMD5.md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
			d = ClaseMD5.md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
			c = ClaseMD5.md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
			b = ClaseMD5.md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
			a = ClaseMD5.md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
			d = ClaseMD5.md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
			c = ClaseMD5.md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
			b = ClaseMD5.md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
			a = ClaseMD5.md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
			d = ClaseMD5.md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
			c = ClaseMD5.md5_ff(c, d, a, b, x[i+10], 17, -42063);
			b = ClaseMD5.md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
			a = ClaseMD5.md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
			d = ClaseMD5.md5_ff(d, a, b, c, x[i+13], 12, -40341101);
			c = ClaseMD5.md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
			b = ClaseMD5.md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

			a = ClaseMD5.md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
			d = ClaseMD5.md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
			c = ClaseMD5.md5_gg(c, d, a, b, x[i+11], 14,  643717713);
			b = ClaseMD5.md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
			a = ClaseMD5.md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
			d = ClaseMD5.md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
			c = ClaseMD5.md5_gg(c, d, a, b, x[i+15], 14, -660478335);
			b = ClaseMD5.md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
			a = ClaseMD5.md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
			d = ClaseMD5.md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
			c = ClaseMD5.md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
			b = ClaseMD5.md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
			a = ClaseMD5.md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
			d = ClaseMD5.md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
			c = ClaseMD5.md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
			b = ClaseMD5.md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

			a = ClaseMD5.md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
			d = ClaseMD5.md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
			c = ClaseMD5.md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
			b = ClaseMD5.md5_hh(b, c, d, a, x[i+14], 23, -35309556);
			a = ClaseMD5.md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
			d = ClaseMD5.md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
			c = ClaseMD5.md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
			b = ClaseMD5.md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
			a = ClaseMD5.md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
			d = ClaseMD5.md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
			c = ClaseMD5.md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
			b = ClaseMD5.md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
			a = ClaseMD5.md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
			d = ClaseMD5.md5_hh(d, a, b, c, x[i+12], 11, -421815835);
			c = ClaseMD5.md5_hh(c, d, a, b, x[i+15], 16,  530742520);
			b = ClaseMD5.md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

			a = ClaseMD5.md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
			d = ClaseMD5.md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
			c = ClaseMD5.md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
			b = ClaseMD5.md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
			a = ClaseMD5.md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
			d = ClaseMD5.md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
			c = ClaseMD5.md5_ii(c, d, a, b, x[i+10], 15, -1051523);
			b = ClaseMD5.md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
			a = ClaseMD5.md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
			d = ClaseMD5.md5_ii(d, a, b, c, x[i+15], 10, -30611744);
			c = ClaseMD5.md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
			b = ClaseMD5.md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
			a = ClaseMD5.md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
			d = ClaseMD5.md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
			c = ClaseMD5.md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
			b = ClaseMD5.md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

			a = ClaseMD5.safe_add(a, olda);
			b = ClaseMD5.safe_add(b, oldb);
			c = ClaseMD5.safe_add(c, oldc);
			d = ClaseMD5.safe_add(d, oldd);
		}
		return Array(a, b, c, d);
	},
	
	md5_cmn : function(q, a, b, x, s, t)
	{
		return this.safe_add(this.bit_rol(this.safe_add(this.safe_add(a, q), this.safe_add(x, t)), s),b);
	},
	
	md5_ff : function(a, b, c, d, x, s, t)
	{
		return this.md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
	},
	
	md5_gg : function(a, b, c, d, x, s, t)
	{
		return this.md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
	},
	
	md5_hh : function(a, b, c, d, x, s, t)
	{
		return this.md5_cmn(b ^ c ^ d, a, b, x, s, t);
	},
	
	md5_ii : function(a, b, c, d, x, s, t)
	{
		return this.md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
	},
	
	bit_rol : function(num, cnt)
	{
	  return (num << cnt) | (num >>> (32 - cnt));
	},
	
	safe_add : function(x, y)
	{
	  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
	  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
	  return (msw << 16) | (lsw & 0xFFFF);
	},
	
	str2binl : function(str)
	{
		var bin = Array();
		var mask = (1 << this.chrsz) - 1;
		for(var i = 0; i < str.length * this.chrsz; i += this.chrsz)
			bin[i>>5] |= (str.charCodeAt(i / this.chrsz) & mask) << (i%32);
		return bin;
	}
}

var valida={

	//private expression
	_nKeypress : window.Event ? true : false,

	numeros : function(evt){
		var key = this._nKeypress ? evt.which : evt.keyCode;
		return (key <= 13 || (key >= 48 && key <= 57));
	},

	alfanumerico : function(evt){
		var key = this._nKeypress ? evt.which : evt.keyCode;
		return (key <= 12 || (key >= 48 && key <= 57) || ((key >= 65 && key <= 90) || (key >= 97 && key <= 122)));
		
	},
	
	alfa_email : function(evt){
		var key = this._nKeypress ? evt.which : evt.keyCode;
		return (key <= 13 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key==64 || key==46 || key==42 || key==43 || key==45 || key==47 || key==95));
	},
	
	alfanumericosignos : function(evt){
		var key = this._nKeypress ? evt.which : evt.keyCode;
		return (key <= 13 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key >= 40 && key <= 47) || (key==38 || key==58 || key==59 || key==64 || key==91 || key==92 || key==93 || key==95));
	},
	
	alfanumericosignos_sinenter: function(evt){
		var key = this._nKeypress ? evt.which : evt.keyCode;
		return (key <= 12 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key >= 40 && key <= 47) || (key==38 || key==58 || key==59 || key==64 || key==91 || key==92 || key==93 || key==95));
	},
	
	numeros_banco : function(evt){
		var key = this._nKeypress ? evt.which : evt.keyCode;
		return (key <= 13 || (key >= 48 && key <= 57) || key==44 || key==46);
	},
	
	letras : function(evt){
		var key = this._nKeypress ? evt.which : evt.keyCode;
		return (key <= 13 || ((key >= 65 && key <= 90) || (key >= 97 && key <= 122)) || (key==193 || key==201 || key==205 || key==209 || key==211 || key==218 || key==225 || key==233 || key==237 || key==241 || key==243 || key==250 || key==32));
	},
	
	decimales : function(evt){
		var key = this._nKeypress ? evt.which : evt.keyCode;
		return (key <= 13 || (key >= 48 && key <= 57) || key == 46);
	},
	
	email : function(str_email){
		var filtrar=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})$/;
		if (filtrar.test(str_email))
		{
			return true;
		}else{
			return false;
		}
	},
	
	correo : function(idObjeto){
		var valor=$('#'+idObjeto).val();
		var filtrar=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})$/;
		if (filtrar.test(valor))
		{
			return true;
		}else{
			alert('Debe proporcionar una dirección de correo electrónico válida');
			$('#'+idObjeto).focus();
			return false;
		}
	},
	
	vacio : function(idObjeto){
		var valor=$('#'+idObjeto).val();
		if(valor!="")
		{
			return true;
		}else{
			alert('Este campo no debe ir vacio');
			$('#'+idObjeto).focus();
			return false;
		}
	}
};

var login={

	_mensaje : '',
	
	usuario : function(txt_usuario){
		if(txt_usuario.length>0)
		{
			return true;
		}else{
			this._mensaje='El nombre del usuario no puede estar vacío!!!';
			return false;
		}
	},
	
	usuario_email : function(txt_usuario){
		if(txt_usuario.length>0)
		{
			if(valida.email(txt_usuario))
			{
				return true;
			}else{
				this._mensaje='Debe proporcionar una dirección de correo válida!!!';
				return false;
			}
		}else{
			this._mensaje='El nombre del usuario no puede estar vacío!!!';
			return false;
		}
	},
	
	clave : function(txt_clave){
		if(txt_clave.length>0)
		{
			return true;
		}else{
			this._mensaje='La clave de acceso del usuario no puede estar vacía!!!';
			return false;
		}
	},
	
	alerta : function (){
		alert(this._mensaje);
	}
}

// Codigo JS para HTML Login
/*
usuarios AEL	login char(20)	clave char(20)
*/
$(document).ready(function(){
	$('#txt_usuario_ael').keypress(function(e){
		return valida.alfa_email(e);
	});
});
$(document).ready(function(){
	$('#txt_clave_ael').keypress(function(e){
		return valida.alfanumericosignos(e);
	});
});
$(document).ready(function(){
	$('#frmLogin_ael').submit(function(){
		if(login.usuario($('#txt_usuario_ael').val()))
		{
			if(login.clave($('#txt_clave_ael').val()))
			{
				$('#idusuario_ael').val(Clase64.encode($('#txt_usuario_ael').val()));
				$('#idclave_ael').val(Clase64.encode($('#txt_clave_ael').val()));
				$('#idvalida_ael').val(ClaseMD5.txt2md5($('#txt_usuario_ael').val()+$('#txt_clave_ael').val()));
				$('#txt_usuario_ael').val('');
				$('#txt_clave_ael').val('');
				return true;
			}
			login.alerta();
			$('#txt_clave_ael').focus();
			return false;
		}		
		login.alerta();
		$('#txt_usuario_ael').focus();
		return false;	
	});
});
/*
usuarios WEB	email char(80)	clave char(50)
*/
$(document).ready(function(){
	$('#txt_usuario_web').keypress(function(e){
		return valida.alfa_email(e);
	});
});
$(document).ready(function(){
	$('#txt_clave_web').keypress(function(e){
		return valida.alfanumericosignos(e);
	});
});
$(document).ready(function(){
	$('#frmLogin_web').submit(function(){
		if(login.usuario($('#txt_usuario_web').val()))
		{
			if(login.clave($('#txt_clave_web').val()))
			{
				$('#idusuario_web').val(Clase64.encode($('#txt_usuario_web').val()));
				$('#idclave_web').val(Clase64.encode($('#txt_clave_web').val()));
				$('#idvalida_web').val(ClaseMD5.txt2md5($('#txt_usuario_web').val()+$('#txt_clave_web').val()));
				$('#txt_usuario_web').val('');
				$('#txt_clave_web').val('');
				return true;
			}
			login.alerta();
			$('#txt_clave_web').focus();
			return false;
		}		
		login.alerta();
		$('#txt_usuario_web').focus();
		return false;	
	});
});