

/*
 * libreria di utilità sia server che client: è presente in ogni pagina
 */

var strBaseUrl_ = "/chiamate"; // url della cartella base
var objErr_;

// converte in double una stringa; se non è numerica, converte in 0

function cDbl(strValue)
	{
	var objVal;
	return (objVal = isFloat(strValue)).is ? objVal.value : 0;
	}

// somma un dato numero di giorni alla data

function dayAdd(dtDate, iDays)
	{
	return new Date(dtDate.getTime() + iDays * 86400000);
	}

// nome del giorno della settimana

function dayName(dtDate)
	{
	switch(dtDate.getDay())
		{
		case 0: return "Domenica"; break;
		case 1: return "Lunedì"; break;
		case 2: return "Martedì"; break;
		case 3: return "Mercoledì"; break;
		case 4: return "Giovedì"; break;
		case 5: return "Venerdì"; break;
		case 6: return "Sabato"; break;
		}
	}

// descrizione delle tipologie

function desTipol(strIdTipol)
	{
	switch(uCase(trim(strIdTipol)))
		{
		case "ERG": return "Ergdis"; break;
		case "WEB": return "Web/Arch.Ott."; break;
		case "HDW": return "Hardware"; break;
		case "OTH": return "Altro"; break;
		}
	}

// restituisce il default value per un parametro di funzione

function dVal(vntVal, vntDef)
	{
	return (vntVal == undefined) ? vntDef : vntVal;
	}

// converte una stringa vuota in null

function emptyToNull(strVal)
	{
	if(trim(strVal) == "")
		return null;
	else
		return strVal;
	}

// inserisce in una stringa gli escape JS

function jsString(strVal)
	{
	var strOut;
	
	strOut = String(strVal).replace(/\\/g, "\\\\");
	strOut = strOut.replace(/\'/g, "\\'");
	strOut = strOut.replace(/\"/g, "\\\"");
	strOut = strOut.replace(/\r\n/g, "\\n");
	strOut = strOut.replace(/\n/g, "\\n");
	strOut = strOut.replace(/\r/g, "\\n");
	
	return strOut;
	}

// verifica se una stringa è composta di soli spazi

function isBlank(strValue)
	{
	return /^\s*$/.test(strValue);
	}

// verifica se una stringa è una data

function isDate(strValue)
	{
	var objPart;
	var iMax;
	var iYear;
	var iMonth;
	var iDay;
	var iHour;
	var iMinute;
	var iSecond;
	var objDate;
	
	objDate = {};
	objDate.is = false;
	
	objPart = /^(\d{1,2})\/(\d{1,2})\/(\d{1,4})(\s+(\d{1,2})([\.:](\d{1,2})([\.:](\d{1,2}))?)?)?$/.exec(strValue); // g/m/y[ h[:n[:s]]]
	if(objPart == null)
		{
		objPart = /^(\d{2})(\d{2})(\d{4})((\d{2})((\d{2})((\d{2}))?)?)?$/.exec(strValue); // ggmmyyyy[hh[nn[ss]]]
		if(objPart == null) return objDate;
		}
	
	iYear = parseInt(objPart[3], 10);
	if(iYear < 100) iYear = (( iYear > 50 ) ? iYear += 1900 : iYear += 2000);
	if(iYear < 1753 || iYear > 9999) return objDate;
	
	iMonth = parseInt(objPart[2], 10);
	if(iMonth < 1 || iMonth > 12) return objDate;
	
	iDay = parseInt(objPart[1], 10);
	iMax = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][iMonth - 1];
	if(iMonth == 2 && ((iYear % 400 == 0) || (iYear % 4 == 0) && (iYear % 100 != 0))) iMax++;
	if(iDay < 1 || iDay > iMax) return objDate;
	
	if(objPart[5] == null || objPart[5] == "")
		iHour = 0;
	else
		{
		iHour = parseInt(objPart[5], 10);
		if(iHour > 23) return objDate;
		}
	
	if(objPart[7] == null || objPart[7] == "")
		iMinute = 0;
	else
		{
		iMinute = parseInt(objPart[7], 10);
		if(iMinute > 59) return objDate;
		}
	
	if(objPart[9] == null || objPart[9] == "")
		iSecond = 0;
	else
		{
		iSecond = parseInt(objPart[9], 10);
		if(iSecond > 59) return objDate;
		}
	
	objDate.is = true;
	objDate.value = new Date(iYear, iMonth - 1, iDay, iHour, iMinute, iSecond);
	
	return objDate;
	}

// verifica se una stringa è un numero decimale

