/****************************************************************
*  acciones.js
*
*  Funciones de JavaScripts para el Sistema de Sitios Webs
*  Ezequiel Villarreal G.	(djflea@gmail.com)
*  Soft-Media Development S.A.
*  Colaboración: Carlos Caballero P.	(vaaleg@gmail.com)
*
*  2005-2006, Todos los derechos reservados.
*****************************************************************/


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 12-28-05
	Modified Date: 12-28-05
	Function Name: InputClassError(obj, error)
	Porpouse: Cambia la Clase (stylesheet) del objeto a validar en una forma.
		obj: Id del Objeto.
		error: Flag de la función.  true o false.
/-------------------------------------------------------------------------------------------------------------*/
function InputClassError(obj, error) {
	obj.className = (!error) ? "input" : "InputClassError";
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 12-31-05
	Modified Date: 12-31-05
	Function Name: KeyAscii(e)
	Porpouse: Retorna el código ascii al ser presionada una tecla.
		e: Evento del Objeto.
/-------------------------------------------------------------------------------------------------------------*/
function KeyAscii(e) {
	return (document.all) ? e.keyCode : e.which;
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 12-31-05
	Modified Date: 12-31-05
	Function Name: KeyAscii(e)
	Porpouse: Retorna el código ascii al ser presionada una tecla.
		e: Evento del Objeto.
/-------------------------------------------------------------------------------------------------------------*/
function HideInput(e, obj) {
	if (KeyAscii(e) == 27) ClearRows(obj);		//IE Only
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 12-31-05
	Modified Date: 12-31-05
	Function Name: jsSetAttribute(obj, _name, _value)
	Porpouse: Asigna un atributo a un objeto o elemento.
		obj: Id del Objeto.
		_name: Nombre del atributo.
		_value: Valor del atributo.
/-------------------------------------------------------------------------------------------------------------*/
function jsSetAttribute(obj, _name, _value) {
	obj = (!jsGetObject(obj)) ? obj : jsGetObject(obj);
	obj.setAttribute(_name, _value);
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 12-28-05
	Modified Date: 12-28-05
	Function Name: ToggleDisplay(obj)
	Porpouse: Muestra u Oculta un Objeto.
		obj: Id del Objeto.
/-------------------------------------------------------------------------------------------------------------*/
function ToggleDisplay(obj) {
	var _style = (jsGetStyle(obj, "display") == "none") ? "block" : "none";
	jsSetStyle(obj, "display", _style);
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 10-21-05
	Modified Date: 10-21-05
	Function Name: openWin(eURL,eName,features)
	Porpouse: Abre una ventana estilo PopUp.
		eURL: Url o dirección de la página o archivo a abrir.
		eName: Nombre del PopUp o ventanna
		features: parámetros que se le enviará para cambiar el estilo y forma del PopUp o ventana
/-------------------------------------------------------------------------------------------------------------*/
function openWin(eURL,eName,features) {
	window.open(eURL,eName,features);
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 01-09-06
	Modified Date: 01-09-06
	Function Name: openCenterWin(eURL,eName,eWidth,eHeight)
	Porpouse: Abre una ventana estilo PopUp.
		eURL: Url o dirección de la página o archivo a abrir.
		eName: Nombre del PopUp o ventanna
		eWidth: Ancho de la ventana
		eHeight: Alto de la ventana
		eFeatures: Opciones varias del PopUp
/-------------------------------------------------------------------------------------------------------------*/
function openCenterWin(eURL,eName,eWidth,eHeight,eFeatures) {
	var _features = (eFeatures == "") ? "resizable=yes,status=no,scrollbars=no" : eFeatures;
	var _win_options = _features + ",width=" + eWidth + ",height=" + eHeight + ",left=" + ((screen.width/2)-(eWidth/2)) + ",top=" + ((screen.height/2)-(eHeight/2));
	openWin(eURL,eName,_win_options);
}


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;
}


/*------------------------------------------------------------------------------------------------------------\
	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, "");
	
	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 ;
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 10-21-05
	Modified Date: 10-21-05
	Function Name: InputType(e, flag)
	Porpouse: Validar diferentes métodos a la hora de introducir o escribir en un input
		e: Evento del objeto
		flag: Tipo de validación (default números)
/-------------------------------------------------------------------------------------------------------------*/
function InputType(e, flag) {
	var patron;
	var tecla = KeyAscii(e); 
	if (tecla == 8 || tecla == 13) 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);
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 11-10-05
	Modified Date: 11-10-05
	Function Name: InputCheck(obj, flag)
	Porpouse: Validar diferentes métodos a la hora de introducir o escribir en un input
		obj: Objeto de la forma
		flag: Tipo de validación (default números)
/-------------------------------------------------------------------------------------------------------------*/
function InputCheck(obj, flag) {
	var patron;
	
	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/g; //Acepta números y letras
			break;
		default:
			patron = /\d/; //Solo acepta números
			break;
	}
	
	return patron.test(obj.value);
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 10-13-05
	Modified Date: 10-13-05
	Function Name: isEmail(str)
	Porpouse: Valida que un correo electrónico esté bien escrito.
		str: E-mail a validar.
/-------------------------------------------------------------------------------------------------------------*/
function isEmail(obj) {
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(obj.value)) {
		return true;
	} else {
		return false;
	}
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 01-01-06
	Modified Date: 01-01-06
	Function Name: jsEnters(str)
	Porpouse: Reemplaza los ENTERS por los tags <br>.
		str: Cadena que se desea reemplazar los ENTERS.
/-------------------------------------------------------------------------------------------------------------*/
function jsEnters(str) {
	if (_jslib_isIE)
		return str.replace( /<br>/gi, String.fromCharCode(13));
	else
		return str.replace( /<br>/gi, String.fromCharCode(10));
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 10-21-05
	Modified Date: 10-21-05
	Function Name: _CloseOnEsc()
	Porpouse: Cierra la ventanana cuando se presione la tecla ESC.
	Function Name: InitESC()
	Porpouse: Ejecuta la funcion _CloseOnEsc cuando se presione cualquier tecla.
/-------------------------------------------------------------------------------------------------------------*/
function _CloseOnEsc(e) {
	if (KeyAscii(event) == 27) { window.close(); return; }
}

function InitESC() {
	document.body.onkeypress = _CloseOnEsc;
}



/*------------------------------------------------------------------------------------------------------------\
	Date Created: 10-23-05
	Modified Date: 10-23-05
	Function Name: jsCenterThis(strObj, strType)
	Porpouse: Obtiene la posición para centrear un objeto.
		strObj: Id del Objeto
		strType:
			t or T: Alto del Objeto en la pantalla
			l or L: Izquierda del Objeto en la pantalla
/-------------------------------------------------------------------------------------------------------------*/
function jsCenterThis(strObj, strType) {
	if (strType.toLowerCase() == "l") {
    	return (screen.width / 2) - (parseInt(jsGetStyle(strObj, "width").replace("px", "")) / 2);
	} else {
    	return (screen.height / 2) - (parseInt(jsGetStyle(strObj, "height").replace("px", "")) / 2);
	}
}


function WinXpEffect(obj, sw) {
	obj = (!jsGetObject(obj)) ? obj : jsGetObject(obj);
	if (sw == true) {
		obj.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
	} else {
		obj.style.filter = "";
	}
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 10-13-05
	Modified Date: 10-13-05
	Function Name: jsResolution(strType)
	Porpouse: Obtiene la resolución de la pantalla.
		strType:
			w or W: Ancho de la Pantalla
			h or H: Alto de la Pantalla
/-------------------------------------------------------------------------------------------------------------*/
function jsResolution(strType) {
	if (strType.toLowerCase() == "w") {
		return (document.all) ? document.body.clientWidth : window.outerWidth;
	} else {
		return (document.all) ? document.body.clientHeight : window.outerHeight;
	}
}


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 MaxResumen(obj, target, total) {
	obj = (!jsGetObject(obj)) ? obj : jsGetObject(obj);
	var label = (!jsGetObject(target)) ? target : jsGetObject(target);
	if (label && obj) {
		if (obj.value.length <= total) {
			label.innerHTML = total - obj.value.length;
		} else {
			if (obj.value.length <= total) {
				label.innerHTML = obj.value.length++;
			} else {
				obj.value = obj.value.substring(0, total);
				label.innerHTML = total-obj.value.length;
			}
		}
	}
}


function MM_jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


/*------------------------------------------------------------------------------------------------------------\
	Date Created: 10-14-05
	Modified Date: 10-14-05
	Function Name: jsPosition(strObj, strType)
	Porpouse: Obtiene la posición absoluta de un objeto.
		strObj: Id del Objeto
		strType:
			t or T: Alto del Objeto con respecto a la pantalla
			l or L: Izquierda del Objeto con respecto a la pantalla
/-------------------------------------------------------------------------------------------------------------*/
function jsPosition(strObj, strType) {
	if (strType.toLowerCase() == "t") {
		return jsGetObject(strObj).offsetTop;
	} else {
		return jsGetObject(strObj).offsetLeft;
	}
}