<!--

/*



    Derechos Reservados © 2008 Ernesto Spiro Peimbert Andreakis



    @archivo  utilerias.js



    @autor    Ernesto Spiro Peimbert Andreakis



    @fecha    junio 25 2008



    @correo   spiro79@gmail.com



    @comentarios

        Herramientas genéricas de javascript.



    INFORMACION GENERAL:



        Alonso de la Veracruz 25

        Cd. Satélite, México

        Mexico, C.P. 53100

        Cel: 0445531935844



    LICENCIA:

        Copyright ©

        This program is free software; you can redistribute it and/or

        modify it under the terms of the GNU General Public License

        as published by the Free Software Foundation; either version 2

        of the License, or (at your option) any later version.



        This program is distributed in the hope that it will be useful,

        but WITHOUT ANY WARRANTY; without even the implied warranty of

        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

        GNU General Public License for more details.



        You should have received a copy of the GNU General Public License

        along with this program; if not, write to the Free Software

        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

        

*/



/*

funcion valorCampo

@param       obj id del objeto u objeto

@return      valor del objeto

@brief       a partir de un objeto pasado como parametro devuelve el valor almacenado en el mismo

@author      Ernesto Spiro Peimbert Andreakis

@date        23 junio 2008

*/

function valorCampo (obj) {

	var valor = '';

	var continua = true;

	if (typeof(obj) != 'undefined') {

		if (typeof(obj) == 'string') {

			var objeto = document.getElementById(obj);

			if ((objeto == null) || (typeof(objeto) == 'undefined')) {

				valor = undefined;

				continua = false;

			}

		}

		else if (typeof(obj) == 'object') {

			var objeto = obj;

		}

		else {

			continua = false;

		}

		if (continua) {

			switch(objeto.type) {

				case 'text':

				case 'textarea':

				case 'button':

				case 'submit':

				case 'password':

				case 'hidden':

				case 'reset':

				case 'file':

					valor = objeto.value;

					break;

				case 'radio':

					var nombre = objeto.name;

					var arreglo_valores = document.getElementsByName(nombre);

					var cantidad = arreglo_valores.length;

					var i;

					for ( i = 0; i < cantidad; i++ ) {

						if ( arreglo_valores[i].checked ) {

							valor = arreglo_valores[i].value;

							break;

						}

					}

					break;

				case 'checkbox':

					if ( objeto.checked ) {

						if ( objeto.value ) {

							valor = objeto.value;

						}

						else {

							valor = 'on';

						}

					}

					else {

						valor = '';

					}

					break;

				case 'select-one':

					if ( objeto.options.length > 0 ) {

						if ( objeto.selectedIndex > -1 ) {

							valor = objeto.options[objeto.selectedIndex].value;

						}

					}

					break;

				case 'select-multiple':

					if ( objeto.options.length > 0 ) {

						if ( objeto.selectedIndex > -1 ) {

							var arr_valores = new Array();

							var arr_indices = new Array();

							var arr_indices2 = new Array();

							while ( objeto.selectedIndex > -1 ) {

								arr_valores.push(objeto.options[objeto.selectedIndex].value);

								arr_indices.push(objeto.selectedIndex);

								objeto.options[objeto.selectedIndex].selected = false;

							}

							while ( arr_indices.length > 0 ) {

								objeto.options[arr_indices.pop()].selected = true;

							}

							var valor = new Array();

							valor[0] = arr_valores.toString();

							valor[1] = arr_valores;

						}

					}

					break;

				default:

					valor = objeto.innerHTML;

					break;

			}

		}

	}

	return valor;

}

