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('close');
		}
	});

	$('#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.id?arr[i].field.id:arr[i].field.name)+'\'');
			return 0;
		}
		 
		if(arr[i].preParse) {
			eval(arr[i].preParse);
		}
		
		if (!arr[i].toMatch) 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();}
}

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

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;	
}

