var szNotValidSet_Generic = '|/\\[]:<>+=;,?"*';
var szNotValidSet_Advanced = szNotValidSet_Generic + " '&";

function Sprintf(szFormat, szValue)
{
	nLen = szFormat.length;
	szOutStr = "" ;

	for ( i=0 ; i<nLen ; i++ ) {
		if (szFormat.charAt (i) != '%' ) {
			szOutStr += szFormat.charAt (i) ;
		} else {
			n = i + 1
			if ( n <= nLen && szFormat.charAt (n) == 's' )
				szOutStr += szValue ;
			i = n ;
		}
	}
	return(szOutStr);
}

function Fprintf(szFormat, szValue, cEnterChar)
{
	nLen = szFormat.length;
	szOutStr = "" ;

	for ( i=0 ; i<nLen ; i++ ) {
		if (szFormat.charAt (i) != '%' ) {
			szOutStr += szFormat.charAt (i) ;
		} else {
			n = i + 1
			if (n <= nLen && szFormat.charAt (n) == cEnterChar ) 
				szOutStr += szValue ;
			else if (szFormat.charAt (n) == '%')
				szOutStr += "%";
			else
				szOutStr += "%" + szFormat.charAt (n);
			i = n ;
		}
	}
	return(szOutStr);
}

function LRTrimLower (szString)
{
	szString = szString.toLowerCase();
	for (var i = 0; i < szString.length; i++)
		if (szString.charAt(i) != ' ')
			break;		
	for (var j = szString.length - 1; j >= 0; j--)
		if (szString.charAt(j) != ' ')
			break;		
	return (szString.substring (i, j+1));
}

function PackString (szString, cCharToRemove)
{
    var inLen = szString.length;
    var szTmp = "";
    var iInit, iEnd, i;

    for (iInit = 0; iInit < inLen && szString.charAt (iInit) == cCharToRemove; 
																	iInit++);
    if (iInit < inLen) {
        for (iEnd = inLen - 1; iEnd > 0 && 
							szString.charAt (iEnd) == cCharToRemove; iEnd--);
        for (i = iInit; i <= iEnd; i++)
            szTmp += szString.charAt (i);
    }
    return (szTmp);
}

function CurrencyToNumber (theField)
{
	var inStr, re;
	
	inStr = "";
	if (theField.type == "select-one") {
        if (theField.selectedIndex != -1)
            inStr = theField.options[theField.selectedIndex].value;
    }
	else
		inStr = theField.value;

	if (!inStr.length)
		return ('');

	//re = /\./g;
	//inStr = inStr.replace (re, '.');
	re = /,/g;
	inStr = inStr.replace (re, '.');
	return (String (Math.round (parseFloat (inStr) * 100)));
}

