// \/ Cookies \/

function createCookie(name, value, days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		value += "; expires="+date.toGMTString();
	}
	document.cookie = name + "=" + value + "; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// /\ Cookies /\

// \/ Asynchronous Update \/

var pageRequestManager;
var currentLockingBox;
var windowOnScroll;
var windowOnResize;
var applicationPath;

function initializeAsynchronousMechanism(path)
{
    applicationPath = path == "/" ? "" : path;
    pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
    pageRequestManager.add_initializeRequest(initializeRequest);
    pageRequestManager.add_endRequest(endRequest);
    
    setupContentLocker();
}

function setupContentLocker()
{
    document.getElementById("ContentLocker").innerHTML =
        "<div id=\"ContentLockerBackground\"></div>" +
        "<div id=\"UpdateProgressBox\">" +
            "<a href=\"javascript:if (pageRequestManager.get_isInAsyncPostBack()) pageRequestManager.abortPostBack();\">" +
                "<img src=\"" + applicationPath + "/App_Themes/Default/Images/Time.gif\" title=\"Annulla l'operazione\" border=\"0\" />" +
            "</a>&nbsp;&nbsp;&nbsp;loading ..." +
        "</div>" +
        "<div id=\"MessageBox\">" +
            "<div id=\"MessageBoxText\"></div>" +
            "<div id=\"MessageBoxCommands\"><input type=\"button\" value=\"ok\" onclick=\"javascript:unlockContent()\"/></div>" +
        "</div>";
}

function centerBox()
{
    document.getElementById("ContentLocker").style.top = document.documentElement.scrollTop;
    document.getElementById("ContentLocker").style.left = document.documentElement.scrollLeft;
    if (currentLockingBox != null) 
    {
        currentLockingBox.style.left = (document.getElementById("ContentLocker").offsetWidth - currentLockingBox.offsetWidth) / 2;
        currentLockingBox.style.top = (document.getElementById("ContentLocker").offsetHeight - currentLockingBox.offsetHeight) / 2;
    }
}

function lockContent(LockingBoxId)
{
    windowOnScroll = window.onscroll;
    window.onscroll = function () { centerBox(); };
    windowOnResize = window.onresize;
    window.onresize = function () { centerBox(); };
    
    document.getElementById("ContentLocker").style.display = "block";
    document.getElementById("ContentLocker").style.zIndex = 10000;
    
    currentLockingBox = document.getElementById(LockingBoxId);
    if (currentLockingBox != null) 
    {
        currentLockingBox.style.display = "block";
        centerBox();
    }
}

function unlockContent()
{
    setupContentLocker();
    window.onscroll = windowOnScroll;
    window.onrezize = windowOnResize;
    document.getElementById("ContentLocker").style.display = "none";
    document.getElementById("ContentLocker").style.zIndex = 0;
    if (currentLockingBox != null) currentLockingBox.style.display = "none";
    currentLockingBox = null;
}


function initializeRequest(sender, arguments) 
{
    if (pageRequestManager.get_isInAsyncPostBack() || currentLockingBox != null) 
        arguments.set_cancel(true);
    else
        lockContent("UpdateProgressBox");
}

var messageBoxText = null;
function endRequest(sender, arguments) 
{
    unlockContent();
    if (arguments.get_error())
    {
        var errorMessage;
        if (arguments.get_response().get_timedOut())
            errorMessage = "Time out";
        else if (arguments.get_response().get_statusCode() == '200')
            errorMessage = "Errore (" + arguments.get_response().get_statusCode() + "): " + arguments.get_response().get_statusCode() + " " + arguments.get_error().message;
        else
            errorMessage = "Errore sconosciuto (" + arguments.get_response().get_statusCode() + "): " + arguments.get_error().message;

        arguments.set_errorHandled(true);
        messageBox (errorMessage);
    }
    if (messageBoxText != null)
    {
        document.getElementById("MessageBoxText").innerHTML = "<p id=\"MessageBoxParagraph\">" + messageBoxText + "</p>";
        messageBoxText = null;
        lockContent("MessageBox");    
    }
}

function messageBox (text)
{
    messageBoxText = text;
}

// /\ Asyncronous Update /\
