//Allarga secondo dimensioni finestra
function get_wsize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	var size;
	size = new Object();
	size.x=myWidth;
	size.y = myHeight;
	return size;
}


//allarga secondo dimensioni totali documento
function xDocSize() {
	var b=document.body, e=document.documentElement;
	var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
	if (e) {
		esw = e.scrollWidth;
		eow = e.offsetWidth;
		esh = e.scrollHeight;
		eoh = e.offsetHeight;
	}
	if (b) {
		bsw = b.scrollWidth;
		bow = b.offsetWidth;
		bsh = b.scrollHeight;
		boh = b.offsetHeight;
	}
	//  alert('compatMode: ' + document.compatMode + '\n\ndocumentElement.scrollHeight: ' + esh + '\ndocumentElement.offsetHeight: ' + eoh + '\nbody.scrollHeight: ' + bsh + '\nbody.offsetHeight: ' + boh + '\n\ndocumentElement.scrollWidth: ' + esw + '\ndocumentElement.offsetWidth: ' + eow + '\nbody.scrollWidth: ' + bsw + '\nbody.offsetWidth: ' + bow);
	return {w:Math.max(esw,eow,bsw,bow),h:Math.max(esh,eoh,bsh,boh)};
}


function xDef() {
	for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
	return true;
}

function xNum(){
	for(var i=0; i<arguments.length; ++i){if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;}
	return true;
}


