var lastFocusElement = null;

function onloadPage()
{
/*
 	var inputs = document.getElementsByTagName("input");  
 	for( var i = 0; i < inputs.length; i++ )
 		if (inputs[i].type == 'text' || inputs[i].type == 'password')
 			if (typeof inputs[i].onblur != "function")
				inputs[i].onblur = function() {lastFocusElement = this;};
*/
/*		
	if (this.name!='fullscreen')
	{ 
		window.open(
			location.href,
			'fullscreen',
			'fullscreen=yes, width='+screen.availWidth+',height='+screen.availHeight);
	} 
*/
}

/* Menu */
var numMenu = 0;
function showMenu(id)
{
	var rowDiv;
	var menuImg;
	for (i = 1; i <= numMenu; i++)
	{
		menuImg = getById("menuImg" + i);
		if (i == id)
			menuImg.src = "/img/baj/menu" + i + ".gif";
		else
			menuImg.src = "/img/baj/menu" + i + "off.gif";

		rowDiv = getById("submenu_" + i);
		if (rowDiv == null)
			continue;
		else if (i == id)
			rowDiv.style.display = 'block';
		else
			rowDiv.style.display = 'none';
	}
	
	var menuDiv = getById("menu");
	menuDiv.style['background'] = "#000000 url(/img/baj/menuBck" + id + ".gif) top left no-repeat";
}

/* News Slider */
scrollStep = 2;
timerLeft = "";
timerRight = "";

function toLeft(id)
{
  document.getElementById(id).scrollTop = 0;
}

function scrollDivTop(id)
{
  clearTimeout(timerRight);
  document.getElementById(id).scrollTop += scrollStep;
  timerRight=setTimeout("scrollDivTop('" + id + "')", 10);
}

function scrollDivBottom(id)
{
  clearTimeout(timerLeft);
  document.getElementById(id).scrollTop -= scrollStep;
  timerLeft=setTimeout("scrollDivBottom('" + id + "')", 10);
}

function toRight(id)
{
  document.getElementById(id).scrollTop = document.getElementById(id).scrollHeight;
}

function stopMe()
{
  clearTimeout(timerRight)
  clearTimeout(timerLeft)
}

function setStyle(objId, style, value)
{
   document.getElementById(objId).style[style]= value;
}

// Generic 
//var isIE = document.all ? true : false;
//if (!isIE) document.captureEvents(Event.MOUSEMOVE);

document.onmousemove = getMouseXY;

var mouseX = 0;
var mouseY = 0;

function getMouseXY(e)
{
  if (!e || e == null)
  	e = window.event; 

  if (document.all ? true : false)
  {
    mouseX = e.clientX; // + document.body.scrollLeft;
    mouseY = e.clientY; // + document.body.scrollTop;
  }
  else
  {
    mouseX = e.pageX;
    mouseY = e.pageY;
  }  

  if (mouseX < 0) mouseX = 0;
  if (mouseY < 0) mouseY = 0;

  return true;
}

function getById(id)
{
	var elem;
	if (document.getElementById) // this is the way the standards work
		elem = document.getElementById(id);
	else if (document.all) // this is the way old msie versions work
		elem = document.all[id];
	else if (document.layers) // this is the way nn4 works
		elem = document.layers[id];
	return elem;
}

function getOpenerById(id)
{
	var elem;
	if (window.top.opener.document.getElementById) // this is the way the standards work
		elem = window.top.opener.document.getElementById(id);
	else if (window.top.opener.document.all) // this is the way old msie versions work
		elem = window.top.opener.document.all[id];
	else if (window.top.opener.document.layers) // this is the way nn4 works
		elem = window.top.opener.document.layers[id];
		
	return elem;
}

function doCommand(formName, command, value)
{
	var objDoCommand = getById("command");
	var objDoCommandValue = getById("commandValue");
	var objForm = getById(formName);

	objDoCommand.value = command;
	objDoCommandValue.value = value;
	objForm.submit();
}

function Size(width, height)
{
    this.width = width;
    this.height = height;
}

// Form function
var TYPE_STRING        = 0;
var TYPE_NUMBER        = 1;
var TYPE_SELECT        = 2;
var TYPE_EMAIL         = 3;
var TYPE_DATE          = 4;
var TYPE_DATE_CHILD    = 5;
var TYPE_DATE_SENIOR   = 6;
var TYPE_MOBILE        = 7;
var ERROR_COLOR = "#ff3030";

function FormItem(fieldName, dataType)
{
    this.fieldName = fieldName;
    this.dataType = dataType;
}

function FormManager(formName)
{
    this.formName = formName;
    this.formItems = new Array(256);
    for (i = 0; i < this.formItems.length; i++)
	    this.formItems[i] = null;
}

FormManager.prototype.add = function(fieldName, dataType)
{
	this.remove(fieldName);
	freeSlot = this.getFreeSlot();
    if (freeSlot == -1)
        alert("No More Slot Available for FormItem.");
	this.formItems[freeSlot] = new FormItem(fieldName, dataType);
}

