//Version: 1.00, 10/03/2007
//Version: 1.01, 03/04/2007: Bugs in SetClass
//Version: 1.02, 25/07/2007: Aggiunta funzione function GetElementsName
//Version: 1.03, 31/07/2007: Aggiunta funzione DropDownListGetChoice
//Version: 1.04, 31/01/2008: Aggiunta funzione Trim
//Version: 1.05, 22/07/2009: Fix per firefox

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var attrValueSeparator = ":";
var attrSeparator = " ";
var DebugArray = new Array();

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Ajax(strURL, fnCallback, strOverrideMime, strMethod, objCaller) {
	//Properties
	this.m_URL = strURL;
	this.m_Callback = fnCallback;
	this.m_Request = null;
	this.m_OverrideMime = strOverrideMime;
	this.m_Method = strMethod;
	this.m_Caller = objCaller;

	//Methods
	this.Get = Get;
	//Create instance of request object
	if (window.XMLHttpRequest) {
		this.m_Request = new XMLHttpRequest();
		if (this.m_Request.overrideMimeType && this.m_OverrideMime != "") {
			this.m_Request.overrideMimeType(this.m_OverrideMime);
		}
	}
	else if (window.ActiveXObject) {
		try {
			this.m_Request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.m_Request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	}
	var objRequest = this.m_Request;
	//Set the callback function for the request
	this.m_Request.onreadystatechange = function () { fnCallback(objRequest, objCaller) };
}
function Get(isAsync) {
	//Open the connection
	if (isAsync == null)
		isAsync = true;
	this.m_Request.open(this.m_Method, this.m_URL, isAsync);
	//Send the request
	this.m_Request.send(null);
	//Se il browser è Firefox e la chiamata è sincrona bisogna chiamare esplicitamente la callback
	if (navigator.appName == 'Netscape' && !isAsync)
		this.m_Callback(this.m_Request, this.m_Caller)
}

function GetNodeText(objXMLNode) {
	var strText = (objXMLNode.nodeValue ? objXMLNode.nodeValue : "");
	for (var i = 0; i < objXMLNode.childNodes.length; ++i) {
		var objChildNode = objXMLNode.childNodes[i];
		//Concateniamo eventuali CDATA o TEXT
		if (objChildNode.nodeType == 3 || objChildNode.nodeType == 4)
			strText += GetNodeText(objChildNode);
	}

	return strText;
}
function GetNodeID(objNode) {
	//No ID if no node
	if (!objNode)
		return "";
	var strNodeID = GetAttribute(objNode, "ID");
	return strNodeID;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var objStartupScripts = new StartupScripts()
function StartupScripts() {
	this.m_Scripts = new Array();
	//RunStartupScripts();
}


if (navigator.appName.search("Internet Explorer") > -1)
	document.onreadystatechange = RunStartupScripts;
else if (addEventListener != undefined)
	addEventListener('DOMContentLoaded', RunStartupScripts, false);
else
	RunStartupScripts();

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function RunStartupScripts() {
	//Run the startupscripts
	for (var i = 0; i < objStartupScripts.m_Scripts.length; ++i) {
		DebugWrite(document, "Running startup script");
		eval(objStartupScripts.m_Scripts[i]);
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Different approach for getting at objects NS/IE
function GetElement(strName) {
	return document.getElementById(strName);
}
function GetElementsName(strName) {
	return document.getElementsByName(strName);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var emptyElements = {
	HR: true, BR: true, IMG: true, INPUT: true
};
var specialElements = {
	TEXTAREA: true
};

function getOuterHTML(node) {
	var html = '';
	switch (node.nodeType) {
		case 1: //Node.ELEMENT_NODE:
			html += '<';
			html += node.nodeName;
			if (!specialElements[node.nodeName]) {
				for (var a = 0; a < node.attributes.length; a++) {
					if (node.attributes[a].nodeValue != null && node.attributes[a].nodeValue != "" && node.attributes[a].nodeName != "contentEditable")
						html += ' ' + node.attributes[a].nodeName +
                  '="' + node.attributes[a].nodeValue + '"';
				}
				html += '>';
				if (!emptyElements[node.nodeName]) {
					html += node.innerHTML;
					html += '<\/' + node.nodeName + '>';
				}
			}
			else switch (node.nodeName) {
				case 'TEXTAREA':
					for (var a = 0; a < node.attributes.length; a++)
						if (node.attributes[a].nodeName.toLowerCase() != 'value' && node.attributes[a].nodeValue != null && node.attributes[a].nodeValue != "")
							html += ' ' + node.attributes[a].nodeName +
                      '="' + node.attributes[a].nodeValue + '"';
						else
							var content = node.attributes[a].nodeValue;
					html += '>';
					html += content;
					html += '<\/' + node.nodeName + '>';
					break;
			}
			break;
		case 3: //Node.TEXT_NODE:
			html += node.nodeValue;
			break;
		case 8: //Node.COMMENT_NODE:
			html += '<!' + '--' + node.nodeValue + '--' + '>';
			break;
	}
	return html;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function SetAttribute(objObject, strAttribute, strValue) {
	//Do we have the attribute
	if (objObject.className == undefined)
		return null;
	//Necessarya FF strips leading space from input
	var strClassName = "";
	//Do we have the attribute
	var objAttributes = objObject.className.split(attrSeparator);
	var bFound = false;
	//It's there, we must change it
	for (var i = 0; i < objAttributes.length; ++i) {
		if (objAttributes[i].indexOf(strAttribute + attrValueSeparator) > -1) {
			//Set the new element
			objAttributes[i] = strAttribute + attrValueSeparator + strValue;
			bFound = true;
			break;
		}
	}
	//No attribute found?
	if (!bFound) {
		//Set the attribute
		if (objObject.className != "" && objObject.className != null)
			strClassName += attrSeparator;
		strClassName += strAttribute + attrValueSeparator + strValue;
		objObject.className += strClassName;
	} else {
		//Rebuild the class string
		objObject.className = "";
		for (var i = 0; i < objAttributes.length; ++i) {
			if (objAttributes[i] != "" && objAttributes[i] != null) {
				if (strClassName != "" && strClassName != null)
					strClassName += attrSeparator;
				strClassName += objAttributes[i];
			}
		}
		objObject.className = strClassName;
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function GetAttribute(objObject, strAttribute) {
	//Do we have the attribute
	if (!objObject)
		return null;
	if (objObject.className == undefined)
		return null;

	var objAttributes = objObject.className.split(attrSeparator);
	//It's there, we must get it
	for (var i = 0; i < objAttributes.length; ++i) {
		var objAttribute = objAttributes[i].split(attrValueSeparator);
		if (objAttribute[0] == strAttribute)
			return objAttribute[1];
	}
	return null;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function CopyAllAttributes(ElementDst, ElementSrc) {
	if (!ElementSrc)
		return null;
	if (ElementSrc.className == undefined)
		return null;

	var arrAttributi = ElementSrc.className.split(attrSeparator);
	for (var iatt = 0; iatt < arrAttributi.length; iatt++) {
		var arrKeyValue = arrAttributi[iatt].split(':');
		SetAttribute(ElementDst, arrKeyValue[0], arrKeyValue[1]);
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function GetControlsByAttribute(objRoot, strAttribute, strValue, objArray) {
	//Do we have a root
	if (!objRoot)
		objRoot = document;
	//Do we have an array
	if (objArray == null)
		objArray = new Array();
	for (var i = 0; i < objRoot.childNodes.length; ++i) {
		var childNode = objRoot.childNodes[i];
		var strAttributeValue = GetAttribute(childNode, strAttribute);
		//Do we have the attribute?
		if (strAttributeValue != null) {
			//Do we have an attribute filter
			if (strValue == null)
				objArray.push(childNode);
			else {
				if (strValue == strAttributeValue)
					objArray.push(childNode);
			}
		}
		//Go for the child nodes
		GetControlsByAttribute(childNode, strAttribute, strValue, objArray);

	}

	return objArray;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function SetClass(objObject, strClass) {

	//DebugWritePerf("SetClass");
	//Do we have the attribute
	if (objObject.className == undefined) {
		//DebugWritePerf("SetClass");
		return null;
	}

	var classAttribute = "CSSclass";

	//Rimuoviano la classe attuale
	var strOriginalClass = GetAttribute(objObject, classAttribute);
	//If the old class name is the same then quit
	if (strOriginalClass != null && strOriginalClass.toLowerCase() == strClass.toLowerCase()) {
		//DebugWritePerf("SetClass");
		return;
	}

	SetAttribute(objObject, classAttribute, strClass);

	var strClassName = objObject.className;

	strClassName = strClassName.replace(eval("/" + strOriginalClass + "/i"), "")

	objObject.className = strClass + attrSeparator + strClassName;
	//DebugWritePerf("SetClass");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function DebugWritePerf(KeySaveTime) {
	DebugWrite('tmp', "DebugWritePerf:", KeySaveTime);
}
function DebugWrite(object, message, KeySaveTime) {
	//disattiva il debug
	return;

	//attiva il debug solo delle perfomance
	if (typeof (KeySaveTime) == 'undefined') return;

	if (!object) return;
	//Get the debug window
	var debugWindow = GetElement("DebugWindow");

	if (debugWindow) {

		if (typeof (object.id) != 'undefined') {
			debugWindow.appendChild(document.createTextNode(object.id + ': ' + message));
			debugWindow.appendChild(document.createElement('BR'));
			debugWindow.scrollTop += 50;
		}
		if (typeof (KeySaveTime) != 'undefined') {
			var KeyTime = GetKeyTime(KeySaveTime)
			if (KeyTime != null) {
				debugWindow.appendChild(document.createTextNode(KeyTime.key + ': ' + KeyTime.timeelapsed));
				debugWindow.appendChild(document.createElement('BR'));
				debugWindow.scrollTop += 50;
			}
			else
				InsertKeyTime(KeySaveTime);
		}
	}
}

function SearchKeyTime(strKey) {
	for (var i = 0; i < DebugArray.length; i++) {
		if (DebugArray[i].key == strKey)
			return DebugArray[i];
	}
	return null;
}

function InsertKeyTime(strKey) {
	var tmpTimestamp = new timestamp();
	DebugArray.push({ key: strKey, timeelapsed: tmpTimestamp });
}

function GetKeyTime(strKey) {
	var tmpTimestamp = new timestamp();
	var KeyTime = SearchKeyTime(strKey);
	if (KeyTime != null) {
		RemoveKeyTime(strKey);
		return { key: KeyTime.key, timeelapsed: (((tmpTimestamp.minutes - KeyTime.timeelapsed.minutes) * 60000) + ((tmpTimestamp.seconds - KeyTime.timeelapsed.seconds) * 1000) + (tmpTimestamp.milliseconds - KeyTime.timeelapsed.milliseconds)) }
	}
	else
		return null;
}

function RemoveKeyTime(strKey) {
	var tmpArray = new Array();
	for (var i = 0; i < DebugArray.length; i++) {
		if (DebugArray[i].key != strKey)
			tmpArray.push(DebugArray[i]);
	}
	DebugArray = tmpArray;
}

function timestamp() {
	this.minutes = new Date().getMinutes();
	this.seconds = new Date().getSeconds();
	this.milliseconds = new Date().getMilliseconds();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function DropDownListGetChoice(object) {
	for (var i = 0; i < object.length; i++) {
		if (object.options[i].selected == true) {
			return object.options[i].index
		}
	}
	return null
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function Trim(stringa, carattere) {
	while (stringa.substring(0, 1) == carattere) {
		stringa = stringa.substring(1, stringa.length);
	}
	while (stringa.substring(stringa.length - 1, stringa.length) == carattere) {
		stringa = stringa.substring(0, stringa.length - 1);
	}
	return stringa;
}
function parseBool(strValue) {
	if (strValue.toLowerCase() == 'false')
		return false
	if (strValue.toLowerCase() == 'true')
		return true

	return null;
}

var Utf8 = {

	// public method for url encoding
	encode: function (string) {
		string = string.replace(/\r\n/g, "\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if ((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// public method for url decoding
	decode: function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while (i < utftext.length) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if ((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i + 2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function AddMultipleItems(Values, CurrentValue) {
	if (Values.length == 0)
		Values = CurrentValue;
	else {
		Values = Values + "," + CurrentValue;
	}
	return Values;
}
function DeleteMultipleItems(Values, CurrentValue) {
	Values = Values.replace(CurrentValue, '');
	Values = Values.replace(',,', ',');
	if (Values.substring(Values.length - 1, Values.length) == ',')
		Values = Values.substring(0, Values.length - 1)
	return Values;
}

function FireEventHandlers(objItem, strEvent) {
	//Fire the submit object
    if (document.createEventObject) {
        if (event) {
            var evt = document.createEventObject(event);
            objItem.fireEvent("on" + strEvent, evt);
        }
        else if (window.top.event) {
            var evt = document.createEventObject(window.top.event);
            objItem.fireEvent("on" + strEvent, evt);
        }
	}
	else {
		var evt = document.createEvent("Events");
		evt.initEvent(strEvent, true, true);
		objItem.dispatchEvent(evt);
	}
}

function RegisterEvent(objDOM, strEvent, fnHandler) {
  if (objDOM.attachEvent)
    objDOM.attachEvent("on" + strEvent, fnHandler);
	else
    objDOM.addEventListener(strEvent, fnHandler, false);		
}