function isFloat(strValue)
	{
	var objFloat;
	
	objFloat = {};
	objFloat.is = /^\s*[\+\-]?\d+([\.\,]\d+)?\s*$/.test(strValue);

	if(!objFloat.is) // prova a controllare i separatori delle migliaia
		{
		objFloat.is = /^\s*[\+\-]?\d{1,3}(\'\d{3})*([\.\,]\d+)?\s*$/.test(strValue);
		if(objFloat.is)
			strValue = String(strValue).replace(/\'/g, "");
		}
	
	if(!objFloat.is) return objFloat;
	
	objFloat.value = parseFloat(String(strValue).replace(/\,/g, "."));
	
	return objFloat;
	}

// vero se la data è una domenica o qualche altra festività

function isHoliday(dtDay)
	{
	var iDay;
	var iMonth;
	var iYear;
	var dtDate;
	var iIdx;
	
	iDay = dtDay.getDate();
	iMonth = dtDay.getMonth() + 1;
	iYear = dtDay.getFullYear();
	
	// domenica
	
	if(dtDay.getDay() == 0)
		return true;
	
	// Pasqua e lunedì dell'angelo
	
	if(iMonth == 3 || iMonth == 4)
		{
		iIdx = (iYear + 1) % 19;
		dtDate = new Date(iYear, [2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 2, 3, 3][iIdx], [27, 14, 3, 23, 11, 31, 18, 8, 28, 16, 5, 25, 13, 2, 22, 10, 30, 17, 7][iIdx], 12, 0, 0, 0);
		dtDate.setTime(dtDate.getTime() + (7 - dtDate.getDay() + 1) * 86400000); // angel's monday
		if((dtDate.getMonth() + 1) == iMonth && dtDate.getDate() == iDay)
			return true;
		}
	
	// festività fisse
	
	switch(iMonth)
		{
		case 1:
			return (iDay == 1 || iDay == 6);
			break;
		case 4:
			return (iDay == 25);
			break;
		case 5:
			return (iDay == 1);
			break;
		case 6:
			return (iDay == 2);
			break;
		case 8:
			return (iDay == 15);
			break;
		case 11:
			return (iDay == 1);
			break;
		case 12:
			return (iDay == 8 || iDay == 25 || iDay == 26);
			break;
		default:
			return false;
			break;
		}
	}

// verifica se una stringa è un numero intero

function isLong(strValue)
	{
	var objLong;
	
	objLong = {};
	objLong.is = /^\s*[\+\-]?\d+\s*$/.test(strValue);
	
	if(!objLong.is) return objLong;
	
	objLong.value = parseInt(strValue, 10);
	
	return objLong;
	}

// verifica se una stringa è un'ora

function isTime(strValue)
	{
	var objPart;
	var iHour;
	var iMinute;
	var iSecond;
	var objTime;
	
	objTime = {};
	objTime.is = false;
	
	objPart = /^(\d{1,2})[\.:](\d{1,2})([\.:](\d{1,2}))?$/.exec(strValue); // h:n[:s]
	if(objPart == null)
		{
		objPart = /^(\d{2})(\d{2})((\d{2}))?$/.exec(strValue); // hhnn[ss]
		if(objPart == null) return objTime;
		}
	
	iHour = parseInt(objPart[1], 10);
	if(iHour > 23) return objTime;
	
	iMinute = parseInt(objPart[2], 10);
	if(iMinute > 59) return objTime;
	
	if(objPart[4] == null || objPart[4] == "")
		iSecond = 0;
	else
		{
		iSecond = parseInt(objPart[4], 10);
		if(iSecond > 59) return objTime;
		}
	
	objTime.is = true;
	objTime.value = new Date(1900, 0, 1, iHour, iMinute, iSecond);
	
	return objTime;
	}

// restituisce una stringa dd/mm/yyyy

function formatDate(dtDate)
	{
	if(dtDate instanceof Date)
		return right("0" + String(dtDate.getDate()), 2) +
			"/" + right("0" + String(dtDate.getMonth() + 1), 2) +
			"/" + dtDate.getFullYear();
	else
		return "";
	}

// restituisce una stringa "n.nnn.nnn,nnn"

function formatNumber(dblValue, iDecimals, bThSep, bZeros)
	{
	var dblBase;
	var strValue;
	var lngIdx;
	
	if(iDecimals != null)
		dblValue = round(dblValue, iDecimals);
	
	strValue = String(dblValue).replace(/\./g, ",");
	
	if(bZeros && iDecimals > 0)
		{
		lngIdx = strValue.lastIndexOf(",");
		if(lngIdx == -1)
			{
			lngIdx = iDecimals;
			strValue += ",";
			}
		else
			lngIdx = iDecimals - (strValue.length - lngIdx - 1);
		for(; lngIdx > 0; lngIdx--)
			strValue += "0";
		}
	
	if(bThSep)
		{
		lngIdx = strValue.lastIndexOf(",");
		if(lngIdx == -1) lngIdx = strValue.length;
		while( lngIdx > 3 )
			{
			strValue = strValue.substr(0, lngIdx - 3) + "'" + strValue.substr(lngIdx - 3);
			lngIdx = lngIdx - 3;
			}
		}
	
	return strValue;
	}

function formatTime(dtTime) // restituisce una stringa hh:nn:ss
	{
	if(dtTime instanceof Date)
		return right("0" + String(dtTime.getHours()), 2) + 
			":" + right("0" + String(dtTime.getMinutes()), 2) + 
			":" + right("0" + String(dtTime.getSeconds()), 2);
	else
		return "";
	}

// funzione Left del VB

function left(strValue, iLen)
	{
	strValue = String(strValue);
	if(iLen < 0) iLen = 0;
	return strValue.substr(0, iLen);
	}

// funzione Right del VB

function right(strValue, iLen)
	{
	strValue = String(strValue);
	iLen = strValue.length - iLen;
	if(iLen < 0) iLen = 0;
	return strValue.substr(iLen);
	}

// arrotonda con un dato numero di decimali

function round(dblVal, iDecimals)
	{
	var dblBase = Math.pow(10, iDecimals);
	return Math.round(dblVal * dblBase) / dblBase;
	}

// effettua RTrim del VB

function rTrim(strVal)
	{
	strVal = String(strVal).replace(/\s*$/, "");
	return strVal;
	}

// ordina un vettore di oggetti (vettore, nome proprietà x ordinamento, "+"/"-" senso ordinamento)

function sort(objVett, strProp, strOrd)
	{
	function compare(objA, objB) // >0 se objA successivo a objB
		{
		if(objA[strProp] > objB[strProp] || objA[strProp] != null && objB[strProp] == null)
			return (strOrd == "+" ? 1 : -1);
		if(objA[strProp] < objB[strProp] || objA[strProp] == null && objB[strProp] != null)
			return (strOrd == "+" ? -1 : 1);
		return 0;
		}
	
	objVett.sort(compare);
	}

// formatta una stringa per SQL

function sqlString(strValue, bSQL)
	{
	var strSQL = String(strValue).replace(/\'/g, "''");
	if(bSQL) strSQL = "'" + strSQL + "'";
	return strSQL;
	}

// accoda una stringa ad un'altra, usando un separatore

function stringAppend(strIn, strSep, strApp)
	{
	return strIn + (String(strIn) == "" ? "" : strSep) + strApp;
	}

// converte una stringa in un valore JS, intercettando anche il valore ""

function sVal(strVal, vntDef, strType)
	{
	var objVal;
	
	if(trim(strVal) == "")
		strVal = vntDef;
	else
		switch(strType)
			{
			case "date":
				objVal = isDate(trim(strVal));
				if(!objVal.is)
					throw new Error(-1 ,"(sVal) Impossibile convertire \"" + jsString(strVal) + "\" in una data.");
				strVal = objVal.value;
				break;
			case "time":
				objVal = isTime(trim(strVal));
				if(!objVal.is)
					throw new Error(-1 ,"(sVal) Impossibile convertire \"" + jsString(strVal) + "\" in un orario.");
				strVal = objVal.value;
				break;
			case "integer":
			case "long":
				objVal = isLong(trim(strVal));
				if(!objVal.is)
					throw new Error(-1 ,"(sVal) Impossibile convertire \"" + jsString(strVal) + "\" in un numero intero.");
				strVal = objVal.value;
				break;
			case "float":
			case "double":
				objVal = isFloat(trim(strVal));
				if(!objVal.is)
					throw new Error(-1 ,"(sVal) Impossibile convertire \"" + jsString(strVal) + "\" in un numero decimale.");
				strVal = objVal.value;
				break;
			case "boolean":
			case "bool":
				strVal = String(trim(strVal)).charAt(0).toUpperCase();
				strVal = strVal == "S" || strVal == "Y" || strVal == "V" || strVal == "T" || strVal == "1";
				break;
			default:
				strVal = rTrim(strVal);
				break;
			}
	
	return strVal;
	}

// converte una data in formato ISO

function toISODate(dtDate)
	{
	return "" +
		right("0000" + String(dtDate.getUTCFullYear()), 4) + "-" +
		right("00" + String(dtDate.getUTCMonth() + 1), 2) + "-" +
		right("00" + String(dtDate.getUTCDate()), 2) + "T" +
		right("00" + String(dtDate.getUTCHours()), 2) + ":" +
		right("00" + String(dtDate.getUTCMinutes()), 2) + ":" +
		right("00" + String(dtDate.getUTCSeconds()), 2) + "Z";
	}

// effettua il Trim del VB

function trim(strVal)
	{
	strVal = String(strVal).replace(/^\s*/, "");
	strVal = strVal.replace(/\s*$/, "");
	return strVal;
	}

// effettua UCase del VB

function uCase(strVal)
	{
	return String(strVal).toUpperCase();
	}

