// -----------------------------
// Corporate Portal Library
// -----------------------------
// 12.01.2005 Aggiunto absoluteUrl su openLink per bypassare exception FireFox 1.0 -  0x80004005 (NS_ERROR_FAILURE) [nsIDOMJSWindow.open]
// 28.02.2006 Aggiunta funzione addEvent per poter accodare eventi (load, onunload, ...) ad oggetti di tipo window.
// 03.03.2006 Aggiunta funzione adjustFrameHeight per poter aggiustare l'altezza del frame in base al contenuto

var defaultWidth = 640;
var defaultHeight = 480;


//CUSTOM CPZ PER FRIENDLYURL
function create_alias(String) {
	var Alias=LRTrim(String);
	
	//rimuovo eventuali tag html
	Alias = Alias.replace(/<[^>]*>/g, '');
	// rimuove le andate a capo
	Alias = Alias.replace(/\n/gi, ' ');
	// sistema gli spazi unbreakabili...
	Alias = Alias.replace(/&nbsp;/gi, ' ');
	
	Alias=Lower(Alias);
	Alias=Strtran(Alias,'-',' ');
	Alias=Strtran(Alias,' ','_');
	Alias=Strtran(Alias,'\"','_');
	Alias=Strtran(Alias,'#','_');
	Alias=Strtran(Alias,'%','_');
	Alias=Strtran(Alias,'+','_');
	Alias=Strtran(Alias,'/','_');
	Alias=Strtran(Alias,':','_');
	Alias=Strtran(Alias,';','_');
	Alias=Strtran(Alias,'<','_');
	Alias=Strtran(Alias,'>','_');
	Alias=Strtran(Alias,'?','_');
	Alias=Strtran(Alias,'&','_');
	Alias=Strtran(Alias,'!','_');
	Alias=Strtran(Alias,'(','_');
	Alias=Strtran(Alias,')','_');
	Alias=Strtran(Alias,'\'','_');
	Alias=Strtran(Alias,',','_');
	Alias=Strtran(Alias,';','_');
	Alias=Strtran(Alias,'.','_');
	return Alias;
}

function removeZero(newId){
  for (var i=0;i<=newId.length;i++){
	if(Left(newId,1)=='0'){
	  newId = newId.substr(1,newId.length);
	  i=0;
	}else
	  return newId;      
  }  
  return newId;
}

function get_titleCms(id) {         
	var URL='../servlet/pocs_fget_title?m_cAction=function&p_CMCONTENTID='+id          
	var url = new ZtVWeb.JSURL(URL,URL.length > 2048)
	var output=url.Response()          
	if(output.indexOf('Function return value:')<0) {
		output='page';
	}
	else {
		output=output.substring(output.indexOf('Function return value:')+22,output.indexOf(' -->'))
		while (Right(output,1)==String.fromCharCode(13) || Right(output,1)==String.fromCharCode(10)){
			output = Left(output, output.length - 1);
		}
	}  
	return output
}

function openLinkCms(id,title,w,h,target)
{
	var url = '../cms/' + create_alias(title) + '-' + removeZero(id) + '.html';	
	openLink(url,w,h,target);
}

function openLinkCms(id,w,h,target) {
	var title = get_titleCms(id)	
	var url = '../cms/' + create_alias(title) + '-' + removeZero(id) + '.html';
	openLink(url,w,h,target);
}


//END CUSTOM CPZ PER FRIENDLYURL


// openLink
// Apre una finestra in un popup
// parametri: URL,Larghezza,Altezza,Titolo
function openLink(url)
{
	openLink(url,defaultWidth,defaultHeight)
}

function openLink(url,w,h)
{
	openLink(url,w,h,target)
}

