﻿//-------------
// Popup
//-------------

// Define popup window parameters
var popupDefaultParams = 'status=no,toolbar=no,menubar=no,location=no,directories=no';
var popupDefaultWidth = 540;
var popupDefaultHeight = 550;

// Open popup
function openPopup(url, name, width, height, resizable, scrollbars)
{
	var windowWidth = (width != null ? width : popupDefaultWidth);
	var windowHeight = (height != null ? height : popupDefaultHeight);
	var windowLeft = null;
	var windowTop = null;
	
	if (screen.availWidth != null)
	{
		windowLeft = (screen.availWidth - windowWidth) / 2;
		windowTop = (screen.availHeight - windowHeight) / 2;
	}
		 
	// Open the window    
	return window.open(
		url, 
		name, 
		popupDefaultParams +
		(windowLeft != null ? ',left=' + windowLeft : ',left=10') +
		(windowTop != null ? ',top=' + windowTop : ',top=10') +
		',width=' + (windowWidth) + 
		',height=' + (windowHeight) + 
		',resizable=' + (resizable != null && resizable ? 'yes' : 'no') +
		',scrollbars=' + (scrollbars != null && scrollbars ? 'yes' : 'no'));
}

// Open help popup
function openHelp(id)
{
		openPopup('helppopup.aspx?id=' + id, 'help', null, null, null, null);
}


//---------------------
// Cross window pasting
//---------------------
var pasteTimer = null;
var pasteFunction = null;

// Get a document selection
function getSelection(context)
{
		if (context.getSelection)
		{
				return context.getSelection();
		}
		else if (context.selection && document.selection.createRange) 
		{
			 var range = context.selection.createRange();
			 return range.text;           
		}
}

// Enable the cross window pasting
function enableCrossWindowPasting(pasteCallbackFunction, context)
{   
		if (!context)
				context = document;

		// Look for the paste input variable
		var src = document.getElementById("__PASTEIN");
		if (src)
		{
				src.value = getSelection(context);      
		}
		
		// Store callback function
		pasteFunction = pasteCallbackFunction;
			
		// Start the timer
		pasteTimer = setInterval('tryPaste()', 1000);
		
} 

// Disable the cross window pasting 
function disableCrossWindowPasting()
{
		// Stop interval
		if (pasteTimer)
		{
				clearInterval(pasteTimer);     
				pasteTimer = null;          
		}
		
		// Clear any paste value
		var elem = document.getElementById('__PASTEOUT');
		if (elem && elem.value.length > 0)
				elem.value = null;  // Clear        
}

// Check if paste is required
function tryPaste()
{
		// Look for the paste value
		var dest = document.getElementById('__PASTEOUT');
		if (dest && dest.value.length > 0)
		{         
			 // Stop interval
			 if (pasteTimer)
			 {
						clearInterval(pasteTimer);         
						pasteTimer = null;          
			 }

				// Paste
				if (pasteFunction)
				{
						eval(pasteFunction);
				}
						
			 // Clear paste information
			 dest.value = null;       
		}
}

// Copy a value from the opener __PASTEIN variable
function copyFromOpener(name)
{
		if (window.opener && !window.opener.closed)
		{
				var src = window.opener.document.getElementById('__PASTEIN'); 
				if(src && src.value.length > 0)
				{
						var dest = document.getElementById(name);
						if (dest)
						{
								dest.value = src.value;
						}
				}
		}    
}

// paste a value to the opener __PASTEOUT variable
function pasteToOpener(value)
{
		if (window.opener && !window.opener.closed)
		{
				var dest = window.opener.document.getElementById('__PASTEOUT'); 
				if(dest)
				{
						dest.value = value;
				}
		}    
}

// Helper to perform a paste to a FCKEditor
function pasteToFCKEditor(name)
{
	 FCKeditorAPI.GetInstance(name).InsertHtml(document.getElementById('__PASTEOUT').value); 
}
 


//--------
// Cookies
//--------

// Imposta o legge il valore di una proprietà
// se value = null va in sola lettura
function manageCookieProperty(property, value)
{
	if (value == null)
	{
		// --> Read
		var re = new RegExp(property + "=[^;]+", "i"); //construct RE to search for target name/value pair
		if (document.cookie.match(re)) //if cookie found
			return document.cookie.match(re)[0].split("=")[1]; //return its value
		return null;
		
		//// cookies are separated by semicolons
		//var cookies = document.cookie.split("; ");
		//for (var i=0; i < cookies.length; i++)
		//{
		//  // a name/value pair (a crumb) is separated by an equal sign
		//  var crumb = cookies[i].split("=");
		//  if (crumb[0] == property)
		//  {
		//    return crumb[1];
		//    break;
		//  }
		//}
		//return null;
	}
	else
	{
		// --> Write
		document.cookie = property + "=" + value;
		return value;
	}
}



//-----------------
// Blinking Anchors
//-----------------

var blinkingCount = 0;
var intervalID = 0;
var originalBG;


function CheckBlinkAnchor()
{
	if (document.location.hash != "")
	{
		elementName = document.location.hash.substring(1);
		var element = document.getElementById(elementName);
		if (element != null && element.parentNode != null)
		{
			originalBG = element.parentNode.style.backgroundColor;
			intervalID = setInterval("Blink('" + elementName + "')", 100);
		}
	}
}


function Blink(elementName)
{
	var element = document.getElementById(elementName).parentNode;
	if (element == null)
	{
		clearInterval(intervalID);
		return;
	}
	
	// Resetta dopo 5 lampeggi
	blinkingCount++;
	if (blinkingCount > 10)
	{
		element.style.backgroundColor = originalBG;			
		blinkingCount = 0;
		clearInterval(intervalID);
		return;
	}
				
	if (blinkingCount % 2 == 0)
		element.style.backgroundColor = originalBG;
	else 
		element.style.backgroundColor = "#efea29";				
}


