function trim(s) {
	var res = s.replace(/^\s*(.*)/, "$1");
	res = res.replace(/(.*?)\s*$/, "$1");
	return res;
}

function isNumber(n) {
	var validChars = "0123456789.";
	var c;
	var res = true;
 
	if (n == '') {
		res = false;
	} 
 
	for (i = 0; i < n.length && res; i++) { 
		c = n.charAt(i); 
		if (validChars.indexOf(c) == -1) {
			res = false;
		}
	}
	return res;
}

function validateEmailField(emailElem) {
	var email = emailElem.value;
	var atIndex = email.indexOf("@");
	var afterAt = email.substring((atIndex + 1), email.length);
	// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAt.indexOf(".");
	// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + atIndex + 1;
	// afterAt will be portion of string from ampersand to dot
	afterAt = email.substring((atIndex + 1), dotIndex);
	// afterDot will be portion of string from dot to end of string
	var afterDot = email.substring((dotIndex + 1), email.length);
	var beforeAmp = email.substring(0,(atIndex));
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
	// index of -1 means "not found"
	if ((email.indexOf("@") != "-1") && (email.length > 5) && (afterAt.length > 0) && (beforeAmp.length > 1) && (afterDot.length > 1) && (email_regex.test(email)) ) {
		return true;
	} else {
		return false;
	}
}

function $type(obj){
	if (obj == undefined) return false;
	if (obj.$family) return (obj.$family.name == 'number' && !isFinite(obj)) ? false : obj.$family.name;
	if (obj.nodeName){
		switch (obj.nodeType){
			case 1: return 'element';
			case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
		}
	} else if (typeof obj.length == 'number'){
		if (obj.callee) return 'arguments';
		else if (obj.item) return 'collection';
	}
	return typeof obj;
};

function $clear(timer){
	clearTimeout(timer);
	clearInterval(timer);
	return null;
};

var loadingImage = '/fe-web/img/gallery/loading.gif';
var closeButton = '/fe-web/img/gallery/close.gif'; 

function previewFoto (imgUrl) {
	// Evita la presenza dell'immagine precedente
	var image = document.getElementById('lightboximg');
	image.src = '/fe-web/img/spacer.gif';	
		
	var imageBox = document.getElementById('lightboxBox');
	image.src = imgUrl;
	Effect.Appear('lightboxBox');		
}

function clearImage(){	
	var image = document.getElementById('lightboximg');
	image.src = '/fe-web/img/spacer.gif';
}

function closePreview () {	
	Effect.Fade('lightboxBox');
	setTimeout("clearImage()",1000);		
					
}


Utility = {};

Utility.insertSwfButton = function(buttonId, text, url, isSelected){
	var swf="/fe-web/swf/buttonHeader.swf";
	var so = new SWFObject(swf, buttonId, "90", "22", "9", "#ffffff");
	so.addVariable("text", text);
	so.addVariable("itemSelected", isSelected);
	so.addVariable("url", ''+url+'');
	so.write(buttonId);
}

Utility.insertSwfButtonLeft = function(buttonId, text, url, isSelected){
	var swf="/fe-web/swf/button_laterale.swf";
	var so = new SWFObject(swf, buttonId, "160", "40", "9", "#ffffff");
	so.addVariable("text", text);
	so.addVariable("itemSelected", isSelected);
	so.addVariable("url", ''+url+'');
	so.write(buttonId);
}

Utility.actionOnClick = function(url, isDetail){
	if (!isDetail){
		document.location.href = url;
	} else {
		var sourceImg = '/pub/image.do?rule=white-border&src=/source/'+url+'&output=PNG&cache=true&interpolation=bicubic2&width=650&height=430&ext=image.png'
		previewFoto(sourceImg);
	}
	return false;
}

Utility.addClassName = function (elId, classNameOver){
	var el = $(elId);
	var className = 'image'
	var classNameOver = 'image_'+classNameOver;	
	if (el && !el.hasClassName(classNameOver)){
		el.addClassName(classNameOver);
		el.removeClassName(className);
	}
}

Utility.removeClassName = function (elId, classNameOver){
	var el = $(elId);
	var className = 'image'
	var classNameOver = 'image_'+classNameOver;	
	if (el && el.hasClassName(classNameOver)){
		el.removeClassName(classNameOver);
		el.addClassName(className);
	}
}

Utility.slideSponsor = function (containerId,mask, className ){
	var divContainerId = $(containerId);	
	var divMask = $(mask);
	if (divContainerId && divMask){
		new ProtoSlide({
			  box: divContainerId,
			  items: $$('#' + containerId + ' div.' + className),
			  mask: divMask,
			  interval: 3,
			  size: 170,
			  mode: 'horizontal',
			  walkLimit: 1,
			  autoPlay: true			 
		});
	 }
};