function openLink(url,w,h,target)
{
	//CUSTOM CPZ PER FRIENDLYURL
	if(url.indexOf('pocs_bnav')>-1){			
		
		
		if (url.indexOf('p_contentTopic')>-1) {// contenuto
			id_content = url.substr(url.indexOf('p_contentTopic')+15,10);		
			var title = get_titleCms(id_content)	
			var url = '../cms/' + create_alias(title) + '-' + removeZero(id_content) + '.html';
		}else{// contenitore
			if (url.indexOf('p_containerTopic')>-1) {// contenitore
				id_container = url.substr(url.indexOf('p_containerTopic')+17,10);		
				title = get_titleCms(id_container)	
				if (url.indexOf('p_Navigator')>-1) {// se c'è il parametro p_Navigator allora passo i due valori differenti
					id_navigator = url.substr(url.indexOf('p_Navigator')+12,10);		
					var url = '../cms/' + create_alias(title) + '-' + removeZero(id_container) + '-' + removeZero(id_navigator) + '.html';
				}else{// se non c'è il parametro p_Navigator allora passo i due valori uguali
					var url = '../cms/' + create_alias(title) + '-' + removeZero(id_container) + '-' + removeZero(id_container) + '.html';
				}
				
			}		
		}
		openLink(url,w,h,target);
		
	}else{
	//END CUSTOM CPZ PER FRIENDLYURL
		
		if (target == undefined) { target = '_blank' }
		// Serve per centrare la finestra nello Schermo
		var left = Math.ceil((screen.width - w) / 2);
		var top = Math.ceil((screen.height - h) / 2);
		var style = 'toolbar=no,scrollbars=no,resizable=yes' + ',' + 'width=' + w + ',' + 'height=' + h +','+ 'top=' + top +','+'left='+left;
		if (url.substring(0,10)=='../servlet') url=absoluteUrl(url)  // fix per Firefox/Mozilla
		window.open(url,target,style);
		
	}
}

// openLinkRS
// Apre una finestra in un popup non ridimensionabile
// parametri: URL,Larghezza,Altezza,Titolo
function openLinkRS(url)
{
	openLinkRS(url,defaultWidth,defaultHeight)
}

function openLinkRS(url,w,h)
{
	openLinkRS(url,w,h,target)
}

function openLinkRS(url,w,h,target)
{
	if (target == undefined) { target = '_blank' }
	// Serve per centrare la finestra nello Schermo
	var left = Math.ceil((screen.width - w) / 2);
	var top = Math.ceil((screen.height - h) / 2);
	var style = 'toolbar=no,scrollbars=no,resizable=no' + ',' + 'width=' + w + ',' + 'height=' + h +','+ 'top=' + top +','+'left='+left;
	if (url.substring(0,10)=='../servlet') url=absoluteUrl(url)  // fix per Firefox/Mozilla
	window.open(url,target,style);
}


// openLinkSB
// Apre una finestra in un popup, la finestra dispone delle Barre di scorrimento
// parametri: URL,Larghezza,Altezza,Titolo
function openLinkSB(url)
{
	openLinkSB(url,defaultWidth,defaultHeight)
}

function openLinkSB(url,w,h)
{
	openLinkSB(url,w,h,target)
}

function openLinkSB(url,w,h,target)
{
	if (target == undefined) { target = '_blank' }
	// Serve per centrare la finestra nello Schermo
	var left = Math.ceil((screen.width - w) / 2);
	var top = Math.ceil((screen.height - h) / 2);
	var style = 'toolbar=no,scrollbars=yes,resizable=yes' + ',' + 'width=' + w + ',' + 'height=' + h +','+ 'top=' + top +','+'left='+left;
	if (url.substring(0,10)=='../servlet') url=absoluteUrl(url)  // fix per Firefox/Mozilla
	window.open(url,target,style);
}

