/**
 * Funzioni comuni
 * @name		pa_singlephoto.js
 * @project		LiberoFoto
 * @package		javascripts
 * @author		Giulio D'Ambrosio <g.dambrosio@pisa.iol.it>, <giulio.dambrosio@poste.it>
 * @creation	Thu Dec 18 2008 15:48:10 GMT+0100 (CET)
 * @version		CVS $Id: pa_common.js,v 1.7 2010/01/12 16:13:13 giulio_dambrosio Exp $
 */
function lf_checkTA(el,opts) {
	var eopts=Object.extend({ml:250,fn:''},opts);
	var ml=eopts.ml,fn=eopts.fn,i,em='';
	if (el.value.length<ml){
		el.validValue=el.value;
		return true;
	}else{
		if (typeof el.validValue!='undefined'){
			el.value=el.validValue;
		}
		n_alert('La lunghezza massima' + (fn ? (' del campo '+fn) : ' consentita')+' e\' di '+ml+' caratteri');
		return false;
	}
}
function lf_checkTags(el,opts) {
	var eopts=Object.extend({mt:12,mtl:32},opts);
	var mt=eopts.mt,mtl=eopts.mtl,cont=el.value, tags=cont.split(/[,]/g),n=tags.length,tm=n>mt,tl=false,i,em='';
	for (i=0;i<n;i++){
		tags[i]=tags[i].trim();
		if (tags[i].length>mtl){
			em='La lunghezza massima di un tag e\' di '+mtl+' caratteri.';
			break;
		}
	}
	if (tm){
		if (em){
			em+="\n";
		}
		em+='Puoi inserire al massimo '+mt+' tags.';
	}
	if (em==''){
		el.validValue=el.value;
	}else if (typeof el.validValue!='undefined'){
		el.value=el.validValue;
	}
	if (em!='') {
		n_alert(em);
	}
	return em=='';
}
function lf_checkForm(fr,opts){
	var el,em,elt=['tags','allTags'];
	for(var i=0,n=elt.length;i<n;i++){
		if (el=$(elt[i]) && el.tagName.toLowerCase()=='input'){
			if (''!=(em=lf_checkTags(el,opts))){
				n_alert(em);
				return false;
			}
		}
	}
	return true;
}
function lf_ghostAll(onoff){
	ghostElement(onoff,document.body);
}
function lf_ghostElement(onoff,elem) {
	if (typeof elem!='object'){
		elem=$(elem);
	}
	var gelm=elem.firstChild;
	while (gelm && gelm.className!='lf_ghostingDiv') {
		gelm=gelm.nextSibling;
	}
	if (gelm && gelm.className=='lf_ghostingDiv') {
		gelm.parentNode.removeChild(gelm);
	}
	if (onoff) {
		gelm=document.createElement('div');
		gelm.className='lf_ghostingDiv';
		if ($s(elem).position!='relative'){
			$s(elem).position='relative';
		}
		$s(gelm).height=elem.scrollHeight;
		$s(gelm).width=elem.scrollWidth;
		if (elem.firstChild){
			elem.insertBefore(gelm,elem.firstChild);			
		}else{
			elem.appendChild(gelm);
		}
	}
}


