function CreateXmlHttpReq(pByRefObject, handler)
{
	pByRefObject = null;
	try 
	{
		pByRefObject = new XMLHttpRequest();
	} 
	catch(e)
	{
		try 
		{
			pByRefObject = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e)
		{
			pByRefObject = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	pByRefObject.onreadystatechange = handler;
	return pByRefObject;
}



function ExecuteJavascriptResponseCode()
{
	HideWaitIcon();
	eval(document.getElementById("JavascriptResponseCode").innerHTML);
	document.getElementById("JavascriptResponseCode").innerHTML = "";
	document.getElementById("PopupActions").src = "void.asp";
	document.getElementById("PopupActions").style.display = "none";
}
		


var m_CurrentExpandedBoxId = 0;


function OpenBox(pBoxId, pBoxFirstTag)
{
	var lDiv = document.createElement("DIV");
	var lFunctionName = "Box" + pBoxId + "_OnExpand";
	var lExpandedBox = document.getElementById("divExpandedBoxContent");
	
	lDiv.id = "TEMP___DIV";
	lDiv.innerHTML = "&nbsp;";
	lDiv.style.display = "block";
	pBoxFirstTag.parentNode.appendChild(lDiv);
	pBoxFirstTag.parentNode.removeChild(pBoxFirstTag);

	lExpandedBox.appendChild(pBoxFirstTag);
	lExpandedBox = document.getElementById("divExpandedBox");

	lExpandedBox.style.display = "block";
	m_CurrentExpandedBoxId = pBoxId;
	
	if(eval("typeof(" + lFunctionName + ")")!="undefined")
		eval(lFunctionName + "();");
}

function CloseBox()
{
	var lDiv = document.getElementById("TEMP___DIV");
	if(lDiv!=null)
	{
		var lBox = lDiv.parentNode;
		lBox.removeChild(lDiv);
		var lExpandedBoxContent = document.getElementById("divExpandedBoxContent").firstChild;
		document.getElementById("divExpandedBoxContent").removeChild(lExpandedBoxContent);
		lBox.appendChild(lExpandedBoxContent);
		document.getElementById("divExpandedBox").style.display = "none";
		var lFunctionName = "Box" + m_CurrentExpandedBoxId + "_OnCollapse";
		if(eval("typeof(" + lFunctionName + ")")!="undefined")
			eval(lFunctionName + "();");
		m_CurrentExpandedBoxId = 0;
	}
}

function ViewAlert(pMessage)
{
	alert(pMessage);
}

function CloseMessageBox()
{
	var lDiv = document.getElementById("MessageBox");
	if(lDiv!=null)
		lDiv.style.display = "none";
}

function ViewMessage(pTitle, pBody)
{
	if(typeof(ViewMessageCustom)!='undefined')
		return ViewMessageCustom(pTitle, pBody);
		
	var lAddToBody = false;
	var lDiv = document.getElementById("MessageBox");
	var lMessage = "";
	var lCenter = null;
	
	if(lDiv==null)
	{
		lAddToBody = true;
		lDiv = document.createElement("div");
		lDiv.id = "MessageBox";
		lDiv.style.backgroundColor = "#ffffe3";
		lDiv.style.color = "black";
		lDiv.style.borderWidth = "2px";
		lDiv.style.borderStyle = "solid";
		lDiv.style.borderColor = "silver";
		
		lDiv.style.width = "300px";
		lDiv.style.height = "110px";
	}
	eval("lCenter = window.center({width:" + parseInt(lDiv.style.width) + ",height:" + parseInt(lDiv.style.height) + "});");
	lDiv.style.top = lCenter.y + "px";
	lDiv.style.left = lCenter.x + "px";
	lDiv.style.position = "absolute";
	
	if(lAddToBody==true)
		document.body.appendChild(lDiv);
	lDiv.style.display = "block";
	if(document.getElementById("MessageBox_Title")!=null)
		document.getElementById("MessageBox_Title").innerHTML = pTitle;
	else
		lMessage = "<div id='MessageBox_Title' style='color:black;border-bottom:dotted 1px silver;padding:3px; width:100%; height:15px; font-weight:bold;'>" + pTitle + "</div>";
	
	if(document.getElementById("MessageBox_Body")!=null)
		document.getElementById("MessageBox_Body").innerHTML = pBody;
	else
		lMessage = lMessage + "<div id='MessageBox_Body' style='color:black;width:100%; text-align:center; height:70px;padding:3px;'>" + pBody + "</div>";
		
	if(document.getElementById("MessageBox_Control")==null)
		lMessage = lMessage + "<div id='MessageBox_Control' style='color:black;width:100%; padding:3px; height:15px; text-align:right;'><a href='javascript:CloseMessageBox();'>Chiudi</a></div>";
	
	if(lMessage != "")
		lDiv.innerHTML = lMessage;
		
}
function FindPage(pName, pParams)
{
	var lParams = "";
	if(pParams!=null)
		lParams = "&" + pParams;
	document.location = "index.asp?pagename=" + pName + lParams;
}
function ShowWaitIcon()
{
	var lWaitTag = document.getElementById("WaitTag");
	if(lWaitTag!=null)
		lWaitTag.style.display = "block";
}
function HideWaitIcon()
{
	var lWaitTag = document.getElementById("WaitTag");
	if(lWaitTag!=null)
		lWaitTag.style.display = "none";
}

function ViewPage(pId, pParams)
{
	document.forms[0].scrolltop.value = document.body.scrollTop;
	document.forms[0].oldpage.value = document.forms[0].page.value;
	document.forms[0].page.value = pId;
	var lParams = "";
	if(pParams!=null)
		lParams = "&" + pParams;
	//document.location = "index.asp?page=" + pId + lParams;
	SubmitForm(null, "index.asp", "");
}

function SubmitForm2(pActionCode, pPageId)
{
	ShowWaitIcon();
	document.forms[0].scrolltop.value = document.body.scrollTop;
	document.forms[0].oldpage.value = document.forms[0].page.value;
	if(pActionCode!=null)
		SetAction(pActionCode);
	document.forms[0].page.value = pPageId;
	document.forms[0].action = "index.asp";
	document.forms[0].method = "get";
	document.forms[0].submit();
}
var m_SubmitFormActionCode = "";
var m_SubmitFormTarget = "";
var m_SubmitFormTimerId = 0;


function SubmitFormToPopup(pActionCode, pDestinationUrl, pMessage, pPostBackFunction)
{
	var lIframe = CreatePopupAction(pMessage);
	document.forms[0].popupactionurl.value = pDestinationUrl;
	if(typeof(pPostBackFunction)!='undefined')
		document.forms[0].popupactionpostbackfunction.value = pPostBackFunction;
	else
		document.forms[0].popupactionpostbackfunction.value = "";
	
	m_SubmitFormActionCode = pActionCode
	m_SubmitFormTarget = lIframe.name;
	lIframe.style.display = "block";
	SubmitForm(m_SubmitFormActionCode, "popupactions.asp", m_SubmitFormTarget);
	m_SubmitFormActionCode = "";
	m_SubmitFormTarget = "";
}

function SubmitForm(pActionCode, pDestinationUrl, pDestinationTarget, pDestinationPlugin)
{
	ShowWaitIcon();
	document.forms[0].scrolltop.value = document.body.scrollTop;
	document.forms[0].oldpage.value = document.forms[0].page.value;
	var lDestinationUrl = document.forms[0].action;
	var lDestinationTarget = document.forms[0].target;
	var lDestinationPlugin = "";
	var lOriginalTarget = lDestinationTarget;
	var lOriginalUrl = lDestinationUrl;
	
	if(typeof(pDestinationUrl)!='undefined')
		if(pDestinationUrl!=null)
			lDestinationUrl = pDestinationUrl;
	if(typeof(pDestinationTarget)!='undefined')
		if(pDestinationTarget!=null)
			lDestinationTarget = pDestinationTarget;
	if(typeof(pDestinationPlugin)!='undefined')
		if(pDestinationPlugin!=null)
			lDestinationPlugin = pDestinationPlugin;

	SetAction(pActionCode);
	
	if(lDestinationUrl!="")
		document.forms[0].action = lDestinationUrl;
	if(lDestinationTarget!="")
		document.forms[0].target = lDestinationTarget;
	document.forms[0].destinationplugin.value = lDestinationPlugin;
	document.forms[0].submit();
	document.forms[0].action = lOriginalUrl;
	document.forms[0].target = lOriginalTarget;
}


window.size = function()
{
	var w = 0;
	var h = 0;
	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}
window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;
	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}
	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY - 100;
	return{x:_x,y:_y};
}

