//FUNZIONE getxmlhttpobject
function GetXmlHttpObject(){
	if (typeof XMLHttpRequest != 'undefined') {// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest(); 
	}
	try {
		return new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch (e) { 
		try { // code for IE6, IE5
			return new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch (e) {alert("Errore Ajax")} 
	}
	return false;
}

//FUNZIONE getimestamp
function getimestamp(){
    var tstmp = new Date();    
    return tstmp.getTime();
} 

//FUNZIONE setvalore
function setValore(o,v){
	if(v!=""){
		document.getElementById(o).value=v;
	}else{
		document.getElementById(o).value="";
	}
}

//FUNZIONE setvaloreTextarea
function setValoreTextarea(o,v){
	if(v!=""){
		tinyMCE.get(o).execCommand('mceSetContent',false,v);
	}else{
		tinyMCE.get(o).execCommand('mceSetContent',false,"");
	}
}

//FUNZIONE setClass
function setClass(elemento,cl){
	var e = document.getElementById(elemento)
	var classDiv =cl
	if(navigator.appName=="Microsoft Internet Explorer"){
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null){
			var rv = parseFloat( RegExp.$1 );
		}
		if ( rv >= 8.0 ){
			e.setAttribute('class',classDiv)
		}else{
			e.setAttribute('className',classDiv)
		}
	}else{
		e.setAttribute('class',classDiv)
	}
}

//FUNZIONE getValore
function getValore(o){
	var v = document.getElementById(o).value;
	return v;
}

//FUNZIONE submitForm
function submitForm(f){
	mostra('caricamento');
	document.getElementById(f).submit();
}

//FUNZIONE mostra
function mostra(o){
	document.getElementById(o).style.display="block";
}

//FUNZIONE nascondi
function nascondi(o){
	document.getElementById(o).style.display="none";
}

//FUNZIONE controlloMail
function controllomail(inputMail){
    var m=document.getElementById(inputMail).value;
    var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
	if (!espressione.test(m)){
	}else return "true";
}

//FUNZIONE contacaratteri
function contacaratteri(t,n){
	var txt = document.getElementById(t);
	var len = txt.value.length;
	if (len>n){
	 	txt.focus();
		alert ("Limite di "+n+" caratteri superato!");
		txt.value = txt.value.substring(0,n);
	}
}

//FUNZIONE setImmagine
function setImmagine(divImg,fileImg,align){
	document.getElementById(divImg).style.backgroundImage='url('+fileImg+')';
	document.getElementById(divImg).style.backgroundRepeat= "no-repeat";
	if(align!=""){
		document.getElementById(divImg).style.backgroundPosition= align;
	}else{
		document.getElementById(divImg).style.backgroundPosition= "center center";
	}
	
}

//FUNZIONE apriPagina
function apriPagina(pagina){
	document.location = pagina;
}

//FUNZIONE centra sullo schermo(qualsiasi)
function centra(elemento,imgTag){
	//dimensioni schermo
	var SCREENwidth = document.body.clientWidth;
	var SCREENheight = document.body.clientHeight;
	
	//dimensioni immagine
	var IMGwidth = document.getElementById(imgTag).clientWidth
	var IMGheight = document.getElementById(imgTag).clientHeight
	
	//controllo dimensioni immagine
	if(IMGwidth > SCREENwidth){
		IMGwidth = SCREENwidth-120;
		document.getElementById(imgTag).style.width=IMGwidth+"px"
	}
	if(IMGheight > SCREENheight){
		IMGheight = SCREENheight-120;
		document.getElementById(imgTag).style.height=IMGheight+"px"
	}
	
	//calcolo dimensioni div e centro
	var DIVwidth = document.getElementById(elemento).clientWidth
	var DIVheight = document.getElementById(elemento).clientHeight
	var posLeft =(SCREENwidth-DIVwidth)/2+"px";
	var posTop =(SCREENheight-DIVheight)/2+"px";
	
	document.getElementById(elemento).style.left=posLeft;
	document.getElementById(elemento).style.top=posTop;
}

//FUNZIONE apri_file_anteprima
function apri_file_anteprima(file_anteprima,idtag,cartella,testo){
	if(idtag!=0){
		var file_anteprima = getValore(idtag)
	}
	var file_open = cartella+file_anteprima;
	document.getElementById('anteprima_img').src =file_open
	document.getElementById('testo_anteprima').innerHTML =testo
	mostra('anteprima_immagine');
	document.getElementById('anteprima_img').onload=function(){
		//centra('anteprima_immagine')
		centra('anteprima_immagine','anteprima_img')
	}
}

//FUNZIONE elimina_file
function elimina_file(campo_file){
	if(window.confirm("Sei sicuro di cancellare il file?")){
		var id = getValore("id")
		var tabella = getValore("tabella")
		document.location='../include/fAction.php?action=2&id='+id+'&tabella='+tabella+'&campo_file='+campo_file;
	}
}

//FUNZIONE elimina record+file
function elimina(tabella,id){
	if(tabella==0&&id==0){
		var id = getValore("id")
		var tabella = getValore("tabella")
	}
	if(window.confirm("Sei sicuro di cancellare l'elemento?")){
		document.location='../include/fAction.php?action=3&id='+id+'&tabella='+tabella;
	}
}

//FUNZIONE rollover
function rollover(div,img){
	document.getElementById(div).src=img;
}

//FUNZIONE strip_tags
function strip_tags (input, allowed) {
    allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
    var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
        commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
    return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {        
    	return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
    });
}

//FUNZIONE condividi su facebook
function condividi(indirizzo){
	indirizzo=indirizzo+'&vr='+getimestamp()
	var indirizzo_facebook="http://www.facebook.com/share.php?u=__URL__";
	var url = encodeURIComponent(indirizzo);
	indirizzoSC = indirizzo_facebook.split("__URL__").join(url);
	window.open(indirizzoSC);
}

//------------------------FUNZIONI GESTIONE COOKIES
//-------------------------------------------------
//FUNZIONE createCookie
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//FUNZIOEN readCookie
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//FUNZIONE eraseCookie
function eraseCookie(name) {
	createCookie(name,"",-1);
}
//------------------------FINE FUNZIONI GESTIONE COOKIES
//------------------------------------------------------
