function select_all(theField) {

  tempval = document.getElementById(theField);
  
  tempval.focus()
  tempval.select()

}

function show_widgetInput(idutente,checkrand) {

	if (document.getElementById("mytestowidget").style.display == 'block')  { 
	
		document.getElementById("mytestowidget").style.display = 'none'; 
	
	} else {
		
		document.getElementById("mytestowidget").style.display = 'block'; 
		
		document.getElementById("mylinkwidget").value = "<div id='mystd_widget'>MySavethedate widget</div><script type='text/javascript' language='javascript' src='http://my.savethedate.it/widget.php?idu="+idutente+"&check="+checkrand+"&larghezza=250px&nr=3'></script>";
	}
}

function show_esportaInput(param,idutente,checkrand) {

	if (document.getElementById("myboxesporta_cal").style.display == 'none')  { 
	
		document.getElementById("myboxesporta_cal").style.display = 'block';
	
	}

		if (param == "outlook") {
			
			url_esporta = "http://my.savethedate.it/export/outlook.php?id_utente="+idutente+"&check="+checkrand;
			
			document.getElementById("mytesto_cal").innerHTML = "Copia il link e incollalo nel calendario di Outlook";
			
		}	else if (param == "google") {
			
			url_esporta = "http://my.savethedate.it/export/google.php?id_utente="+idutente+"&check="+checkrand;
			
			document.getElementById("mytesto_cal").innerHTML = "Copia il link e incollalo nel calendario di Google";
			
		} else if (param == "apple") {
			
			url_esporta = "http://my.savethedate.it/export/apple.php?id_utente="+idutente+"&check="+checkrand;
			
			document.getElementById("mytesto_cal").innerHTML = "Copia il link e incollalo nel tuo iCal";
			
		} else if (param == "rss") {
			
			url_esporta = "http://my.savethedate.it/export/rss.php?id_utente="+idutente+"&check="+checkrand;
			
			document.getElementById("mytesto_cal").innerHTML = "Copia il link e incollalo nel tuo lettore di feed preferito";
			
		}
		
		document.getElementById("mylink_cal").value = url_esporta;
	

}

function switchElement( aOffElements, strOnElement, aOffLinks, strOnLink, strOffClassName, strOnClassName )
{
	
	if (document.getElementById("myboxesporta_cal").style.display == 'block')  { 
	
		document.getElementById("myboxesporta_cal").style.display = 'none'; 
	
	}
	
	for ( var i = 0; i < aOffElements.length; i++ )
	{
		var oElement = document.getElementById( aOffElements[i] );
		if ( oElement ) oElement.style.display = 'none';
	}
	
	var oOnElement = document.getElementById( strOnElement );
	if ( oOnElement ) oOnElement.style.display = 'block';
	
	if ( strOffClassName != null && strOnClassName !=  null )
	{
		for ( var i = 0; i < aOffLinks.length; i++ )
		{
			var oOffLink = document.getElementById( aOffLinks[i] );
			if ( oOffLink ) oOffLink.className = strOffClassName;
		}
		
		var oOnLink = document.getElementById( strOnLink );
		if ( oOnLink ) oOnLink.className = strOnClassName;
	}
}

function doClock() {
	
  window.setTimeout( "doClock()", 1000 );
  today = new Date();
	h = today.getHours();
	if (h<10){ h="0"+h; }
	m = today.getMinutes();
	if (m<10){ m="0"+m; }
	s = today.getSeconds();
	if (s<10){ s="0"+s; }
	d = today.getDate();
	if (d<10){ d="0"+d; }
	a = today.getMonth()+1;
	if (a<10){ a="0"+a; }
	y = today.getYear();
	document.getElementById('LabelData').innerHTML=h+":"+m+":"+s+" - "+d+"/"+a+"/"+y;

}

function testoSMS(idtesto) {
	
	//alert(idtesto);
	
	for (var i = 1; i < 10; i++) {
		
		if ( (document.getElementById('testoSMS'+i)) && (document.getElementById('testoSMS'+i).style.display == 'block') && (i != idtesto) ) { document.getElementById('testoSMS'+i).style.display = 'none'; }	
	
	}
	
	
	if (document.getElementById('testoSMS'+idtesto).style.display == 'none') { document.getElementById('testoSMS'+idtesto).style.display = 'block'; }
	else if (document.getElementById('testoSMS'+idtesto).style.display == 'block') { document.getElementById('testoSMS'+idtesto).style.display = 'none'; }

	if (document.getElementById('smsEditor'+idtesto).style.display == 'block') { document.getElementById('smsEditor'+idtesto).style.display = 'none'; }

}

