﻿//************************************************************************************
// Restituisce l'elemento identificato da ID (getElementById) oppure document.all[id]
// ***********************************************************************************
function F_getId(id) {
	var elemento;
 	if(document.getElementById) elemento = document.getElementById(id); // se esiste il metodo getElementById		
	else elemento = document.all[id]; // altrimenti è necessario usare un vecchio sistema		
	return elemento;
}
// ***********************************************************************************

// ***********************************************************************************
// Assegnazione dell'oggetto XMLHttpRequest con compatibilità browsers
// ***********************************************************************************
function F_defXMLHttpRequest() {
	var XHR = null, browserUtente = navigator.userAgent.toUpperCase();
	// browser standard con supporto nativo
	// non importa il tipo di browser
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object") XHR = new XMLHttpRequest();

	// browser Internet Explorer
	// è necessario filtrare la versione 4
	else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
		// la versione 6 di IE ha un nome differente
		// per il tipo di oggetto ActiveX
		if(browserUtente.indexOf("MSIE 5") < 0)	XHR = new ActiveXObject("Msxml2.XMLHTTP");
		// le versioni 5 e 5.5 invece sfruttano lo stesso nome
		else XHR = new ActiveXObject("Microsoft.XMLHTTP");
	}
 return XHR;
}
// ***********************************************************************************

// oggetto di verifica stato (readyState)
var readyState = {
	INATTIVO:	0,
	INIZIALIZZATO:	1,
	RICHIESTA:	2,
	RISPOSTA:	3,
	COMPLETATO:	4
};
// array descrittivo dei codici restituiti dal server
// [la scelta dell' array è per evitare problemi con vecchi browsers]
var statusText = new Array();
statusText[100] = "Continue";
statusText[101] = "Switching Protocols";
statusText[200] = "OK";
statusText[201] = "Created";
statusText[202] = "Accepted";
statusText[203] = "Non-Authoritative Information";
statusText[204] = "No Content";
statusText[205] = "Reset Content";
statusText[206] = "Partial Content";
statusText[300] = "Multiple Choices";
statusText[301] = "Moved Permanently";
statusText[302] = "Found";
statusText[303] = "See Other";
statusText[304] = "Not Modified";
statusText[305] = "Use Proxy";
statusText[306] = "(unused, but reserved)";
statusText[307] = "Temporary Redirect";
statusText[400] = "Bad Request";
statusText[401] = "Unauthorized";
statusText[402] = "Payment Required";
statusText[403] = "Forbidden";
statusText[404] = "Not Found";
statusText[405] = "Method Not Allowed";
statusText[406] = "Not Acceptable";
statusText[407] = "Proxy Authentication Required";
statusText[408] = "Request Timeout";
statusText[409] = "Conflict";
statusText[410] = "Gone";
statusText[411] = "Length Required";
statusText[412] = "Precondition Failed";
statusText[413] = "Request Entity Too Large";
statusText[414] = "Request-URI Too Long";
statusText[415] = "Unsupported Media Type";
statusText[416] = "Requested Range Not Satisfiable";
statusText[417] = "Expectation Failed";
statusText[500] = "Internal Server Error";
statusText[501] = "Not Implemented";
statusText[502] = "Bad Gateway";
statusText[503] = "Service Unavailable";
statusText[504] = "Gateway Timeout";
statusText[505] = "HTTP Version Not Supported";
statusText[509] = "Bandwidth Limit Exceeded";

// ***********************************************************************************
// Assegnazione dell'oggetto XMLHttpRequest con compatibilità browsers
// ***********************************************************************************
function F_richiestaGet(id,url) {
	// variabili di funzione
	var txt="", ajax = F_defXMLHttpRequest(), elemento = F_getId(id), attivaLink=true;
	
	// se l'oggetto XMLHttpRequest non è nullo
	if(ajax) { 
		// Mostra il Preload qui 
		attivaLink = false; // il link al file non deve essere usato
		// impostazione richiesta asincrona in GET
		// del file specificato
		ajax.open("get", url, true);
		// rimozione dell'header "connection" come "keep alive"
		ajax.setRequestHeader("connection", "close");
		// impostazione controllo e stato della richiesta
		ajax.onreadystatechange = function() {
	  
			// verifica dello stato
			if(ajax.readyState === readyState.COMPLETATO) {
				// verifica della risposta da parte del server
				if(statusText[ajax.status] === "OK") txt = ajax.responseText; // operazione avvenuta con successo
				else {
				  // errore di caricamento
				  txt = "Impossibile effettuare l'operazione richiesta.<br />";
				  txt += "Errore riscontrato: " + statusText[ajax.status];
				}
	  		} 
			// Ferma il Preload qui
			elemento.innerHTML=txt;
		}
		ajax.send(null); // invio richiesta
	}
	return attivaLink;
} 
// ***********************************************************************************

