/********************************************************************************
* Nombre:		  lib_java                                                          *
* Version:		1.00                                                              *
* Fecha:		  2008-05-01                                                        *
* Autor:  	  Iván Miranda                                                      *
* Licencia:   OpenSource                                                        *
*********************************************************************************
* Compilado de rutinas generales en desarrollos que relizan javascript          *
*********************************************************************************
*                                                                               *
*********************************************************************************
* Licencia:		OpenSource                                                        *
* Puedes modificar libremente este código, respetando el nombre de sus autores	*
* en las aplicaciones involucradas.                                             *
* Por favor, informa de las actualizaciones a este código en:                   *
* 		pa.ivan.miranda@gmail.com                                                 *
********************************************************************************/

/* *************************** */
/* ******* PROTOTIPOS ******** */
/* *************************** */

//Agrega la función trim al objeto String
String.prototype.trim= function() {
   return this.replace(/(^\s*)|(\s*$)/g,""); //elimina espacios a izquierda y derecha
};

//Cuando un numero no puede ser convertido, la variable adquiere el valor 'NaN', con esto, eso se corrige
//  asignando el valor 0,evitando ese tipo de validaciones en el codigo
Number.prototype.NaN=function() {
  return isNaN(this)?0:this;
};

//Regresa los elementos que contienen cierta clase asignada
function getElementsByClassName(clase, tag) 
{
  var objObjetos = [];
  var strClase = new RegExp('\\b'+clase+'\\b');
  var objElementos = document.getElementsByTagName(tag);
  for (var i = 0; i < objElementos.length; i++) {
    var classes = objElementos[i].className;
    if (strClase.test(classes)) objObjetos.push(objElementos[i]);
  }
  return objObjetos;
};

//Regresa la posicion actual del mouse (siempre en un evento del mouse)
function javPosMouse(ev){
	if(ev.pageX || ev.pageY){
		return {
  		x:ev.pageX,
  		y:ev.pageY
  	}
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	}
};

//Regresa el ancho disponible del documento
document.ancho = function(){
  if(typeof(window.innerWidth) == 'number')
    return parseInt(window.innerWidth);
  else 
    if(document.body && document.body.clientHeight)
    return parseInt(document.body.clientWidth);
};

//Regresa el alto disponible del documento
document.alto = function(){
  if(typeof(window.innerHeight) == 'number')
    return parseInt(window.innerHeight);
  else 
    if(document.body && document.body.clientHeight)
    return parseInt(document.body.clientHeight);
};

//Cancela la propagación de eventos anteriores, haciendo que se ejecute sólo el evento actual
function javCancelaPropagacion(event) {
  event.cancelBubble = true;
  if(javIdentificaNavegador() != "MIE")
    event.stopPropagation();
}

/* *************************** */
/* ********* NAVEGADOR ******* */
/* *************************** */

//Identifica el explorador usado
function javIdentificaNavegador() {
	if(navigator.appName=="Microsoft Internet Explorer")
		return "MIE";
	if(navigator.appName=="Opera")
		return "OPERA";
	return "MOZILLA";
};

//Detecta si flash se encuentra instalado
function javDetectaFlash() {
	if(navigator.plugins["Shockwave Flash"])
		return 1;
	else 
		return 0;
};

/* **************************** */
/* ********* VENTANAS ********* */
/* **************************** */

//Lanza una venta pop
function javPopUp(Pagina,Titulo,Ancho,Alto,Herramientas,Menu,Scroll,Maximizar,Modal) {
	try {
  	window.open(Pagina, Titulo, 'width=' + Ancho + ',height=' + Alto + ',location=no,toolbar=' + Herramientas + ',menubar=' + Menu + ',scrollbars=' + Scroll + ',resizable=' + Maximizar);
	}
	catch(e){
	  window.open(Pagina, 'titulo');
  }
};

//Pide confirmación para abandonar una página
function javConfirmarCierre(){
//se puede validar la condicion para pedir confirmación
	if(1 != 1) {
	//solo si se quiere confirmación se debe regresar un valor
		var msj = "Los cambios no guardados se perderán.";
		return msj;
	}
};

/* *************************** */
/* ********* OBJETOS ********* */
/* *************************** */

//Obtiene un objeto del documento por su ID
function javObtenObjeto(objeto){
var objObjeto=(document.getElementById) ? document.getElementById(objeto) : eval("document.all['" + objeto + "']");
	return objObjeto;
};

//Obtiene el elemento seleccionado de un combo
function javValorCombo(combo) {
  var objCombo = javObtenObjeto(combo);
  return objCombo.options[objCombo.selectedIndex].value;
};

//Cambia es estatus de visibilidad de un objeto
function javDivisionVisible(division,valor){
	var objFrame=(document.getElementById) ? document.getElementById(division) : eval("document.all['" + division + "']");
	if(valor.trim()=="si" || valor.trim()=="SI" || valor.trim()=="Si")
	  objFrame.style.display="";
	else
		objFrame.style.display="none";
};

//Obtiene el HTML contenido en un objeto
function javInnerHTML(elemento){
	var objObjeto=javObtenObjeto(elemento);
	return objObjeto.innerHTML;
};

