var __strMainDir;

var __S = document.getElementsByTagName("script");
if (__S.length) {
	__strMainDir= __S[0].src.substr(__S[0].src.indexOf("main_dir"), __S[0].src.length);
	__strMainDir = __strMainDir.replace("main_dir=", "");
}


function isEmail(email) {
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(email)) {
		return true;
	} else {
		return false;
	}
}

/*------------------------------------------------------------------------------------------------------------\
	Date Created: 12-30-05
	Modified Date: 12-30-05
	Function Name: ClearHTML(sHTML)
	Porpouse: Limpia el código HTML.
		sHTML: HTML Tags a limpiar.
/-------------------------------------------------------------------------------------------------------------*/
function ClearHTML(sHTML) {
	sHTML = CleanWord(sHTML);
	sHTML = sHTML.replace(/<[^>]*>/gi, "");
	sHTML = sHTML.replace( /\n/gi, "") ;
	sHTML = sHTML.replace(/&nbsp;/gi, "");
	var RegX = new RegExp(String.fromCharCode(10), 'g');
	sHTML = sHTML.replace(RegX, "");
	RegX = new RegExp(String.fromCharCode(13), 'g');
	sHTML = sHTML.replace(RegX, "");
	return sHTML;
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 12-30-05
	Modified Date: 12-30-05
	Function Name: CleanWord(html)
	Porpouse: Limpia el código HTML generado por Microsoft Word.
		html: HTML Tags a limpiar.
/-------------------------------------------------------------------------------------------------------------*/
function CleanWord(html) {
	html = html.replace(/<o:p>\s*<\/o:p>/g, "") ;
	html = html.replace(/<o:p>.*?<\/o:p>/g, "&nbsp;") ;
	
	// Remove mso-xxx styles.
	html = html.replace( /\s*mso-[^:]+:[^;"]+;?/gi, "" ) ;

	// Remove margin styles.
	html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, "" ) ;
	html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" ) ;

	html = html.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, "" ) ;
	html = html.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" ) ;

	html = html.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" ) ;

	html = html.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" ) ;

	html = html.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" ) ;

	html = html.replace( /\s*tab-stops:[^;"]*;?/gi, "" ) ;
	html = html.replace( /\s*tab-stops:[^"]*/gi, "" ) ;

	// Remove FONT face attributes.
	html = html.replace( /\s*face="[^"]*"/gi, "" ) ;
	html = html.replace( /\s*face=[^ >]*/gi, "" ) ;

	html = html.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, "" ) ;
	
	// Remove Class attributes
	html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;

	// Remove styles.
	html = html.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" ) ;

	// Remove empty styles.
	html =  html.replace( /\s*style="\s*"/gi, '' ) ;
	
	html = html.replace( /<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi, '&nbsp;' ) ;
	
	html = html.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ;
	
	// Remove Lang attributes
	html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	
	html = html.replace( /<SPAN\s*>(.*?)<\/SPAN>/gi, '$1' ) ;
	
	html = html.replace( /<FONT\s*>(.*?)<\/FONT>/gi, '$1' ) ;

	// Remove XML elements and declarations
	html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
	
	// Remove Tags with XML namespace declarations: <o:p></o:p>
	html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
	
	html = html.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;

	html = html.replace( /<H1([^>]*)>/gi, '<div$1><b><font size="6">' ) ;
	html = html.replace( /<H2([^>]*)>/gi, '<div$1><b><font size="5">' ) ;
	html = html.replace( /<H3([^>]*)>/gi, '<div$1><b><font size="4">' ) ;
	html = html.replace( /<H4([^>]*)>/gi, '<div$1><b><font size="3">' ) ;
	html = html.replace( /<H5([^>]*)>/gi, '<div$1><b><font size="2">' ) ;
	html = html.replace( /<H6([^>]*)>/gi, '<div$1><b><font size="1">' ) ;

	html = html.replace( /<\/H\d>/gi, '</font></b></div>' ) ;
	
	html = html.replace( /<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;' ) ;

	// Remove empty tags (three times, just to be sure).
	html = html.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
	html = html.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
	html = html.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;

	// Transform <P> to <DIV>
	var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ;	// Different because of a IE 5.0 error
	html = html.replace( re, "<div$2</div>" ) ;

	return html ;
}


function InputType(e, flag, _teclas) {
	var patron;
	var tecla = KeyAscii(e);
	var extrakey = -1;
	if (_teclas != "" && _teclas) {
		_teclas = _teclas.toString();
		extrakey = _teclas.indexOf(tecla.toString());
	}
	
	if (tecla == 8 || tecla == 13 || extrakey >= 0) return true; 
	//Tecla de retroceso (para poder borrar) y enter (para poder enviar)
	
	switch (flag) {
		case 0:
			patron = /[A-Za-z]/; //Solo acepta letras
			break;
		case 1:
			patron = /\D/; //No acepta números
			break;
		case 2:
			patron = /\w/; //Acepta números y letras
			break;
		default:
			patron = /\d/; //Solo acepta números
			break;
	}
	
	var key = String.fromCharCode(tecla);
	return patron.test(key);
}

function ValidarLogin(f) {
	if (f.email.value == "") {
		alert("Por favor, ingrese su correo electrónico.");
		f.email.focus();
		return false;
	}
	
	f.b_entrar.disabled = true;
	
	var chk_user = new SetAjax();
	chk_user.FilePath = "includes/conectar.php";
	chk_user.FileRedirect = "reportes.php";
	chk_user.setVar = [["email", f.email.value]];
	chk_user.manageError = function(err, val) {
		document.body.style.cursor = "default";
		f.b_entrar.disabled = false;
		f.b_entrar.value = 'Entrar';
		alert(val);
	}
	chk_user.onLoading = function() {
		document.body.style.cursor = "wait";
		f.b_entrar.value = 'Espere';
	}
	chk_user.onCompletion = function() {
		document.body.style.cursor = "default";
	}
	chk_user.Execute();
	
	return false;
}


function ValidarAdmin(f) {
	if (f.usuario.value == "") {
		alert("Por favor, ingrese su Usuario.");
		f.usuario.focus();
		return false;
	}
	
	if (f.clave.value == "") {
		alert("Por favor, ingrese su Contraseña.");
		f.clave.focus();
		return false;
	}
	
	f.b_entrar.disabled = true;
	
	var chk_user = new SetAjax();
	chk_user.FilePath = "conectar.php";
	chk_user.FileRedirect = "main.php";
	chk_user.setVar = [["u", f.usuario.value], ["c", f.clave.value]];
	chk_user.manageError = function(err, val) {
		document.body.style.cursor = "default";
		f.b_entrar.disabled = false;
		f.b_entrar.value = 'Entrar';
		alert(val);
	}
	chk_user.onLoading = function() {
		document.body.style.cursor = "wait";
		f.b_entrar.value = 'Espere';
	}
	chk_user.onCompletion = function() {
		document.body.style.cursor = "default";
	}
	chk_user.Execute();
	
	return false;
}


function ReplaceChars(sHTML, CharCode, sString) {
	var RegX = new RegExp(String.fromCharCode(CharCode), 'g');
	sHTML = sHTML.replace(RegX, sString);
	
	return sHTML;
}


function ReplaceEnters(sHTML, sString) {
	var RegX = new RegExp(String.fromCharCode(10), 'g');
	sHTML = sHTML.replace(RegX, sString);
	var RegX = new RegExp(String.fromCharCode(13), 'g');
	sHTML = sHTML.replace(RegX, sString);
	
	return sHTML;
}

function jsSetAttribute(obj, _name, _value) {
	obj = (!jsGetObject(obj)) ? obj : jsGetObject(obj);
	obj.setAttribute(_name, _value);
}

function Log_Out() {
	ht = document.getElementsByTagName("body");
	ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
	if (confirm('¿Desea Cerrar su Sesión?')) {
		return true;
	} else {
		ht[0].style.filter = "";
		return false;
	}
}


function CargarSelect(obj1, obj2, parent_field, tabla, _idioma, _default_txt) {
	if (obj1) {
		if (!_default_txt) _default_txt = "Seleccione";
		if (!_idioma) _idioma = '';
		var obj_values = jsGetObject("txtcargar_select_elements");
		obj2 = jsGetObject(obj2);
		obj_values.value = "";
		
		var cargar = new SetAjax();
		cargar.FilePath = __strMainDir + "includes/procesos/cargar_select.php";
		cargar.setVar = [["tabla", tabla], ["parentid", obj1.value], ["parent_field", parent_field], ["idioma", _idioma]];
		cargar.Element = obj_values;
		cargar.ElementAttribute = "value";
		cargar.onLoading = function() {
			document.body.style.cursor = "wait";
			obj1.disabled = true;
			obj2.disabled = true;
			ClearSelect(obj2);
			AddSelectOption(obj2, "Cargando...", "0");
		}
		cargar.onCompletion = function() {
			document.body.style.cursor = "default";
			obj1.disabled = false;
			ClearSelect(obj2);
			AddSelectOption(obj2, _default_txt, "0");
			
			var split1 = obj_values.value.split("[|]");
			if (split1.length > 1) {
				for(i=0; i < split1.length; i++) {
					if (split1[i].length > 0) {
						var split2 = split1[i].split("[-]");
						AddSelectOption(obj2, split2[1], split2[0]);
					}
				}
			}
			
			obj2.disabled = false;
		}
		cargar.Execute();
	}
}


document.write('<input type="hidden" name="txtcargar_select_elements" id="txtcargar_select_elements">');

function ClearSelect(obj) {
	obj = (!jsGetObject(obj)) ? obj : jsGetObject(obj);
	obj.options.length = 0;	
}

function AddSelectOption(obj, txt, val, selected) {
	obj = (!jsGetObject(obj)) ? obj : jsGetObject(obj);
	
	selected = (selected == true) ? "defauldSelected" : "";
	var sub0 = new Option(txt, val, selected);
	obj.options[obj.options.length] = sub0;
}

function KeyAscii(e) {
	return (document.all) ? e.keyCode : e.which;
}
