// inizio g.tonelli 



/**
 *  assegna ad uno oggetto dom stili css
 *  
 * @param {Object} domObj oggetto dom 
 * @param {Object} style 
 */
if (typeof(setStyles) == 'undefined') {
	function setStyles(domObj, style){
		if (typeof(domObj) != 'object' && typeof(style) != 'object') return;
		for (var i in style) {
			if (typeof(style[i]) == 'string' || typeof(style[i]) == 'number') {
				if (p = i.indexOf('-') != -1) {
					str = i.split('-');
					var sub = str[1].charAt(0).toUpperCase();
					$s(domObj)[str[0] + sub + str[1].substr(1)]=style[i];
				}else {$s(domObj)[i]=style[i];}
			}
		}
	}
}

if (typeof(String.prototype.toInt) == 'undefined') {
	String.prototype.toInt = function(){
		if(this.indexOf('%')!=-1){
			vr=this.substring(0,this.indexOf('%'));
		}
		if(this.indexOf('px')!=-1){
			vr=this.substring(0,this.indexOf('px'));
		}
		if(this.indexOf('pt')!=-1){
			vr=this.substring(0,this.indexOf('pt'));
		}
		return parseInt(vr);
	};
}
/**
 * funzione per la gestione del menu MyBlog
 */
var menuMyBlogF=function(){
	var t=15,z=50,s=6,a;
	function dd(n){
		this.n=n; this.h=[]; this.c=[]
	}
	dd.prototype.init=function(p){
		var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode;
			this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		alphaOp=Math.ceil(o*100);
		c.style.opacity=o; c.style.filter='alpha(opacity='+(alphaOp)+')';
		c.style.height=h+((d*f))+'px'
	}
	return{dd:dd}
}();

function getPageSizeWithScroll(){
	 var vr={};
	 vr.h=0;
	 vr.w=0;
    if (window.innerHeight && window.scrollMaxY) {// Firefox
        h = window.innerHeight + window.scrollMaxY;
        w = (window.innerWidth + window.scrollMaxX)-17;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        h = document.body.scrollHeight;
        w = document.body.scrollWidth;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        h = document.body.offsetHeight;
        w = document.body.offsetWidth;
      }
	 vr.h=h;
	 vr.w=w;
    return vr;
}

function centerDiv(ele){
	 var vr={};
	 vr.y=0;
	 vr.x=0;
 	elw=ele.offsetWidth;
	if(elw==0){
		elw=$s(ele).width.replace('px','');
	}
	elh=ele.offsetHeight;
	if(elh==0){
		elh=$s(ele).height.replace('px','');
	}
	if (document.getElementById && !document.all){
		vr.y = (window.innerHeight - elh)/2 +(window.pageYOffset);
		vr.x = (window.innerWidth - elw)/2 +(window.pageXOffset);
	}else{
		vr.y = (document.body.clientHeight - elh)/2 + (document.body.scrollTop);
		vr.x = (document.body.clientWidth - elw)/2 +(document.body.scrollLeft);
	}
	return vr;
}



/**
 * crea un div dopo elemento body al di sopra di tutto l'html
 * @param {Double} opacity opacità dell'elemento lasciare vuoto per non settare
 * @param {String} bgcolor colore di sfondo  lasciare vuoto per non settare
 */
function createPageVeil(opacity,bgcolor){
		b=document.getElementsByTagName("body")[0];
		dimension=getPageSizeWithScroll()
		bw=dimension.w;
		bh=dimension.h;
		newdiv=document.createElement('div');
		newdiv.setAttribute('id','bacheca-veil');
		newdiv.style.height=bh+'px';
		newdiv.style.width=bw+'px';
		newdiv.style.top='0px';
		newdiv.style.left='0px';
		if(typeof(bgcolor) != 'undefined' && bgcolor != ''){
			newdiv.style.backgroundColor=bgcolor;
		}
		newdiv.style.position='absolute';
		newdiv.style.zIndex='10001';
		if(typeof(opacity)!= 'undefined' && opacity != ''){
			Libero.Utils.prototype.setOpacity(newdiv,opacity);
		}
		newdiv.innerHtml=' ';
		newdiv.onclick=null;
		b.appendChild(newdiv);
}
/**
 * rimuove il velo al di sopra di tutto l'html
 */
function removePageVeil(){
		if(p=$('bacheca-veil')){
			p.parentNode.removeChild(p);
		}
}
/** 
 * prende le coordinate di un ele,ento del dom
 * @param {Object} element
 * @return {Object} y e x
 */
