var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadstatustext="<img src='../images/loading.gif' /> Caricamento..."

////NO NEED TO EDIT BELOW////////////////////////
var loadedobjects=""
var defaultcontentarray=new Object()
var bustcacheparameter=""

function ajaxpage(url, containerid, targetobj){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")

} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
var ullist=targetobj.parentNode.parentNode.getElementsByTagName("li")
for (var i=0; i<ullist.length; i++)
ullist[i].className=""  //deselect all tabs
targetobj.parentNode.className="selected"  //highlight currently clicked on tab
if (url.indexOf("#default")!=-1){ //if simply show default content within container (verus fetch it via ajax)
document.getElementById(containerid).innerHTML=defaultcontentarray[containerid]
return
}
document.getElementById(containerid).innerHTML=loadstatustext
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.setRequestHeader("Content-Type","text/html; charset=utf-8")
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
{
var prova=transchars(page_request.responseText)

document.getElementById(containerid).innerHTML=page_request.responseText
}
}

function loadobjs(revattribute){
if (revattribute!=null && revattribute!=""){ //if "rev" attribute is defined (load external .js or .css files)
var objectlist=revattribute.split(/\s*,\s*/) //split the files and store as array
for (var i=0; i<objectlist.length; i++){
var file=objectlist[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}
}

function savedefaultcontent(contentid){// save default ajax tab content
if (typeof defaultcontentarray[contentid]=="undefined") //if default content hasn't already been saved
defaultcontentarray[contentid]=document.getElementById(contentid).innerHTML
}

function startajaxtabs(){
for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
var ulobj=document.getElementById(arguments[i])
var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
for (var x=0; x<ulist.length; x++){ //loop through each LI element
var ulistlink=ulist[x].getElementsByTagName("a")[0]
if (ulistlink.getAttribute("rel")){
var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
ulistlink.setAttribute("href", modifiedurl) //replace URL's root domain with dynamic root domain, for ajax security sake
savedefaultcontent(ulistlink.getAttribute("rel")) //save default ajax tab content
ulistlink.onclick=function(){
ajaxpage(this.getAttribute("href"), this.getAttribute("rel"), this)
loadobjs(this.getAttribute("rev"))
return false
}
if (ulist[x].className=="selected"){
ajaxpage(ulistlink.getAttribute("href"), ulistlink.getAttribute("rel"), ulistlink) //auto load currenly selected tab content
loadobjs(ulistlink.getAttribute("rev")) //auto load any accompanying .js and .css files
}
}
}
}
}
//*********
function utf16to8(str) {
    var out, i, len, c;

    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
	c = str.charCodeAt(i);
	if ((c >= 0x0001) && (c <= 0x007F)) {
	    out += str.charAt(i);
	} else if (c > 0x07FF) {
	    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
	    out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
	    out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
	} else {
	    out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
	    out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
	}
    }
    return out;
}
function utf8to16(str) {
    var out, i, len, cxc;
    var char2, char3;

    out = "";
    len = str.length;
    i = 0;
    while(i < len) {
	cxc = str.charCodeAt(i++);
	switch(cxc >> 4)
	{ 
	  case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
	    // 0xxxxxxx
	    out += str.charAt(i-1);
	    break;
	  case 12: case 13:
	    // 110x xxxx   10xx xxxx
	    char2 = str.charCodeAt(i++);
	    out += String.fromCharCode(((cxc & 0x1F) << 6) | (char2 & 0x3F));
	    break;
	  case 14:
	    // 1110 xxxx  10xx xxxx  10xx xxxx
	    char2 = str.charCodeAt(i++);
	    char3 = str.charCodeAt(i++);
	    out += String.fromCharCode(((cxc & 0x0F) << 12) |
					   ((char2 & 0x3F) << 6) |
					   ((char3 & 0x3F) << 0));
	    break;
	}
    }

    return out;
}

function my_hex(n, len) {
    var out, ixi;
    out = "";
    for(ixi = 0; ixi < len; ixi++) {
	out = "0123456789ABCDEF".charAt(n & 0xf) + out;
	n >>= 4;
    }
    return out;
}

function my_unescape(str) {
    var ixi, sxs, cxc, out;

   //if(navigator.appName != "Microsoft Internet Explorer") 
   //return str.replace(/%[0-9A-Za-z][0-9A-Za-z]|%u[0-9A-Za-z][0-9A-Za-z][0-9A-Za-z][0-9A-Za-z]/g, function(sxs) {
	//    if(sxs.charAt(1) != "u")
	//	return String.fromCharCode(parseInt(sxs.substr(1), 16));
	//    return String.fromCharCode(parseInt(sxs.substr(2), 16));
	//});

    out = "";
    ixi = 0;
    while(ixi < str.length) {
	sxs = str.charAt(ixi++);
	if(sxs != "%")
	    out += sxs;
	else {
	    if(str.charAt(ixi) != "u") {
		out += String.fromCharCode(parseInt(str.substr(ixi, 2), 16));
		ixi += 2;
	    } else {
		out += String.fromCharCode(parseInt(str.substr(ixi+1, 4), 16));
		ixi += 5;
	    }
	}
    }
    return out;
}

function my_escape(str) {
    var c, i, out;

    //if(navigator.appName != "Microsoft Internet Explorer")
	//return str.replace(/[^.0-9A-Z_a-z-]/g, function(s) {
	//    c = s.charCodeAt(0);
	//    if(c < 256)
	//	return "%" + my_hex(c, 2);
	//    return "%u" + my_hex(c, 4);
	//});

    out = "";
    for(i = 0; i < str.length; i++) {
	c = str.charAt(i);
	if(c.match(/[^.0-9A-Z_a-z-]/)) {
	    c = str.charCodeAt(i);
	    if(c < 256)
		out += "%" + my_hex(c, 2);
	    else
		out += "%u" + my_hex(c, 4);
	} else
	    out += c;
    }
    return out;
}

      function transchars(phrase) {

        // Acentos
        phrase = phrase.replace(/Ã¡/g,"á");
        phrase = phrase.replace(/Ã©/g,"é");
        phrase = phrase.replace(/Ã­/g,"í");
        phrase = phrase.replace(/Ã³/g,"ó");
        phrase = phrase.replace(/Ãº/g,"ú");
        phrase = phrase.replace(/Ã²/g,"ò");


        // Tildes
        phrase = phrase.replace(/Ã£/g,"ã");
        phrase = phrase.replace(/Ã±/g,"ñ");

        // Cedilla
        phrase = phrase.replace(/Ã§/g,"ç");

        // Gorritos
        phrase = phrase.replace(/Ã¢/g,"â");
        phrase = phrase.replace(/Ãª/g,"ê");
        phrase = phrase.replace(/Ã®/g,"î");
        phrase = phrase.replace(/Ã´/g,"ô");
        phrase = phrase.replace(/Ã»/g,"û");

        // Doble dieresis
        phrase = phrase.replace(/Ã«/g,"ë");
        phrase = phrase.replace(/Ã¯/g,"ï");
        phrase = phrase.replace(/Ã¶/g,"ö");
        phrase = phrase.replace(/Ã¼/g,"ü");

        // Acentos invertidos
        phrase = phrase.replace(/Ã¨/g,"è");
        phrase = phrase.replace(/Ã¹/g,"ù");

        phrase = phrase.replace(/Ã/g,"à");

        phrase = phrase.replace(/Â¿/g,"¿");
        phrase = phrase.replace(/vbCrLf/g," ");
        

phrase=unescape(phrase);       
// æ
        return phrase;
      }