// ***********************************************************************************
// Funzione Mostra Errori o Warning
// ***********************************************************************************
function showWarning(classe,txt) {
	var msgbox=F_getId("msgbox");
	if (msgbox!=null) {
		if (txt.length>0) {
			msgbox.innerHTML=txt;
			if (classe) msgbox.className=classe;
			msgbox.style.display = "block";
		} else msgbox.style.display = "none";
	} else alert ("Message box not found!");
}

// ***********************************************************************************
// Funzione che limita i caratteri imputabili nei campi textarea
// Esempio: <TEXTAREA name="testo" id="testo" onKeyUp="return limitTXT(this,250);" onBlur="return limitTXT(this,250);" onFocus="return limitTXT(this,250);" rows="5" cols="34"></TEXTAREA>
// 			<span id="testo_span"></span> max 250
// ***********************************************************************************
function limitTXT(txtarea,maxLen){
	total = eval(maxLen);
	char = eval(txtarea.value.length);
	left = eval(total - char);
	if (left <= -1) {
		var dif=eval(char-maxLen);
		var value=txtarea.value.substr(0,char-dif);
		txtarea.value=value;
		var left=0;
	}
	var span=F_getId(txtarea.id+"_span");
	span.innerHTML=left;
	return true;
}


// **********************************************************************
// Funzioni utilizzate dalle gallerie immagini per evidenziare la foto
// **********************************************************************
function evidenzia(idname,classe) {
	var activeCell=F_getId(idname);
	activeCell.className=classe;
}

// ********************************************************
// Richiama l'swfObject per una pagina
// ********************************************************
function flashLoad(file,w,h,divID) {
	var flashvars = false;
	var params = {
	  bgcolor: "#FFFFFF",
	  allowScriptAccess: "always"
	};
	var attributes = { id: "my"+divID };
    swfobject.embedSWF(file, divID, w, h, "9.0.0", "expressInstall.swf", flashvars, params, attributes);
}

// ********************************************************************************************************************
// Setta una variabile nella QueryString
// Se omesso url viene preso direttamente window.location.href
// Quindi replaceQueryString(param, value) equivale a replaceQueryString(param, value, window.location.href)
// ********************************************************************************************************************
function replaceQueryString(param, value, url) {   
	var preURL = "";   
	var postURL = "";   
	var newURL = "";   
	url = url || window.location.href;

	var start = url.indexOf(param+"=");   
	if(start > -1) {   
		var end = url.indexOf("=", start);   
		preURL=url.substring(0,end) +"="+value;   
		  
		var startRest = url.indexOf("&",start);   
		postURL="";   
		if(startRest > -1) postURL=url.substring(startRest);      
	} else {   
		var delimeter = "";   
		preURL=url;   
		if (url.indexOf("?") > 0) delimeter = '&';   
		else delimeter = '?';   
		postURL=delimeter+param+"="+value;   
	}   
	newURL = preURL+postURL;   
	var index = newURL.indexOf('id=',0);   
	if(index > -1) {   
		var Nurl = newURL.substring(0,index);   
		var EUrl = newURL.substr(index,newURL.length - index);   
		var eIndex = EUrl.indexOf('&',0);   
		if(eIndex > -1) EUrl = EUrl.substr(eIndex, EUrl.length - eIndex);   
		//newURL = newURL.substring();   
		newURL = Nurl + EUrl;   
	}   
	return newURL;   
}
// ********************************************************************************

