/******************************************************************************/
/* Funzioni javascript comuni                                                 */
/*                                                                            */
/* Autore: SkyNet s.r.l.                                                      */
/******************************************************************************/

/* Data entry certificati - Ricerca dei modelli *******************************/
function ricercaModelli(val)
{
	if(document.dataOld!=undefined && document.dataOld!=0)
		window.clearTimeout(document.dataOld);
	
	document.nomeModello = val;
	if (val.length>2)
	{
		document.dataOld=window.setTimeout(ricercaModelloRealTime, 700);
	}
	else
	{
		if (val.length==0)
			document.getElementById('divRicercaModello').innerHTML='';
	}
}

/* Funzione per visualizzare lo schermo modale ********************************/
function grayOut(vis, options) {
	// Pass true to gray out screen, false to ungray
	// options are optional.  This is a JSON object with the following (optional) properties
	// opacity:0-100         // Lower number = less grayout higher = more of a blackout 
	// zindex: #             // HTML elements with a higher zindex appear on top of the gray out
	// bgcolor: (#xxxxxx)    // Standard RGB Hex color code
	// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
	// Because options is JSON opacity/zindex/bgcolor are all optional and can appear
	// in any order.  Pass only the properties you need to set.
	var options = options || {}; 
	var zindex = options.zindex || 50;
	var opacity = options.opacity || 70;
	var opaque = (opacity / 100);
	var bgcolor = options.bgcolor || '#000000';
	var dark=document.getElementById('darkenScreenObject');
	if (!dark) {
		// The dark layer doesn't exist, it's never been created.  So we'll
		// create it here and apply some basic styles.
		// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
		var tbody = document.getElementsByTagName("body")[0];
		var tnode = document.createElement('div');           // Create the layer.
		tnode.style.position='absolute';                 // Position absolutely
		tnode.style.top='0px';                           // In the top
		tnode.style.left='0px';                          // Left corner of the page
		tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
		tnode.style.display='none';                      // Start out Hidden
		tnode.id='darkenScreenObject';                   // Name it so we can find it later
		tbody.appendChild(tnode);                            // Add it to the web page
		dark=document.getElementById('darkenScreenObject');  // Get the object.
	}
	
	if (vis) {
		// Calculate the page width and height 
		if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
			var pageWidth = document.body.scrollWidth+'px';
			var pageHeight = document.body.scrollHeight+'px';
		} else if( document.body.offsetWidth ) {
			var pageWidth = document.body.offsetWidth+'px';
			var pageHeight = document.body.offsetHeight+'px';
		} else {
			var pageWidth='100%';
			var pageHeight='100%';
		}   
		//set the shader to cover the entire page and make it visible.
		dark.style.opacity=opaque;                      
		dark.style.MozOpacity=opaque;                   
		dark.style.filter='alpha(opacity='+opacity+')'; 
		dark.style.zIndex=zindex;        
		dark.style.backgroundColor=bgcolor;  
		dark.style.width= pageWidth;
		dark.style.height= pageHeight;
		dark.style.display='block';
	} else {
		dark.style.display='none';
	}
}

/* Funzione per ripulire una stringa dalle virgole ****************************/
function commaCleanObj(obj)
{
	var theString;

	theString = obj.value;
	
	var comma =',';
	var point ='.';
	var newString='';
	var i=0;
	for (i=0; i<theString.length;i++)
	{
		if(theString.substring(i,i+1)==comma)
		{ 
			newString += point;
		} else { 
			newString += theString.substring(i,i+1);
		}
	}

    obj.value = newString;
}