function getPageCoords (element) {
var myElement, coords = {x: 0, y: 0}; 
    myElement = element; 
    while (element) { 
        coords.x -= element.scrollLeft; 
        coords.y -= element.scrollTop; 
        element = element.offsetParent; 
    } 
    element = myElement;
    while (element) { 
        coords.x += element.offsetLeft; 
        coords.y += element.offsetTop; 
        element = element.offsetParent; 
    } 
	coords.y += document.body.scrollTop; 
	coords.x += document.body.scrollLeft; 
    return coords; 
} 

/**
 *Controlla se un elemento dom ha una classe css
 * @param {Object/ Strind} ele id elemento o oggeto 
 * @param {String} cls nome classe da aggiungere 
 */
function hasClass(ele,cls) {	
	if(typeof(ele)!=='object'){
			ele=$(ele);
	}
	if(!ele)
		return;
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
} 
/**
 * aggiunge una classe di stile ad una esistent 
 * @param {Object/ Strind} ele id elemento o oggeto
 * @param {string} cls nome classe da aggiungere 
 */
function addClass(ele,cls) {	
	if(typeof(ele)!=='object'){
			ele=$(ele);
	}
	if (!hasClass(ele, cls)) {
		ele.className += " "+cls;
	}

} 
/**
 * rimuove una classe di stile
 * @param {Object/ Strind} ele id elemento o oggeto
 * @param {string} cls classe da rimuovere
 */
function removeClass(ele,cls) {
	ele=$(ele);
	if (hasClass(ele,cls)) { 
	   	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');		
		ele.className=ele.className.replace(reg,' ');	
	}
}
/**
 * imposta una classe di stile all'elemento
 * @param {Object/ Strind} ele id elemento o oggeto
 * @param {string} class nomeclasse
 */
function setClass(el,classe){
	el=$(el);
	if(el){
		el.className=classe;
	}
}
/**
 * seleziona uno o + elemnti per classe
 * @param {String} cl nome della classe
 * @param {String} tagsel filtra il tag velocizzando
 */
document.getElementsByClassName = function(cl,tagsel) {
	if(!tagsel){
		tagsel='*';
	}
   var retnode = [];
   var myclass = new RegExp("\\b"+cl+"\\b");
   var elem = this.getElementsByTagName(tagsel);
   for (var i = 0; i < elem.length; i++) {
      var classes = elem[i].className;
      if (myclass.test(classes)) retnode.push(elem[i]);
   }
   return retnode;
};
if (typeof($c) == 'undefined') {
	var $c = function(cl, tagsel){
		return document.getElementsByClassName(cl, tagsel);
	}
}
/**
 * prima pulizia di tag
 * @param {String} val
 * @return String
 */
function stripHTML(val){
	var re= /<\S[^><]*>/g
	return val.replace(re, "");
}

function stripScript(val){
	var re= /<script.*?>*?<\/.*?script>/ig
	return val.replace(re, "");
}


/**
 * Fonzione generica per mostrare errori lato client
 * @param {String} message messaggio da visualizzare
 */
function showError(message){
	alert(stripHTML(message));
	//@TODO
}

/**
 * Funzione per ridimensionare dinamicamente le text area
 * @param {Object} ta text area
 * @param {Integer} textMaxChar caratteri massimi consentiti per la textarea
 * @param {Integer} textareaMinHeight altezza minima 
 * @param {Integer} textareaMaxHeight altezza massima
 */