/** //MINIMIZADO

function valorCampo(obj){var valor = '';var continua = true;if(typeof(obj)!= 'undefined'){if(typeof(obj)== 'string'){var objeto = document.getElementById(obj);if((objeto == null)||(typeof(objeto)== 'undefined')){valor = undefined;continua = false;}}else if(typeof(obj)== 'object'){var objeto = obj;}else {continua = false;}if(continua){switch(objeto.type){case 'text':case 'textarea':case 'button':case 'submit':case 'password':case 'hidden':case 'reset':case 'file':valor = objeto.value;break;case 'radio':var nombre = objeto.name;var arreglo_valores = document.getElementsByName(nombre);var cantidad = arreglo_valores.length;var i;for(i = 0; i < cantidad; i++){if(arreglo_valores[i].checked){valor = arreglo_valores[i].value;break;}}break;case 'checkbox':if(objeto.checked){if(objeto.value){valor = objeto.value;}else {valor = 'on';}}else {valor = '';}break;case 'select-one':if(objeto.options.length > 0){if(objeto.selectedIndex > -1){valor = objeto.options[objeto.selectedIndex].value;}}break;case 'select-multiple':if(objeto.options.length > 0){if(objeto.selectedIndex > -1){var arr_valores = new Array();var arr_indices = new Array();var arr_indices2 = new Array();while(objeto.selectedIndex > -1){arr_valores.push(objeto.options[objeto.selectedIndex].value);arr_indices.push(objeto.selectedIndex);objeto.options[objeto.selectedIndex].selected = false;}while(arr_indices.length > 0){objeto.options[arr_indices.pop()].selected = true;}var valor = new Array();valor[0] = arr_valores.toString();valor[1] = arr_valores;}}break;default:valor = objeto.innerHTML;break;}}}return valor;}

// */



/*

funcion agregaEvento

@param       obj id del objeto u objeto

@param       evt evento al que respondera el objeto

@param       funcion apuntador a una funcion que se ejecutara

@return      bool true si se agregó correctamente, false de lo contrario

@brief       agrega una funcion que respondera al ocurrir un evento determinado en el objeto

@author      Ernesto Spiro Peimbert Andreakis

@date        27 junio 2008

*/

function agregaEvento (obj,evt,funcion) {

	var regresa = false;

	if (typeof(obj) != 'undefined') {

		if (typeof(obj) == 'string') {

			var objeto = document.getElementById(obj);

			if ((objeto != null) && (typeof(objeto) != 'undefined')) {

				regresa = true;

			}

		}

		else if (typeof(obj) == 'object') {

			var objeto = obj;

			regresa = true;

		}

		if (regresa && (evt != '')) {

			if(window.addEventListener){ // Mozilla, Netscape, Firefox

				objeto.addEventListener(evt, funcion, false);

			}

			else { // IE

				objeto.attachEvent('on'+evt, funcion);

			}

		}

	}

	return regresa;

}

/** //MINIMIZADO

function agregaEvento (obj,evt,funcion) {var regresa = false;if (typeof(obj) != 'undefined') {if (typeof(obj) == 'string') {var objeto = document.getElementById(obj);if ((objeto != null) && (typeof(objeto) != 'undefined')) {regresa = true;}}else if (typeof(obj) == 'object') {var objeto = obj;regresa = true;}if (regresa && (evt != '')) {if(window.addEventListener){objeto.addEventListener(evt, funcion, false);}else {objeto.attachEvent('on'+evt, funcion);}}}return regresa;}

// */



/*

funcion quitaEvento

@param       obj id del objeto u objeto

@param       evt evento al que respondera el objeto

@param       funcion apuntador a una funcion que se ejecutara

@return      bool true si se agregó correctamente, false de lo contrario

@brief       elimina una funcion que respondera al ocurrir un evento determinado en el objeto

@author      Ernesto Spiro Peimbert Andreakis

@date        27 junio 2008

*/

function quitaEvento (obj,evt,funcion) {

	var regresa = false;

	if (typeof(obj) != 'undefined') {

		if (typeof(obj) == 'string') {

			var objeto = document.getElementById(obj);

			if ((objeto != null) && (typeof(objeto) != 'undefined')) {

				regresa = true;

			}

		}

		else if (typeof(obj) == 'object') {

			var objeto = obj;

			regresa = true;

		}

		if (regresa && (evt != '')) {

			if(window.addEventListener){ // Mozilla, Netscape, Firefox

				objeto.removeEventListener(evt, funcion, false);

			}

			else { // IE

				objeto.detachEvent('on'+evt, funcion);

			}

		}

	}

	return regresa;

}

