/*
 * @(#)liquid.js 1.0.0 04/09/2006 12:00
 *
 * COPYRIGHT 2006 ETERBIT, S.R.L. ALL RIGHTS RESERVED.
 * ETERBIT PROPRIETARY/CONFIDENTIAL. 
 * USE IS SUBJECT TO LICENSE TERMS.
 *
 */
 
var ie5 = (document.getElementById && document.all);
var ns6 = (document.getElementById && !document.all);
var liquidEnabled = false;
var flashPannel = null;
function initLiquid(lEnabaled) {
	liquidEnabled = lEnabaled;
	liquid();
}

function liquid() {
	divs = document.getElementsByTagName("div");
	tLeftH = 0;
	tLeftDH = 0;
	if (divs['tLeft']) {
		tLeftH = divs['tLeft'].offsetHeight;
		tLeftDH = tLeftH - tLeftHeight;
	}
	tContentH = divs['tContent'].offsetHeight;
	tContentDH = tContentH - tContentHeight;
	tRightH = 0;
	tRightDH = 0;
	if (divs['tRight']) {
		tRightH = divs['tRight'].offsetHeight;
		tRightDH = tRightH - tRightHeight;
	}
	tMaxDH = tLeftDH;
	if (tContentDH>tMaxDH)
		tMaxDH=tContentDH;
	if (tRightDH>tMaxDH)
		tMaxDH=tRightDH;
	if (tMaxDH>0) {
		if (divs['tLeft'])
			divs['tLeft'].style.height = tLeftH + tMaxDH-tLeftDH+"px";
		divs['tContent'].style.height = tContentH + tMaxDH-tContentDH+"px";
		if (divs['tRight'])
			divs['tRight'].style.height = tRightH + tMaxDH-tRightDH+"px";
	}
	divs['texteMainBox'].style.top = 0;
	if (!liquidEnabled)
		return;	
	if(ie5) {
		innerWidth = document.body.offsetWidth;
		innerHeight = document.body.offsetHeight;
	}
	divs['texteMainBox'].style.left = "" + (innerWidth/2 - divs['texteMainBox'].offsetWidth/2) + "px";
	divs['texteMainBox'].style.top = 0;
}


function fullOpenPopup (url, targetName, width, height, scrollbars, toolbar, resizable)
{
    parameters = ""
    if ((width > 0) && (height > 0)) {
        parameters = "width=" + width + ",height=" + height + ",";
        firstParam = false;
    }
    if (scrollbars)
        parameters += "scrollbars=yes,";    
    else
        parameters += "scrollbars=no,";    
    if (toolbar) {
        parameters += "toolbar=yes,";    
        parameters += "menubar=yes,";    
    } else {
        parameters += "toolbar=no,";    
        parameters += "menubar=no,";    
    }
 	parameters += "location=yes,";    
    if (resizable)
        parameters += "resizable=yes";    
    else
        parameters += "resizable=no";    
    return window.open(url, targetName, parameters);
}

function openPopup (url, targetName, width, height, scrollbars, toolbar, resizable) {
	fullOpenPopup (url, targetName, width, height, scrollbars, toolbar, resizable);
}

function formReset(formName) {
	forms = document.getElementsByTagName("form");
    forms['texteMainForm'].reset();
}

function formSubmit(formName, subAction) {
	forms = document.getElementsByTagName("form");
	formSelect(formName, subAction);
    forms['texteMainForm'].submit();
}

function autoFormSubmit(formName) {
	forms = document.getElementsByTagName("form");
    forms[formName].submit();
}

function formSelect(formName, subAction) {
	inputs = document.getElementsByTagName("input");
	inputs['jsFormName'].value = formName;
	if (subAction != 'null')
    	inputs['jsSubAction'].value=subAction;
}