function CheckCurrencyField (theField, FieldName, CheckNull, UseLocalAlerts, nMinValue, nMaxValue)
{
	var inStr, nValue;
	var nLen;
	var nDot = 0;
	var nColon = 0;

	if (!theField)
		return (true);

	inStr = "";
	if (theField.type == "select-one") {
        if (theField.selectedIndex != -1)
            inStr = theField.options[theField.selectedIndex].value;
    }
	else
		inStr = theField.value;

	nLen = inStr.length;
	if (CheckNull && !nLen) {
		if (UseLocalAlerts) {
			alert (Sprintf(szMsgField, FieldName));
		}
		return (false);
	}
	if (nLen) {
		for (i = 0; i < nLen; i++) {
			if (inStr.charAt (i) == '.') {
				nDot++;
			}
			else if (inStr.charAt (i) == ',') {
				if (inStr.length - i > 3)
					nColon++;
				nColon++;
			}
			else if (inStr.charAt (i) < '0' || inStr.charAt (i) > '9')
				if  (i || inStr.charAt (i) != '-')
					break;
		}
		if (i < nLen) {
			if (UseLocalAlerts) {
				alert (Sprintf(szMsgCurr, FieldName)) ;
			}
			return (false);
		}
		if (nColon > 1 || nDot > 1) {
			if (UseLocalAlerts)
				alert (Sprintf(szMsgCurr6, FieldName)) ;
			return (false);
		}
		if (nMinValue || nMaxValue) {
			var nValue = eval (inStr);
			if (nMinValue) {
				if (nValue < nMinValue) {
					if (UseLocalAlerts) {
						var strBuff = Sprintf(szMsgCurr4, FieldName) ;
						strBuff += Sprintf(szMsgCurr2, inStr) ;
						strBuff += Sprintf(szMsgCurr5, nMinValue) ;
						alert (strBuff);
					}
					if (theField.type != "hidden")
						theField.focus ();
					return (false);
				}
			}	
			if (nMaxValue) {
				if (nValue > nMaxValue) {
					if (UseLocalAlerts) {
						var strBuff = Sprintf(szMsgCurr1, FieldName) ;
						strBuff += Sprintf(szMsgCurr2, inStr) ;
						strBuff += Sprintf(szMsgCurr3, nMaxValue) ;
						alert (strBuff);
					}
					if (theField.type != "hidden")
						theField.focus ();
					return (false);
				}
			}
		}
	}
	return (true);
}


function CheckNumField (theField, FieldName, CheckNull, UseLocalAlerts, nMinValue, nMaxValue)
{
	var inStr, nValue;
	var nLen;

	if (!theField)
		return (true);

	inStr = "";
	if (theField.type == "select-one") {
        if (theField.selectedIndex != -1)
            inStr = theField.options[theField.selectedIndex].value;
    }
	else
		inStr = theField.value;

	nLen = inStr.length;
	if (CheckNull && !nLen) {
		if (UseLocalAlerts) {
			alert (Sprintf(szMsgField, FieldName));
		}
		return (false);
	}
	if (nLen) {
		for (i = 0; i < nLen; i++) {
			if (inStr.charAt (i) < '0' || inStr.charAt (i) > '9')
				if  (i || inStr.charAt (i) != '-')
					break;
		}
		if (i < nLen) {
			if (UseLocalAlerts) {
				alert (Sprintf(szMsgInt, FieldName)) ;
			}
			return (false);
		}
		if (nMinValue || nMaxValue) {
			var nValue = eval (inStr);
			if (nMinValue) {
				if (nValue < nMinValue) {
					if (UseLocalAlerts) {
						var strBuff = Sprintf(szMsgInt4, FieldName) ;
						strBuff += Sprintf(szMsgInt2, inStr) ;
						strBuff += Sprintf(szMsgInt5, nMinValue) ;
						alert (strBuff);
					}
					if (theField.type != "hidden")
						theField.focus ();
					return (false);
				}
			}	
			if (nMaxValue) {
				if (nValue > nMaxValue) {
					if (UseLocalAlerts) {
						var strBuff = Sprintf(szMsgInt1, FieldName) ;
						strBuff += Sprintf(szMsgInt2, inStr) ;
						strBuff += Sprintf(szMsgInt3, nMaxValue) ;
						alert (strBuff);
					}
					if (theField.type != "hidden")
						theField.focus ();
					return (false);
				}
			}
		}
	}
	return (true);
}

