
// Busca un valor dentro de un array, sino devuelve el valor por defecto
function getValorInArray(arr, obj, def) {
	str = arr[obj];
	if (str == null) {
		str = def;
	}
	return str;
}

// Abre una ventana
function openWindow(url,name,w,h) {
	var altura = screen.height;
	var anchura = screen.width;
	var vent_x = w;
	var vent_y = h;	
	var posx2 = Math.round(anchura/2-(vent_x/2));
	var posy2 = Math.round(altura/2-(vent_y/2)) - 60;

	newWindow = window.open(url,name,'resizable=0,scrollbars=0,status=0,location=0,toolbar=0,menubar=0,width='+vent_x+',height='+vent_y+',screenX='+posx2+',screenY='+posy2+',left='+posx2+',top='+posy2+'');
	newWindow.focus();
}

function openWindowModal(url,name,w,h){
	if (document.all&&window.print) { //if ie5
		eval('window.showModelessDialog(url,"'+name+'","help:0;resizable:1;dialogWidth:'+w+'px;dialogHeight:'+h+'px")')
	} else {
		eval('window.open(url,"'+name+'","width='+w+'px,height='+h+'px,resizable=1,scrollbars=1")')
	}
}

function makePopup(url, width, height, overflow) {
	// || !/^(scroll|resize|both)$/.test(overflow)
	if (overflow == '') {
		overflow = 'both';
	}
	//alert(overflow);
	//alert((/^(scroll|both)$/.test(overflow) ? 'yes' : 'no'));
	//alert((/^(resize|both)$/.test(overflow) ? 'yes' : 'no'));
	var altura = screen.height;
	var anchura = screen.width;
	var vent_x = width;
	var vent_y = height;	
	var posx2 = Math.round(anchura/2-(vent_x/2));
	var posy2 = Math.round(altura/2-(vent_y/2)) - 60;
	var win = window.open(url, '',
		'width=' + width + ',height=' + height
		+ ',scrollbars=' + (/^(scroll|both)$/.test(overflow) ? 'yes' : 'no')
		+ ',resizable=' + (/^(resize|both)$/.test(overflow) ?	'yes' : 'no')
		+ ',status=yes,toolbar=no,menubar=no,location=no,width='+vent_x+',height='+vent_y+',screenX='+posx2+',screenY='+posy2+',left='+posx2+',top='+posy2+''
	);
	return win;
}

// Obtiene un elemento a partir del id
function el(id) {
  if (document.getElementById) {
    return document.getElementById(id);
  } else if (window[id]) {
    return window[id];
  }
  return null;
}

var idDisplay = '';
function switchDisplay(id) {
	//if (idDisplay != '') changeDisplay(idDisplay, 'none');
	if (el(id).style.display == 'none') {
		el(id).style.display = 'block';
	} else {
		el(id).style.display = 'none';
	}
	//idDisplay = id;
}

function changeDisplay(id, q) {
	el(id).style.display = q;
};

function mouseOver(msg) {
	self.status=msg;
}

function mouseOut() {
	// Se podria poner un mensaje por defecto
	self.status="";
}

function findPos(obj) {
	var vueltas = 0;
	var curleft = curtop = 0;
	if ((obj != null) && (obj.offsetParent)) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while ((obj = obj.offsetParent) && (vueltas < 200)) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			vueltas++;
		}
	}
	return [curleft,curtop];
}

function anularBotonDerecho(e) {
	if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2)){
		alert('Botón derecho inhabilitado')
		return false;
	}
	else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2)){
		alert('Botón derecho inhabilitado')
	}
}
//document.onmousedown=anularBotonDerecho

// Obtiene el width de un elemento
function widthElement(ta) {
  var w = ta.offsetWidth;
  return w;
}

// Obtiene el width del documento
function widthDocument() {
  var iw;
  if (window.innerWidth == null) {
    iw = document.body.clientWidth;
  }else {
    iw = window.innerWidth;
  }
  return iw;
}

// Obtiene el height del documento
function heightDocument() {
  var ih;
  if (window.innerHeight == null) {
    ih = document.body.clientHeight;
  }else {
    ih = window.innerHeight;
  }
  return ih;
}

// Añade una funcion a un evento
function addLoadEvent(func, event) {
  
   if (typeof func != 'function') {
   	eval("func = function() { "+func+"();}");
   }
   
   var type = eval('typeof window.'+event);
   if (type != 'function') {
      eval('window.'+event+' = '+func+';');
   } else {
	   var oldevent = eval('window.'+event);
      eval('window.'+event+' = function() { oldevent(); func(); }');
   }
   
}

// Ejecuta una url mediante ajax
function executeURL(url) {
	url += '&';
	new Ajax.Request(url, {
		onComplete: function(request) {
			onExecuteURL(request);
		},
		asynchronous: true
		}
	)
};

function onExecuteURL(c) {
}

// Carga una url mediante ajax
function loadData(url, onload) {
	url += '&';
	new Ajax.Request(url, {
		onComplete: function(request) {
			eval(onload+'(request);');
		},
		asynchronous: true
		}
	)
};