function checkKey(evnt, formName, subAction) {
    if(ie5) {
    	if (evnt.keyCode == 13) {
    		evnt.keyCode = 0;
    		formSubmit(formName, subAction);
    	}
    } else {
    	if (evnt.which == 13) {
    		formSubmit(formName, subAction);
    	}
    }
	return 0;
}

function editableClick(edturl, wname, entity, rId) {
	var wnd = fullOpenPopup(edturl+'?id='+entity+'.'+rId, wname, 800, 600, true, true, true);
	wnd.focus();
}


function sendValue(target, formName, fieldName, val, mustClose) {
	forms = target.document.getElementsByTagName("form");
	form = forms['texteMainForm'];
	form['texteForm_'+formName+'_'+fieldName].value = val;
	if (mustClose)
	  window.close();
}

function up() {
	nome=window.location.href;
	punto=nome.lastIndexOf(".");
	suffisso=nome.substring(punto-2, punto);
	if (suffisso=="it"||suffisso=="fr"||suffisso=="sp"||suffisso=="ge"||suffisso=="po") {	
		window.location="../index_"+suffisso+".htm"
	}
		else
	{		
		window.location="../index.htm"
	}
}

function top() {
	
	scroll(0, 0);
	
}

onresize = liquid;


//TreeView.js

// Copyright (c) 1996-1997 Tomer Shiran. All rights reserved.
// Permission given to use the script provided that this notice remains as is.
// Additional scripts can be found at http://www.geocities.com/~yehuda/

// Boolean variable specified if alert should be displayed if cookie exceeds 4KB
var caution = false

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "")
        if (!caution || (name + "=" + escape(value)).length <= 4000)
                document.cookie = curCookie
        else
                if (confirm("Cookie exceeds 4KB and will be cut!"))
                        document.cookie = curCookie
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
        if (getCookie(name)) {
                document.cookie = name + "=" + 
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-70 00:00:01 GMT"
        }
}

// date - any instance of the Date object
// * you should hand all instances of the Date object to this function for "repairs"
// * this function is taken from Chapter 14, "Time and Date in JavaScript", in "Learn Advanced JavaScript Programming"
function fixDate(date) {
        var base = new Date(0)
        var skew = base.getTime()
        if (skew > 0)
                date.setTime(date.getTime() - skew)
}

// constructor function to create an entry (parent or child)
function item(parent, text, depth) {
        this.parent = parent // is this item a parent?
        this.text = text // text for link (may include HTML)
        this.depth = depth // nested depth
}

// constructor function to create array (compatible with all browsers)
function makeArray(length) {
        this.length = length // length of array (integer)
}

// create items of outline


function setStates() {
        // assign current cookie to local variable
        var storedValue = getCookie("outline")
        
        // if desired cookie not found (null)
        if (!storedValue) {
                // set states to default if no cookie found
                for (var i = 0; i < outline.length; ++i) {
                        // only topmost level is visible by default
                        if (outline[i].depth == 0)
                                outline[i].state = true
                        else
                                outline[i].state = false
                }
        } else {
                // extract current states from cookie (0 => false, 1 => true)
                for (var i = 0; i < outline.length; ++i) {
                        if (storedValue.charAt(i) == '1')
                                outline[i].state = true
                        else
                                outline[i].state = false
                }
        }
}

function setImages() {
        // loop through all elements of the outline "array" (object)
        for (var i = 0; i < outline.length; ++i) {
                if (outline[i].state)
                        if (outline[i].parent) // outline[i] is a parent
                                if (outline[i + 1].state) // outline[i] is exploded
                                        outline[i].pic = '<A HREF="javascript:toggle(' + i + ')"><IMG SRC="exploded.gif" BORDER=0></A>'
                                else // outline[i] is collapsed
                                        outline[i].pic = '<A HREF="javascript:toggle(' + i + ')"><IMG SRC="collapsd.gif" BORDER=0></A>'
                        else // outline[i] is only a child (not a parent)
                                outline[i].pic = '<IMG SRC="child.gif" BORDER=0>'
        }
}