var IFrameObj;
// *****************************
// Call external URLS in Iframe
// *****************************
function loadFrame(url,divID,frameW,frameH,frameBorder,clearDiv) {
	if (!document.createElement) {return true};
	var IFrameDoc;
	var divObj=F_getId(divID);
	frameW=frameW || 250;
	frameH=frameH || 250;
	frameBorder=frameBorder || 250;
	clearDiv=clearDiv || false;
	if (clearDiv) divObj.innerHTML="";
	
	if (!IFrameObj && document.createElement) {
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time 
		// callToServer() is called
		try {
			var tempIFrame=document.createElement("iframe");
			
			var h={id:"STWframe",name:"STWframe",frameBorder:frameBorder,width:frameW,height:frameH,marginWidth:"0",marginHeight:"0",hspace:"0",vspace:"0",allowTransparency:"true",scrolling:"auto",padding:"0",margin:"0"};
			for(var n in h){tempIFrame.setAttribute(n,h[n])}
			
			IFrameObj = divObj.appendChild(tempIFrame);
			
			if (document.frames) {
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames['STWframe'];
			}
		} catch(exception) { // VECCHI BROWSER
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML='\<iframe id="STWframe" style="';
			iframeHTML+='border:0;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			divObj.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('STWframe');
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('callToServer()',10);
		return false;
	}
	
	if (IFrameObj.contentDocument) {
		// For NS6
		IFrameDoc = IFrameObj.contentDocument; 
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		IFrameDoc = IFrameObj.document;
	} else return true;

	IFrameDoc.location.replace(url);
	return false;
}

// *****************************
// TRADUZIONI DI GOOGLE
// *****************************
function translateDiv(divID,destLang) {
	var source=F_getId(divID+"_source");
	var englishText=F_getId(divID+"_EN");
	var destination=F_getId(divID+"_translation");
	var sourceText=source.innerHTML;
	if (sourceText) {
		preloadDiv(destination);
		google.language.detect(sourceText, function(result) { 
			if (!result.error && result.language) { 
				google.language.translate(sourceText, result.language, destLang, function(result) { 														  
					if (result.translation) destination.innerHTML=result.translation;
					else {
						if (englishText) {
							google.language.translate(englishText, "en", destLang, function(result) { 														  
								if (result.translation) destination.innerHTML=result.translation;
								else destination.innerHTML="ERROR: this text cannot be translated!";
							}); 
						} else destination.innerHTML="ERROR: this text cannot be translated!";
					}
				}); 
			} else {
				if (englishText) {
					google.language.translate(englishText, "en", destLang, function(result) { 														  
						if (result.translation) destination.innerHTML=result.translation;
						else destination.innerHTML="ERROR: this text cannot be translated!";
					}); 
				} else destination.innerHTML="ERROR: this text cannot be translated!";
			}
		}); 
	}
}
function translatePost(divID,destLang) {
	var source=F_getId(divID+"_source");
	var englishText=F_getId(divID+"_english");
	var destination=F_getId(divID+"_current");
	var sourceText=source.innerHTML;
	if (destLang!="EN") {
		if (sourceText) {
			preloadDiv(destination);
			google.language.detect(sourceText, function(result) { 
				if (!result.error && result.language) { 
					google.language.translate(sourceText, result.language, destLang, function(result) { 														  
						if (result.translation) destination.innerHTML=result.translation;
						else {
							if (englishText) {
								google.language.translate(englishText, "en", destLang, function(result) { 														  
									if (result.translation) destination.innerHTML=result.translation;
									else destination.innerHTML="ERROR: this text cannot be translated!";
								}); 
							} else destination.innerHTML="ERROR: this text cannot be translated!";
						}
					}); 
				} else {
					if (englishText) {
						google.language.translate(englishText, "en", destLang, function(result) { 														  
							if (result.translation) destination.innerHTML=result.translation;
							else destination.innerHTML="ERROR: this text cannot be translated!";
						}); 
					} else destination.innerHTML="ERROR: this text cannot be translated!";
				}
			}); 
		}
	} else destination.innerHTML=englishText.innerHTML;
}

function preloadDiv(destination) {
	destination.innerHTML="<img src='/img/preload.gif'>";
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function parentExists() {
	return (parent.location == window.location)? false : true;
}