function addContatto(amico) {
	
	if (amico != 0) {

		document.getElementById('campoDestinatario').value += amico + ";";
		document.getElementById('campoDestinatario').focus();

	}
}

function myhide() {
	
	document.getElementsByTagName("body")[0].style.overflow 	= 'hidden';	
	document.body.style.overflow='hidden';
	window.document.body.scroll = 'no';

}

function smsEditor(ideditor) {
	
	for (var i = 1; i < 10; i++) {
			
		if ( (document.getElementById('testoSMS'+i)) && (document.getElementById('testoSMS'+i).style.display == 'block') && (i != ideditor) ) { document.getElementById('smsEditor'+i).style.display = 'none';document.getElementById('testoSMS'+i).style.display = 'none'; }	
	
	}
	
	if (document.getElementById('testoSMS'+ideditor).style.display == 'none') { document.getElementById('testoSMS'+ideditor).style.display = 'block'; }
	

	var stringa = document.getElementById('smsTitolo'+ideditor).innerHTML;

	//alert(stringa.substr(0,3));
	
	if (document.getElementById('smsEditor'+ideditor).style.display == 'none') { 
		
		document.getElementById('smsEditor'+ideditor).style.display = 'block';
		
		if (stringa.substr(0,3) == 're:') { document.getElementById('editorTitolo'+ideditor).value = document.getElementById('smsTitolo'+ideditor).innerHTML; }
		else {
		
			document.getElementById('editorTitolo'+ideditor).value = 're: ' + document.getElementById('smsTitolo'+ideditor).innerHTML; 
		
		}

	}
	
	else if (document.getElementById('smsEditor'+ideditor).style.display == 'block') { document.getElementById('smsEditor'+ideditor).style.display = 'none'; }
	
	

}


function myEditor(azione,tipo) {
	
		if (azione == 'show') { document.getElementById('myEditor').style.display = 'block'; }
		else if (azione == 'close') { document.getElementById('myEditor').style.display = 'none'; }
	

}

function rubrica() {
	
//alert(document.getElementById('puls_rubrica').left);
	
	var rubrica	=	document.createElement('div');
	
	rubrica.className						=	'rubrica';
	rubrica.setAttribute('id','rubrica');

	rubrica.style.position				=	'absolute';
	rubrica.style.background			= '#fff';
	rubrica.style.border					= '1px solid #90A6BC';
	rubrica.style.top							=	"325px";
	rubrica.style.left						=	'650px';
	rubrica.style.width						=	"140px";
	rubrica.style.height					=	"140px";
	rubrica.style.display					=	'block';
	
	document.getElementsByTagName('body')[0].appendChild(rubrica);

//	document.getElementById('puls_rubrica').appendChild(rubrica);
	
}

function splash(what) {
	
	if (document.getElementById('mytextarea').style.display	==	'block') { document.getElementById('mytextarea').style.display = 'none' }
	
	myhide();
	
//setTimeout("myhide()",10000);
		
	var myA=document.createElement("A");
	url = "javascript:hide_splash('myA');";
	myA.setAttribute("href",url);
	myA.style.fontSize		= '20px';
	myA.setAttribute('id','myA');
	
	var splash	=	document.createElement('div');
	
	splash.className						=	'splash';
	splash.setAttribute('id','splash');
	splash.style.position				=	'absolute';
	splash.style.zIndex					=	'1000';
	splash.style.display				=	'block';
	splash.style.top						=	'0px';
	splash.style.left						=	'0px';	

	splash.style.width					=	window.screen.width-2+"px";
	splash.style.height					=	"1400px";

	splash.style.background			= '#000000';
	splash.style.opacity 				= '0.5';
	splash.style.MozOpacity 		= '0.5';
	splash.style.KhtmlOpacity 	= '50';
	splash.style.filter 				= "alpha(opacity=50)";
	
	
	myA.appendChild(splash);
	
	
	if (document.getElementById('larghezza').value > 400) { show_error('Widget & Gadget','La larghezza impostata è superiore ai 400px.'); } 
	else if (document.getElementById('larghezza').value < 200) { show_error('Widget & Gadget','La larghezza impostata è inferiore ai 200px.'); } 
	else if (what == 'widget') { 	inviaXHR(obj,"ajax.php","compile_widget",document.getElementById('idu').value,null); }
	else if (what == 'error') { show_error('Widget & Gadget','Il tuo profilo è privato pertanto non puoi generare widget.'); }

	document.getElementsByTagName('body')[0].appendChild(myA);
	
//setTimeout("hide_splash('errorbox')",15000);
	
}