function resizeTa(ta,textMaxChar,textareaMinHeight,textareaMaxHeight){
	if (typeof(textMaxChar) == 'undefined') {
		textMaxChar=20000;
	}
	if (typeof(textareaMinHeight) == 'undefined') {
		textareaMinHeight=40;
	}
	if (typeof(textareaMaxHeight) == 'undefined') {
		textareaMaxHeight=10000;
	}
	var success=true;
	if (typeof(ta['oldvalue'])=='undefined'){
		ta.oldvalue='';
	}
	if (typeof ta.value!='undefined') {
		if (ta.value.length > textMaxChar) {
			ta.value = ta.value.substring(0,textMaxChar);
			//ta.value = ta.oldvalue;
			ta.newvalue=ta.value;
			success=false;
		}
		ta.newvalue=ta.value;
	}else {
		if (typeof ta.textContent!='undefined') {
			if (ta.textContent.length > textMaxChar) {
				ta.textContent = ta.textContent.substring(0,textMaxChar);
				//ta.textContent = ta.oldvalue;
				success = false;
			}
			ta.newvalue = ta.textContent;
		}
		if (typeof ta.innerText!='undefined') {
			if (ta.innerText.length >  textMaxChar) {
				ta.innerText = ta.innerText.substring(0,textMaxChar);
				//ta.innerText = ta.oldvalue;
				success=false;
			}
			ta.newvalue=ta.innerText;
		}else{
			if (ta.innerHTML.length >  textMaxChar) {
				ta.innerHTML = ta.innerHTML.substring(0,textMaxChar);
				//ta.innerHTML = ta.oldvalue;
				success=false;
			}
			ta.newvalue=ta.innerHTML;
		}	
	}
	h=textareaMinHeight;
	if (false && typeof ta['createTextRange'] != 'undefined') {
		var tr;
		tr = ta.createTextRange();
		tr.collapse();
		tr.expand("textedit");
		if (tr.boundingHeight < ta.clientHeight) {
			if (ta.clientHeight > textareaMinHeight) {
				h=(tr.boundingHeight + 2);
				$s(ta).height =  h + 'px';
			}else {
				h=textareaMinHeight;
				$s(ta).height = h +'px';
			}
		}else {
			h=(tr.boundingHeight + 2);
			$s(ta).height = h + 'px';
		}
	}else {
		while (ta.clientHeight > textareaMinHeight && ta.scrollHeight<=ta.clientHeight) {
			h=(ta.clientHeight -  textareaMinHeight/2);
			$s(ta).height = h + 'px';
		}
		if (ta.scrollHeight>ta.clientHeight){
			h=(ta.scrollHeight+2);
			$s(ta).height=h+'px';
		}
	}
	if(h > textareaMaxHeight && textareaMaxHeight > 0){
		h=textareaMaxHeight;
		$s(ta).height=h + 'px';
		$s(ta).overflowY='scroll';
	}else{
		$s(ta).overflowY='hidden';
	}
	
	if(h<textareaMinHeight){
		h=textareaMinHeight;
		$s(ta).height=h + 'px';
	}
	if (typeof ta.value!='undefined') {
		ta.oldvalue=ta.value;
	}else {
		if (typeof ta.textContent!='undefined') {
			ta.oldvalue = ta.textContent;
		}if (typeof ta.innerText!='undefined') {
			ta.oldvalue=ta.innerText;
		}else{
			ta.oldvalue=ta.innerHTML;
		}	
	}
/*
	if (ta.scrollHeight<ta.clientHeight){
		h=(ta.scrollHeight+2);
		if(h<textareaMinHeight)
			h=textareaMinHeight;
		$s(ta).height = h + 'px';
	}
*/
	return success;
}
/**
 * validazione url
 * @param {String} s url da validare
 */
 function validateUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}
/**
 * validazione mail
 * @param {String} s mail da validare
 */
function validateMail(s){
   var regexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   return regexp.test(s);
 }

/**
 * prende il testo e la lunghezza dei valori di un campo
 * @param {Object} ta campo form
 * @return {Object}
 */
function getFieldValue(ta){
	var t={};
	if (typeof ta.value!='undefined') {
		t.text=ta.value;
		t.len=ta.value.length;
	}else {
		if (typeof ta.textContent!='undefined') {
			t.text=ta.textContent;
			t.len=ta.textContent.length;
		}
		if (typeof ta.innerText!='undefined') {
			t.text=ta.innerText;
			t.len=ta.innerText.length;
		}else{
			t.text=ta.innerHTML;
			t.len=ta.innerHTML.length;
		}	
	}
	return t;
}
/**
 * setta il testo di un elemento html
 * @param {Object} ta campo form
 * @param {String} value valore 
 */
function setFieldValue(ta,value){
	if (ta.type == 'checkbox' || ta.type == 'radio') {
		if (value == true || value.toLowerCase() == 'checked') {
			ta.checked = true;
		}else {
			ta.checked = false;
		}
	}
	if (typeof ta.value!='undefined') {
		ta.value= value;
	}else {
		if (typeof ta.textContent!='undefined') {
			ta.textContent= value;
		}
		if (typeof ta.innerText!='undefined') {
			ta.innerText= value;
		}else{
			ta.innerHTML = value;
		}	
	}
	
	if (ta.type == 'checkbox' || ta.type == 'radio') {
		if (value == true || value.toLowerCase() == 'checked') {
			ta.checked = true;
		}
		else {
			ta.checked = false;
		}
	}

}