function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_setTextOfTextfield(objName,x,newText) { //v3.0
	var obj = MM_findObj(objName); if (obj) obj.value = newText;
}

function selectThisType(nameField, chkState)
{
	var frm = document.forms[0].elements;
	var parent;
	//var stringa = 0;
	if(chkState == false)
	{
		for(i = 0; i < frm.length; i++)
		{
			if (frm.elements[i].type == "hidden")
			{
				continue;
			}
			if((frm.elements[i].type == 'checkbox') && (frm.elements[i].name == nameField))
			{
				//var stringa = (stringa+frm.elements[i].name+'='+i+'\n');
				parent = document.getElementById(frm.elements[i].id+'_parent').value;
				if (parent != '')
				{
					document.getElementById('id_'+parent).checked = true;
				}
				frm.elements[i].checked = true;
			}
		}
	}
	else
	{
		for(i = 0; i < frm.length; i++)
		{
			if (frm.elements[i].type == "hidden")
			{
				continue;
			}
			if((frm.elements[i].type == 'checkbox') && (frm.elements[i].name == nameField))
			{
				//var stringa = (stringa+frm.elements[i].name+'='+i+'\n');
				frm.elements[i].checked = false;
			}
		}
	}
	//alert(stringa);
}

function imposta(variabile, valore) {
	var IE = navigator.appName.indexOf("Microsoft") != -1;
	var filmato = IE ? window.topbar_engine : window.document.topbar_engine;
	filmato.SetVariable(variabile, valore);
}

function toggleSpan(spanLabel) {
	if (document.getElementById(spanLabel).style.display == 'none') {
		document.getElementById(spanLabel).style.display = 'block';
	} else {
		document.getElementById(spanLabel).style.display = 'none';
	}
}

function ConfirmCancel(CancelURL) {
	if(confirm('ATTENZIONE: Sei sicuro di voler annullare l\'operazione?'))
		document.location.href = CancelURL;
}

function ConfirmEmpty(ActionURL) {
	if(confirm('ATTENZIONE: Sei sicuro di voler svuotare il gruppo selezionato?'))
		document.location.href = ActionURL;
}

function ConfirmDelete(CancelURL, type) {
	var string = 'ATTENZIONE: Sei sicuro di voler eliminare ';
	switch (type) {
		case 'user':
			string += 'l\'utente';
			break;
		case 'account':
			string += 'l\'account';
			break;
	}
	string += ' selezionato?';
	if(confirm(string))
		document.location.href = CancelURL;
}

function ConfirmCredit(CreditURL) {
	if(confirm('ATTENZIONE: Sei sicuro di voler acquistare i crediti selezionati?'))
		document.location.href = CreditURL;
}