/* Funzione per visualizzare un popup con una immagine ************************/
function popImage(imageURL,imageTitle)
{
    var PositionX = 100;
    var PositionY = 100;

    // Set these value approximately 20 pixels greater than the size of the largest image to be used (needed for Netscape)
    var defaultWidth  = 600;
    var defaultHeight = 400;

    // Set autoclose true to have the window close automatically. Set autoclose false to allow multiple popup windows
    var AutoClose = false;

    if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
	    var isNN=(navigator.appName=="Netscape") ? 1 : 0;
	    var isIE=(navigator.appName.indexOf("Microsoft") != -1) ? 1 : 0;
    }

    var optNN='scrollbars=yes,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
    var optIE='scrollbars=yes,width=100,height=150,left='+PositionX+',top='+PositionY;	
	
	if (isNN) {
		imgWin=window.open('about:blank','',optNN);
	}

	if (isIE) {
		imgWin=window.open('about:blank','',optIE);
	}

	with (imgWin.document) {
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');
		writeln('<sc'+'ript>');
		writeln('var isNN,isIE;');
		writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');
		writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');
		writeln('if (isIE){');
		writeln('window.resizeTo(100,100);');
		writeln('width=100+(document.body.clientWidth-document.images[0].width);');
		writeln('width=100-(document.body.clientWidth-document.images[0].width);');
		writeln('width+=15;');
		writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		//writeln('height=document.images[0].height + 15;');
		writeln('window.resizeTo(width,height);}');
		writeln('if (isNN){');       
		writeln('window.innerWidth=document.images["George"].width+15;');
		writeln('window.innerHeight=document.images["George"].height+15;}');
		writeln('}');
		writeln('function doTitle(){document.title="'+imageTitle+'";}');
		writeln('</sc'+'ript>');
		if(!AutoClose) { 
			writeln('</head><body bgcolor=#FFFFFF scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
		} else {
			writeln('</head><body bgcolor=#FFFFFF scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
        }		
        writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
		
        close();		
	}
}

/* Funzione per visualizzare un popup a centro schermo ************************/
function showPopup(URL, largh, altez)
{
    var defaultWidth  = 600;
    var defaultHeight = 400;

	// a seconda del tipo di documento, decido cosa devo aprire
	if((URL.indexOf( ".htm", 0) > 0) || (URL.indexOf( ".php", 0) > 0) || (URL.indexOf( ".pdf", 0) > 0)) {
		if (isNaN(Number(largh))) {
			largh = defaultWidth;
        }
		
        if(isNaN(Number(altez))) {
			altez = defaultHeight;
        }

		flags = "toolbar=0,location=0,scrollbars=1,resizable=1,statusbar=0,menubar=0";
 		flags += ",width=" + largh + ",height=" + altez;
 	 	flags += ",left=" +  ( screen.availWidth - largh)/2 ;
  		flags += ",top="+(screen.availHeight-altez ) /2;
		
		window.open( URL, '', flags);
	} else {
		popImage ( URL, "Dettaglio") ;
	}	
}

/* Funzione per editare una riga senza usera l'innerHTML **********************/
function editaRiga(indice, stringa)
{
	var tr = document.getElementById(indice);
	var numeroCelle = tr.cells.length;
	
	for (var i=1; i<numeroCelle; i++)
	{
		tr.deleteCell(1);
	}
	
	var td = document.getElementById(indice + ".0");
	var attributo = document.createAttribute("colspan");
	attributo.nodeValue = numeroCelle;
	td.setAttributeNode(attributo);
	td.innerHTML = stringa;
}


function zoom_in(object_id)
{
	imgHeight = document.getElementById(object_id).offsetHeight;		
	imgHeight = imgHeight + ((imgHeight * 33) / 100);		
	document.getElementById(object_id).style.height = imgHeight + "px";
}
	
function zoom_out(object_id)
{
	imgHeight = document.getElementById(object_id).offsetHeight;		
	imgHeight = imgHeight - ((imgHeight * 33) / 100);		
	document.getElementById(object_id).style.height = imgHeight + "px";
}
	
function default_size(container_id, object_id)
{
	divHeight = document.getElementById(container_id).offsetHeight;
	document.getElementById(object_id).style.height = (divHeight - 25) + "px";	
}

function loadImage(path, idImmagine, object_id, object_ref_id)
{
	document.getElementById(object_id).src = path;
	document.getElementById(object_ref_id).innerHTML = idImmagine;
}

function inserisciMarcatoreTextarea(object, marcatore)
{
	var tb = document.getElementById(object);

	 //IE support
	if (document.selection) {
		tb.focus();
		sel = document.selection.createRange();
		sel.text = marcatore;
	} else if (tb.selectionStart || tb.selectionStart == '0') {
		var startPos = tb.selectionStart;
		var endPos = tb.selectionEnd;

		tb.value = tb.value.substring(0, startPos) + marcatore + tb.value.substring(endPos, tb.value.length);
	} else {
		tb.value += marcatore;
	}
	
	setTimeout(function() { document.getElementById(object).focus()}, 1);
}

