// PHPFrameWork
// Libreria de auxiliares
// ====================================================

// Cambia la visibilidad de un elemento via estilo CSS
function toggleVisibility(e){

	if (Element.visible(e)){
		Element.hide(e);
	}
	else {
		Element.show(e);
	}
}

// hace swap de visibilidad entre 2 elementos
// @param desaparece elemento a desaparecer
// @param aparece elemento a apareer
function swapVisible(desaparece, aparece) {

	Element.hide(desaparece);
	Element.show(aparece);
}



var estadoVentanas = new Array();
// Muestra o esconde una ventanaDiv indicando el tipo de efecto a utilizar
// en la posicion del mouse, desplazado x horizontalmente e y verticalmente
// @param id ventana
// @param tipo de transicion
// @param x desplazamiento horizontal desde la posicion X del mouse
// @param y desplazamiento vertical desde la posicion Y del mouse
function toggleVentanaDivOnMouse(id, tipo, x, y) {
	
	var o = $(id);
	

	if(estadoVentanas[id] == true) {

		Effect.toggle(id, tipo, {duration: 0.3});
		estadoVentanas[id] = null;
		return true;
	}

	o.style.left = mouseX - parseInt(o.style.width);
	o.style.top = mouseY + 15;
	Effect.toggle(id, tipo);
	estadoVentanas[id] = true;

	//alert("x = " + mouseX + " y = " + mouseY + " width = " + o.style.width);
}



// SCRIPT DE CAPTURA DE POSICION DE MOUSE
// ======================================
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

var mouseX;
var mouseY;

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    mouseX = event.clientX + document.body.scrollLeft
    mouseY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX
    mouseY = e.pageY
  }  
  // catch possible negative values in NS4
  if (mouseX < 0){tempX = 0}
  if (mouseY < 0){tempY = 0}  

  return true

}
document.onmousemove = getMouseXY;




// Rellena el contenido de un combobox en base a un arreglo, dependiendo de la posicion
// seleccionada en otro combobox
// =====================================================================================
function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {

	var i, j;
	var prompt;

	// empty existing items
	for (i = selectCtrl.options.length; i >= 0; i--) {
		selectCtrl.options[i] = null; 
	}

	prompt = (itemArray != null) ? goodPrompt : badPrompt;
	
	if (prompt == null) {
		j = 0;
	}
	else {
		selectCtrl.options[0] = new Option(prompt);
		j = 1;
	}

	if (itemArray != null) {
		// add new items
		for (i = 0; i < itemArray.length; i++) {
			selectCtrl.options[j] = new Option(itemArray[i][0]);

			if (itemArray[i][1] != null) {
				selectCtrl.options[j].value = itemArray[i][1]; 
			}
			j++;
		}
		// select first item (prompt) for sub list
		selectCtrl.options[0].selected = true;
	}
}

function xmlValue(xml, tagName, index) {

	if(xml.getElementsByTagName(tagName).length == 0)
		return null;

	return xml.getElementsByTagName(tagName)[index].childNodes[0].nodeValue;	
}

function getSelectIndexOf(select, value) {


	try {
		
		for(var i = 0; i < select.options.length; i++) {
			
			if(select.options[i].value == value)
				return i;
		}
	}
	catch(e) { return -1 }
	
}

// Agrega los puntos separadores de miles, ASUME ENTRADA ENTERA
// EJ: 232234 -> 232.234
function clNumber(numero) {
	
	if(numero == null || typeof(numero) == "undefined")
		return null;	
		
	if((numero+"").indexOf(".") >= 0)
		return numero;
	
	var aux = numero + "";
	var retorno = "";	
		
	while(aux.length > 3) {

		var trozo = aux.substr(aux.length-3, 3);
		aux = aux.substring(0, aux.length-3);
		
		retorno = "." + trozo + retorno;
	}
	
	retorno = aux + retorno;
	
	return retorno;
	
}

function unClNumber(numero) {

	if(numero == null || typeof(numero) == "undefined")
		return;
		
	var aux = "" + numero;
	
	for(var i = 0; i < aux.length; i++) {

		if(aux.charAt(i) == '.') {
			aux = aux.substr(0, i) + aux.substring(i+1, aux.length);
		}
	}
	
	return parseInt(aux);
}

function tacharFila(element) {
	
	if(typeof(element) == "undefined" || element == null)
		return;

	var tr = getParentTR(element)

	var hijos = tr.childNodes;
	
	for(var i = 0; i < hijos.length; i++) {
	
		if(hijos[i].tagName == "TD")
			Element.addClassName(hijos[i], 'tachado');
	}
}

function getParentTR(element) {

	for(var aux = element; aux != null; aux = aux.parentNode) {
		
		if(aux.tagName == "TR")
			return aux;
	}	
}

function nl2br(s) {

		return s.replace(/\n/g, "<br>");
}