function base() {
	return window.location.protocol+"//"+window.location.host;
}

function cargaSelect(direccion, parametros, seleccion) {
	var temp =$("option:first", seleccion).text();
	$("option:first", seleccion).text("recogiendo datos, por favor espere...");
	$(seleccion).attr("disabled", "true"); 
	$("body").attr("style","cursor:wait");
	$.ajax({
	  type: "GET",
	  url: direccion,
	  data: parametros,
	  dataType: "xml",
	  success: function(dev) {
		$(seleccion).text("");
		var tam = $("campo",dev).length;		
		$(seleccion).addOption("", temp, true);
		$("campo",dev).each(function() {
			seleccionado = false;	
			if (($(this).attr("seleccionado"))=="1") {
				seleccionado = true;
			}
			$(seleccion).addOption($(this).attr("id"),$(this).text(),seleccionado);
		});	  
		if (tam==0) {
			$(seleccion).removeClass("vrequerido");
			$(seleccion).parent().hide();
		} else {
			$(seleccion).parent().show();
			$(seleccion).addClass("vrequerido");
		}		
		if ($(seleccion).hasClass("noRequerido")) {
			$(seleccion).removeClass("vrequerido");
		}
		$("body").removeAttr("style");
		$(seleccion).removeAttr("disabled");
	  }
	});
}

/*
 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
*/
$.fn.addOption = function() {
	var a = arguments;
	if(a.length == 0) return this;
	// select option when added? default is true
	var sO = true;
	// multiple items
	var m = false;
	if(typeof a[0] == "object")	{
		m = true;
		var items = a[0];
	}
	if(a.length >= 2) {
		if(typeof a[1] == "boolean") sO = a[1];
		else if(typeof a[2] == "boolean") sO = a[2];
		if(!m) {
			var v = a[0];
			var t = a[1];
		}
	}
	this.each(function() {
			if(this.nodeName.toLowerCase() != "select") return;
			if(m) {				
				for(var i in items) {
					$(this).addOption(i, items[i], sO);
				}
			} else {
				var option = document.createElement("option");
				option.value = v;
				option.text = t;
				var i;
				var r = false;
				// get options
				var o = this.options;
				// get number of options
				var oL = o.length;
				// loop through existing options
				for(i = 0; i < oL; i++)	{
					// replace existing option
					if(o[i].value == option.value) {
						r = true;
						break;
					}
				}
				if(i < oL && !r) i = oL;
				this.options[i] = option;
				if(sO) {
					o[i].selected = true;
				}
			}
		}
	)
	return this;
}

function abreVentana(direccion) {
	if (!(window.open(direccion))) {
		alert("No se ha podido abrir la nueva ventana. Por favor, habilite la apertura de ventanas emergentes en su navegador e int�ntelo nuevamente.");
	}
	return false;
}

$(document).ready(function() {
	$("a:not([class*='noauto'])").click(function() {
		if (this.href.indexOf(location.hostname) == -1 || $(this).attr("rel")=="external") {
			abreVentana(this.href);
			return false;
		}
    });
});
