﻿
	// creates an XMLHttpRequest instance
	function createXMLHttpRequestObject() {
		var xmlHttp; // xmlHttp will store the reference to the XMLHttpRequest object
		try { // try to instantiate the native XMLHttpRequest object
		  xmlHttp = new XMLHttpRequest(); // create an XMLHttpRequest object
		} catch(e) {
		  try { // assume IE6 or older
		    xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		  } catch(e) { }
		}
		if (!xmlHttp) { // return the created object or display an error message
		  alert("Error creating the XMLHttpRequest object.");
		} else {
		  return xmlHttp;
		}
	}
	
	// Common generic AJAX function
	function ajaxc(url, vars, pos) {
		var request =  new createXMLHttpRequestObject();
		request.open("POST", url, true);
		request.setRequestHeader("Content-Type",
				                     "application/x-www-form-urlencoded");
		request.onreadystatechange = function() {
			var done = 4, ok = 200;
			if (request.readyState == done && request.status == ok) {
				if (request.responseText && pos != "") {
				  document.getElementById(pos).innerHTML = request.responseText;
				}
			}
		};
		request.send(vars);
	}
	
	// Encodes values for URLs
	function urlencode(string) {
		string = encodeURIComponent(string);
		return string.replace(/~/g,'%7E').replace(/%20/g,'+');
	}

	// Open centered popup window
	function OpenCentered(psUrl, psName, piWidth, piHeight, psFlags) {
		var iX=(screen.width-piWidth-20)/2;
		var iY=(screen.height-piHeight-30)/2;
		open(psUrl, psName, 'width=' + piWidth + ',height=' + piHeight + ',scrollbars=1,left=' + iX + ',top=' + iY +  ',screenX=' + iX + ',screenY=' + iY + psFlags);
	}

	function setStyleByClass(t,c,p,v) {
		var elements;
		if(t == '*') {
			// '*' not supported by IE/Win 5.5 and below
			elements = (ie) ? document.all : document.getElementsByTagName('*');
		} else {
			elements = document.getElementsByTagName(t);
		}
		for(var i = 0; i < elements.length; i++){
			var node = elements.item(i);
			for(var j = 0; j < node.attributes.length; j++) {
				if(node.attributes.item(j).nodeName == 'class') {
					if(node.attributes.item(j).nodeValue == c) {
						eval('node.style.' + p + " = '" +v + "'");
					}
				}
			}
		}
	}
	
	
	function numbersonly(e) {
		// onkeypress="return numbersonly(event)" onkeyup="cleardots(this)"
		// https://developer.mozilla.org/en/DOM/Event/UIEvent/KeyEvent
		var unicode = e.charCode? e.charCode : e.keyCode;
		var allow = ".8.37.39.46.48.49.50.51.52.53.54.55.56.57."; // allowed characters
		if(allow.search("." + unicode + ".") >= 0) {
			return true;
		} else {
			return false;
		}
	}
	
	function cleardots(el) {
		var string = el.value;
		string = string.replace(".", "");
		el.value = string;
	}

	function setOpacity(eID, opacityLevel) {
		var eStyle = document.getElementById(eID).style;
		eStyle.opacity = opacityLevel / 100;
		eStyle.filter = 'alpha(opacity='+opacityLevel+')';
	}

	function fade(eID, startOpacity, stopOpacity, duration) {
		var speed = Math.round(duration / 100);
		var timer = 0;
		if (startOpacity < stopOpacity) { // fade in
			for (var i=startOpacity; i<=stopOpacity; i++) {
				setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
				timer++;
			} return;
		}
		for (var i=startOpacity; i>=stopOpacity; i--) { // fade out
			setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
			timer++;
		}
	}
	
		
	function EUROMANAGER_busquedas(box) {
		ajaxc("http://www.euromanager.es/_srv/ajax.busquedas.php", "box=" + box, box);
	}
	
	function EUROMANAGER_popup(id) {
		var state = document.getElementById("popup_" + id).style.visibility;
		if(state == "visible") {
			document.getElementById("popup_" + id).style.visibility = "hidden";
		} else {
			document.getElementById("popup_" + id).style.visibility = "visible";
			document.getElementById("popup_last").value = id;
		}
	}