/** //MINIMIZADO

function quitaEvento (obj,evt,funcion) {var regresa = false;if (typeof(obj) != 'undefined') {if (typeof(obj) == 'string') {var objeto = document.getElementById(obj);if ((objeto != null) && (typeof(objeto) != 'undefined')) {regresa = true;}}else if (typeof(obj) == 'object') {var objeto = obj;regresa = true;}if (regresa && (evt != '')) {if(window.addEventListener){objeto.removeEventListener(evt, funcion, false);}else {objeto.detachEvent('on'+evt, funcion);}}}return regresa;}

// */



/*

funcion redondea

@param       numero numero a redondear

@param       decimales numero de decimales

@return      numero redondeado

@brief       redondea una cantidad a los decimales deseados

@author      Ernesto Spiro Peimbert Andreakis

@date        27 junio 2008

*/

function redondea (numero,decimales) {

	var factor = Math.pow(10,decimales);

	var rounded = Math.round(numero * factor)/factor;

	return rounded;

}

/** //MINIMIZADO

function redondea (numero,decimales) {var factor = Math.pow(10,decimales);var rounded = Math.round(numero * factor)/factor;return rounded;}

// */



/*

funcion formatoNumero

@param       valor numero a formatear

@param       separador_miles caracter utilizado para separar unidades enteras

@param       separador_decimales caracter empleado para separar la parte entera de la fraccionaria

@return      numero con formato

@brief       formatea una cantidad

@author      Ernesto Spiro Peimbert Andreakis

@date        27 junio 2008

*/

function formatoNumero (valor,separador_miles, separador_decimales) {

	var regresa = '0';

	var partes = String(valor).split('.');

	if ( typeof(partes[1]) == 'undefined' ) {

		partes[1] = '00';

	}

	if ( partes[1].length == 1 ) {

		partes[1] = partes[1] + '0';

	}

	var tamano = partes[0].length;

	if ( tamano >= 4 ) {

		var cuantas_comas = parseInt((tamano - 1) / 3);

		var digitos_no_agrupados = tamano % 3;

		var auxiliar = 0;

		var indice_inicial = 0;

		var numero = "";

		for ( auxiliar = 0; auxiliar <= cuantas_comas; auxiliar++ ) {

			if ( auxiliar == 0 && digitos_no_agrupados != 0 ) {

				numero += partes[0].substr(indice_inicial,digitos_no_agrupados) + separador_miles;

				indice_inicial += digitos_no_agrupados;

			}

			else {

				if ( auxiliar != cuantas_comas ) {

					numero += partes[0].substr(indice_inicial,3) + separador_miles;

				}

				else {

					numero += partes[0].substr(indice_inicial,3);

				}

				indice_inicial += 3;

			}

		}

		partes[0] = numero;

	}

	regresa = partes[0] + separador_decimales + partes[1];

	return regresa;

}

/** //MINIMIZADO

function formatoNumero (valor,separador_miles, separador_decimales) {var regresa = '0';var partes = String(valor).split('.');if ( typeof(partes[1]) == 'undefined' ) {partes[1] = '00';}if ( partes[1].length == 1 ) {partes[1] = partes[1] + '0';}var tamano = partes[0].length;if ( tamano >= 4 ) {var cuantas_comas = parseInt((tamano - 1) / 3);var digitos_no_agrupados = tamano % 3;var auxiliar = 0;var indice_inicial = 0;var numero = "";for ( auxiliar = 0; auxiliar <= cuantas_comas; auxiliar++ ) {if ( auxiliar == 0 && digitos_no_agrupados != 0 ) {numero += partes[0].substr(indice_inicial,digitos_no_agrupados) + separador_miles;indice_inicial += digitos_no_agrupados;}else {if ( auxiliar != cuantas_comas ) {numero += partes[0].substr(indice_inicial,3) + separador_miles;}else {numero += partes[0].substr(indice_inicial,3);}indice_inicial += 3;}}partes[0] = numero;}regresa = partes[0] + separador_decimales + partes[1];return regresa;}

// */



