/* ************* Inizializzazione funzioni **************** */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') { window.onload = func; } 
	else {
    window.onload = function() {
      if (oldonload) { oldonload(); }
      func();
    }
  }
}

// Accetto solo numeri, backspace <-, tabulazione e canc
// uso: onkeypress="event.returnValue=onKeyNumeric();"
function onKeyNumeric(e) { 
  if (((e.keyCode >= 48) && (e.keyCode <= 57)) || (e.keyCode == 8) || (e.keyCode == 9) || (e.keyCode == 46)) return true;
  else return false;
}
	
// Controlla che un determinato campo contenga solo numeri
function isValidNumber(obj) {
	chrOk = "0123456789"; 
	o = obj.value; 
	a = o.charAt(o.length-1); 
	if (chrOk.indexOf(a)==-1) { 
		alert("Il carattere digitato non e\' consentito!!!\nSolo numeri interi"); 
		//obj.value=o.substr(0,o.length-1); 
		obj.value="";
		obj.focus();
	}
}

/* FUNZIONE per conoscere il tipo di browser utilizzato */
function RicavaBrowser(QualeBrowser) {
	if (navigator.userAgent.indexOf("MSIE") != (-1)) {
		var Classe = "Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5") != (-1)); {
			Classe = "Microsoft.XMLHTTP";
		} try {
			OggettoXMLHTTP = new ActiveXObject(Classe);
			OggettoXMLHTTP.onreadystatechange = QualeBrowser;
			return OggettoXMLHTTP;
		} catch(e) { alert("Errore: l'ActiveX non verrà eseguito!"); }
	} else if (navigator.userAgent.indexOf("Mozilla") != (-1)) {
		OggettoXMLHTTP = new XMLHttpRequest();
		OggettoXMLHTTP.onload = QualeBrowser;
		OggettoXMLHTTP.onerror = QualeBrowser;
		return OggettoXMLHTTP;
	} else { alert("L'esempio non funziona con altri browser!"); }
}

/* FUNZIONE per aprire una finestra di PopUp centrata   */
function openPopUp(url,w,h,scrl) {
  l = 50;
  t = 50;
  newWin = window.open(url,"nuovafinestra", "width="+w+", height="+h+", left="+l+", top="+t+", scrollbars="+scrl+", resizable=no") 
}

/* ****** Elemento a comparsa con un click ********* */
function scambio(id){
	if (document.getElementById){
		if(document.getElementById(id).style.display == 'none')	document.getElementById(id).style.display = 'block';
		else document.getElementById(id).style.display = 'none';
	}
}

// da testare ma dovrebbe essere ok - 18 marzo 2008
/* ****** FUNZIONE che Verifica la validità della mail  */
function ControllaMail(mail){	
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;	
	if (espressione.test(mail)) return true;
	else return false;
	return true;
}

/* ****** FUNZIONE che Verifica la validità dell'URL ** */
function ControllaURL(dominio){	
	//var RE = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	var RE = /^http:\/\/(www\.)?[a-zA-Z0-9-]{3,}\.([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-zA-Z]{2,}(\/)?$/;
	//var RE = /^http:\/\/(www\.)?[a-zA-Z0-9-]{3,}\.[a-zA-Z]{2,}(\/)?$/;
	if (RE.test(dominio)) return true;
	else return false;
}

/* ****** COUNTDOWN CARATTERI SULLE TEXTAREA ******* */
function countChars(d) {
	if (navigator.appName.indexOf("Netscape")>=0 &&
	  parseInt(navigator.appVersion) >=4) { var nn4 = true }
		document.formCMS_edit.testo.onkeydown = countChars;
	if (nn4) document.captureEvents(Event.KEYDOWN);

	var oldString = "";

	if (document.formCMS_edit.testo.value.length> 255) {
    var agree = confirm("Hai raggiunto il numero massimo di caratteri disponibili (255)\n\n• \'\'OK\'\' per svuotare l\'area di testo\n• \'\'Annulla\'\' per visualizzare solo i primi 255 caratteri");
    if (agree) {
			document.formCMS_edit.rimasti.value = 255;
		  document.formCMS_edit.testo.value = "";
			return true;
		} else {
			document.formCMS_edit.rimasti.value = 0;
		  document.formCMS_edit.testo.value = document.formCMS_edit.testo.value.substring(0,255);
			return false;
		}
	} else {
	  document.formCMS_edit.rimasti.value = 255 - document.formCMS_edit.testo.value.length;
	  oldString = document.formCMS_edit.testo.value.substring(0,255);
	}
}

/* jQuery Scroll to Top Control script */
var scrolltotop={
	setting: {startline:100, scrollduration:1000, fadeduration:[500, 100]}, //startline=number of pixels from top of doc scrollbar is at before showing control
	controlHTML: '<a href="#up_to_wrapper"> </a>', //HTML for control, which is auto wrapped in DIV w/ ID="topcontrol"
	controlattrs: {offsetx:15, offsety:15}, //offset of control relative to right/ bottom of window corner
	anchorkeyword: '#up_to_wrapper', //Enter href value of HTML anchors on the page that should also act as "Scroll Up" links

	state: {isvisible:false, shouldvisible:false},

	scrollup:function(){
		if (!this.cssfixedsupport)
			this.$control.css({opacity:0})
			this.$body.animate({scrollTop: 0}, this.setting.scrollduration);
	},

	keepfixed:function(){
		var $window=jQuery(window)
		var controlx=$window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx
		var controly=$window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety
		this.$control.css({left:controlx+'px', top:controly+'px'})
	},

	togglecontrol:function(){
		var scrolltop=jQuery(window).scrollTop()
		if (!this.cssfixedsupport)
			this.keepfixed()
			this.state.shouldvisible=(scrolltop>=this.setting.startline)? true : false
		if (this.state.shouldvisible && !this.state.isvisible){
			this.$control.stop().animate({opacity:1}, this.setting.fadeduration[0])
			this.state.isvisible=true
		}
		else if (this.state.shouldvisible==false && this.state.isvisible){
			this.$control.stop().animate({opacity:0}, this.setting.fadeduration[1])
			this.state.isvisible=false
		}
	},
	
	init:function(){
		jQuery(document).ready(function($){
			var mainobj=scrolltotop
			var iebrws=document.all
			mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode
			mainobj.$body=$('html,body')
			mainobj.$control=$('<div id="return_top">'+mainobj.controlHTML+'</div>')
				.css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', bottom:mainobj.controlattrs.offsety, right:mainobj.controlattrs.offsetx, opacity:0, cursor:'pointer'})
				.attr({title:'^ back to top of the page ^'})
				.click(function(){mainobj.scrollup(); return false})
				.appendTo('body')
			if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') //loose check for IE6 and below, plus whether control contains any text
				mainobj.$control.css({width:mainobj.$control.width()}) //IE6- seems to require an explicit width on a DIV containing text
				mainobj.togglecontrol()
			$('a[href="' + mainobj.anchorkeyword +'"]').click(function(){
				mainobj.scrollup()
				return false
			})
			$(window).bind('scroll resize', function(e){
				mainobj.togglecontrol()
			})
		})
	}
}