function xScrollTop(e, bWin) {
	var offset=0;
	if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		var w = window;
		if (bWin && e) w = e;
		if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
		else if(w.document.body && xDef(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
	} else {
		e = xGetElementById(e);
		if (e && xNum(e.scrollTop)) offset = e.scrollTop;
	}
	return offset;
}



function resize(this_div,centra_div) {
	var size = xDocSize();
	if (document.getElementById(this_div)) {
		document.getElementById(this_div).style.width = size.w+"px";
	/*	document.getElementById(this_div).style.height = size.h+"px";*/
		centra (centra_div);
	}
}




function centra(tdiv,dx,dy) {
//	dx = document.getElementById(tdiv).style.width;
//	dy = document.getElementById(tdiv).style.height;
	if (!dx) dx=700;
	if (!dy) dy=200;	


	var size = get_wsize();
	var scroll = xScrollTop();
	newx = ( (size.x-dx) /2);	
	newy = scroll+(size.y/2)-dy;
	
	document.getElementById(tdiv).style.top = newy+"px";
	document.getElementById(tdiv).style.left = newx+"px";
}

function close_generic(divn) {
/*
	var root = document.getElementById(divn);
	if (root) {
		root.style.visibility="visible";

		var new1 = document.createElement("div");
		new1.id = "close_mark";
		new1.innerHTML="<a href='javascript:close_generic.close(\""+divn+"\")'>x</a>";
		root.appendChild(new1);
	//alert( document.getElementById("contenitore").id );
	//alert (root.id);	
	}*/

	root = $(divn);
	if (root) {
		id= root.id;
		root.style.visibility="visible";
		val1 = root.innerHTML;
		root.innerHTML="<div ><div id='close_mark'><a href='javascript:close_generic.close(\""+divn+"\")'>x</a></div><div >"+val1+"</div></div>";
		root.style.width=get_wsize().x+"px";
	}
}
close_generic.close = function(divn) {
	if ( aaa = document.getElementById(divn) ) {
		aaa.parentNode.removeChild(aaa);
	}
}

function close_delayed(divn,tempo) {
	var root = document.getElementById(divn);
	if (root) {
		var aclose = setTimeout("close_generic.close('"+divn+"')",tempo);
	}
}


//esempio di chiamata ajax
/*
dettagli = {
	open: function(thismacro, pos){		
		if ((thismacro) && (pos)) {
			new Ajax.Request("_comp/_dettagli.php", {
				method: "post",
				parameters: { this_macro: thismacro, this_pos: pos},
				onSuccess: function (a) { 
					$("dettagli_div").innerHTML = a.responseText
					resize("dettagli");
					$("dettagli_div").style.visibility = "visible";
					centra("finestra");
				}
			});
		}

	},
	close: function(thismacro) {
		$("dettagli").style.width = "100%";
		$("dettagli").style.height = "100%";
		$("dettagli_div").style.visibility="hidden";
	}
}

*/


var cookie = {
	load:function(NameOfCookie){
		if (document.cookie.length > 0){
			begin = document.cookie.indexOf(NameOfCookie+"=");
			if (begin != -1) begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		}
		return null;
	},
	save:function(NameOfCookie, value, expiredays){
		var ExpireDate = new Date ();
		ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
		document.cookie = NameOfCookie + "=" + escape(value) +
		((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
	},
	del:function(NameOfCookie) {
		ExpireDate = "Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = NameOfCookie + "=" + escape(0) + "; expires=" + ExpireDate;
	}
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


//Cambia l'opacita' secondo i browser;
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
function shiftOpacity(id, millisec) {
    //if an element is invisible, make it visible, else make it ivisible
    if(document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 100, 0, millisec);
    }
} 
function shade(id,millisec,start) {
	setTimeout("opacity('"+id+"', 100, 10, "+millisec+")",start);
}

function on_press_enter(e) {
	if (window.event) keye = window.event.keyCode;
	else if (e) keye = e.which;
	if (keye) {
		return (keye == 13) ? true : false;
	}
}

function on_press_tab(e) {
	
	if (window.event) var keye = window.event.keyCode;
	else if (e) var keye = e.which;
	return (keye == 8) ? true : false;
}

function btnsubmit(idhid, val, formname) {
	if (document.getElementById(idhid)) {
		root = document.getElementById(idhid);
	} else {
			root = document.getElementsByName(idhid)[0];
	}
	root.value=val;
	if (!formname) {
		formname = 0;
	}
	if (root) {
		document.forms[formname].submit;
	}
}

function postsubmit(idhid, val, formname,sendform,warningmessage) {
	if (!formname) {
		formname = 0;
	}
	if (!warningmessage) {
		warningmessage=0;
	}
	var temphid = document.createElement("input");
	temphid.name=idhid;
	temphid.id=idhid;
	temphid.type="hidden";
	temphid.value=val;
	document.forms[formname].appendChild(temphid);

	msg = (warningmessage=="default") ? "Si desidera continuare?" : warningmessage;
	if (warningmessage!=0){
		conf = confirm(msg);
		if (conf == false) {
			return false;
		}
	}

	if (sendform!="false") {
		document.forms[formname].submit();
	}
}

myhidden = {
	set:function(idhid,val,formname) {
		if (!formname) {
			formname = 0;
		}	
		if (!$(idhid)) {
			var temphid = document.createElement("input");
			temphid.name=idhid;
			temphid.id=idhid;
			temphid.type="hidden";
			temphid.value=val;
			document.forms[formname].appendChild(temphid);
		} else {
			$(idhid).value = val;
		}	
	},
	del:function(idhid) {
		remove($(idhid));
	}
}

function include(script_filename) {
    document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + script_filename + '">');
    document.write('</' + 'script' + '>');
}
function include_dhtml(script_filename) {
	var headID = document.getElementsByTagName("head")[0];
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.src = script_filename;
	headID.appendChild(newScript);
}

function isdefined(variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}

//arrotonda a x decimali
function roundNumber(num, dec) {
   if (dec > 0) {
      if ((num.toString().length - num.toString().lastIndexOf('.')) > (dec + 1)) {
         var Rounder = Math.pow(10, dec);
         return Math.round(num * Rounder) / Rounder;
      }
      else return num;
   }
   else return Math.round(num);
}

function RoundLim(num,dec) {
	if (dec>0) {
		a = parseString(num);
		b = a.split(".");
		c = b[0]+"."+b[1]+substr(0,dec);
		return c;
	} else {
		return num;
	}
}

//trova la grandezza della barra di scroll
function scrollbarWidth() {
	document.body.style.overflow = 'hidden';
	var width = document.body.clientWidth;
	document.body.style.overflow = 'scroll';
	width -= document.body.clientWidth;
	// Per IE
	if(!width) width = document.body.offsetWidth-document.body.clientWidth;
	document.body.style.overflow = '';
	return width;
}

function jsDataConv(q,e) {
	var a = new Array();
	ev = q.event;
	fr = q.form;
	r = q.value;
	if ((r.length == 0) || (r.length>=10)) {
		doflag = 1;
	}

	//cattura evento
	var keye;
	if (window.event) keye = window.event.keyCode;
	else if (e) keye = e.which;	
	if (((keye<48) || (keye>57)) && keye!=8) {
		doflag = 0;
	}
	
	a[0] = r.substr(0,2);
	a[1] = r.substr(2,2);
	a[2] = r.substr(4,2);
	
	if ((r.length == 5) && (keye!=8)) {
		res = a[0]+"/"+a[1]+"/"+"20"+a[2];
		if (doflag == 1) {
			q.value = res;
		}
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function addResizeEvent(func) {
  var oldonresize = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func;
  } else {
    window.onresize = function() {
      if (oldonresize) {
        oldonresize();
      }
      func();
    }
  }
}

// Correctly handle PNG transparency in Win IE 5.5 or higher.
// http://homepage.ntlworld.com/bobosola. Updated 02-March-2004

function correctPNG()
  {
  for(var i=0; i<document.images.length; i++)
     {
     var img = document.images[i]
     var imgName = img.src.toUpperCase()
     if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
        {
        var imgID = (img.id) ? "id='" + img.id + "' " : ""
        var imgClass = (img.className) ? "class='" + img.className + "' " : ""
        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
        var imgStyle = "display:inline-block;" + img.style.cssText 
        if (img.align == "left") imgStyle = "float:left;" + imgStyle
        if (img.align == "right") imgStyle = "float:right;" + imgStyle
        if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle        
        var strNewHTML = "" 
        img.outerHTML = strNewHTML
        i = i-1
        }
     }
  }
  

  
	function Ctoupper(e) {
		if (window.event) keye = window.event.keyCode;
			else if (e) keye = e.which;
		if ((keye!="8") && (keye!=0)) {
			return keye.toUpperCase();
		}
	}