/*

funcion ponValorCampo

@param       obj objeto/string id del objeto

@param       valor que valor se asignará

@return      bool true si se logró realizar la operación false en caso contrario

@brief       asigna un valor a un campo

@author      Ernesto Spiro Peimbert Andreakis

@date        27 junio 2008

*/

function ponValorCampo (obj,valor) {

	var regresa = false;

	var continua = true;

	if (typeof(obj) != 'undefined' && valor != '') {

		if (typeof(obj) == 'string') {

			var objeto = document.getElementById(obj);

			if ((objeto == null) || (typeof(objeto) == 'undefined')) {

				valor = undefined;

				continua = false;

			}

		}

		else if (typeof(obj) == 'object') {

			var objeto = obj;

		}

		else {

			continua = false;

		}

		if (continua) {

			regresa = true;

			switch(objeto.type) {

				case 'text':

				case 'textarea':

				case 'button':

				case 'submit':

				case 'password':

				case 'hidden':

				case 'reset':

				case 'file':

					objeto.value = valor;

					break;

				case 'radio':

					var nombre = objeto.name;

					var arreglo_valores = document.getElementsByName(nombre);

					var cantidad = arreglo_valores.length;

					var i;

					for ( i = 0; i < cantidad; i++ ) {

						if ( arreglo_valores[i].value == valor ) {

							arreglo_valores[i].checked = true;

						}

						else {

							arreglo_valores[i].checked = false;

						}

					}

					break;

				case 'checkbox':

					if ( objeto.value == valor ) {

						objeto.checked = true;

					}

					break;

				case 'select-one':

					var cantidad = objeto.length;

					var auxiliar;

					for ( auxiliar = 0; auxiliar < cantidad; auxiliar++ ) {

						if ( objeto.options[auxiliar].value == valor ) {

							objeto.options[auxiliar].selected = true;

						}

						else {

							objeto.options[auxiliar].selected = false;

						}

					}

					break;

				case 'select-multiple':

					var valores = valor.split(",");

					var cantidad = objeto.length;

					var cantidad2 = valores.length;

					var auxiliar;

					var auxiliar2;

					var arr_auxiliar = new Array();

					for ( auxiliar = 0; auxiliar < cantidad; auxiliar++ ) {

						for ( auxiliar2 = 0; auxiliar2 < cantidad2; auxiliar2++ ) {

							objeto.options[auxiliar].selected = false;

							if ( objeto.options[auxiliar].value == valores[auxiliar2] ) {

								objeto.options[auxiliar].selected = true;

								break;

							}

						}

					}

					break;

				default:

					objeto.innerHTML = valor;

					break;

			}

		}

	}

	return regresa;

}

/** //MINIMIZADO

function ponValorCampo(obj,valor){var regresa = false;var continua = true;if(typeof(obj)!= 'undefined' && valor != ''){if(typeof(obj)== 'string'){var objeto = document.getElementById(obj);if((objeto == null)||(typeof(objeto)== 'undefined')){valor = undefined;continua = false;}}else if(typeof(obj)== 'object'){var objeto = obj;}else {continua = false;}if(continua){regresa = true;switch(objeto.type){case 'text':case 'textarea':case 'button':case 'submit':case 'password':case 'hidden':case 'reset':case 'file':objeto.value = valor;break;case 'radio':var nombre = objeto.name;var arreglo_valores = document.getElementsByName(nombre);var cantidad = arreglo_valores.length;var i;for(i = 0; i < cantidad; i++){if(arreglo_valores[i].value == valor){arreglo_valores[i].checked = true;}else {arreglo_valores[i].checked = false;}}break;case 'checkbox':if(objeto.value == valor){objeto.checked = true;}break;case 'select-one':var cantidad = objeto.length;var auxiliar;for(auxiliar = 0; auxiliar < cantidad; auxiliar++){if(objeto.options[auxiliar].value == valor){objeto.options[auxiliar].selected = true;}else {objeto.options[auxiliar].selected = false;}}break;case 'select-multiple':var valores = valor.split(",");var cantidad = objeto.length;var cantidad2 = valores.length;var auxiliar;var auxiliar2;var arr_auxiliar = new Array();for(auxiliar = 0; auxiliar < cantidad; auxiliar++){for(auxiliar2 = 0; auxiliar2 < cantidad2; auxiliar2++){objeto.options[auxiliar].selected = false;if(objeto.options[auxiliar].value == valores[auxiliar2]){objeto.options[auxiliar].selected = true;break;}}}break;default:objeto.innerHTML = valor;break;}}}return regresa;}

// */