function CreatePopupAction(pMessage)
{
	var lAddToBody = false;
	var retVal = document.getElementById("PopupActions")
	if(retVal==null)
	{
		lAddToBody = true;
		retVal = document.createElement("iframe");
		retVal.id = "PopupActions";
		retVal.name = retVal.id;
	}
	retVal.src = "wait.asp?message=" + pMessage;
	retVal.style.position = "absolute";
	var lCenter = window.center({width:300,height:50});
	retVal.style.top = lCenter.y + "px";
	retVal.style.left = lCenter.x + "px";
	retVal.style.width = "300px";
	retVal.style.height = "100px";
	if(lAddToBody==true)
		document.body.appendChild(retVal);
	//retVal.style.display = "block";
	return retVal;
}

function SetAction(pActionCode)
{
	document.forms[0].act.value = pActionCode;
}

function trim(pValue)
{
	var retVal = pValue;
	while (retVal.substring(0,1) == ' ')
		retVal = retVal.substring(1, retVal.length);

	while (retVal.substring(retVal.length - 1, retVal.length) == ' ')
		retVal = retVal.substring(0, retVal.length-1);

	return retVal;
}

function CheckEmail(pValue)
{
	regexp = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+[.][A-Za-z]{2,6}$/;
	return regexp.test(pValue);
}