function ConfirmAttach() {
	if ((document.forms[0].elements[2].length > 0) && (document.forms[0].elements[2].value == '')) {
		if(confirm('ATTENZIONE: Non sono stati selezionati allegati, continuare?')) {
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}

function goToPage(goToURL) {
	document.location.href = goToURL;
}

function printzone() {
	var width="600", height="410";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open("printzone.php","printzone", styleStr);
}

function printgraph(graphtype, format) {
	var width="600", height="410";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open("printgraph.php?graphtype="+graphtype+"&format="+format,"printzone", styleStr);
}

function printzoneid(myid) {
	var width="700", height="510";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open("printzoneid.php?id="+myid,"printzoneid", styleStr);
}

function insertfoto() {
	var width="400", height="168";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open("insertfoto.php","insertfoto", styleStr);
}

function verifymail(email) {
	var width="400", height="168";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open("verifymail.php?email="+email,"verifymail", styleStr);
}

function deletefoto() {
	document.frmContact.suFoto.value='';
	document.getElementById('fotoContainer').style.display='none';
	document.getElementById('fotoContainer').src='';
}

function showselected(myid, mywhat) {
	var width="600", height="410";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('showselected.php?id='+myid+'&what='+mywhat,'showselected', styleStr);
}

function importattach(myid, mywhat) {
	var width="400", height="192";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('inportattach.php?id='+myid+'&what='+mywhat,'importattach', styleStr);
}

function importemailattach(emailid) {
	var width="400", height="192";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('inportemailattach.php?id='+emailid,'importemailattach', styleStr);
}

function ConfirmStats() {
	if(confirm('ATTENZIONE: Le statistiche sono generate in tempo reale dal database, l\'operazione può durare dai 5 secondi a 1 minuto in base al numero di iscritti presenti nel database.'))
		document.location.href = 'statistiche.php';
}

function showTrack() {
	var width="400", height="250";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('importtrack.php','showTrack', styleStr);
}

function showimpressions(myid) {
	var width="700", height="510";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('showimpressions.php?myid='+myid,'showimpressions', styleStr);
}

function printsearch() {
	var width="700", height="510";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('printsearch.php','printsearch', styleStr);
}

function ConfirmBackup() {
	if(confirm('ATTENZIONE: Verrà eseguito il backup dell\'intero database. Dalla prossima finestra avrai la possibilità di salvare il backup nel tuo hard disk. Per ripristinare il database utilizzare la funzione "Ripristina Backup".'))
		document.location.href = 'rubrica.php?what=backup';
}

function reportcontacts(typeid, valueid) {
	var width="700", height="510";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('statscontacts.php?typeid='+typeid+'&valueid='+valueid,'reportcontacts', styleStr);
}

function reportcontact(contactid) {
	var width="700", height="510";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('reportcontact.php?id='+contactid,'reportcontact', styleStr);
}

function openFile(file) {
	var width="700", height="510";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open(file,'file', styleStr);
}

function showCalendar(textField, viewField) {
	var width="250", height="260";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('showcalendar.php?textfield='+textField+'&viewfield='+viewField,'showcalendar', styleStr);
}

function openHtmlEditor(url) {
	var w = 740;
	var h = 540;
	var l = (screen.width/2) - w/2;
	var t = (screen.height/2) - h/2;
	win = window.open(url, 'openHtmlEditor', 'toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=yes,width='+w+',height='+h+',left='+l+',top='+t);
}

function emailCheck(emailStr) {
	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) {
//		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];
	for (i=0; i < user.length; i++) {
		if (user.charCodeAt(i) > 127) {
//			alert("Ths username contains invalid characters.");
			return false;
   		}
	}
	for (i=0; i < domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
//			alert("Ths domain name contains invalid characters.");
			return false;
		}
	}
	if (user.match(userPat) == null) {
//		alert("The username doesn't seem to be valid.");
		return false;
	}
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i=1; i <= 4; i++) {
			if (IPArray[i] > 255) {
//				alert("Destination IP address is invalid!");
				return false;
		   }
		}
		return true;
	}
	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	for (i = 0; i < len; i++) {
		if (domArr[i].search(atomPat) == -1) {
//			alert("The domain name does not seem to be valid.");
			return false;
	   }
	}
	if (checkTLD && domArr[domArr.length-1].length != 2 && domArr[domArr.length-1].search(knownDomsPat) == -1) {
//		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	if (len < 2) {
//		alert("This address is missing a hostname!");
		return false;
	}
	return true;
}

function isArray(obj) {
	//returns true is it is an array
	if (obj.constructor.toString().indexOf("Array") == -1)
		return false;
	else
		return true;
}

function in_array(needle, haystack) {
	for (h in haystack)
	{
		if (haystack[h] == needle)
		{
			return true;
			// or if you prefer to get the key of the first found match use
			// return h;
		}
	}
	return false;
}

function openWindow(url,w,h) {
	var l = (screen.width/2) - w/2;
	var t = (screen.height/2) - h/2;
	win = window.open(url, 'openWindow', 'toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=yes,width='+w+',height='+h+',left='+l+',top='+t);
}

function printsendsearch(type, fromgg, frommm, fromaa, togg, tomm, toaa, user) {
	var width="700", height="510";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('printsendsearch.php?type='+type+'&fromgg='+fromgg+'&frommm='+frommm+'&fromaa='+fromaa+'&togg='+togg+'&tomm='+tomm+'&toaa='+toaa+'&user='+user,'printsearch', styleStr);
}

function printuserslogsearch(type, deltype, fromgg, frommm, fromaa, togg, tomm, toaa) {
	var width="700", height="510";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('printuserslogsearch.php?type='+type+'&deltype='+deltype+'&fromgg='+fromgg+'&frommm='+frommm+'&fromaa='+fromaa+'&togg='+togg+'&tomm='+tomm+'&toaa='+toaa ,'printsearch', styleStr);
}

function openContactDetail(action, id) {
	var width="700", height="410";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('printcontactdetail.php?id='+id+'&what='+action,'printcontact', styleStr);
}
	
function print_page() {
	var elementId;
	var counter = 1;
	while (elementId = document.getElementById('noprint'+counter)) {
		elementId.style.display='none';
		counter += 1;
	}
	self.print();
	self.close();
}

function openNewsletterDetail(action, id) {
	var width="700", height="410";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var msgWindow = window.open('printnewsletterdetail.php?id='+id+'&what='+action,'printnewsletter', styleStr);
}

function printmodel(modelid) {
		var width="700", height="510";
		var left = (screen.width/2) - width/2;
		var top = (screen.height/2) - height/2;
		var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
		var msgWindow = window.open('printmodel.php?id='+modelid,'printmodel', styleStr);
}