/*

funcion getObjeto

@param       obj objeto/string id del objeto

@return      objeto objeto seleccionado

@brief       obtiene un objeto a partir de un id

@author      Ernesto Spiro Peimbert Andreakis

@date        27 junio 2008

*/

function getObjeto (obj) {

	var regresa = obj;

	if (typeof(obj) != 'undefined') {

		if (typeof(obj) == 'string') {

			regresa = document.getElementById(obj);

			if ((regresa == null) || (typeof(regresa) == 'undefined')) {

				regresa = false;

			}

		}

	}

	return regresa;

}

/** //MINIMIZADO

function getObjeto (obj) {var regresa = obj;if (typeof(obj) != 'undefined') {if (typeof(obj) == 'string') {regresa = document.getElementById(obj);if ((regresa == null) || (typeof(regresa) == 'undefined')) {regresa = false;}}}return regresa;}

// */



/*

funcion soloAcepta

@param       e evento, por lo general no se declara, es una captura

@return      validos caracteres que acepta objeto seleccionado

@brief       para un campo de texto evita que se puedan introducir otros caracteres

@author      Ernesto Spiro Peimbert Andreakis

@date        29 junio 2008

*/

function soloAcepta (e, validos) {

	var tecla;

	var resultado;

	if(!e) var e = window.event;

	tecla = window.event?parseInt(e.keyCode): parseInt(e.which);

	resultado = validos.indexOf(String.fromCharCode(tecla));

	if ( resultado == -1 && tecla != 0 && tecla != 8 && tecla != 13 && tecla != 9 ) {

		if (e.stopPropagation) {

			e.stopPropagation();

		}

		else {

			e.cancelBubble = true;

		}

		if (e.preventDefault) {

			e.preventDefault();

		}

		else {

			e.returnValue = false;

		}

		alert('El campo no acepta este valor.\nLos caracteres permitidos se listan a continuación:\n '+ validos.split(''));

	}

}

/** //MINIMIZADO

function soloAcepta (e, validos) {var tecla;var resultado;if(!e) var e = window.event;tecla = window.event?parseInt(e.keyCode): parseInt(e.which);resultado = validos.indexOf(String.fromCharCode(tecla));if ( resultado == -1 && tecla != 0 && tecla != 8 && tecla != 13 ) {if (e.stopPropagation)e.stopPropagation();else{e.cancelBubble = true;}if (e.preventDefault)e.preventDefault();else{e.returnValue = false;}alert('El campo no acepta este valor.\nLos valores permitidos se listan a continuacion:\n '+ validos);}}

// */



/*

funcion desactivaEnter

@param       e evento, por lo general no se declara, es una captura

@brief       desactiva la tecla enter para un campo

@author      Ernesto Spiro Peimbert Andreakis

@date        29 junio 2008

*/

function desactivaEnter (e) {

	var tecla;

	var resultado;

	if(!e) var e = window.event;

	tecla = window.event?parseInt(e.keyCode): parseInt(e.which);

	if ( tecla == 13 ) {

		if (e.stopPropagation) {

			e.stopPropagation();

		}

		else {

			e.cancelBubble = true;

		}

		if (e.preventDefault) {

			e.preventDefault();

		}

		else {

			e.returnValue = false;

		}

	}

}