function CheckField (theField, FieldName, NotValidSet, CheckNull, UseLocalAlerts, Packing)
{
	if (!NotValidSet)
		NotValidSet = "";
	
	var inLenNVS = NotValidSet.length;
	for (var j = 0; j < inLenNVS; j++) {	
		if (NotValidSet.charAt(j) == '|')
			break;
	}
	if (j == inLenNVS)
		NotValidSet += "|";
	
	inLenNVS = NotValidSet.length;
	var inStr, inLen, Value, ValueNVS, NotValidSetToDisplay;
	
	NotValidSetToDisplay = NotValidSet;
	
	if (!theField)
		return (true);
	if (Packing)
		theField.value = PackString (theField.value, ' ');
	
	inStr = theField.value;
	inLen = inStr.length;
	if (CheckNull && inLen == 0) {
		if (UseLocalAlerts)
			alert (Sprintf(szMsgField, FieldName));
		return (false);
	}
	for (j = 0; j < inLenNVS; j++) {	
		if (NotValidSet.charAt(j) == '\n')
			break;
	}
	if (j < inLenNVS) {
		//Found Carriage Return
		var szTmp = "";
		var szTmp2 = "";
		
		if (j > 0)
				szTmp = NotValidSet.substr (0, j);  //Get substring before '\n'
		
		j++;
		if (j < inLenNVS)
				szTmp2 = NotValidSet.substr (j, inLenNVS - j);  //Get substring after '\n'
		
		szTmp+= "\\n";
		szTmp+= szTmp2;

		NotValidSetToDisplay = szTmp;
	}
	for (i = 0; i < inLen; i++) {
		Value = inStr.charAt(i);
		for (j = 0; j < inLenNVS; j++) {	
			ValueNVS = NotValidSet.charAt(j);
			if (Value == ValueNVS) {
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgContent, FieldName) ;
					szMsg += NotValidSetToDisplay;
					alert(szMsg);
				}
				return (false);
			}
		}
	}	
	return (true);
}	

function CheckSelect (theField, FieldName, CheckNull, UseLocalAlerts)
{
	//With CheckNull == false, this function does nothing
	if (!theField)
		return (true);
		
	var nLen = theField.options.length;
	if (CheckNull) {
		if (!nLen) {
			if (UseLocalAlerts) {
				if (theField.size > 1)
					alert (Sprintf(szMsgSelectMulti, FieldName));
				else
					alert (Sprintf(szMsgSelectOne, FieldName));
			}
			return (false);
		}
		else if (theField.size > 1) {
			for (var i = 0; i < theField.length; i++)
				if (theField.options[i].selected == true)
					break;
			
			if (i == theField.length) {
				if (UseLocalAlerts)
					alert (Sprintf(szMsgSelectMulti, FieldName));
				return (false);
			}
		}
		else if (theField.selectedIndex <= 0) {
			if (UseLocalAlerts)
				alert (Sprintf(szMsgSelectOne, FieldName));
			return (false);
		}
	}
	return (true);
}

function LengthControl (theField, nLength, FieldName)
{
	if (!theField)
		return (true);
	var strDigited = theField.value;
	
	if (strDigited.length > nLength) {
		var szType = theField.type;
		if (szType.indexOf ("select") != -1) {
			var strBuff = Sprintf(szMsgSelect1, FieldName);
			alert (strBuff);
		}
		else {
			var strBuff = Sprintf(szMsgField1, FieldName) ;
			strBuff += Sprintf(szMsgField2, strDigited.length) ;
			strBuff += Sprintf(szMsgField3, nLength) ;
			alert (strBuff);
		}
		if (theField.type != "hidden")
				theField.focus ();
				
		return (false);
	}
	return (true);
}

function MinLengthControl (theField, nLength, FieldName, bCheckIfNull)
{
	if (!theField)
		return (true);
	var strDigited = theField.value;
	
	if (strDigited.length < nLength) {	
		if (!bCheckIfNull && strDigited.length == 0)
			return (true);
		var strBuff = Sprintf(szMsgField4, FieldName) ;
		strBuff += Sprintf(szMsgField2, strDigited.length) ;
		strBuff += Sprintf(szMsgField5, nLength) ;
		alert (strBuff);
		if (theField.type != "hidden")
			theField.focus ();
		return (false);
	}
	return (true);
}