//Asigna el HTML contenido en un objeto
function javCambiaInnerHTML(elemento,valor){
  var objObjeto=javObtenObjeto(elemento);
  objObjeto.innerHTML = valor;
  return;
};

/* ***** TEXTOS ***** */
//Obtiene el valor de un texto de formulario
function javValorText(elemento) {
	var objObjeto=javObtenObjeto(elemento);
	texto=objObjeto.value;
	return texto.trim();
};

//Asignar valor a un texto de formulario
function javAsignaValorText(elemento,valor) {
	var objObjeto=javObtenObjeto(elemento);
	texto=objObjeto.value=valor;
	return;
};

//Posiciona el cursor en un texto y marca su contenido si se desea
function javAsignarFoco(elemento,marcar) {
	var objObjeto=javObtenObjeto(elemento);
	objObjeto.focus();
	if(marcar)
		objObjeto.select();
	return;
};

//Selecciona el contenido de una caja de texto
function javSeleccionarTexto(elemento) {
	var objObjeto=javObtenObjeto(elemento);
	objObjeto.select();
	return;
};

//Devuelve el ancho y alto de un objeto
function javTamanoObjeto(objeto) {
  if(typeof(objeto) == "object") {
    return {
      ancho:parseInt(objeto.style.width.replace(/px/gi, "")),
    	alto:parseInt(objeto.style.height.replace(/px/gi, ""))
    }
  }
  else {
    var objObjeto = javObtenObjeto(objeto);
    return {
      ancho:parseInt(objeto.style.width.replace(/px/gi, "")),
    	alto:parseInt(objeto.style.height.replace(/px/gi, ""))
    }
  }
};

//Devuelve la posicion x-y de un objeto
function javPosicionObjeto(objeto) {
  if(typeof(objeto) == "object") {
    return {
      left:parseInt(objeto.style.left.replace(/px/gi, "")),
    	top:parseInt(objeto.style.top.replace(/px/gi, ""))
    }
  }
  else {
    var objObjeto = javObtenObjeto(objeto);
    return {
      left:parseInt(objeto.style.left.replace(/px/gi, "")),
    	top:parseInt(objeto.style.top.replace(/px/gi, ""))
    }
  }
};


/* *************************** */
/* ******* DIVISIONES ******** */
/* *************************** */

//Obtiene las coordenadas de una division
function javPosicionDiv(elemento) {
  var objObjeto=javObtenObjeto(elemento);
  var x=(document.defaultView && document.defaultView.getComputedStyle) ?
        document.defaultView.getComputedStyle(bloque,'').getPropertyValue("top") :
        objObjeto.currentStyle ? objObjeto.currentStyle.top : "";
  var y=(document.defaultView && document.defaultView.getComputedStyle) ?
        document.defaultView.getComputedStyle(bloque,'').getPropertyValue("left") :
        objObjeto.currentStyle ? objObjeto.currentStyle.left : "";
  x= parseInt(x);
  y= parseInt(y);
  return x + ',' + y;
};

function javAnchoDiv(elemento) {
  var objObjeto=javObtenObjeto(elemento);
  return objObjeto.style.width;
};

/* *************************** */
/* ********* REDONDEO ******** */
/* *************************** */

//Aplicar el redondeo de los elementos
function javRedondearDivs(){
  var settings = {
    tl: { radius: 1  },
    tr: { radius: 1  },
    bl: { radius: 10 },
    br: { radius: 10 },
    antiAlias: true,
    autoPad: true,
    validTags: ["div","fieldset"]
  }
  var myBoxObject = new curvyCorners(settings, "div_principal_titulos");
  myBoxObject.applyCornersToAll();
  var settings = {
    tl: { radius: 1  },
    tr: { radius: 10 },
    bl: { radius: 1  },
    br: { radius: 10 },
    antiAlias: true,
    autoPad: true,
    validTags: ["div","fieldset"]
  }
  var myBoxObject = new curvyCorners(settings, "div_principal_contenidos");
  myBoxObject.applyCornersToAll();
  var settings = {
    tl: { radius: 10 },
    tr: { radius: 1  },
    bl: { radius: 10 },
    br: { radius: 1  },
    antiAlias: true,
    autoPad: true,
    validTags: ["div","fieldset"]
  }
  var myBoxObject = new curvyCorners(settings, "div_principal_menu");
  myBoxObject.applyCornersToAll();
};

function javRedondearFrames(){
   settings = {
    tl: { radius: 5 },
    tr: { radius: 1 },
    bl: { radius: 1 },
    br: { radius: 5 },
    antiAlias: true,
    autoPad: true,
    validTags: ["fieldset","div"]
  }
  var myBoxObject = new curvyCorners(settings, "principal_frame");
  myBoxObject.applyCornersToAll();
  var myBoxObject = new curvyCorners(settings, "pop_mensaje");
  myBoxObject.applyCornersToAll();
  var myBoxObject = new curvyCorners(settings, "pop_mediano");
  myBoxObject.applyCornersToAll();
  var myBoxObject = new curvyCorners(settings, "pop_grande");
  myBoxObject.applyCornersToAll();
};
