///  LLAMADA A FUNCIONES EN EVENTOS  ///
function attachEventListener(target, eventType, functionRef, capture) {
    if (typeof target.addEventListener != "undefined") {
        target.addEventListener(eventType, functionRef, capture);
    } else if (typeof target.attachEvent != "undefined") {
        target.attachEvent("on" + eventType, functionRef);
    } else {
        return false;
    }
    return true;
}
/// LIMPIA LOS ESPACIOS EN BLANCO DE LOS CAMPOS DE TEXTO ///
function reset_form() {
	if (!document.getElementsByTagName) {
		return;
	}

	var los_inputs = document.getElementsByTagName("input");
	var los_textareas = document.getElementsByTagName("textarea");
	var i;
	for (i = 0; i < los_inputs.length; i++) {
		if (los_inputs[i].getAttribute("type").toLowerCase() == "text" || los_inputs[i].getAttribute("type").toLowerCase() == "password") {
			los_inputs[i].onfocus = function() {
				if ((this.value.charCodeAt(0) == 32 || this.value.charCodeAt(0) == 160) && this.value.length == 1) this.value = "";
			};
		}
	}	
	for (i = 0; i < los_textareas.length; i++) {
		los_textareas[i].onfocus = function() {
			if (this.value.charCodeAt(0) == 32 || this.value.charCodeAt(0) == 160 && this.value.length == 1) this.value = "";
		};
	}
}
/// ALINEACION CENTRADA EN VERTICAL DE UN ELEMENTO RESPECTO A OTRO  ///
function getElementHeight(ElementId) {
	var Element = document.getElementById(ElementId);
	var ElementHeight = 0;
	if (typeof(Element.clientHeight) == 'number') {
		ElementHeight = Element.clientHeight;
	}
	return ElementHeight;
}
function AlinearVertical(containerId, contentId) {
    if  (document.getElementById(containerId) && document.getElementById(contentId)) {
        var containerHeight = getElementHeight(containerId);
        var contentHeight = getElementHeight(contentId);
        if(contentHeight < containerHeight) {
            var newTop= (containerHeight - contentHeight)/2;
            document.getElementById(contentId).style.marginTop=newTop+"px";
        };
    };
}
function target_blank() {
	if (!document.getElementsByTagName) return;
	var i;
	var enlaces = document.getElementsByTagName("a");
	var cuantos = enlaces.length;
	for (i = 0; i < cuantos; i++) {
		if (enlaces[i].getAttribute("href") && enlaces[i].getAttribute("rel") == "external"){
		     enlaces[i].target = "_blank";
		     
		     //if (enlaces[i].getAttribute("href").indexOf('DocumentosVisor')>=0){
		     if (enlaces[i].getAttribute("href").indexOf('Documentos/') >= 0) {
		         //Añadir un nuevo parámetro al href que señaliza que hay JavaScript activado		     
		         if (enlaces[i].getAttribute("href").indexOf('?')>=0){
		            enlaces[i].href  += "&js=1";
		         }
		         else{
		            enlaces[i].href  += "?js=1";
		         }   
		     }		     
		}
	}
}
function fototecaAlinearVertical() {
    AlinearVertical('imgScroll','imgPreview');
}

function isset(variable_name) {
    try {
         if (typeof(eval(variable_name)) != 'undefined')
         if (eval(variable_name) != null)
         return true;
     } catch(e) { }
    return false;
   }

function analytics() {
	$("a[rel*='external']").click(function(){
		var url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "");
		pageTracker._trackEvent('/web/', '/enlace/', url);
	});
	$("a[href*='mailto']").click(function(){
		var url = $(this).attr("href").replace(/^mailto\:*/i, "");										  
		pageTracker._trackEvent('/web/', '/mailto/', url);
	});
	$("a[href*='DocumentosVisor.aspx']").click(function(){
		var url = $(this).attr("href").split("DocumentosVisor.aspx?")[1].split("&")[0];		
		pageTracker._trackEvent('/web/', '/downloads/', url);
	});	
}


/// ROLLOVER DE LOS RESUMENES ///

/// LLAMADA A TODAS LAS FUNCIONES  ///
attachEventListener(window, "load", reset_form, false);
attachEventListener(window, "load", fototecaAlinearVertical, false);
attachEventListener(window, "load", target_blank, false);
attachEventListener(window, "load", analytics, false);