function CheckPhoneField (theField, FieldName, CheckNull, UseLocalAlerts)
{
	var inStr, nValue;
	var nLen;

	if (!theField)
		return (true);

	inStr = "";
	if (theField.type == "select-one") {
        if (theField.selectedIndex != -1)
            inStr = theField.options[theField.selectedIndex].value;
    }
	else
		inStr = theField.value;

	nLen = inStr.length;
	if (CheckNull && !nLen) {
		if (UseLocalAlerts) {
			alert (Sprintf(szMsgField, FieldName));
		}
		return (false);
	}
	if (nLen) {
		for (i = 0; i < nLen; i++) {
			if (inStr.charAt (i) < '0' || inStr.charAt (i) > '9')
				if  (i || inStr.charAt (i) != '+')
					break;
		}
		if (i < nLen) {
			if (UseLocalAlerts) {
				alert (Sprintf(szMsgPhone, FieldName)) ;
			}
			return (false);
		}
	}
	return (true);
}

function CheckCheckBox (theField, FieldName, CheckNull, UseLocalAlerts)
{
	if (CheckNull && !theField.value.length) {
		if (UseLocalAlerts) {
			alert (Sprintf(szMsgCheckBox, FieldName));
		}
		return (false);
	}
	return (true);
}

function CheckEmailField (theField, FieldName, CheckNull, UseLocalAlerts)
{
	var inStr, nValue;
	var nLen;

	if (!theField)
		return (true);

	inStr = theField.value;

	nLen = inStr.length;
	if (CheckNull && !nLen) {
		if (UseLocalAlerts) {
			alert (Sprintf(szMsgField, FieldName));
		}
		return (false);
	}
	if (!nLen) 
		return (true);
	var isValid = false;
	var nextStep = false;
	for (i = 0; i < nLen; i++) {
		if (nextStep || inStr.charAt (i) == '@') {
			nextStep = true;
			if  (inStr.charAt (i) == '.') {
				isValid = true;
				break;
			}
		}	
	}
	if (!isValid)
		if (UseLocalAlerts) 
			alert (Sprintf(szMsgMail, FieldName)) ;
	
	if (isValid == false)
		return (isValid);

	return (CheckField (theField, FieldName, szNotValidSet_Advanced, CheckNull, UseLocalAlerts));
}

function CheckFiscalCode (theField, FieldName, CheckNull, UseLocalAlerts)
{
	var inStr, nValue;
	var nLen;

	if (!theField)
		return (true);

	inStr = theField.value;

	nLen = inStr.length;
	if (CheckNull && !nLen) {
		if (UseLocalAlerts) {
			alert (Sprintf(szMsgField, FieldName));
		}
		return (false);
	}
	if (!nLen) 
		return (true);
	
	var isValid = false;
	if (nLen == 16) {
		//Codice fiscale
		isValid = true;
		for (i = 0; i < nLen; i++) {
			if (inStr.charAt (i) >= 'a' && inStr.charAt (i) <= 'z')
				continue;
			if (inStr.charAt (i) >= 'A' && inStr.charAt (i) <= 'Z')
				continue;
			if (inStr.charAt (i) >= '0' && inStr.charAt (i) <= '9')
				continue;
			
			isValid = false;
			break;
		}
	}
	else if (nLen == 11) {
		//Partita IVA
		isValid = true;
		for (i = 0; i < nLen; i++) {
			if (inStr.charAt (i) >= '0' && inStr.charAt (i) <= '9')
				continue;
			
			isValid = false;
			break;
		}
	}
	if (!isValid)
		if (UseLocalAlerts) 
			alert (Sprintf(szMsgGenericError, FieldName)) ;
	return (isValid);	
}

function SetCheckboxInput(theCheckbox, theInput, szOn, szOff)
{
	if (theCheckbox.checked)
		theInput.value = szOn;
	else
		theInput.value = szOff;
}

						
function SetKeep(szCheckbox)
{
	var theCheckbox = eval ("document.PSIXForm." + szCheckbox);
	if (theCheckbox)
		theCheckbox.checked = false;
}