FormManager.prototype.remove = function(fieldName)
{
	for (i = 0; i < this.formItems.length; i++)
	{
		formItem = this.formItems[i];
		if (formItem != null && formItem.fieldName == "fieldName")
		{
			this.formItems[i] = null;
			return;
		}
	}
}

FormManager.prototype.getFreeSlot = function()
{
	for (i = 0; i < this.formItems.length; i++)
		if (this.formItems[i] == null)
			return i;
}

function trim(stringa)
{
	while (stringa.charCodeAt(0) == 32 || stringa.charCodeAt(0) == 160)
		stringa = stringa.substring(1, stringa.length);    
	while (stringa.charCodeAt(stringa.length - 1) == 32 || stringa.charCodeAt(stringa.length - 1) == 160)
		stringa = stringa.substring(0,stringa.length-1);
	
	return stringa;
}

FormManager.prototype.validate = function()
{
    var i;
    var formItem;
    var thisForm;
    var thisElement;
    var isError = false;

    try
    {
        thisForm = document.forms[this.formName];
        for (i = 0; i < this.formItems.length; i++)
        {
            formItem = this.formItems[i];
            if (formItem == null)
            	continue;

            thisElement = thisForm.elements[formItem.fieldName];
            if (thisElement.type == "hidden")
                continue;
                
            if (formItem.dataType == TYPE_STRING)
            {
                value = String(thisElement.value);
                if (trim(value).length == 0)
                {
                    thisElement.style.backgroundColor = ERROR_COLOR;
                    isError = true;
                }
                else
                    thisElement.style.backgroundColor = "";
            }
            else if (formItem.dataType == TYPE_NUMBER)
            {
                value = String(thisElement.value);
                if (value.length == 0 || isNaN(value))
                {
                    thisElement.style.backgroundColor = ERROR_COLOR;
                    isError = true;
                }
                else
                    thisElement.style.backgroundColor = "";
            }
            else if (formItem.dataType == TYPE_SELECT)
            {
                value = String(thisElement.options[thisElement.selectedIndex].value);
                if (value.length == 0)
                {
                    thisElement.style.backgroundColor = ERROR_COLOR;
                    isError = true;
                }
                else
                    thisElement.style.backgroundColor = "";
            }
            else if (formItem.dataType == TYPE_EMAIL)
            {
                value = String(thisElement.value);
                if (value.length == 0)
                {
                    thisElement.style.backgroundColor = ERROR_COLOR;
                    isError = true;
                }
                else
                {
                	var posAt = value.indexOf("@");
                	if(posAt < 0)
                	{
	                    thisElement.style.backgroundColor = ERROR_COLOR;
	                    isError = true;
                	}
                }
                if (!isError)
                    thisElement.style.backgroundColor = "";
            }
            else if (formItem.dataType == TYPE_DATE)
            {
                value = String(thisElement.value);
                if(!isValidDate(value))
                {
                	isError = true;
					thisElement.style.backgroundColor = ERROR_COLOR;
                }
                else
                {
                	thisElement.value = formatDate(value);
                    thisElement.style.backgroundColor = "";
                }
            }
            else if (formItem.dataType == TYPE_DATE_CHILD)
            {
                value = String(thisElement.value);
                
                if(!isValidDate(value))
                {
                	isError = true;
					thisElement.style.backgroundColor = ERROR_COLOR;
                }
                else
                {
                	if(!isYearBetween(1991, 2008, value))
                	{
	                	isError = true;
						thisElement.style.backgroundColor = ERROR_COLOR;
                	}
                	else
                	{
	                	thisElement.value = formatDate(value);
                    	thisElement.style.backgroundColor = "";
                    }
                }
            }
            else if (formItem.dataType == TYPE_DATE_SENIOR)
            {
                value = String(thisElement.value);
                
                if(!isValidDate(value))
                {
                	isError = true;
					thisElement.style.backgroundColor = ERROR_COLOR;
                }
                else
                {
                	if(!isYearBetween(1800, 1943, value))
                	{
	                	isError = true;
						thisElement.style.backgroundColor = ERROR_COLOR;
                	}
                	else
                	{
	                	thisElement.value = formatDate(value);
                    	thisElement.style.backgroundColor = "";
                    }
                }
            }
            else if (formItem.dataType == TYPE_MOBILE)
            {
                value = String(thisElement.value);
                if (value.length < 11 || isNaN(value))
                {
                    thisElement.style.backgroundColor = ERROR_COLOR;
                    isError = true;
                }
                else
                    thisElement.style.backgroundColor = "";
            }
            else
            {
                alert("Invalid formItem.dataType = " + formItem.dataType);
                isError = true;
            }
        }
    }
    catch (e)
    {
        alert(e.description);
        isError = true;
    }

    if (isError)
    {
        alert("Please check the red field(s).");
        return false;
    }

    return true;
}

// Gestione Label
function Label(key, value)
{
    this.key = key;
    this.value = value;
}

function Labels()
{
	this.labelList = new Array(128);
	this.numLabels = 0;
}

