function $(el){
	return document.getElementById(el);
}
function compruebaFocus(id){
	// si existe el elemento de id le da focus
	var obj = $(id);
	if(obj)	obj.focus();
}
function meteTexto(id, texto){
	var obj = $(id);
	wrapText(obj, texto, '');
	obj.focus();
}
function meteTag(id, tag, prompt1, prompt1def, prompt2, prompt2def){
	var obj = $(id);
	var texto_user;
	var tag_ini = '[' + tag +']';
	var tag_fin = '[/' + tag +']';
	if(prompt2.length > 0){
		// el prompt2 sera preguntado siempre, formara parte de la tag... cosas tipo [url=http://blah][/url]
		texto_user = prompt(prompt2, prompt2def);
		var tag_ini = '[' + tag + '=' + texto_user + ']';
	}
	var seleccion = dameSeleccion(obj);
	if(seleccion.length > 0) wrapText(obj, tag_ini, tag_fin);
	else{
		texto_user = prompt(prompt1, prompt1def);
		wrapText(obj, tag_ini + texto_user, tag_fin);
	}
	obj.focus();
}
function dameSeleccion(obj){
	if(typeof obj.selectionStart == 'number')	{
		// Mozilla, Opera, and other browsers
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;
		if(start == end) return "";
		else return obj.value.substring(start, end);
	}
	else{
		if(document.selection){
			// Internet Explorer
			obj.focus(); // make sure it's the textarea's selection
			var range = document.selection.createRange();
			if(range.parentElement() != obj) return false;
	   		if(typeof range.text == 'string') return range.text
		}
		else return "";
	}
}
function wrapText(obj, beginTag, endTag){
	if(typeof obj.selectionStart == 'number')
	{
		// Mozilla, Opera, and other browsers
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;
		
		obj.value = obj.value.substring(0, start) + beginTag + obj.value.substring(start, end) + endTag + obj.value.substring(end, obj.value.length);
	}
	else if(document.selection)
	{
		// Internet Explorer

		// make sure it's the textarea's selection
		obj.focus();
		var range = document.selection.createRange();
		if(range.parentElement() != obj) return false;

	    if(typeof range.text == 'string')
	        document.selection.createRange().text = beginTag + range.text + endTag;
	}
	else
		obj.value += text;
		
}
function toggleLayer(whichLayer){
	var elem = $(whichLayer);
	var vis = elem.style;
	// if the style.display value is blank we try to figure it out here
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
	    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0) ? 'block' : 'none';
		vis.display = (vis.display==''||vis.display=='block') ? 'none' : 'block';
}

function confirmSubmit(msg){
	var agree = confirm(msg);
	if(agree) return true;
	else return false;
}

function loadingDiv(capa){ $(capa).innerHTML = '<img src="img/loading.gif" alt="Cargando...">';	}
function limpiaDiv(capa){ $(capa).innerHTML = ""; }

//--- funciones ajax ---

function AJAXInteraction(url, params, doWhile, callback){
	var req = init();
	req.onreadystatechange = processRequest;
	function init() {
		if (window.XMLHttpRequest){
			return new XMLHttpRequest();
		}else if (window.ActiveXObject){
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	function processRequest(){
		if(req.readyState == 1){
			if(doWhile) doWhile();
		}
		// readyState of 4 signifies request is complete
		if (req.readyState == 4) {
		// status of 200 signifies sucessful HTTP call
			if(req.status == 200) {
				if(callback){
					(req.responseXML==null) ? callback(req.responseText) : callback(req.responseXML);
				}
			}
		}
	}
	this.doGet = function(){
		req.open("GET", url, true);
		req.send(null);
	}
	this.doPost = function(){
		req.open("POST", url, true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.send(params);
	}
}

function AJAXRellena(url, capaId, callback){
	doWhile = function(){
		loadingDiv(capaId);
	}
	generalCallback = function(resp){
		var root = resp.getElementsByTagName('resp');
		var items = root[0].getElementsByTagName("elem");
		$(capaId).innerHTML = items[0].firstChild.nodeValue;
		if(callback) callback(resp);
	}
	var ajax = new AJAXInteraction(url, null, doWhile, generalCallback);
	ajax.doGet();
}

function AJAXRellena2(url, capaId, callback){
	doWhile = function(){
		loadingDiv(capaId);
	}
	generalCallback = function(resp){
		$(capaId).innerHTML = resp;
		if(callback) callback(resp);
	}
	var ajax = new AJAXInteraction(url, null, doWhile, generalCallback);
	ajax.doGet();
}
function AJAXRellena3(url, capaId, html){
	doWhile = function(){
		loadingDiv(capaId);
	}
	insertaMensaje = function(){
		$(capaId).innerHTML = html;
	}
	var ajax = new AJAXInteraction(url, null, doWhile, insertaMensaje);
	ajax.doGet();
}