function GetSelectValue (theField, szSeparator, bDoubleSelect)
{
	//bDoubleSelect is true if there are two select lists: SelectionList and SelectedList
	
	if (!theField)
		return ("");
		
	var szValue = "";
	
	if (theField.size == 1) {
        if (theField.selectedIndex != -1)
            szValue = theField.options[theField.selectedIndex].value;
    }
    else if (bDoubleSelect == true) {
		if (!szSeparator || !szSeparator.length)
			szSeparator = "|";
		
		for (var i = 0; i < theField.length; i++) {
			if (szValue.length)
				szValue += szSeparator;
			szValue += theField.options[i].value;
		}
    }
	else {
		if (!szSeparator || !szSeparator.length)
			szSeparator = "|";
		
		for (var i = 0; i < theField.length; i++) {
			if (theField.options[i].selected == true) {
				if (szValue.length)
					szValue += szSeparator;
				szValue += theField.options[i].value;
			}
		}
	}
	return (szValue);
}

function SetSelectValue (theField, szValue, szSeparator, objSelectedList, objSelectionList)
{
	if (!theField)
		return;
	
	if (theField.size == 1) {
		if (!szValue.length) {
			theField.options[0].selected = true;	//theField.selectedIndex = -1;
			return;
		}
		var nLen = theField.options.length;

		for (var i = 0; i < nLen; i++) {
			if (theField.options[i].value == szValue) {
				theField.options[i].selected = true;
				return;
			}
		}
    }
    else {
		theField.selectedIndex = -1;
	
		if (objSelectedList && objSelectionList) {
			//Move all elements from objSelectedList to objSelectionList
			objSelectedList.SelectAll ();
			objSelectionList.AddToList (objSelectedList, false, true);
		}
		if (szValue.length) {
			if (!szSeparator || !szSeparator.length)
				szSeparator = "|";

			var arrValue = szValue.split (szSeparator);
			var nArrayLen = arrValue.length;
			
			var nLen = theField.options.length;
			for (var i = 0; i < nLen; i++) {
				for (var j = 0; j < nArrayLen; j++) {
					if (theField.options[i].value == arrValue[j]) {
						theField.options[i].selected = true;
						break;
					}
				}
			}
			if (objSelectedList && objSelectionList)
				objSelectedList.AddToList (objSelectionList);
		}
	}
	return;
}

function CheckRadio (theField, FieldName, CheckNull, UseLocalAlerts, szMandatoryValue)
{
	if (!theField)
		return (false);
		
	var szValue = "";

	for (var i = 0; i < theField.length; i++) {
		if (theField[i].checked)
			break;
	}
	if (i < theField.length) {
		szValue = theField[i].value;
		
		if (szMandatoryValue && szMandatoryValue.length && szValue != szMandatoryValue) {
			if (UseLocalAlerts)
				alert (Sprintf(szMsgRadioMandatoryValue, FieldName));
			return (false);
		}
	}
	else if (CheckNull) {
		if (UseLocalAlerts)
			alert (Sprintf(szMsgRadio, FieldName));
		return (false);
	}
	return (true);
}

function GetRadioValue (theField)
{
	if (!theField)
		return ("");
		
	var szValue = "";

	for (var i = 0; i < theField.length; i++) {
		if (theField[i].checked)
			break;
	}
	if (i < theField.length)
		szValue = theField[i].value;
	
	return (szValue);
}

function GetCheckBoxValue (theField, szDefaultValue)
{
	if (!szDefaultValue)
		szDefaultValue = "";

	if (theField && theField.checked)
		return (theField.value);
	else
		return (szDefaultValue);
}

function GetTextValue (theField)
{
	if (!theField)
		return ("");
		
	return (theField.value);
}

function SetTextValue (theField, szValue)
{
	if (!theField)
		return;
		
	theField.value = szValue;
}