Labels.prototype.add = function(key, value)
{
    if (this.numLabels >= this.labelList.length)
        alert("No more space for Label.");
    else
        this.labelList[this.numLabels++] = new Label(key, value);
}

Labels.prototype.get = function(key)
{
    for (i = 0; i < this.numLabels; i++)
    	if (this.labelList[i].key == key)
        	return this.labelList[i].value;

	return "Label " + key + " not found.";
}

var labels = new Labels();

// Gestione AJAX
function Ajax()
{
    var ajaxInitialized= false;
    this.xmlHttp = null;

    try
    {
        this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        ajaxInitialized = true;
    }
    catch (e)
    {
        try
        {
            this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            ajaxInitialized = true;
        }
        catch (e)
        {
        }
    }
    if (!ajaxInitialized && typeof XMLHttpRequest != 'undefined')
    {
        try
        {
            this.xmlHttp = new XMLHttpRequest();
            ajaxInitialized = true;
        }
        catch (e)
        {
        }
    }
    if (!ajaxInitialized)
    {
        try
        {
            this.xmlHttp = window.createRequest();
            ajaxInitialized = true;
        }
        catch (e)
        {
        }
    }

    if (!ajaxInitialized)
        alert("Unable to initialize AJAX.");
}

Ajax.prototype.getStatus = function()
{
    return this.xmlHttp.status;
}

Ajax.prototype.getData = function(url)
{
    var urlts;
    var data;

    url_ts = url;
    if (url.indexOf("?") != -1)
        url_ts += "&ts=";
    else
        url_ts += "?ts=";
    url_ts += new Date();

    try
    {
        this.xmlHttp.open(
            "GET",
            url_ts,
            false);
        this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        this.xmlHttp.send(null);

        data = this.xmlHttp.responseText;

        if (this.xmlHttp.status == 200)
            return data;
        else
        {
            alert("Unable to get data from " + url + " error " + this.xmlHttp.status);
            return null;
        }
    }
    catch (e)
    {
        alert(e.description);
    }
    finally
    {
    }
}

// Functions For Popup
function newWindow(url, myname, w , h, scroll)
{
	leftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	topPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+topPosition+',left='+leftPosition+',scrollbars='+scroll+',resizable';
	if (url.indexOf("?") != -1)
		url += "&nohistory=true";
	else 
		url += "?nohistory=true";
	var win = window.open(url, myname, settings)
	if (win.window.focus)
		win.window.focus();
}

/*
 *	FaceBook
 */
function fbs_click()
{
	u = location.href;
	t = document.title;
	window.open(
		'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,width=626,height=436');
	return false;
}

/*
 *	Del.icio.us
 */
function dls_click()
{
	u = location.href;
	t = document.title;
	window.open(
		'http://del.icio.us/post?url=' + encodeURIComponent(u) + '&title=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,height=436');
	return false;
}

/*
 *	Digg
 */
function dig_click()
{
	u = location.href;
	t = document.title;
	window.open(
		'http://digg.com/submit?phase=2&url=' + encodeURIComponent(u) + '&title=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,height=436');
	return false;
}

/*
 *	Furl
 */
function frl_click()
{
	u = location.href;
	t = document.title;
	window.open(
		'http://www.furl.net/storeIt.jsp?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,height=436');
	return false;
}

/*
 *	Reddit
 */
function rdt_click()
{
	u = location.href;
	t = document.title;
	window.open(
		'http://reddit.com/submit?u=' + encodeURIComponent(u) + '&title=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,height=436');
	return false;
}

/*
 *	Yahoo!
 */
function yho_click()
{
	u = location.href;
	t = document.title;
	window.open(
		'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,resizable=1,scrollbars=1,height=436');
	return false;
}

/*
 *	Smarking
 */
function skg_click()
{
	u = location.href;
	//t = document.title;
	window.open(
		'http://smarking.com/editbookmark/?u=' + encodeURIComponent(u), //+ '&t=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,height=436');
	return false;
}

/*
 *	Magnolia
 */
function mgl_click()
{
	u = location.href;
	t = document.title;
	window.open(
		'http://ma.gnolia.com/bookmarklet/add?url=' + encodeURIComponent(u), + '&title=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,height=436');
	return false;
}

/*
 *	Segnalo
 */
function sgl_click()
{
	u = location.href;
	t = document.title;
	window.open(
		'http://segnalo.com/post.html.php?url=' + encodeURIComponent(u), + '&title=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,height=436');
	return false;
}

/*
 *	Ok notizie
 */
function okn_click()
{
	u = location.href;
	t = document.title;
	window.open(
		'http://oknotizie.alice.it/post?url=' + encodeURIComponent(u), + '&title=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,height=436');
	return false;
}

/*
 *	Wikio
 */
function wki_click()
{
	u = location.href;
	//t = document.title;
	window.open(
		'http://www.wikio.it/vote?url=' + encodeURIComponent(u),// + '&title=' + encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,resizable=1,scrollbars=1,width=626,height=436');
	return false;
}
