// JavaScript Document
function Greeting()
{
	now = new Date;

	if(now.getHours() < 12) {
		document.write("Buon giorno");
	}
	else if(now.getHours() < 17) {
		document.write("Buon pomeriggio");
	}
	else {
		document.write("Buona sera");
	}
}

function randomString(argLength, charSet) {
	switch(charSet)
	{
		case 'lCaseCharsOnly': pool = new String("abcdefghijklmnopqrstuvwxyz"); break;
		case 'uCaseCharsOnly': pool = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); break;
		case 'lCaseCharsAndDigs': pool = new String("abcdefghijklmnopqrstuvwxyz0123456789"); break;
		case 'uCaseCharsAndDigs': pool = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); break;
		default: pool = new String("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	}

	var i = 0;
	var passwd = " ";

	while (i <= argLength) {
		rand = parseFloat(Math.random()) * parseInt(pool.length);
		passwd += pool.charAt(rand);
		i++;
	}
		
	passwd = passwd.substring(1, 8);
	return passwd
}

function pause(numberMillis) {
		var now = new Date();
		var exitTime = now.getTime() + numberMillis;
		while (true) {
				now = new Date();
				if (now.getTime() > exitTime)
						return;
		}
}

function CheckCF(cf, quite) 
{ 
	var valid, i, s, set1, set2, setpari, setdisp; 
	if(cf == '') 
		return false; 
	cf = cf.toUpperCase(); 
	if(cf.length != 16) 
	{
		if(quite != true) alert('Il codice fiscale deve essere lungo 16 caratteri.');
		return false; 
	}
	valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
	for(i = 0; i < 16; i++)
	{ 
		if(valid.indexOf(cf.charAt(i)) == -1) 
		{
			alert('Il codice fiscale contiene un carattere non valido (' + cf.charAt(i) + ').'); 
			return false;
		}	
	} 
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX"; 
	s = 0; 
	for(i = 1; i <= 13; i += 2) 
		s += setpari.indexOf(set2.charAt(set1.indexOf(cf.charAt(i)))); 
	for(i = 0; i <= 14; i += 2) 
		s += setdisp.indexOf(set2.charAt(set1.indexOf(cf.charAt(i)))); 
	if(s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0)) 
	{
		if(quite != true) alert('Il codice fiscale non è corretto.');
		return false; 
	}
	return true; 
}

function CheckPIVA(pi, quite) 
{ 
	if(pi == '') return false; 
	if( pi.length != 11 ) 
	{
		if(quite != true) 
		{
			if(confirm('La partita IVA deve essere lunga 11 caratteri. Vuoi proseguire ugualmente?'))
				return true;
			else
				return false; 
		}
		else
			return false;
	}
	valid = "0123456789"; 
	for(i = 0; i < 11; i++)
	{ 
		if(valid.indexOf(pi.charAt(i)) == -1) 
		{
			if(quite != true) alert('La partita IVA contiene un carattere non valido (' + pi.charAt(i) + ').');
			return false;
		}
	} 
	s = 0; 
	for(i = 0; i <= 9; i += 2) 
		s += pi.charCodeAt(i) - '0'.charCodeAt(0); 
	for(i = 1; i <= 9; i += 2)
	{ 
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) ); 
		if(c > 9) c = c - 9; s += c; 
	} 
	if((10 - s%10)%10 != pi.charCodeAt(10) - '0'.charCodeAt(0)) 
	{
		if(quite != true) 
		{
			if(confirm('La partita IVA non e\' valida. Vuoi proseguire ugualmente?'))
				return true;
			else
				return false;
		}
		else
			return false;
	}
	return true; 
}  

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function forceFieldUpperCase(fieldObj)
{
	fieldObj.value = fieldObj.value.toUpperCase().replace(/([^0-9A-Z])/g,"");
}

function forceFieldLowerCase(fieldObj)
{
	fieldObj.value = fieldObj.value.toLowerCase();
}

function setFieldUpperCaseFirst(fieldObj)
{
	if(fieldObj.value == fieldObj.value.toUpperCase() || fieldObj.value == fieldObj.value.toLowerCase())
	{
		str = fieldObj.value.toLowerCase();
		fieldObj.value = str.substr(0, 1).toUpperCase() + str.substr(1);
	}
}

function checkFieldForInvalidChars(fieldObj, strInvsalidChrs, bShowMsg)
{
	/*
	for(i = 0; i < strInvsalidChrs.length(); i++)
	{
		while(
	}
	*/
	return false;
}