function fnMoney(valore){
	money=""
	if (valore!=""){
		money=valore.replace(".",",");
		PosVirgola=money.lastIndexOf(",");
		if (PosVirgola==-1){
			money=money + ",00";
		}else{
			money=money.substr(0,PosVirgola+3);
		}
	}
	return money;
}

//da rivedere
function fnCheckFilter(str){
	if (str.indexOf("'")==0 || str.indexOf('"')==0){
		alert("Attenzione la stringa di ricerca non puņ iniziare con i seguenti caratteri speciali: ' \"");
		return false;
	}else{
		return true;
	}
}

function fnReplaceSpecialChars(str){
	return str.replace("'","''").replace('"','""')
}

function fnLTrim(str){
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1){
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

function fnRTrim(str){
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1){
      var i = s.length - 1;
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

function fnTrim(str){
   return fnRTrim(fnLTrim(str));
}

function fnSvuotaCampi(sValore, aCampi){
	for(i=0; i<aCampi.length;i++){
		if(document.getElementById(aCampi[i]) && sValore==''){
			document.getElementById(aCampi[i]).value = '';
		}
	}
}

// da rivedere
function fnPageLoad(){
	fnSettaCampi();
}

// da rivedere
function fnSettaCampi(){
	var campo;
	for (campo in aCampi){
		if(document.getElementById(aCampi[campo][0])){
			//stili
			if (aCampi[campo][4]){
				document.getElementById(aCampi[campo][0]).className = aCampi[campo][4];
			}
			//disabled
			if (aCampi[campo][2]){
				document.getElementById(aCampi[campo][0]).disabled='true';
			}
		}
	}
}

// da rivedere
function fnIsPageValid(){
	var bIsPageValid = true;
	var campo;
	for (campo in aCampi){
		if(document.getElementById(aCampi[campo][0])){
			if (aCampi[campo][1] && Trim(document.getElementById(aCampi[campo][0]).value)==''){//obbligatoreita
				document.getElementById(aCampi[campo][0]).className = 'input-error';
				bIsPageValid = false;
			} else if (aCampi[campo][3] && !document.getElementById(aCampi[campo][0]).value.match(aCampi[campo][3])){//controllo regex
				document.getElementById(aCampi[campo][0]).className = 'input-error';
				bIsPageValid = false;
			} else {
				document.getElementById(aCampi[campo][0]).className = aCampi[campo][4];
			}
		}
	}
	return bIsPageValid;
}

function fnResizeWindow(width,height,position){
	// width,height: dimensioni del popup
	// position=0 posiziona il popup in alto a destra
	// position=1 posiziona il popup al centro schermo
	self.resizeTo(width,height);
	if (position==1){
		self.moveTo((window.screen.width - width) / 2,(window.screen.height - height) / 2);
	}else if (position==0){
		self.moveTo(0,0);
	}
}

// da rivedere
function fnResizeDialog(largo,alto) {
	window.dialogLeft = (screen.width-largo)/2;
	window.dialogTop = (screen.height-alto)/2;
	window.dialogWidth = largo + 'px';
	window.dialogHeight = alto + 'px';
}

// da rivedere
function fnOperazioniApertura(){
	opener.pseudomodal = self;
}

// da rivedere
function fnOperazioniChiusura(){
	opener.pseudomodal = undefined;
}

function lenghtFieldValidate(arrayLabel, arrayCampi, arrayLength){
	var messaggio="";
	var errore=false;
	for (i=0; i<arrayCampi.length; i++){
		if ((window.document.getElementById(arrayCampi[i]).value).length>arrayLength[i] && arrayCampi[i]!=""){
			messaggio=messaggio+"- "+arrayLabel[i]+": al massimo "+arrayLength[i]+" caratteri\n";
			errore=true;
		}
	}
	if (errore==true){
		alert("Attenzione i seguenti campi sono limitati a un certo numero di caratteri:\n" + messaggio);
		return false;
	}else {
		return true;
	}
}

function requiredCheckboxValidate(arrayLabel, arrayCampi){
	var messaggio="";
	var errore=false;
	for (i=0; i<arrayCampi.length; i++){
		if (!isChecked(arrayCampi[i]) && arrayCampi[i]!=""){
			messaggio=messaggio+"- "+arrayLabel[i]+"\n";
			errore=true;
		}
	}
	if (errore==true){
		alert("Attenzione selezionare almeno uno tra gli elementi contenuti nelle liste dei seguenti campi:\n" + messaggio);
		return false;
	}else {
		return true;
	}
}

function isChecked(field){
	result=false;
	collection_numero_ddt=window.document.getElementsByName(field);
	for (v=0;v<collection_numero_ddt.length;v++){
		//alert(collection_numero_ddt[i].value + " --- " + collection_numero_ddt[i].checked);
		if (collection_numero_ddt[v].checked==true){
			result=true;
		}
	}
	return result;
}

function requiredFieldValidate(arrayLabel, arrayCampi){
	var messaggio="";
	var errore=false;
	for (i=0; i<arrayCampi.length; i++){
		if (window.document.getElementById(arrayCampi[i]).value=="" && arrayCampi[i]!=""){
			messaggio=messaggio+"- "+arrayLabel[i]+"\n";
			errore=true;
		}
	}
	if (errore==true){
		alert("Attenzione i seguenti campi sono obbligatori:\n" + messaggio);
		return false;
	}else {
		return true;
	}
}

function rotatedFieldValidate(arrayLabel, arrayCampi){
	var messaggio="";
	var errore=true;
	for (i=0; i<arrayCampi.length; i++){
		messaggio=messaggio+"- "+arrayLabel[i]+"\n";
		if (window.document.getElementById(arrayCampi[i]).value!=""){
			errore=false;
		}
	}
	if (errore==true){
		alert("Attenzione uno dei seguenti campi deve essere riempito:\n" + messaggio);
		return false;
	}else {
		return true;
	}
}

function optionFieldValidate(form, option, arrayLabel, arrayCampi){
	for (i=0; i<arrayCampi.length; i++){
		if (window.document.forms[form][option][i].checked==true){
			break;
		}
	};	
	if (window.document.getElementById(arrayCampi[i]).value==""){
		alert("Attenzione il campo " + arrayLabel[i] + " deve essere riempito.");
		return false;
	}		
	return true;
}

function intFieldValidate(arrayLabel, arrayCampi){
	var messaggio="";
	var errore=false;
	for (i=0; i<arrayCampi.length; i++){
		if (!isNumberInt(window.document.getElementById(arrayCampi[i]).value) && arrayCampi[i]!=""){
			messaggio=messaggio + "- " + arrayLabel[i] + "\n";
			errore=true;
		}
	}
	if (errore){
		alert("Attenzione i seguenti campi devono contenere un valore intero:\n" + messaggio);
	}
	return !errore;
}

function floatFieldValidate(arrayLabel, arrayCampi){
	var messaggio="";
	var errore=false;
	for (i=0; i<arrayCampi.length; i++){
		if (!isNumberFloat(window.document.getElementById(arrayCampi[i]).value) && arrayCampi[i]!=""){
			messaggio=messaggio + "- " + arrayLabel[i] + "\n";
			errore=true;
		}
	}
	if (errore){
		alert("Attenzione i seguenti campi devono contenere un valore decimale:\n" + messaggio);
	}
	return !errore;
}
	
function toFloat(inputString){
	if (isNumberFloat(inputString) && inputString!=""){
		return parseFloat(inputString.replace(",","."));
	}else{
		return 0;
	}
}
	
function isNumberFloat(inputString){
	n=inputString.indexOf(",")
	m=inputString.lastIndexOf(",");
	s=inputString.indexOf("-");
	t=inputString.lastIndexOf("-");
	if (n==m && s==t) {
		if (s!=0 && inputString.charAt(s)=="-"){
			return false;
		}
		for (x=0; x<inputString.length; x++){
			if (isNaN(inputString.charAt(x)) && inputString.charAt(x)!="," && inputString.charAt(x)!="-"){
				return false;
			}
		}
		return true;
	}
	return false;
}

function isNumberInt(inputString){
	s=inputString.indexOf("-");
	t=inputString.lastIndexOf("-");
	if (s==t) {
		if (s!=0 && inputString.charAt(s)=="-"){
			return false;
		}		
		for (x=0; x<inputString.length; x++){
			if (isNaN(inputString.charAt(x))){
				return false;
			}
		}

		return true;
	}
	return false;
}

function isEmail(inputString) {
	//regular expression per controllo email
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(inputString));
}
	
function emailFieldValidate(arrayLabel, arrayCampi){
	var messaggio="";
	var errore=false;
	for (i=0; i<arrayCampi.length; i++){
		if (!isEmail(window.document.getElementById(arrayCampi[i]).value) && arrayCampi[i]!=""){
			messaggio=messaggio+"- "+arrayLabel[i]+"\n";
			errore=true;
		}
	}
	if (errore==true){
		alert("Attenzione i seguenti campi devono contenere una e-mail valida:\n" + messaggio);
		return false;
	}else {
		return true;
	}
}

// da rivedere
function fnInsert(){
	if(fnIsPageValid()){
		window.parent.document.getElementById('self-form').submit();
	} else {
		document.getElementById('msgPagina').innerHTML = 'Errore di compilazione';
	}
}

// da rivedere
function fnModifica(){
	if(fnIsPageValid()){
		window.parent.document.getElementById('self-form').submit();
	} else {
		document.getElementById('msgPagina').innerHTML = 'Errore di compilazione';
	}
}

// da rivedere
function fnElimina(){
	if(fnIsPageValid()){
		window.parent.document.getElementById('self-form').submit();
	} else {
		document.getElementById('msgPagina').innerHTML = 'Errore di compilazione';
	}
}

function fnVai(sUrl){
	window.location = sUrl;
}

// da rivedere
function fnCheckTutto(id,form){
	for (var i=0;i<form.elements.length;i++){
		if(form.elements(i).type == "checkbox"){
			if(form.elements(i).id.split("~")[0] == id) {
				form.elements(i).checked = "yes";
			}
		}
	}
}

// da rivedere
function fnUnCheckTutto(id,form){
	for (var i=0;i<form.elements.length;i++){
		if(form.elements(i).type == "checkbox"){
			if(form.elements(i).id.split("~")[0] == id) {
				form.elements(i).checked = "";
			}
		}
	}
}

function fnOra(fieldName,current_time){
	pseudomodal=window.open('/script/time_picker.asp?fieldName='+fieldName+'&current_time='+current_time,'time_picker','width=1,height=1,titlebar=no,status=no,menubar=no,scrollbars=no');
	pseudomodal.focus();
}

function fnOpenWindow(myUrl,myName,myTop,myLeft,myWid,myHei,myMenu,myStatus,myScrolls,myTool){
	var pseudomodal;
	var winOpts;
	if (myTop=='') myTop=0;
	if (myLeft=='') myLeft=0;
	if (myWid=='') myWid=800;
	if (myHei=='') myHei=600;
	if (myMenu=='') myMenu='no';
	if (myStatus=='') myStatus='no';
	if (myScrolls=='') myScrolls='no';
	if (myTool=='') myTool='no';
	winOpts="top="+myTop+",left="+myLeft+",width="+myWid+",height="+myHei+",menubar="+myMenu+",status="+myStatus+",scrollbars="+myScrolls+",toolbar="+myTool;
	pseudomodal=window.open(myUrl,myName,winOpts);
	pseudomodal.focus();
//  se si vuole aprire come finestra MODALE:
//	winOpts = "dialogTop:0px; dialogLeft:0px; dialogWidth:" + myWid + "px;dialogHeight:" + myHei
//		+ "px; help:No; resizable:No; status:No";
//	win = window.showModalDialog(myUrl,'',winOpts);	
}
