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 setPaddingElement(idElement, sizeChars1, sizeChars2){
	var el = $(idElement);
	
	var w = el.getWidth();
	var h = el.getHeight();
	var he = el.getStyle('height');
	
	if(sizeChars1 > 0 || sizeChars2 > 0){
		var fontSize = el.getStyle('font-size');
		
		fontSize = fontSize.substring(0, fontSize.indexOf('px'));	
		if(fontSize.indexOf('.')>0){
			var dot = fontSize.indexOf('.');
			decimal = fontSize.substring(dot+1, dot+3);
			
			if (parseInt(decimal) < 50){
				fontSize = parseInt(fontSize)+1;
			}
		}
		
		// Quanti caratteri vanno su una riga
		var c = w / parseInt(fontSize);		
		if (c != 0){
			var setPadding = false;
			// Numero di righe
			var rows1 = 0;
			var rows2 = 0;
			if (sizeChars1 > 0) {
				rows1 = sizeChars1 / c ;	
				if (rows1 >= 2){
					setPadding = true;
				}
			}			
			if (sizeChars2 > 0){
				rows2 = sizeChars2 / c ;	
				if (rows2 >= 2){
					setPadding = true;
				}
			}
			if (setPadding == true){
				rows = rows1 + rows2;
				var line = el.getStyle('line-height');
				line = line.substring(0, line.indexOf('px'));
				var pTop = el.getStyle('padding-top');
				pTop = pTop.substring(0, pTop.indexOf('px'));
				var pBot = el.getStyle('padding-bottom');
				pBot = pBot.substring(0, pBot.indexOf('px'));
				
				if ( rows >= 2 && rows < 3){							
					el.setStyle({paddingTop: '10px', paddingBottom: '10px' });
				}
				if ( rows >= 3 ){
					var pBottom = parseInt(h) - (parseInt(fontSize) * 2) - (parseInt(line)*2) - 2;					
					el.setStyle({paddingTop: '2px', paddingBottom: '8px'  });
				}
			}
		}
	}
	return false;
}

function swapImageInGallery(idLink){
	/*$$('#handles4 span a.linkOn').each(
		function(link){
			link.removeClassName('linkOn');
			link.addClassName('linkOff');
			link.onmouseout=function(){MM_swapImgRestore();};
			var imageOff = link.down();			
			imageOff.src = '/fe-web/img/square_off.png';
		});
	$(idLink).removeClassName('linkOff');
	$(idLink).addClassName('linkOn');
	$(idLink).onmouseout='';
	var imageOn = $(idLink).down();
	imageOn.src='/fe-web/img/square_on.png';*/
	return false;
}

/**
 * leading whitespace -> null,trailing whitespace -> null,multiple whitespace -> space
 * 
 * @param inStr String
 * @returns String
 */
function eraseSpace(inStr) {
 	var val = (arguments.length) ? (inStr) ? inStr : '' : this.toString(); // if called as a method
	    val = val.replace(/^[\s]+/g,'');    // leading whitespace -> null
		val = val.replace(/[\s]+$/g,'');    // trailing whitespace -> null
		val = val.replace(/[\s]{2,}/g,' '); // multiple whitespace -> space
		return val;
}

/**
 * Calcola l'altezza del contenuto di un iframe e setta l'iframe con l'altezza calcolata.
 * Questo evita scrollbar nell'iframe.
 * Settare l'iframe nel seguente modo (width puo' essere qualsiasi valore non verrà resettato):
 * <iframe width="100%" id="the_iframe" onLoad="changeIFrameHeight('the_iframe');" src="testing_page.shtml" scrolling="NO" frameborder="0" height="1">
 */
function changeIFrameHeight(iframeId)
{
  //find the height of the internal page
  var iframe = document.getElementById(iframeId); 	
  var the_height = iframe.contentWindow.document.body.scrollHeight;
  //change the height of the iframe
  iframe.height = the_height;
}


/*
 * Mi restituisce true se il tasto   premuto è invio
 * */
function isSubmit(event){
	if(event.keyCode==13){
		return true;
	}
	return false;
}


