String.prototype.startsWith = function(str) { return (this.match("^"+str)==str) }

String.prototype.endsWith = function(str) { return (this.match(str+"$")==str ) }

var _currentWidth = 0;

function fitToContent(o) {
   if (_currentWidth == 0) {
      if (document.getElementById("content")) {
         el = document.getElementById("content");
         _currentWidth = Math.round(el.offsetWidth * 0.80 * 0.90);
      }
   }
   o.style.width = _currentWidth + 'px';	
}

function writeCookie(name, value, hours) {
	var expires = "";
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	document.cookie = name + "=" + 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) {
	writeCookie(name, "", -1);
}

function Preferiti() {
   var url    = "http://www.aismac.org";
   var titolo = "AISMAC - Associazione Italiana Siringomielia e Arnold Chiari";

	if (window.sidebar && window.sidebar.addPanel) {
		window.sidebar.addPanel(titolo, url, '');
	}
	else {
   	window.external.AddFavorite(url, titolo);
	}
}

function cambioTagsLinkEsterni() {
   var outstr = "";
   var tmp = "";
   var c = document.getElementsByTagName("body")[0].getElementsByTagName("*");
   c = document.getElementsByTagName("a");
   for (var x = 0; x < c.length; x++) {
      if (c[x].tagName == 'A') {
         tmp = c[x].href.toLowerCase();
			if (c[x].rel == 'PopUp') {
				c[x].target = '_blank';
			}
			else if (tmp.substr(tmp.length - 4) == '.pdf') { c[x].target = '_blank'; }
			else if (tmp.substr(tmp.length - 4) == '.jpg') { c[x].target = '_blank'; }
         else if ((tmp.indexOf('www.aismac.org') == -1) && (tmp.indexOf('localhost') == -1) && (tmp.indexOf('javascript:') == -1) && (tmp.indexOf('mailto:') == -1)) { c[x].target = '_blank'; }
         else {
            if (c[x].href.endsWith('.php')) {
               //c[x].href += '?' + new Date().getTime();
            }
         }
      }
   }

   writeCookie("resolution", "0000x000", 30);
   if (screen.width) {
      writeCookie("resolution", screen.width + 'x' + screen.height, 30);
   }

   // Browser - INIZIO

   var BN  = navigator.appName;
   var BV  = '' + parseFloat(navigator.appVersion); 

   if (navigator.userAgent.indexOf("MSIE") != -1) {
      BN = "Explorer";
      BV = navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5);
   }
   else if (navigator.userAgent.indexOf("Opera") != -1 ) {
      BN = "Opera";
      BV = navigator.userAgent.substring(navigator.userAgent.indexOf("Opera") + 6);
   }
   else if (navigator.userAgent.indexOf("Chrome") != -1 ) {
      BN = "Chrome";
      BV = navigator.userAgent.substring(navigator.userAgent.indexOf("Chrome") + 7);
   }
   else if (navigator.userAgent.indexOf("Safari") != -1 ) {
      BN = "Safari";
      BV = navigator.userAgent.substring(navigator.userAgent.indexOf("Safari") + 7);
   }
   else if (navigator.userAgent.indexOf("Firefox") != -1 ) {
      BN = "Firefox";
      BV = navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox") + 8);
   }
   else if (navigator.userAgent.lastIndexOf(' ') < navigator.userAgent.lastIndexOf('/')) {
      BN = navigator.userAgent.substring(navigator.userAgent.lastIndexOf(' ') + 1, navigator.userAgent.lastIndexOf('/'));
      BV = navigator.userAgent.substring(navigator.userAgent.lastIndexOf('/') + 1);
   }

   if (BV.indexOf(";") != -1) {
      BV = BV.substring(0, BV.indexOf(";"));
   }

   if (BV.indexOf(" ") != -1) {
      BV = BV.substring(0, BV.indexOf(" "));
   }

   writeCookie("Browser", BN + ' ' + BV, 30);
   
   // Browser - FINE

}

var _Evidezia_Off = false;

function EvidenziaParoleCercate() {
	if (_Evidezia_Off) return;
	
   var lista = readCookie('Parole');
	if (lista != null && document.getElementById("content")) {
		if (lista != "") {
			var vParole = lista.split(',');
			var sHTML1 = document.getElementById("content").innerHTML; 
			var sHTML2 = sHTML1;
		
			for (var x = 0; x < vParole.length; x++) {
				var pa = vParole[x];
				if (pa.length > 3) {
					var re = new RegExp(pa, "gi");
					sHTML1 = sHTML1.replace(re, '<span class="found">' + pa + '</span>');
				}
			}
			if (sHTML2 != sHTML1) {
				document.getElementById("content").innerHTML = sHTML1; 
			}
		}
	}
}



// Gestione dei CSS, vengono gestiti solo i CSS a video, per quello di stampa si utilizzerà lo stile 2 (medio)

var _VCSS = Array('Stile S', 'Stile M', 'Stile L', 'Stile XL', 'Stile XXL');
var _NCSS = 1;
var _CSSLOCKED = false;

// Funzione per cambiare i fogli di stile
function MySetStyleSheets() {
	var ss = document.styleSheets;
	if (!ss.length) {
		_CSSLOCKED = true;
		//alert(' ATTENZIONE!\n Il browser non supporta il cambiamento\n dinamico dei fogli di stile.');
	}
	else if (ss.length < 5) { 
		_CSSLOCKED = true;
		//alert(' ATTENZIONE!\n Il browser non supporta il cambiamento\n dinamico dei fogli di stile.');
	}
	else if ((_NCSS < 0) || (_NCSS > 4)) { 
		_NCSS = 1;
	}
	else {
		for (var x = 0; x < ss.length; x++) {
			if (ss[x].title) {
				if (ss[x].title == _VCSS[_NCSS]) {
					ss[x].disabled = false;
				}
				else if (ss[x].title.indexOf('Stile') == 0) {
					ss[x].disabled = true;
				}
			}
		}
		document.getElementById('imgCssUp').src = (_NCSS == 4) ? 'icons/font_non.png' : 'icons/font_suc.png' ;
		document.getElementById('imgCssDn').src = (_NCSS == 0) ? 'icons/font_non.png' : 'icons/font_pre.png' ;
		writeCookie('StyleSheetNumber', _NCSS, 48);
	}
}

// Funzione per cambiare i fogli di stile
function DisableStyleSheets() {
	var ss = document.styleSheets;
   for (var x = 0; x < ss.length; x++) {
      ss[x].disabled = true;
   }
}

function MySetStyleSheetsAdd() {
if (_CSSLOCKED) {
		alert ('Azione non supportata dal browser in uso');
   }
	else if (_NCSS >= 4) {
		alert ('Azione non disponibile');
	}
	else {
		_NCSS++;
		MySetStyleSheets();
	}
}

function MySetStyleSheetsSub() {
	if (_CSSLOCKED) {
		alert ('Azione non supportata dal browser in uso');
   }
	else if (_NCSS <= 0) {
		alert ('Azione non disponibile');
	}
	else {
		_NCSS--;
		MySetStyleSheets();
	}
}
