function changeClass(obj,className)
{ 
	obj.className = className;
}

function HideLayer(id)
{
        elem=document.getElementById(id);
        elem.style.display="none";
}

function ApriFinestra(url)
{
        id=window.open(url,'finestra','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=640,height=300')
        id.focus();
}
function VisualizzaCarrello()
{
        id=window.open("EditShopCart.php","shopping_cart",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=640,height=300')
        id.focus();
}

function OpenWindow(url,pars,replace,fs)
{
        id=window.open(url,'finestra',pars,replace);
        if (fs) id.focus();
        return (id);
}

function ApriCalcolatrice(prezzoAcquisto,event)
{
    var x;
    var y;
    var w = 350;
    var h = 500;	
	if(!event) event = window.event; 
	x=event.clientX-w;
	y=event.clientY;
    var position = [x,y]
    var html='<iframe id="overlayer_iframe" src="tools/calcolatrice.php?PrAcq='+prezzoAcquisto+'" style="border: 0px; width: 310px; height:410px"></iframe>';
    if (!document.getElementById('calcolatrice'))  JQ_DialogBox(html,'Calcolatrice',true, h, w, false, false, position, 'ui-dialog-calc')
}

function addElement(father,divIdName,text)
{
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = text;
  father.appendChild(newdiv);
}

function removeElement(father,divIdName)
{
	var olddiv = document.getElementById(divIdName);
	var container = document.getElementById(father);
	container.removeChild(olddiv);
}


function MostraOverlayer(testo,event,layerName,x,y,w,h)
{
        var layer = document.getElementById(layerName);
        var services = '<div align="right"><a style="cursor: pointer" onClick="NascondiOverlayer('+layerName+');"><img src="images/delete_icon.gif"></a></div>';
        if(!event) event = window.event;
        layer.innerHTML        = services+testo; // IMPOSTA IL TESTO NEL LIVELLO
        layer.style.visibility = "visible"; // MOSTRA IL LIVELLO
        layer.style.position   = "absolute"; // POSIZIONA IL LIVELLO IN MODALITA' ASSOLUTA
        layer.style.left       = x; // IMPOSTA LE COORDINATE IN ORIZZONTALE
        layer.style.top        = y; // IMPOSTA LE COORDINATE IN VERTICALE
        layer.style.width      = w; // IMPOSTA LA LARGHEZZA
        layer.style.height      = h; // IMPOSTA L'ALTEZZA
}
function NascondiOverlayer(layer)
{
        //var layer = document.getElementById(layerName);
        layer.style.visibility = "hidden"; // NASCONDE IL LIVELLO
}

function ValidaData(data)
{

	data=data.replace(/-/g,"/");
	var giorno = data.substr(0,2);
	var mese = data.substr(3,2);
	var anno = data.substr(6,4);
	var source_date = new Date(anno,mese-1,giorno);
    if(anno != source_date.getFullYear() | mese-1 != source_date.getMonth() | giorno != source_date.getDate())
    {
    	AlertBox('Data non valida: il formato deve essere del tipo: gg-mm-aaaa!');
       return false;
    }
    return true;
}

function ValidaOra(ora)
{
	var regex=/^(2[0-3])|[01][0-9]:[0-5][0-9]:[0-5][0-9]$/;
	if(!regex.test(ora)) 
	{
		AlertBox('Ora non valida!');
		return false;
	}
	return true;
}

function ControllaSeparatore(oggetto)
{
	oggetto.value = oggetto.value.replace (",",".");
}

function ChiediConferma(url)
{
	if (confirm('Sei sicuro ?')) window.location=url; 
}

function RitornaMese(numero)
{
	if (parseInt(numero)==1) return "Gennaio";
	if (parseInt(numero)==2) return "Febbraio";
	if (parseInt(numero)==3) return "Marzo";
	if (parseInt(numero)==4) return "Aprile";
	if (parseInt(numero)==5) return "Maggio";
	if (parseInt(numero)==6) return "Giugno";
	if (parseInt(numero)==7) return "Luglio";
	if (parseInt(numero)==8) return "Agosto";
	if (parseInt(numero)==9) return "Settembre";
	if (parseInt(numero)==10) return "Ottobre";
	if (parseInt(numero)==11) return "Novembre";
	if (parseInt(numero)==12) return "Dicembre";
	return ("Mese errato");
}

var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "IE",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				   string: navigator.userAgent,
				   subString: "iPhone",
				   identity: "iPhone/iPod"
		    },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]

	};

function AlertBox(messaggio)
{
	JQ_DialogBox(messaggio,"Attenzione",false, 150, 300, true, false, 'center','ui-dialog-info')
}

function JQ_DialogBox(messaggio,titolo,drag, h, w, mod, resize, pos,classe)
{
	if (!document.getElementById('dialogbox'))
	{
		var dialogBox = document.createElement('div');
		dialogBox.setAttribute('id','dialogbox');
		dialogBox.style.textAlign='center';
		if (document.getElementById('contenuto')) document.getElementById('contenuto').appendChild(dialogBox);
		if (document.getElementById('contenuto_widen')) document.getElementById('contenuto_widen').appendChild(dialogBox);
	}
	else var dialogBox = document.getElementById('dialogbox');
	dialogBox.setAttribute('title',titolo);
	dialogBox.innerHTML=messaggio;
	$("#dialogbox").dialog({width: w, height: h, draggable: drag, modal: mod, resizable: resize, position: pos, show: {effect: 'fade', duration: 300}, hide: {effect: 'fade', duration: 300}, dialogClass: classe});
}
function JQ_DatePicker(dp)
{
	$(dp).datepicker();
}
function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
	return 1;
	else
	return 0;
}