function hide_splash(id) {
	
		document.getElementById(id).style.display			=	'none';

		var d	=	document.getElementById(id);
	
		if (document.getElementById("errorbox")) { document.getElementsByTagName('body')[0].removeChild(document.getElementById("errorbox")); }
		if (document.getElementById("myA")) {document.getElementsByTagName('body')[0].removeChild(document.getElementById("myA")); }
		if (document.getElementById("chiusura")) {document.getElementsByTagName('body')[0].removeChild(document.getElementById("chiusura")); }
		
		document.body.style.overflow 	= 'auto';
		

//	clearTimeout(timer_name);
	

}

function compile_preview() {
	
	var y = document.createElement('div');
  
  y.className						=	'chiusura';
	y.setAttribute('id','chiusura');
	y.style.position			=	'absolute';
	y.style.zIndex				=	'1015';
	y.style.display				=	'block';
	y.style.marginBottom	= '10px'
//y.style.paddingBottom	= '50px'
	y.style.width					=	document.getElementById('larghezza').value+"px";
	y.style.textAlign			= 'center';  
	y.style.color					= '#ffffff';
	y.style.fontFamily		= 'verdana, arial, helvetica, sans-serif';
	y.style.fontSize			= '14px';
	y.style.fontWeight		= 'bold';
	y.style.top						=	((900/2)-(400/2))+"px";
	y.style.left					=	((800/2)-(10/2))+"px";
    
	
	var mytxt = document.createTextNode('Per chiudere premere sullo sfondo');
	
	y.appendChild(mytxt);
	
	document.getElementsByTagName('body')[0].appendChild(y);
	

	var x	=	document.createElement('div');
	
	x.className						=	'errorbox';
	x.setAttribute('id','errorbox');
	x.style.position			=	'absolute';
	x.style.zIndex				=	'1011';
	x.style.display				=	'block';
//x.style.border				=	document.getElementById('sb').value + "px " + document.getElementById('tb').value + " " + document.getElementById('cb').value;
	x.style.width					=	document.getElementById('larghezza').value+"px";

//if (document.getElementById('cs').value !=  0) { x.style.background		=	document.getElementById('cs').value; }

	x.style.padding				= '0px';

//x.style.top						=	'0px';
//x.style.left					=	'0px';

	x.style.top						=	((1000/2)-(400/2))+"px";
	x.style.left					=	((800/2)-(10/2))+"px";
		
//	x.style.top						=	((window.screen.height/2)-(300/2))+"px";
//	x.style.left					=	((window.screen.width/2)-(150/2))+"px";
	
	x.style.opacity 			= '100';
	x.style.MozOpacity 		= '100';
	x.style.KhtmlOpacity 	= '100';
	x.style.filter 				= "alpha(opacity=100)";
  
  

	document.getElementsByTagName('body')[0].appendChild(x);


}