// stringToDate
// Data una stringa in formato AAAA/MM/GG HH:MM:SS.mmm restituisce un oggetto Date
// La funzione è indipendente dal separatore
function stringToDate(sDate){

	var anno = sDate.substring(0,4);
	// I mesi vanno da 0 a 11
	var mese = sDate.substring(5,7);	
	var giorno = sDate.substring(8,10);	
	var ore = sDate.substring(11,13);	
	var minuti = sDate.substring(14,16);	
	var secondi = sDate.substring(17,19);	
	var millisecondi = sDate.substring(20,23);
	
	// costruisco la data
	var data = new Date(anno, mese - 1, giorno, ore, minuti, secondi,millisecondi);	
	
	return data;	
}

// DateTimeFormat
// Funzione che trasforma il valore non formattato di un datatime
// restituito da una query IN UN CAMPO DI UN PORTLET in formato italiano
// Da AAAA/MM/GG HH:MM:SS.mmm a DD-MM-YYYY hh:mm:ss
// Se l'ora è 00:00.00 viene restituita solo la data
function DateTimeFormat(DateToFormat) {
 if (typeof(DateToFormat)=='string') {
     if (!(Empty(DateToFormat))) {
        DateToFormat=Strtran(DateToFormat,'-','');
        DateToFormat=Strtran(DateToFormat,':','');
        DateToFormat=Strtran(DateToFormat,' ','');
        DateFormatted=FormatDateTime(CharToDateTime(DateToFormat),'DD-MM-YYYY hh:mm:ss');
     }
     else {
        DateFormatted="";
     }
 }
 else {
		try{
			if (typeof(DateToFormat)=='object' && DateToFormat.constructor==Date) {
				  if (DateToFormat.getHours() > 0 || DateToFormat.getMinutes() > 0 || DateToFormat.getSeconds() > 0) {
					DateFormatted=FormatDateTime(DateToFormat,'DD-MM-YYYY hh:mm:ss')
				  }
				  else {
					DateFormatted=FormatDate(DateToFormat,'DD-MM-YYYY')
				  }
				}
		}catch(e){
			return "";
		}
	}
return DateFormatted;
}

// openCont
// Funzione che effettua il Reload del Frame Centrale
// parametri: URL
function openCont(framename,url)
{
 // Funzione che effettua il Reload del Frame Centrale
        framename=LRTrim(framename);
        if (window.name==framename) {
	window.location=url;
	       }
	else {
	     if (parent.window.name==framename) {
	        parent.window.location=url;
	     }
	     else {
	eval('window.'+framename+'.location=url');
	     }
	}
}

// checkLink
// Controlla che l'indirizzo web comincia con http:// o https://
// Utilizzata per aprire un sito web da un portlet (es. sito dell'azienda)
// parametri: URL
function checkLink(url) {
        var httpProt=window.location.protocol+'//';
	if (url.substring(0,httpProt.length) == httpProt) {	      
		window.open(url);
	} else {
		window.open(httpProt+url);
	}
}

// absoluteUrl
// Converte un url in assoluto rispetto alla window.location
// parametri: URL
// case test:
//  url="../servlet/pippo"
//  url="jsp/portlet.jsp"
//  url="/jsp/portlet.jsp"
//  url="http://127.0.0.1:8100/Corporate/jsp/portlet.jsp"
function absoluteUrl(url) {
        var httpProt=window.location.protocol+'//';
	if (url.substring(0,httpProt.length) != httpProt) {	      				
		// determina http context path
		var pathlocation=window.location.pathname
		//controllo se  window.location.pathname	inizia con / 
		if (pathlocation.substr(0,1) != '/') {
		   pathlocation='/'+pathlocation
		}
		var httpContext=httpProt+window.location.host+pathlocation			
		if (url.substr(0,1) == '/') {
			// url è un path assoluto rispetto al contesto inizia per /			
			var httpPath=window.location.pathname.substring(1,window.location.pathname.length)
			httpPath=httpPath.substring(0,httpPath.indexOf("/"))
			var httpContext=httpProt+window.location.host+"/"+httpPath			
			url=httpContext+url;
		} else {			
			// url è un path relativo
			httpContext=httpContext.substring(0,httpContext.lastIndexOf("/")+1)			
			url=httpContext+url;
		}
	}
	return url	
}


