//
var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }
        return string;
    }
}
//AJAX Functions
//Global Vars
var httpRequest;
function isInput(oNode) {
	var aInputs = new Array();
		aInputs[0] = "input";
		aInputs[1] = "textarea";
		aInputs[2] = "select";
	for (var i = 0; i < aInputs.length; i++)
		if (oNode.tagName.toLowerCase() == aInputs[i]) return(true);
	//LLegaremos aquí si no es un nodo de input
	return(false);
}
function getChildInputs(oNode) {
	var aInputs = new Array();
	var aInputsAux = new Array();
	if (oNode.hasChildNodes) {
		var aChilds = oNode.childNodes;
		var oChild; //explicit declaration, to everride the warnings
		for (var i = 0; i < aChilds.length; i++) {
			oChild = aChilds[i];
			if (oChild.nodeType == 1 && isInput(oChild)) {
				aInputs[aInputs.length] = oChild;
			} else {
				if (oChild.hasChildNodes)
					aInputsAux = getChildInputs(oChild);
					for (var k = 0; k < aInputsAux.length; k++)
						aInputs[aInputs.length] = aInputsAux[k];
			}
		}
	}
	return(aInputs);
}
function serializeForm(oForm) {
	var sSerialized="";
	var aInputs = getChildInputs(oForm);
	if (aInputs.length > 0) {
		for (var i = 0; i < aInputs.length; i++) {
			oInput = aInputs[i];
			switch (oInput.type.toLowerCase()) {
				case "radio":
				case "checkbox":
					if (oInput.checked) {
						if (sSerialized.length > 0) sSerialized += "&";
						sSerialized += oInput.name + "=" + encodeURI(oInput.value);
					}
				break;
				case "select":
					if (sSerialized.length > 0) sSerialized += "&";
					sSerialized += oInput.name + "=" + encodeURI(oInput[oInput.selectedIndex].value);
				break;
				default:
					if (sSerialized.length > 0) sSerialized += "&";
					sSerialized += oInput.name + "=" + encodeURI(oInput.value);
				break;
			}
		}
	}
	return(sSerialized);
}
function readyState(httpRequest, sCallBack) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			eval(sCallBack);
		} else {
			switch (httpRequest.status) {
			    case 12029:
			    case 12030:
			    case 12031:
			    case 12152:
			    case 12159:
			        eval(sCallBack);
			    break;
			    default:
			    	alert('There was a problem with the request. ' + httpRequest.status);
			    break;
			}
		}
	}
}
function makeGetRequest(url, sCallBack) {
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		var httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}

	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	httpRequest.onreadystatechange = function () {readyState(httpRequest, sCallBack)};
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function makePostRequest(url, oForm, sCallBack) {
	var sSerialized = serializeForm(oForm);
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		var httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}

	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	httpRequest.onreadystatechange = function () {readyState(httpRequest, sCallBack)};
	httpRequest.open('POST', url, true);
	var sEncoding = 'application/x-www-form-urlencoded';
	if (oForm.enctype) sEncoding = oForm.enctype;
	httpRequest.setRequestHeader('Content-Type', sEncoding);
	httpRequest.setRequestHeader("Content-length", sSerialized.length);
	httpRequest.setRequestHeader("Connection", "close");
	httpRequest.send(sSerialized);
}