function palert(message,param) {
  $(document).ready(function() {

	// Creazione della finestra div se non presente.
	if (!document.getElementById('dialog')) {
		var mydiv=document.createElement('div');
		mydiv.setAttribute('id','dialog');
		//var mybody=document.getElementsByTagName('body')[0].appendChild(mydiv);
		document.body.appendChild(mydiv);
		$.ui.dialog.defaults.bgiframe = true;
	}
	
	if (!param) param=new Array();

	// Impostazione del messaggio
	// Chiedere Cristian uno style per l'allineamento corretto.
	document.getElementById('dialog').innerHTML='<span style="height:100%; width:100%; vertical-align:middle; text-align:center"></span>'+message+'</span>';
	document.getElementById('dialog').title=param['title']?param['title']:document.title;
	

	var overlay = param['overlay']?param['overlay']:new Object({
		backgroundColor: '#000',
		opacity: 0.5
	});



	var buttons = param['buttons']?param['buttons']:new Object({
		'Chiudi': function() {
			$(this).dialog('destroy');
		}
	});

	$('#dialog').dialog({ 
			autoOpen: false, 
			modal: true,
			buttons: buttons,
			overlay: overlay
	});
	
	$('#dialog').dialog( 'open' );
 });
}


function checkModulo(arr) {
	for (var i = 0; i < arr.length ; i++ ) {
		//var field=arr[i].field instanceof Object?arr[i].field:document.getElementById(arr[i].field);
		var field=document.getElementById(arr[i]['field']);
		if(!field) {
			palert('ERR CM001: campo non trovato (\''+arr[i]['field']+'\')\''+(arr[i]['field'].id?arr[i]['field'].id:arr[i]['field'].name)+'\'');
			return 0;
		}
		 
		if(arr[i].preParse) {
			eval(arr[i].preParse);
		}

		if (arr[i].toMatch==null) arr[i].toMatch=new RegExp(/./); // Se non specificato verifico che il campo sia almeno obbligatorio
		
		if(!field.value.match(arr[i].toMatch)){
			if(arr[i].actionFailed) {
				eval(arr[i].actionFailed);
			} else {
				palert('Errore compilazione campo:' + (field.name?field.name:field.id));
			}
			//field.focus();
			return 0;
		} else if(arr[i].actionOk) eval(arr[i].actionOk);
	}
	return 1;
}

function setToErr(el) {
	el.className+=el.className.match(/form-error/i)?'':' form-error';
	if(el.type=='text') {el.focus();}
	return 1;
}

function setToOk(el) {
	el.className = el.className.replace( 'form-error','');
	return 1;
}

function getEl(id) {
	return document.getElementById(id);
}

function controllaPIVAFacoltativa(pi) {
	if( pi == '' ) return 1;
	return controllaPIVA(pi);
}

function controllaPIVA(pi){
	if ( !pi.match(/^\d{11}$/) ) {
		return 0;
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 ) s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 ) c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) ) {
		return 0;
	}
	return 1;	
}


jQuery.fn.autoCrop = function(a){
	var imgarr=this;
	return $(imgarr).each(function() {
		//return $(this).load(function(){
			var dwidth = 0;
			var dheight = 0;
			var parent = $(this).parent();
			while ( ( dheight == 0) || (dheight=='auto') ) {
				dwidth = $(parent).css('width').replace('px','');
				dheight = $(parent).css('height').replace('px','');
				parent = $(parent).parent();
			}

			var swidth = $(this).width();
			var sheight =$(this).height();
			
			var sratio=swidth/sheight;
			var dratio=dwidth/dheight;

			// Resize
			
			
			// Contenitore orizzontale pił piccolo.
			if ( (dratio >= 1) && (dwidth < swidth) ) {
				if (sratio < dratio) {	// Immagine verticale ?
					//alert('o/v');
					$(this).attr('width',dwidth);
				} else {	// Immagine orizzontale ?
					//alert('o/o'+sratio);
					$(this).attr('height',dheight);
					//$(this).attr('width',dwidth);
				}
			}
			
			// Contenitore verticale pił piccolo.
			if ( (dratio < 1) && (dwidth < swidth) ) {
				if (sratio < dratio) {	// Immagine verticale ?
					//alert('v/o');
					$(this).attr('height',dheight);
				} else {			// Immagine orizzontale ?
					//alert('v/v');
					$(this).attr('width',dwidth);
					//$(this).attr('height',dheight);
				}
			}
			
			// Posizionamento 
			//$(this).wrap('<div style="position:relative; height:'+dheight+'px; width:'+dwidth+'px; overflow:hidden; background-color: #fff;"></div>');			
			$(this).wrap('<div style="position:relative; height:'+dheight+'px; width:'+dwidth+'px; overflow:hidden; "></div>');
			$(this).css('position','absolute');
			
			if ( (dratio >= 1) && (sratio < 1) ) {
				// Contenitore orizzontale / Immagine verticale
				$(this).css('top','0');
				$(this).css('left',Math.floor((dwidth-($(this).width()))/2));
			} else {
				$(this).css('top',Math.floor((dheight-($(this).height()))/2));
				$(this).css('left',Math.floor((dwidth-($(this).width()))/2));
			}
		//});
	});		
}

function isMail(txt) {
	if (txt.match(/^[a-z0-9\.\-_\+]{2,}@[a-z0-9\.\-_]{2,}\.[a-z]{2,4}$/i)) return true;
	return false;
}