// formatMoneyAmount
// Restituisce l'importo formattato secondo il numero di cifre decimali associate
// alla valuta più la valuta oppure, se l'importo è 0, la classe.
// p_Importo: Importo|Valuta|Decimali|Classe
// NB Ora i caratteri '|' vengono convertiti in ',' alla chiamata della funzione, quindi
// p_Importo: Importo,Valuta,Decimali,Classe

function formatMoneyAmount(amount,cusimbol,cudec,amclass){
    if(typeof amclass=='undefined')
		amclass=0;
	var nAmount=Val(amount);
	if(nAmount==0)
		return amclass+' '+cusimbol; //Classe
	else{
		return (GASPLib.Transform(nAmount,GASPLib.PS(20,cudec))+' '+cusimbol);
	}
}

function PlatformPathStart(p_cName){
	return "../servlet/" + p_cName;
}

// getImage
// Funzione utilizzata nelle portlet grid per visulizzare un immagine
// Dati il nome dell'immagine e il Tooltip (obbligatori) restituisce il tag HTML dell'immagine
// sImageName --> Nome Immagine (es. grid_delete)
// sToolTip --> Tooltip associato all'immagine
// sTheme --> Tema Grafico corrente
// w --> larghezza
// h --> altezza
function getImage(sImageName,sToolTip,sTheme,w,h){

	var src = null;

	if (typeof(sTheme) != 'undefined')
		src = '../' + sTheme + '/images/' + sImageName;
	else
		src = '../images/'+ sImageName;
  
	if ((typeof(w) != 'undefined')  && (typeof(h) != 'undefined'))
		return "<IMG BORDER='0' width='"+w+"' height='"+h+"' TITLE='"+sToolTip+"' SRC='"+src+"'></IMG>";    
	else
		return "<IMG BORDER='0' TITLE='"+sToolTip+"' SRC='"+src+"'></IMG>";    
}

// getMimeImage
// Funzione utilizzata per visualizzare l'immagine assiciata la tipo di documento
// sImageName --> Nome Immagine (es. mime_doc.gif)
function getMimeImage(sImageName){
	var src = null;	
	src = '../images/Mime/'+ sImageName.toLowerCase();  
	return "<IMG BORDER='0' SRC='"+src+"'></IMG>";    
}

// getLangImage
// Funzione utilizzata per visualizzare l'immagine assiciata alla lingua
// sCodeLang --> Codice Lingua (es. ITA)
// sToolTip --> Tooltip associato all'immagine
function getLangImage(sCodeLang,sToolTip,className){
	var src = null;	
	src = '../images/Language/flag_'+ sCodeLang.toLowerCase() + '.gif';  
	if ((typeof(sToolTip) != 'undefined')){
		if ((typeof(className) != 'undefined')){
			return "<IMG BORDER='0'  TITLE='"+sToolTip+"' SRC='"+src+"' class='"+className+"'></IMG>";    
		}
		else{
			return "<IMG BORDER='0'  TITLE='"+sToolTip+"' SRC='"+src+"'></IMG>";    
		}
	}
	else{
		if ((typeof(className) != 'undefined')){
			return "<IMG BORDER='0' SRC='"+src+"' class='"+className+"'></IMG>";    
		}
		else{
			return "<IMG BORDER='0' SRC='"+src+"'></IMG>";    
		}
	}
}