var m_CookieKeys = new Array();
var m_CookieValues = new Array();

function LoadCookies()
{
	var lList = document.cookie.split(";");
	var lItem;
	var lKey = new String();
	var i = 0;
	m_CookieKeys = new Array();
	m_CookieValues = new Array();
	for(i=0; i<lList.length; i++)
	{
		lItem = lList[i].split("=");
		if(lList[0]!="")
		{
			lKey = lItem[0];
			if(lKey.substr(0, 1)== " ")
				lKey = lKey.substr(1, lKey.length - 1);
			m_CookieKeys[m_CookieKeys.length] = lKey;
			if(typeof(lItem[1])!="undefined")
				m_CookieValues[m_CookieValues.length] = lItem[1];
			else
				m_CookieValues[m_CookieValues.length] = "";
		}
	}
}

function SaveCookie()
{

}

function GetCookieIndex(pKey)
{
	var i = 0;
	for(i=0; i<m_CookieKeys.length; i++)
		if(m_CookieKeys[i]==pKey)
			return i;
	return -1;
}

function GetCookie(pKey)
{
	var retVal = "";
	var lIndex = GetCookieIndex(pKey);
	if(lIndex!=-1)
		retVal = m_CookieValues[lIndex];
	return retVal;  
}

function SetCookie(pKey, pValue)
{
	var lIndex = GetCookieIndex(pKey);
	if(lIndex>-1)
		m_CookieValues[lIndex] = pValue;
	else
		AddCookie(pKey, pValue); 
}

function AddCookie(pKey, pValue)
{
	m_CookieKeys[m_CookieKeys.length + 1] = pKey;
	m_CookieValues[m_CookieValues.length + 1] = pValue;
}
