// ---------------------------------------
// cross-browser add-remove event to object
function addEvent(obj, e, func)
{
	//W3C, Mozilla
	if(obj.addEventListener)
	{
		obj.addEventListener(e, func, false);
	}
	//IE
	else if(obj.attachEvent)
	{
		obj.attachEvent("on"+e, func);
	}
	//others
	else
	{
		obj["on"+e] = func;
	}
}

function removeEvent(obj, e, func)
{
	if(obj.removeEventListener)
	{
		obj.removeEventListener(e, func, false);
	}
	else if(obj.detachEvent)
	{
		obj.detachEvent("on"+e, func);
	}
	else
	{
		obj["on"+e] = null;
	}
}

// ---------------------------------------
// Permuta una imagen cuyo nombre acabe en "_on." o "_off." (versión 2.0)
function commuteImg()
{
	var sSrc = this.src;
	var rSearch = new RegExp("_(on|off)\\.");
	var sEstado = sSrc.match(rSearch);
	var sRepl = (RegExp.$1 == "off") ? "on" : "off";
	var sSrc = sSrc.replace("_"+RegExp.$1+".", "_"+sRepl+".");
	this.src = sSrc;
}
function prepareChangeImages()
{
	var allImg = document.getElementsByTagName("IMG");
	for(var i=0; i<allImg.length; i++)
	{
		var oAttr = String(allImg[i].getAttribute("src"));
		if(oAttr.toLowerCase().match("_off."))
		{
			var o = allImg[i];
				o.onmouseover	= commuteImg;
				o.onmouseout	= commuteImg;
		}
	}
}
addEvent(window, "load", prepareChangeImages);

// ---------------------------------------
// NODE OPS
// get next no-whitespace node for Gecko-based browsers (IE, Mozilla)
function getNextSibling(o)
{
	var el = o.nextSibling;
	while(el && el.nodeName == "#text") el = el.nextSibling;
	return el;
}

// get the first child no-whitespace node
function getFirstChild(o)
{
	var el = o.firstChild;
	while(el && el.nodeName == "#text") el = el.nextSibling;
	return el;
}

// return an array with the object's child nodes without whitespace nodes
function getChildNodes(o)
{
	var arr = new Array();
	for(var i=0; (oNode = o.childNodes[i]); i++)
	{
		if(oNode && oNode.nodeName != "#text") arr.push(oNode);
	}
	return arr;
}

// return the value of the node
function getNodeValue(o)
{
	return o.firstChild.nodeValue;
}

// return an array with all the child nodes matching the class name
function getChildByClassName(o, classname)
{
	var arr = new Array();
	for(var i=0; (oNode = getChildNodes(o)[i]); i++)
	{
		if(oNode.className == classname) arr.push(oNode);
	}
	return arr;
}

// ---------------------------------------
// String to XML
String.prototype.parseXML = function()
{
	var xml = this;
	// Firefox
	if (window.DOMParser)
	{
		var xmldom = new DOMParser();
		var dom = xmldom.parseFromString(this, "application/xml");
		if (!dom) return xml;
		xml = dom;
	}
	// IE
	else if(window.ActiveXObject)
	{
		var xmldom = new ActiveXObject('Microsoft.XMLDOM');
			xmldom.async = false;
			xmldom.loadXML( this );
		xml = xmldom;
	}
	return xml;
	}

// ---------------------------------------
// Open Window
var mac = (navigator.appVersion.indexOf("Mac") >0 )? true : false;

function getPos(w,h)
{
    var nW = (mac && document.all)? w-16 : w;
    var nH = h;
    var list = {
        x : Math.round((screen.availWidth/2) - (nW/2)),
        y : Math.round((screen.availHeight/2) - (nH/2)),
        w : nW,
        h : nH      
    }
    return list;
}

function existWin(oWindow)
{
    clearInterval(tmr);
    if(!w_pop) alert("Por favor, activa la opción de abrir ventanas emergentes de tu navegador para imprimir la información. Gracias.");
}

function openWin(pag,w,h,s)
{
    if(w == null) w = 650;
    if(h == null) h = 500;
    if(s == null) s = "yes";
    var pos = getPos(w,h);
    w_pop = window.open(pag, "new_window","width=" + pos.w + ", height=" + pos.h + ", top=" + pos.y + ", left=" + pos.x + ", scrollbars=" + s);
    tmr = setInterval(existWin, 1000, w_pop);

}