/** //MINIMIZADO

function desactivaEnter (e) {var tecla;var resultado;if(!e) var e = window.event;tecla = window.event?parseInt(e.keyCode): parseInt(e.which);if ( tecla == 13 ) {if (e.stopPropagation)e.stopPropagation();else{e.cancelBubble = true;}if (e.preventDefault)e.preventDefault();else{e.returnValue = false;}}}

// */



/*

funcion createRequest

@return      false si no se pudo crear el objeto, o el objeto mismo en caso contrario

@brief       crea un objeto xmlhttprequest

@author      Ernesto Spiro Peimbert Andreakis

@date        29 junio 2008

*/

function createRequest() {

	try {

		var request = new XMLHttpRequest();

	} catch (trymicrosoft) {

		try {

			var request = new ActiveXObject("Msxml2.XMLHTTP");

		} catch (othermicrosoft) {

			try {

				var request = new ActiveXObject("Microsoft.XMLHTTP");

			} catch (failed) {

				var request = false;

			}

		}

	}

	return request;

}

/** //MINIMIZADO

function createRequest() {try {var request = new XMLHttpRequest();} catch (trymicrosoft) {try {var request = new ActiveXObject("Msxml2.XMLHTTP");} catch (othermicrosoft) {try {var request = new ActiveXObject("Microsoft.XMLHTTP");} catch (failed) {var request = false;}}}return request;}

// */



/*

funcion ponValorRadio

@param       nombre, atributo name u objeto

@brief       establece un valor para un conjunto de campos de tipo radio

@author      Ernesto Spiro Peimbert Andreakis

@date        03 julio 2008

*/

function ponValorRadio(nombre,valor) {

	if ( typeof(nombre) == 'string' ) {

		var arreglo_valores = document.getElementsByName(nombre);

	}

	else {

		var arreglo_valores = nombre;

	}

	var cantidad = arreglo_valores.length;

	var i;

	for ( i = 0; i < cantidad; i++ ) {

		if ( arreglo_valores[i].value == valor ) {

			arreglo_valores[i].checked = true;

		}

		else {

			arreglo_valores[i].checked = false;

		}

	}

}

/** //MINIMIZADO

function ponValorRadio(nombre,valor) {if ( typeof(nombre) == 'string' ) {var arreglo_valores = document.getElementsByName(nombre);}else {var arreglo_valores = nombre;}var cantidad = arreglo_valores.length;var i;for ( i = 0; i < cantidad; i++ ) {if ( arreglo_valores[i].value == valor ) {arreglo_valores[i].checked = true;}else {arreglo_valores[i].checked = false;}}}

// */



/*

funcion valorRadio

@param       nombre, atributo name u objeto

@return      valor

@brief       obtiene el valor para un conjunto de campos de tipo radio

@author      Ernesto Spiro Peimbert Andreakis

@date        03 julio 2008

*/

function valorRadio(nombre) {

	valor = '';

	if ( typeof(nombre) == 'string' ) {

		var arreglo_valores = document.getElementsByName(nombre);

	}

	else {

		var arreglo_valores = nombre;

	}

	var cantidad = arreglo_valores.length;

	var i;

	for ( i = 0; i < cantidad; i++ ) {

		if ( arreglo_valores[i].checked ) {

			valor = arreglo_valores[i].value;

			break;

		}

	}

	return valor;

}

/** //MINIMIZADO

function valorRadio(nombre) {valor = '';if ( typeof(nombre) == 'string' ) {var arreglo_valores = document.getElementsByName(nombre);}else {var arreglo_valores = nombre;}var cantidad = arreglo_valores.length;var i;for ( i = 0; i < cantidad; i++ ) {if ( arreglo_valores[i].checked ) {valor = arreglo_valores[i].value;break;}}return valor;}

// */