// funciones de trimming de espacios, tabs y newlines para objetos tipo string
String.prototype.trim = function() {
	return this.replace(/^\s+|^\t+|^\n+|^\r+|\s+$|\t+$|\n+$|\r+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+|^\t+|^\n+|^\r+/g,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$|\t+$|\n+$|\r+$/g,"");
}

String.prototype.singleSpace = function() {
	return this.replace(/(\s+)/g, ' ');
}

// funciones para manejar UTF8
function encode_utf8(s) {
	return unescape( encodeURIComponent(s) );
}

function decode_utf8(s) {
	return decodeURIComponent( escape(s) );
}

String.prototype.toUTF8 = function() {
	return encode_utf8(this);
}

String.prototype.fromUTF8 = function() {
	return decode_utf8(this);
}

// convierte un texto con caracteres con acento en caracteres sin acento
String.prototype.toAscii = function() {
	var depured;
	depured =    this.replace(/[áäà]/g,"a");
	depured = depured.replace(/[ÁÄÀ]/g,"A");
	depured = depured.replace(/[éëè]/g,"e");
	depured = depured.replace(/[ÉËÈ]/g,"E");
	depured = depured.replace(/[íïì]/g,"i");
	depured = depured.replace(/[ÍÏÌ]/g,"I");
	depured = depured.replace(/[óöò]/g,"o");
	depured = depured.replace(/[ÓÖÒ]/g,"O");
	depured = depured.replace(/[úüù]/g,"u");
	depured = depured.replace(/[ÚÜÙ]/g,"U");
	depured = depured.replace(/[ñ]/g,"n");
	depured = depured.replace(/[Ñ]/g,"N");
	return depured;
}

// funciones para ordenar elementos de listas HTML en jQuery
// ejemplo de uso: $('#caseListing li').sort(sortDescending).appendTo('#caseListing');
jQuery.fn.sort = function() { 
    return this.pushStack([].sort.apply(this, arguments), []); 
};
function sortAscending(a, b) { 
    return a.innerHTML.toAscii() > b.innerHTML.toAscii() ? 1 : -1; 
}
function sortDescending(a, b) { 
    return a.innerHTML.toAscii() < b.innerHTML.toAscii() ? 1 : -1; 
}

// funcion para tomar el outerHTML con jQuery
jQuery.fn.outerHTML = function(s) {
	return (s)
		? this.before(s).remove()
		: jQuery("<p>").append(this.eq(0).clone()).html();
}


/* begin get-query managment */
function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
		else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key){
	var page = new PageQuery(window.location.search);
	return unescape(page.getValue(key));
}

function displayItem(key){
	if(queryString(key)=='false') {
		document.write("you didn't enter a ?name=value querystring item.");
	} else {
		document.write(queryString(key));
	}
}
/* end get-query managment */


/* begin cookies managment */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else var expires = "";
	document.cookie = name+"="+escape(value)+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
	return readCookie(name);
}
/* end cookies managment */


// decodifica las urls para poderlas manipular normalmente
function decodeURL (string) {
	return unescape(string).replace(/\+/g, " ");
}

function change_parameter_in_uri(parameter, value) {
	
	var pageGet = new PageQuery(window.location.search);
	var getParameters = pageGet.getKeyValuePairs();
	var numberParameters = pageGet.getLength();
	var uri = window.location.protocol +'//'+ window.location.host + window.location.pathname +'?';
	
	for(var i=0; i < numberParameters; i++){
		var pKey   = getParameters[i].split("=")[0];
		var pValue = getParameters[i].split("=")[1];
		if(pKey != parameter){
			uri += pKey+'='+pValue+'&';
		}
	}
	uri += parameter+'='+value;
	return uri;
}