function GetTextSplitValue (theForm, theField, szSeparator)
{
	var szValue = "";
	
	if (theField) {
		if (!szSeparator || !szSeparator.length)
			szSeparator = "|";
		
		for (var i = 0; true; i++) {
			var obj = eval (theForm.name + "." + theField.name + i);
			if (!obj)
				break;
			if (i)
				szValue += szSeparator;
			szValue += obj.value;
		}
		theField.value = szValue;
	}
	return (szValue);
}

function SetTextSplitValue (theForm, theField, szValue, szSeparator)
{
	/*
	Example:
		<input type="hidden" name="tBank" />
		<input type="text" name="tBank0" />
		<input type="text" name="tBank1" />
		<input type="text" name="tBank2" />
		
		SetTextSplitValue (theForm, theForm.tBank, "Valuebank0-Valuebank1-Valuebank2", "-");
		
		The value of tBank will be: "Valuebank0-Valuebank1-Valuebank2"
	*/

	if (theField) {
		if (!szSeparator || !szSeparator.length)
			szSeparator = "|";
		
		//Reset all values
		for (var i = 0; true; i++) {
			var obj = eval (theForm.name + "." + theField.name + i);
			if (!obj)
				break;
			obj.value = "";
		}
		if (!szValue || !szValue.length) {
			theField.value = "";
			return;
		}
		var arrValue = szValue.split (szSeparator);
		var nLen = arrValue.length;
		for (var i = 0; i < nLen; i++) {
			var obj = eval (theForm.name + "." + theField.name + i);
			obj.value = arrValue[i];
		}
		theField.value = szValue;
	}		
}

function GetCheckboxSplitValue (theForm, theField, szSeparator, bByIndex)
{
	var szValue = "";
	
	if (theField) {
		if (!szSeparator || !szSeparator.length)
			szSeparator = "|";
		
		for (var i = 0; true; i++) {
			var obj = eval (theForm.name + "." + theField.name + i);
			if (!obj)
				break;

			if (bByIndex == true) {
				if (szValue.length)
					szValue += szSeparator;
				
				if (obj.checked == true)
					szValue += "1";
				else
					szValue += "0";
			}
			else if (obj.checked == true && obj.value.length) {
				if (szValue.length)
					szValue += szSeparator;
				szValue += obj.value;
			}
		}
		theField.value = szValue;
	}
	return (szValue);
}

function SetCheckboxSplitValue (theForm, theField, szValue, szSeparator, bByIndex)
{
	if (theField) {
		if (!szSeparator || !szSeparator.length)
			szSeparator = "|";
		
		var arrValue = szValue.split (szSeparator);
		var nLen = arrValue.length;
		for (var i = 0; true; i++) {
			var obj = eval (theForm.name + "." + theField.name + i);
			if (!obj)
				break;
			
			obj.checked = false;
			if (bByIndex == true) {
				if (arrValue[i] == "1")
					obj.checked = true;
			}
			else {
				for (var j = 0; j < nLen; j++) {
					if (obj.value == arrValue[j]) {
						obj.checked = true;
						break;
					}
				}
			}
		}
		theField.value = szValue;
	}
}

//
// CheckDate supported formats:
//	  
//    only number values with or whitout 0 are supported.
//	  separator	/ or -
//		
//	 szFormat only say the order:  d/m/y or dd-mm-yy are the same
//
//
//	valid dates examples:  d/m/y       ->   3/06/4 or 03/6/2004 
//						   yyyy-mm-dd  ->   2004-06-03 or 04/6/3 	
//
var SHORT_YEAR_VALUE = 2000;

