// ****************************************************
function formatNumber(price,decim)
{
	if(price != null)
	{
		//	Convert price to a string and split it.
		var priceString = "" + price;
		var priceArray = priceString.split(".");
		var decimalPart = "";

		if(priceArray.length == 1)
		{
			var integerPart = priceArray[0];
			for (i = 0 ; i < decim ; i++)
			{
				decimalPart += "0";
			}
		}
		else
		{
			var integerPart = priceArray[0];
			var decimalPart = priceArray[1];
	
			if(priceArray[1].length < decim)
			{
				for (i = priceArray[1].length ; i < decim ; i++)
				{
					decimalPart += "0";
				}
			}
			else if(priceArray[1].length > decim)
			{
				decimalPart = decimalPart.substr(0,decim);
			}
		}

		var finalIntegerPart = "";

		var splittedIntegerPart = integerPart.split("");
	
		var i = 0;
		var j = 0;
	
		for(i = (splittedIntegerPart.length - 1); i >= 0; i--, j++)
		{
			if((j % 3) == 0 && j != 0) finalIntegerPart = "." + finalIntegerPart;
			finalIntegerPart = splittedIntegerPart[i] + finalIntegerPart;
		}
		if (decim!=0)
			str2Ret = finalIntegerPart + "," + decimalPart;
		else
			str2Ret = finalIntegerPart;
			
		return(str2Ret);
	}
	else
	{
		return(null);
	}
}

// ****************************************************
function isDate(dateStr)
{
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		alert("La data inserita non è nel formato corretto.");
		return false;
	}
	
	day = matchArray[1]; // p@rse date into variables
	month = matchArray[3];
	year = matchArray[5];
	
	if (month < 1 || month > 12) { // check month range
		alert("Il mese deve essere compreso tra 1 e 12.");
		return false;
	}
	
	if (day < 1 || day > 31) {
		alert("Il giorno deve essere compreso tra 1 e 31.");
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Il mese "+month+" non ha 31 giorni!")
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("Febbraio " + year + " non ha " + day + " giorni!");
			return false;
		}
	}
	return true; // date is valid
}

// ****************************************************
function roundDownNumber(numberField,rlength) {
	var newnumber = Math.round(numberField*Math.pow(10,rlength)-0.1)/Math.pow(10,rlength);
	return newnumber;
}

// ****************************************************
function roundUpNumber(numberField,rlength) {
	var newnumber = Math.round(numberField*Math.pow(10,rlength)+0.1)/Math.pow(10,rlength);
	return newnumber;
}

// ****************************************************
function roundNumber(numberField,rlength) {
	var newnumber = Math.round(numberField*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}

// ****************************************************
function Trim(s) 
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}


//****************************************************
var popupWin;
function OpenPopup(Url, width, height, scrollbar)
{
	
	popupWin = window.open(Url,'Balocco','width=' + width + ',height=' + height + ',location=no,toolbar=no,menubar=no,status=no,resizable=yes,scrollbars=' + scrollbar);
	if (popupWin)
		popupWin.focus();
}

//****************************************************
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function $id(id){ return document.getElementById(id) }


var IE = document.all?true:false
//if (!IE) document.captureEvents(Event.MOUSEMOVE)

var tempX = 0
var tempY = 0

function getMouseXY(e)
{
	try
	{
	
    if (IE) {
        if (document.documentElement && !document.documentElement.scrollTop) {
            tempY = event.clientY + document.body.scrollTop;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            tempY = event.clientY + document.documentElement.scrollTop;
        } else if (document.body && document.body.scrollTop) {
            tempY = event.clientY + document.body.scrollTop;
        }
        tempX = event.clientX + document.body.scrollLeft;
    } else {
        tempX = e.pageX;
        tempY = e.pageY;
    }
    if (tempX < 0) {
        tempX = 0;
    }
    if (tempY < 0) {
        tempY = 0;
    }
    return true;
	}
	catch(e)
	{}
}

//document.onmousemove = getMouseXY;

// FUNCTION SOLO PER IE6 VISIBILITY HIDDEN DI TUTTE LE SELECT CHE ALTRIMENTI SI POSIZIONEREBBERO SOPRA AL SOTTO MENU
function disable_select(flag){
	if (typeof document.body.style.maxHeight == "undefined") {
	
		sel=document.getElementsByTagName("select");
		for(k=0;k<sel.length;k++) 
			sel[k].style.visibility=flag;
	}
}


function checkForm(theForm)
{
	if (theForm.email.value=="")
	{
		alert("Inserire l'email");
		return false;
	}
	
	if (!IsEmailValid(theForm.email.value))
	{
		alert("Controlla l'indirizzo email inserito");
		return false;
	}
	return true;
}
