//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 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 cStyle crossbrowser style
function cStyle(ele) {
	var style
	if(window.getComputedStyle){
		style = document.defaultView.getComputedStyle(ele, null)
	}else{
		style = ele.currentStyle
	}
    return style || ele.style; //se entrambe le funzioni falliscono restituisce l'oggetto style dell'elemento
}

//FUNZIONE Centra
function centra(elemento){
	var SCREENwidth =document.width;
	var DIVwidth = parseInt(cStyle(document.getElementById(elemento)).width);
	var DIVheight = parseInt(cStyle(document.getElementById(elemento)).height);
	var posLeft =(SCREENwidth-DIVwidth)/2+"px"
	document.getElementById(elemento).style.left=posLeft;
}

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

//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(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
    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 : '';
    });
}