// getSystemImage
// Dato il percorso di un immagine restituisce il tag HTML relativo
// Se nel percorso viene trovato SpTheme_XX e viene passato come secondo parametro anche
// il tema SpTheme_XX vine sostituito con il tem corrente
// sPathImage --> Percorso immagine (es. /images/...)
// sTheme --> Tema Grafico corrente
// w --> larghezza
// h --> altezza
function getSystemImage(sPathImage,sTheme,w,h){
	var src = null;
	var indexOfTheme = sPathImage.indexOf("SpTheme");
	if (indexOfTheme > -1){	
		// L'immagine è sotto tema
		// Sostituisco SpTheme_XX con il tema corrente
		var indexSlash = null;
		for (i = indexOfTheme; i < sPathImage.length; i++){
			if (sPathImage.charAt(i) == '/'){
				indexSlash = i;
				break;
			} 
		}
		var sPath = sPathImage.substring(indexOfTheme,indexSlash);		
		sPathImage = Strtran(sPathImage,sPath,sTheme);				
	}
		src = ".." + sPathImage;
		if ((typeof(w) != 'undefined')  && (typeof(h) != 'undefined'))
			return "<IMG BORDER='0' width='"+w+"' height='"+h+"' SRC='"+src+"'></IMG>";    
		else
			return "<IMG BORDER='0' SRC='"+src+"' style='valign:middle'></IMG>";			
}

// getParentCSS
// Copia tutti i fogli si stile presenti nel padre
// Supporta IE, Mozilla e Firefox
function getParentCSS(rewrite,otherCSS){
	// Recupera CSS dal padre
	try{
		if (window.navigator.userAgent.indexOf("MSIE") > 0) {
			// Internet Explorer
			base=document.styleSheets.length;
			for (i=0; i<window.parent.document.styleSheets.length; i++) {
				document.createStyleSheet();
				if (rewrite){
					document.styleSheets[i].cssText= window.parent.document.styleSheets[i].cssText;
				}
				else{
					document.styleSheets[base+i].cssText= window.parent.document.styleSheets[i].cssText;
				}
			}
			if (typeof(otherCSS) != 'undefined'){
				document.createStyleSheet(otherCSS);
			}
		}
		else{
			// Mozilla
			var nCSS = parent.document.getElementsByTagName("LINK").length;
			for(i = 0;i < nCSS; i++){
				var href = parent.document.getElementsByTagName("LINK")[i].getAttribute("href");
				var objCSS = document.createElement("LINK");
				objCSS.setAttribute("REL","stylesheet");
				objCSS.setAttribute("TYPE","text/css");
				objCSS.setAttribute("HREF",href);
				document.getElementsByTagName("HEAD").item(0).appendChild(objCSS);
			}
		}
	}catch(e){}
}

// addEvent
// Accoda un evento all'oggetto passato come parametro
// es. addEvent(window, 'load', funzione);
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;  
	} 
	else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;  
	} 
	else{
    return false;
	} 
}

// adjustFrameHeight
// Funzione per aggiustare l'altezza del frame in cui è contenuta la pagina
// La funzione viene eseguita all'evento load della finestra
// La funzione usa le due funzione di appoggio adjustFrameHeightTimeout() e afh()
function adjustFrameHeight(){	
	var isIE = navigator.userAgent.indexOf("MSIE") > 0;
	addEvent(window, "load", new Function("",'setTimeout("afh()",500);'));	
	if (!isIE){
		addEvent(window, "unload", new Function("",'parent.document.getElementById("main").style.height = screen.height - 100;'));
	}	
}

function afh(){
	try{
		// Altezza minima		
		var minHeight = screen.height - 300;		
		parent.document.getElementById("main").style.height = minHeight;
		var h = document.body.scrollHeight;  				
		if (h > minHeight){
			parent.document.getElementById("main").style.height = h + 100;
		}
	}
	catch(e){}	
}

// Funzione che formatta una string per la visualizzazione HTML
function toHtml (s) {
	s = s.replace(/&/g,'&amp;');
	s = s.replace(/"/g,'&quot;');
	s = s.replace(/</g,'&lt;');
	s = s.replace(/>/g,'&gt;');
	s = s.replace(/©/g,'&copy;');
	s = s.replace(/@/g,'&cong;');
	return s;
}

// Funzione che formatta una string per la visualizzazione HTML
function toHtmlLite (s) {
	s = s.replace(/'/g,'&#39;');
	s = s.replace(/’/g,'&#180;');
	return s;
}