function show_error(titolo,msg) {
	
	var x	=	document.createElement('div');
	
	x.className						=	'errorbox';
	x.setAttribute('id','errorbox');
	x.style.position			=	'absolute';
	x.style.zIndex				=	'1001';
	x.style.display				=	'block';
	x.style.border				=	'0px solid #A9A9A9';
	x.style.width					=	'300px';
	x.style.height				=	'150px';
	x.style.background		=	'#000';	
	x.style.padding				= '10px';
//x.style.top						=	'0px';
//x.style.left					=	'0px';

	x.style.top						=	((1000/2)-(400/2))+"px";
	x.style.left					=	((800/2)-(10/2))+"px";
		
//	x.style.top						=	((window.screen.height/2)-(300/2))+"px";
//	x.style.left					=	((window.screen.width/2)-(150/2))+"px";
	
	x.style.opacity 			= '0.5';
	x.style.MozOpacity 		= '0.5';
	x.style.KhtmlOpacity 	= '50';
	x.style.filter 				= "alpha(opacity=50)";
    
	
	document.getElementsByTagName('body')[0].appendChild(x);

	var h								=	document.createElement('h1');
	
	h.setAttribute('id','titolobox');
	h.style.width				=	'300px';
	h.style.color				= '#fff';
	h.style.fontFamily	= 'verdana, arial, helvetica, sans-serif';
	h.style.fontSize		= '16px';
	h.style.padding			= '30px 0px 0px 0px'
	h.style.textAlign		= 'left';
	
	x.appendChild(h);
	
	document.getElementById('titolobox').innerHTML = titolo;


	var e								=	document.createElement('span');
	
	e.setAttribute('id','testobox');
	e.style.width				=	'300px';
	e.style.color				= '#fff';
	e.style.fontFamily	= 'verdana, arial, helvetica, sans-serif';
	e.style.fontWeight	= 'bold';
	e.style.fontSize		= '14px';
	e.style.padding			= '0px 0px 0px 0px'
	e.style.textAlign		= 'left';

	x.appendChild(e);
	
	document.getElementById('testobox').innerHTML = msg;

}

function changeColor(elemento) {
	
	var myel = document.getElementById(elemento);
	
	if (myel.value == 0) { myel.style.backgroundColor	=	"#ffffff"; }
	else { myel.style.backgroundColor	=	myel.value; }
	

}

function compileMultiSelect(fromItem, toItem) {

	var fItem = document.getElementById(fromItem);
	var tItem = document.getElementById(toItem);

	if ( fItem.options.selectedIndex >= 0 ) {
	  
	  while ( fItem.options.selectedIndex >= 0) {
	  	
	      ind   = fItem.selectedIndex;
	      text  = fItem.options[ind].text;
	      value = fItem.options[ind].value;
	      	      
	      fItem.options[ind] 										= null;
	      var newOption 												= new Option(text,value);
	      toFormLength 													= tItem.options.length;
	      tItem.options[toFormLength] 					= newOption;
	      
	    }
    }
    return false;
}

function selectMultiSelect(form) {
	
	var myTagID 		= document.getElementById("canali");	
	
	for (var i = 0; i < myTagID.options.length; i++) {
		
		myTagID.options[i].selected = true;	
	
	}
	
	form.submit();
	
}

function istanzaXHR() {
	
	var obj;
	
	if (window.XMLHttpRequest) {

		obj = new XMLHttpRequest();
	
	} else if (window.ActiveXObject) {
	
		obj = new ActiveXObject("Microsoft.XMLHTTP");
	
	}
	
	return obj;

}

function inviaXHR(obj,url,tipo,attr,attr1) {
	
	try {	

		obj.open("POST", url, true);	// Preparazione comunicazione
		obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		if (tipo == 'compile_comuni') {
			
			obj.onreadystatechange = function() { compile_comuni(obj, attr1); };
			obj.send("action="+tipo+"&idref="+attr);
		
		}
		else if (tipo == 'status_sms') {
			
			obj.onreadystatechange = function() { status_sms(obj, attr1); };
			
//			alert('attr '+attr);
					
				obj.send("action="+tipo+"&idref="+attr);
		
		}
		else if (tipo == 'mail_alert') {
			
			obj.onreadystatechange = function() { mail_alert(obj, attr, attr1); };
			
//			alert('attr '+attr);
					
				obj.send("action="+tipo+"&idref="+attr+"&status="+attr1);
		
		}		
		else if (tipo == 'compile_widget') {
							
			obj.onreadystatechange = function() { compile_widget(obj, attr); };
			
			
			
			if (document.getElementById('cs').value !=  0) { 
				
				if (document.getElementById('cs').value == '#91A7BD') { colorebordo = '#E8E8E8'; }
				else if (document.getElementById('cs').value == '#EFF4F8') { colorebordo = '#91A7BD'; }
				else if (document.getElementById('cs').value == '#E8E8E8') { colorebordo = '#91A7BD'; }
				else if (document.getElementById('cs').value == '#FFFFFF') { colorebordo = '#000000'; }
				else if (document.getElementById('cs').value == '#FFCA05') { colorebordo = '#C80000'; }
				
				obj.send("action="+tipo+"&idu="+attr+"&nr="+document.getElementById('ne').value+"&sb="+document.getElementById('sb').value + "&tb=" + document.getElementById('tb').value + "&cb=" + colorebordo + "&cs=" + document.getElementById('cs').value + "&cc=#000000");	
			
			}	
			else { colorebordo = '#000000';obj.send("action="+tipo+"&idu="+attr+"&nr="+document.getElementById('ne').value+"&sb="+document.getElementById('sb').value + "&tb=" + document.getElementById('tb').value + "&cb=" + colorebordo + "&cc=#000000");	}					
			
		}
		

	} 
	catch (e) {
		alert("Errore: "+e);
	}
	
}