// change from expanded to collapsed and vice versa
function toggle(num) {
        // loop starts at item following argument
        // terminate loop when:
        //   a) last element of outline "array" reached
        //   b) current item (outline[i]) is not deeper than toggled item (outline[num])
        for (var i = num + 1; i < outline.length && outline[i].depth >= outline[num].depth + 1; ++i) {
                // if current item (outline[i]) is a direct child of outline[num]
                if (outline[i].depth == outline[num].depth + 1)
                        outline[i].state = !outline[i].state // toggle state
        }
        // store new states in cookie
        setStorage()
        // reload page
        history.go(0)
}

function setStorage() {
        // initialize local variable to empty string
        var text = ""
        // loop through all properties of outline "array"
        for (var i = 0; i < outline.length; ++i) {
                // use "1" character to represent true state, and "0" for false state
                text += (outline[i].state) ? "1" : "0"
        }
        // create cookie named "outline" with "binary" string
        setCookie("outline", text)
}

function launchScript(content,formName){
    var elem = document.getElementById("contentIdForShopcart");
    var jsformname = document.getElementsByName("jsFormName")[0];
    if(!elem)
		appendToForm(document.forms[0],"input","contentIdForShopcart","hidden",content);
	else 	
		elem.value = content;
	
	if(!jsformname)	
		appendToForm(document.forms[0],"input","jsFormName","hidden",formName);
	else
	 	jsformname.value = formName;
	document.forms[0].name = formName;
	//document.forms[0].submit();	non ci vuole il submit è automatico
}		


function appendToForm(formElement,node,inputFieldName,inputFieldType,defaultValue){
	var oNewNode = document.createElement(node);
	oNewNode.type=inputFieldType;
	oNewNode.name=inputFieldName;
	oNewNode.id=inputFieldName; 
	oNewNode.value=defaultValue;
	formElement.appendChild(oNewNode);
}

function openDetail(codice,template){
	//alert('codice:'+codice);
	//alert('template:'+template);
	url = "/portal/template/viewTemplate?templateId="+template+".psml&codice="+codice;
	window.open( url, "Dettaglio ufficio", "" );
}

function initFlashPanel(panelWidth,panelHeight,textHeader){
	var arr = getWindowSize();
	var widthWin	= arr[0] ;
	var heightWin	= arr[1] ;
	x_index  = 100;
	try{
	x_index = (widthWin - parseInt(panelWidth,10))/2;
	}catch(exception){
	}
	
	
	fnAppendDiv("flashContainerPanel","");
	divInnerPanel = "<div id=\"hdFlashContainer\" class=\"hd\">"+textHeader+"</div>";
	divInnerPanel += "<div class=\"bd\" id=\"bdFlashContainer\" ></div>";
	divInnerPanel += "<div class=\"fd\"></div>";
	document.getElementById('flashContainerPanel').innerHTML  = divInnerPanel;
	flashPannel= new YAHOO.widget.Dialog("flashContainerPanel",{visible:true, fixedcenter : false,x:x_index,modal:true, constraintoviewport:true });
	flashPannel.cfg.setProperty("width",panelWidth);
	flashPannel.cfg.setProperty("height",panelHeight);
	//flashPannel.cfg.setProperty("x",x_index);
	flashPannel.hideEvent.subscribe(function() {flashPannel.destroy();} );
	flashPannel.render();
}

