function URLDecode( text )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var encoded = text;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2)
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				try
				{
					plaintext += decodeURIComponent( encoded.substr(i, 3) );
				}
				catch(e)
				{
					if ( encoded.substr(i+1, 2) == "92")
	                    plaintext += "'";
					else if ( encoded.substr(i+1, 2) == "2F" || encoded.substr(i+1, 2) == "2f")
	                    plaintext += "/";
					else if ( encoded.substr(i+1, 2) == "95")
	                    plaintext += "";
					else
					    plaintext += unescape( encoded.substr(i, 3) );
				}
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

function URLEncode( text )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = text;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function scrollToElement(element)
{
	var valueT = 0;
	var valueL = 0;
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;
      element = element.offsetParent;
    } while (element);
    window.scrollTo(valueT, valueL);
}


function changeImage(objId, newId)
{
	text = getFromFile( "http://www.lescaducees.com/getImage.php", "docid="+newId );
	document.getElementById(objId).innerHTML = URLDecode( text );
	scrollToElement( document.getElementById(objId) );
}

function changeSection(objId, newId)
{
	text = getFromFile( "http://www.lescaducees.com/getSection.php", "rubid="+newId );
	document.getElementById(objId).innerHTML = URLDecode( text );
}

function changeArticle(objId, newId)
{
	text = getFromFile( "http://www.lescaducees.com/getArticle.php", "artid="+newId );
	document.getElementById(objId).innerHTML = URLDecode( text );
	scrollToElement( document.getElementById(objId) );
}

function getFromFile(file, infos)
{
	var xhr_object = null;

    if(window.XMLHttpRequest) // Firefox
	{
    	xhr_object = new XMLHttpRequest();
	}
    else if(window.ActiveXObject) // Internet Explorer
	{
    	xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	}
    else // XMLHttpRequest non support? par le navigateur
    {
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return false;
	}
	xhr_object.open("POST", file, false);
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	if (infos == "")
    {
		xhr_object.send(null);
	}
    else
    {
		xhr_object.send(infos);
	}
	
	if (xhr_object.readyState == 4)
    {
		return xhr_object.responseText;
	}
}