obj=istanzaXHR();

function comuni(attr,attr1) {
	
	inviaXHR(obj,"ajax.php","compile_comuni",attr,attr1);

}

function mail_alert(obj, attr, attr1) {
	
	if (obj.readyState == 4) { 
				
			if (obj.status == 200) { 
				
				if (obj.responseText == 1) {
					
					document.getElementById('td_mail_alert'+attr).removeChild(document.getElementById("link_mail_alert"+attr)); 
	
					if (attr1 == 0) {							
							
//						document.getElementById('link_mail_alert'+attr).innerHTML = "disattiva"; 
							
							document.getElementById('td_mail_alert'+attr).innerHTML = "<a href='javascript:;' onClick=\"inviaXHR(obj,'ajax.php','mail_alert',"+attr+",1);return false;\" title='attiva mail alert' id='link_mail_alert"+attr+"' class='a a_avviso'>attiva</a>";
//						document.getElementById('ico_mail_alert'+attr).src = '';								
					
					}
					else {

//						document.getElementById('link_mail_alert'+attr).innerHTML = "attiva"; 

							document.getElementById('td_mail_alert'+attr).innerHTML = "<a href='javascript:;' onClick=\"inviaXHR(obj,'ajax.php','mail_alert',"+attr+",0);return false;\" title='disattiva mail alert' id='link_mail_alert"+attr+"' class='a a_avviso'>disattiva</a>";			
//						document.getElementById('ico_mail_alert'+attr).src = '/img/emailalert.jpg';
					
					}			
				} else {
					
					alert('Errore: nella gestione del messaggio');
				
				}
				
			} else {

			alert("Errore1: "+obj.statusText);

		}
	}	
}

function status_sms(obj, attr) {
	
	if (obj.readyState == 4) { 
				
			if (obj.status == 200) { 
				
				if (obj.responseText == 1) {
	
				if (document.getElementById('sms_nuovi').innerHTML > 1) { document.getElementById('sms_nuovi').innerHTML = document.getElementById('sms_nuovi').innerHTML-1; }
				else if (document.getElementById('sms_nuovi').innerHTML == 1) { document.getElementById('sms_nuovi').innerHTML = document.getElementById('sms_nuovi').innerHTML-1; document.getElementById('sms_nuovi').style.fontWeight = "normal";	/*document.getElementById('ico_email_dx').src = '/img/posta_letta.gif';*/ }
				
				document.getElementById('newsms'+attr).className = "text_sms_letto";
				
				document.getElementById('ico_mail'+attr).src = '/img/posta_letta.gif';								
								
				} else {
					
					alert('Errore: nella gestione del messaggio');
				
				}
				
			} else {

			alert("Errore1: "+obj.statusText);

		}
	}	
}

function compile_widget(obj, attr) {

	if (obj.readyState == 4) { 
				
			if (obj.status == 200) { 
				
		
				if (obj.responseText) {
					
					compile_preview();
									
					var temp = new Array();
					temp = obj.responseText.split('|||||');
					
					//alert(temp.length);
				
					for (var i=0;i<temp.length;i++) {		
							
							document.getElementById('errorbox').innerHTML += temp[i];
																			
					}				
				} else {
					
					show_error('Widget & Gadget','Non è presente nessun evento pubblico nel tuo calendario per questo mese.');
				
//				alert('Errore: nessun evento presente nel tuo calendario');
				
				}
				
			} else {

			alert("Errore1: "+obj.statusText);

		}
	}	
}

