function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

// Example:
// common_writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function common_writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; path=\; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

// Example:
// alert( common_readCookie("myCookie") );
function common_readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}


// Restituisce un URL con aggiunto un parametro GET
// Se esiste già lo sostituisce
function common_setURLVar(urlAddress, varName, value){
	// Rimuovo le ancore
	urlAddress = common_removeURLAnchor(urlAddress);

	if (urlAddress.indexOf('?') == -1){
		return urlAddress + '?' + varName + '=' + value;
	}else{
		// L'indirizzo contiene già delle variabili GET
		// Estrae la richiesta GET
		request = urlAddress.substr(urlAddress.indexOf("?")+1);
		// Estrae i singoli parametri in un array
		params = request.split("&");
		// Se trova già il parametro lo sostituisce
		found = false; request = "";
		for (i = 0; i < params.length; i++){
			if (params[i].split("=")[0] == varName){
				request = request + ((request=="")?"":"&") + varName + "=" + value;
				found=true;
			}else{
				request = request + ((request=="")?"":"&") + params[i];
			}
		}

		if (found)
			return urlAddress.substr(0,urlAddress.indexOf("?")+1) + request;
		else
			return urlAddress.substr(0,urlAddress.indexOf("?")+1) + request + "&" + varName + "=" + value;
	}
}

// Ricarica l'indirizzo corrente modificando un parametro GET
function common_setGETVar(varName, value){
	document.location.href = common_setURLVar(document.location.href, varName, value);
}

// Mostra una finestra con il dump dell'oggetto
function debug_dump(obj){
	s = "";
	for(it in obj){
		try{
			s = s + it + " = " + obj[it] + "\n<br />";
		}catch(e){}
	}
	wnd = window.open('','debug');
	wnd.document.writeln(s);
}

// Restituisce un URL senza i parametri delle ancore
function common_removeURLAnchor(urlAddress){
	// Mi assicuro che urlAddress sia una String e non una Location
	urlAddress = "" + urlAddress;
	// Rimuovo le ancore
	if (urlAddress.indexOf('#') != -1){
		return urlAddress.substr(0,urlAddress.indexOf('#'));
	}else{
		return urlAddress;
	}
}


// Aggiunge il valore selezionato in una select ad una textarea
function common_addSelectedItem(selectElement, textArea){
	textArea.innerText = textArea.innerText + ((textArea.innerText != '')?',':'') + selectElement.value;
}

// Costanti
lngVarName = "lng";
tplVarName = "tpl";

// Cambia la lingua tramite cookie
function changeLanguage(lngID){
	common_writeCookie(lngVarName, lngID, 24*365);
//	if (common_readCookie(lngVarName) == lngID){
		// Ricarica la pagina (elimina le ancore perchè potrebbero dare fastidio nel reloading)
		location = common_setURLVar(common_removeURLAnchor(location), 'r', Math.random()); 
//	}else{
		// Se i cookie non funzionano tenta con una richiesta GET
//		location = common_setURLVar(common_setURLVar(location, lngVarName, lngID), 'r', Math.random()); 
//	}
}

function changeTemplate(tplName){
	location = common_setURLVar(location, tplVarName, tplName);	
}

// Cambia un valore GET
function changeGetVar(varName, varValue){
	// Ricarica la pagina (elimina le ancore perchè potrebbero dare fastidio nel reloading)
	location = common_setURLVar( common_setURLVar(common_removeURLAnchor(location), 'r', Math.random()),
								varName, varValue);
}

// Ricarica la pagina corrente
function reloadRandom(){
	location = common_setURLVar(common_removeURLAnchor(location), 'r', Math.random());
}