function openFlashInYahooPanel(psml,panelHeight,panelWidth,frameHeight,frameWidth,textHeader,fullscreen,scrollbars){
	// SE E' IN FULLSCREEN NON USO IL PANNELLO
	sUrl = "portal/template/viewTemplate?templateId="+psml+".psml";
	if(fullscreen == "yes"){
		if(document.all){
			window.open(sUrl,"","resizable=yes,titlebar=yes,directories=no,location=no,menubar=no,status=no,toolbar=no,top=0,left=0,width=" +screen.availWidth+",height="+screen.availHeight);
		}else{
			window.open(sUrl,"","fullscreen=yes,titlebar=yes,top=0,left=0,width=" +screen.availWidth+",height="+screen.availHeight);
		}	
	}else{
		if(document.all){
			frameHeight = parseInt(panelHeight, 10) - 43 + "px";
			frameWidth = parseInt(panelWidth, 10)- 20 + "px";
		}else{
			frameHeight = parseInt(panelHeight, 10) - 45 + "px";
			frameWidth = parseInt(panelWidth, 10)- 25 + "px";
		}
		panelHeight = parseInt(panelHeight, 10) + "px";
		panelWidth = parseInt(panelWidth, 10) + "px";

		if(document.getElementById("flashContainerPanel")){ //if already initialized
			flashPannel.destroy();	
		}  
		initFlashPanel(panelWidth,panelHeight,textHeader);

		document.getElementById('bdFlashContainer').innerHTML= 
			"<iframe src=\""+sUrl+"\" width='"+frameWidth+ "' height='"+frameHeight+"'  scrolling='"+scrollbars+"' ></iframe>";	 
		setWMode('transparent');
		flashPannel.show();
		flashPannel.render();
		/*var ajax = assegnaXMLHttpRequest();
	ajax.open("get",sUrl, true);
	ajax.onreadystatechange = function() {
	// verifica dello stato
	if(ajax.readyState === 4) {
        // verifica della risposta da parte del server
        if(ajax.status === 200 ){
          // operazione avvenuta con successo

	 }else {
          document.getElementById('bdFlashContainer').innerHTML= "<br><b>errore di caricamento</b>";
        }
      } 
    }
	ajax.send(null);
		 */
	}
}	
function callPrintDiv(divId){
	
	try{
		var headID 	= document.getElementsByTagName("head")[0];
		var baseTag 	= headID.getElementsByTagName("base")[0];
		var basehref =  baseTag.getAttributeNode('href').nodeValue;
		var links= headID.getElementsByTagName("link");
		var defaultCssHref = "";
		for(var i=0;i<links.length;i++){
			node = links[i];
			var cssHref = node.getAttributeNode('href').nodeValue;
			
			if(cssHref != null && cssHref.indexOf('default.css')>0){
				defaultCssHref=cssHref;
				break;
			}
		}
		var divContent  = document.getElementById(divId).innerHTML;
		
		var res = new String(document.documentElement.innerHTML);
		//head  = res.substring(0,res.lastIndexOf('<body')) ;
		var a = window.open('','','width=1200,height=800,scrollbars=1,menubar =1');  
		a.document.open("text/html");  
		a.document.write("<html>");
		a.document.write("<head>");
		a.document.write("<base href='"+basehref+"'>");
		a.document.write("<link rel='stylesheet' type='text/css' href='"+defaultCssHref+"'>");
		a.document.write("<style type='text/css'>*.contentStructure {border: 0px; margin: 0px; padding: 0px; float:left; position: relative}</style>");
		a.document.write("<style type='text/css'>*.box1Structure {}</style>");
		a.document.write("<style type='text/css'>*.box2Structure {}</style>");
		a.document.write("<style type='text/css'>*.box1Structure {overflow:hidden}</style>");
		a.document.write(" <style type='text/css'>*.box2Structure {overflow-x:hidden;}</style>");
		a.document.write("<script src='/liquid.js' type='text/javascript'></script>");
		a.document.write("</head>");
		a.document.write("<body>");
		
		a.document.write("<div style='position: absolute; top: 0pt;'>");
		a.document.write(divContent);
		a.document.write("</div >");
		a.document.write("</body></html>");
		a.document.close();
		//alert(a.document.documentElement.innerHTML);
		a.focus();  
		a.print(); 
		a.close();
	}catch(e){}	
}