function compile_comuni(obj, attr) {
	
	if (obj.readyState == 4) { 
			
			if (obj.status == 200) { 
		
				if (obj.responseText > '0') {
										
					document.getElementById('selz1').disabled = false;
					
					var temp = new Array();
					temp = obj.responseText.split('|||||');

 					var sel = document.getElementById('selz1');

					for (var i=1;i<temp.length;i++) {
							
							var temp_codici = new Array();
							
							temp_codici = temp[i].split('---');
							
							sel.options[i] = new Option(temp_codici[0], temp_codici[1], false, false);
							
							if (temp_codici[1] == attr) { document.getElementById('selz1').options.selectedIndex = i; }
							
					}
					
					if (!attr) { document.getElementById('selz1').options.selectedIndex = 0; }
				
				} else {
				
//				alert('Errore: nessun comune trovato per la provincia selezionata');

					document.getElementById('selz1').disabled = true;
				
				}
				

			} else {

			alert("Errore1: "+obj.statusText);

		}
	}	
}

function addWidget(form,nreventi) {
	
	if (nreventi > 0) {
	
		document.getElementById('mytextarea').style.display				=	'block';
		
	//	if (document.getElementById('cs').value !=  0) { codice = "<div id='mystd_widget'></div><script type='text/javascript' language='javascript' src='http://my.savethedate.it/widget.php?idu="+document.getElementById('idu').value+"&check="+document.getElementById('check').value+ "&nr="+document.getElementById('ne').value+"&sb="+document.getElementById('sb').value + "&tb=" + document.getElementById('tb').value + "&cb=" + document.getElementById('cb').value + "&cs=" + document.getElementById('cs').value +"'></script>"; }
	//	else {	codice = "<div id='mystd_widget'></div><script type='text/javascript' language='javascript' src='http://my.savethedate.it/widget.php?idu="+document.getElementById('idu').value+"&check="+document.getElementById('check').value+ "&nr="+document.getElementById('ne').value+"&sb="+document.getElementById('sb').value + "&tb=" + document.getElementById('tb').value + "&cb=" + document.getElementById('cb').value +"'></script>"; }
		
		var coloresfondo 	= document.getElementById('cs').value.substr(1,6);
//	var colorebordo 	= document.getElementById('cb').value.substr(1,6);
//	var colorefont 		= document.getElementById('cc').value.substr(1,6);
		var colorefont 		= '000000';
		
		if (document.getElementById('cs').value !=  0) { 
		
				if (document.getElementById('cs').value == '#91A7BD') { colorebordo = 'E8E8E8'; }
				else if (document.getElementById('cs').value == '#EFF4F8') { colorebordo = '91A7BD'; }
				else if (document.getElementById('cs').value == '#E8E8E8') { colorebordo = '91A7BD'; }
				else if (document.getElementById('cs').value == '#FFFFFF') { colorebordo = '000000'; }
				else if (document.getElementById('cs').value == '#FFCA05') { colorebordo = 'C80000'; }
			
			codice = "<div id='mystd_widget'></div><script type='text/javascript' language='javascript' src='http://my.savethedate.it/widget.php?idu="+document.getElementById('idu').value+"&check="+document.getElementById('check').value+ "&nr="+document.getElementById('ne').value+"&sb="+document.getElementById('sb').value + "&tb=" + document.getElementById('tb').value + "&cb=" + colorebordo + "&cs=" + coloresfondo + "&larghezza=" + document.getElementById('larghezza').value + "&cc=" + colorefont +"'></script>";
		
		}
		
		else {	colorebordo='ffffff';codice = "<div id='mystd_widget'></div><script type='text/javascript' language='javascript' src='http://my.savethedate.it/widget.php?idu="+document.getElementById('idu').value+"&check="+document.getElementById('check').value+ "&nr="+document.getElementById('ne').value+"&sb="+document.getElementById('sb').value + "&tb=" + document.getElementById('tb').value + "&cb=" + colorebordo + "&larghezza=" + document.getElementById('larghezza').value + "&cc=" + colorefont +"'></script>"; }
	
		
		document.getElementById("widgetbox").value = codice;
		document.getElementById("widgetbox").focus();
		document.getElementById("widgetbox").select();
	}
	else {
		
		splash('widget');
	
	}
	
	
}