function CheckDateField (theField, FieldName, CheckNull, UseLocalAlerts, szFormat, bReturnDate)
{
	if (!theField)
		return (true);
	var szVal = theField.value;
		
	if (CheckNull && !szVal.length) {
		if (UseLocalAlerts)
			alert (Sprintf(szMsgField, FieldName));
		return (false);
	}
	else if (!szVal.length)
		return true;

	var data = new Array();
	var cSeparator = '/';
	var bLongYear = true;
	var Idx1 = szFormat.indexOf ("d");
	var Idx2 = szFormat.indexOf ("m");
	var Idx3 = szFormat.indexOf ("y");
	if (szVal.indexOf(cSeparator) == -1)
		cSeparator = '-';
	if (Idx1 == -1 || Idx2 == -1 || Idx3 == -1 || szFormat.length != szVal.length) {
		alert (Sprintf(szMsgInvalidFormat, FieldName));
		return (false);
	}
	
	if (szVal.indexOf(cSeparator) == -1) {
		if (UseLocalAlerts)
			alert (Sprintf(szMsgDateField, FieldName));
		return (false);
	}
	
	dd = parseInt(szVal.substring(0, szVal.indexOf(cSeparator)),10);
	mm = parseInt(szVal.substring(szVal.indexOf(cSeparator)+1, szVal.lastIndexOf(cSeparator)),10);
	yy = parseInt(szVal.substr(szVal.lastIndexOf(cSeparator)+1),10);
	
	// sorting
	if (Idx2 < Idx1) {
		var swap = Idx1; Idx1 = Idx2; Idx2 = swap;
		swap = dd; dd = mm; mm = swap;
	}
	if (Idx3 < Idx1) {
		var swap = Idx1; Idx1 = Idx3; Idx3 = swap;
		swap = dd; dd = yy; yy = swap;
	}
	if (Idx3 < Idx2) {
		var swap = Idx2; Idx2 = Idx3; Idx3 = swap;
		swap = mm; mm = yy; yy = swap;
	}
			
	if (mm < 1 || mm > 12) {
		if (UseLocalAlerts) {
			alert (Sprintf(szMsgDateField, FieldName));
		}
		return (false);
	}
	if (dd < 1 || dd > 31) {
		if (UseLocalAlerts) {
			alert (Sprintf(szMsgDateField, FieldName));
		}
		return (false);
	}
	if((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) {
		if(dd == 31) {
			if (UseLocalAlerts)
				alert (Sprintf(szMsgDateField, FieldName));
			return (false);
		}
	}
	if ((dd == 29) && (mm == 2)) {
		if(((yy % 4) != 0) || ((yy % 400) == 0)) {
			if (UseLocalAlerts)
				alert (Sprintf(szMsgDateField, FieldName));
			return (false);
		}
	}

	if ((mm == 2) && (dd > 29)) {
		if (UseLocalAlerts)
			alert (Sprintf(szMsgDateField, FieldName));
		return (false);
	}
	if (yy < 0 || yy > 9999) {
		if (UseLocalAlerts)
			alert (Sprintf(szMsgDateField, FieldName));
		return (false);
	}
	if (yy < 100)
		yy += SHORT_YEAR_VALUE;

	if (bReturnDate)
		return (new Date(yy, mm-1, dd));

	return (true);
}

function SetFocusOnFirstInput (theForm)
{
	//Set focus on the first not hidden input
	var oInputs = theForm.getElementsByTagName("input");
	for (var i = 0; i < oInputs.length; i++) {
		if (oInputs[i].type != "hidden" && oInputs[i].disabled == false)
			break;
	}
	if (i < oInputs.length && i < 20)
		oInputs[i].focus();
}

function MyEscape (szString)
{	
	//Questa funzione permette di fare l'escape sia dei caratteri XML sia dei caratteri speciali (es à, è, ecc.).
	//E' utile soprattutto nel caso di form inputs di tipo r2b_...
	
	var szEscapedString = "";
	for (var i = 0; i < szString.length; i++) {
		var cChar = szString.charAt (i);
		switch (cChar) {
			case '&':
			case '<':
			case '>':
				szEscapedString += escape(cChar);
				break;
			default:
				szEscapedString += encodeURI (cChar);
		}
	}
	return szEscapedString;
}
