// JScript source code

function getXMLDoc(xml) {

	var xmlDoc;
	if(document.implementation && document.implementation.createDocument) { 
		// MOZILLA 
       	xmlDoc = document.implementation.createDocument("", "", null);
	}
	else if (window.ActiveXObject) {
    	//IE
    	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	xmlDoc.async = "false";
	xml = xml.replace(/#/g,"");
	var xmlOK = xmlDoc.loadXML(xml);
	var rootDoc = xmlDoc.documentElement;

	if (!xmlOK && rootDoc==null) {
	// errorCode:	Returns a long integer error code
	// reason:		Returns a string containing the reason for the error
	// line:		Returns a long integer representing the line number for the error
	// linepos:		Returns a long integer representing the line position for the error
	// srcText:		Returns a string containing the line that caused the error
	// url:			Returns the URL pointing the loaded document
	// filepos:		Returns a long integer file position of the error

		var errMsg = "Errore nel parsing della risposta AXL:\n";
		errMsg+="Codice errore: " + xmlDoc.parseError.errorCode + "\n";
		errMsg+="Descrizione: " + xmlDoc.parseError.reason + "\n";
		errMsg+="Linea: " + xmlDoc.parseError.line + "\n";
		errMsg+="Posizione: " + xmlDoc.parseError.linepos + "\n";
		errMsg+="Stringa contenente l'errore:\n" + xmlDoc.parseError.srcText + "\n";
		errMsg+="Url del documento:\n" + xmlDoc.parseError.url + "\n";
		errMsg+="Posizione nel file:\n" + xmlDoc.parseError.url + "\n";
				
		alert(errMsg);
		hideLayer('loading');
		return null;
	}
	else {
		return xmlDoc;
	}
	
}

function getWindowSize() {
	var windowWidth = 0;
	var windowHeight = 0;

	if (typeof(window.innerWidth) == 'number' && typeof(window.innerHeight) == 'number') {
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientWidth && document.body.clientHeight) {
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
		}
	}
	
	var windowSize = new Array;
	windowSize[0] = windowWidth;
	windowSize[1] = windowHeight;
	return windowSize;
}
			
			
			
			
function findPos(obj) {
	if (obj == null) return;
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function getStyle(el,styleProp) {
	var x = document.getElementById(el);
	if (x.currentStyle) {
		var y = x.currentStyle[styleProp];
	}
	else if (window.getComputedStyle) {
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	}
	return y;
}

//	===========================================================
//	***********************************************************
//	Funzioni di validazione
//	***********************************************************
//	Questa funzione controlla che l'input contenga caratteri:
function isBlank(s) {
	for(i=0;i<s.length;i++) {
		c=s.charAt(i);
        if((c!=' ')&&(c!='\n')&&(c!='\t'))return false;
	}
    return true;
}
//	Questa funzione invece verifica che l'input sia un intero valido
function isValidInt(s) {
	var i, c;

    for (i=0; i<s.length; i++)  {
		c = s.charCodeAt(i);
        if ((c < 48) || (c > 57)) return false;
	}
    return true;
}

function lTrim(str)	{	return (new String(str)).replace(/^\s*/, "");	}
function rTrim(str)	{	return (new String(str)).replace(/\s*$/, "");	}
function trim(str)	{	return lTrim(rTrim(new String(str)));	}


function autoComplete (field, select, property, forcematch) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
	if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
		found=true; break;
		}
	}
	if (found) { select.selectedIndex = i; }
	else { select.selectedIndex = -1; }
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1); 
			return;
			}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
				}
			}
		}
	}
	
function clearOptions(SelectBoxId) {
	var selbox = document.getElementById(SelectBoxId);
	for (var i = selbox.options.length-1; i>=0; i--) {
		selbox.options[i] = null;
	}
	selbox.selectedIndex = -1;
}

function sortOptionText(a, b) { 
    if (a.text < b.text) return -1; 
    else if (a.text > b.text) return 1; 
    else return 0; 
}

function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
};

function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
}


function formatNumber(theNumber,theDecimals) {
	var p = Math.pow(10,theDecimals);
	return Math.round(theNumber*p)/p;
}


function handleEnter() {
	return !(window.event && window.event.keyCode == 13); 
 }
