String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}

function sure (msg, url) {
	if (confirm (msg))
		document.location=url;
}

function MinLength( val, minlength, filler )
{
	while( val.length < minlength )
	{
		val = filler + val;
	}
	return val;
}
function MinLengthYear( val )
{
	if( val.length == 1 )
	{
		return "200" + val;
	}
	if( val.length == 2 )
	{
		return "20" + val;
	}
	return val;
}

function FormatSqlDate( valueDate )
{	
	var arIds = new Array();
	arIds = valueDate.split( "/" );  
	if ( arIds.length == 3 )
	{
		return MinLengthYear( arIds[2] ) + MinLength( arIds[1], 2, "0" ) + MinLength( arIds[0], 2, "0" );
	}
	return "___invalid_data___";
}

function FormatSqlText( sql, control, fieldname )
{    
	if ((control) && !isEmpty(control.value)) 
  {          
        if(!isEmpty(sql))
	        sql += " AND ";
        sql += " " + fieldname + " like '%" + sqlEscape(control.value) + "%' ";        
  }
  
  return sql;
}

function Trim( str )
{
  return str.replace( / /g, "" );
}

function FormatSqlNumber( sql, value, fieldname, operator )
{    
	if(!isEmpty(sql))
		sql += " AND ";
	sql += " " + fieldname + " " + operator + " " + sqlEscape(value) + " ";
  	return sql;
}
/*
function MinorMajorNumber( minorControl, majorControl )
{
  var temp = 0;
  minorControl.value = Trim( minorControl.value );
  majorControl.value = Trim( majorControl.value );
  
  if ( minorControl.value > majorControl.value )
  {
    alert("bad");
    minorControl.focus();
    return false;
  }
  
  return true;
}
*/
function checkProfilo() {
	var err = "";	
	if (document.profilo_edit.username != null)
	{
	    var sUsername = document.profilo_edit.username.value;
	    var sPassword = document.profilo_edit.password.value;
	}
	var sEmail = document.profilo_edit.email.value;
	var sPre = document.profilo_edit.cellpre.value;
	var sNr = document.profilo_edit.cellnr.value;

	if (!isEmail(sEmail))
		err += "Inserire un indirizzo valido di email.\r\n";
	if (isEmpty(sPre) || !isAllDigit(sPre))
		err += "Inserire il prefisso.\r\n";
	if (isEmpty(sNr) || !isAllDigit(sNr))
		err += "Inserire il numero di telefono.\r\n";
		
    if (document.profilo_edit.username != null)
    {
	    if (isEmpty(sUsername))
		    err += "Inserire il nome utente.\r\n";
	    if (isEmpty(sPassword))
		    err += "Inserire la password.\r\n";
    }

	if (String(document.location).indexOf("default_popup.asp")!=-1){
		var indAddress = document.profilo_edit.indAddress.value;
		var indZip = document.profilo_edit.indZip.value;
		var indCity = document.profilo_edit.indCity.value;
		var indProvince = document.profilo_edit.indProvince.value;
		if (isEmpty(indAddress))
			err += "Inserire un indirizzo valido.\r\n";
		if (isEmpty(indZip))
			err += "Inserire un cap valido.\r\n";
		if (isEmpty(indCity))
			err += "Inserire una città valida.\r\n";
		if (isEmpty(indProvince))
			err += "Inserire una provincia valida.\r\n";
	}

	if (err.length > 0)
	{
		alert(err);
		return;
	}

	document.profilo_edit.submit();
}
function closeWindow(formname)
{
	window.opener.document.forms[formname].mode.value='U';
	window.opener.document.forms[formname].submit();
	window.close();
}
function OpenURL(szUrl, iWidth, iHeight)
{
	var iX = (window.screen.availWidth - iWidth) >> 1;
	var iY = (window.screen.availHeight - iHeight) >> 1;
	objNewWindow = window.open(szUrl,
							   "newWindow",
							   "toolbar=no,directories=no,menubar=no,scrollbars=yes,left=" + iX + ",top=" + iY + ",width=" + iWidth + ",height=" + iHeight,
							   false);
}
function OpenURL2(szUrl, iWidth, iHeight, bToolbar)
{
	bToolbar = (String(bToolbar)=="undefined")? "no" : winName;
	
	var i=0;
	var objWin = window; 
	while(objWin) {
		winName = "newWin"+i;
		i++
		objWin=objWin.opener;
		if(i>100) break;
	}
	
	var iX = (window.screen.availWidth - iWidth) >> 1;
	var iY = (window.screen.availHeight - iHeight) >> 1;
	objNewWindow = window.open(szUrl,
							   winName,
							   "toolbar="+bToolbar+",directories=no,menubar=no,scrollbars=yes,left=" + iX + ",top=" + iY + ",width=" + iWidth + ",height=" + iHeight,
							   false);
}
function OpenURL3(szUrl, iWidth, iHeight, windowname)
{
	var iX = (window.screen.availWidth - iWidth) >> 1;
	var iY = (window.screen.availHeight - iHeight) >> 1;
	objNewWindow = window.open(szUrl,
							   windowname,
							   "toolbar=no,directories=no,menubar=no,scrollbars=yes,left=" + iX + ",top=" + iY + ",width=" + iWidth + ",height=" + iHeight,
							   false);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function composeFilter(formname)
{

	var cat="";
	switch (formname) {
		case "frmMyDistrJunior":
			if ((document.forms[formname].f_indLastName) && !isEmpty(document.forms[formname].f_indLastName.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indLastName like '%" + sqlEscape(document.forms[formname].f_indLastName.value) + "%'";
			}
			if ((document.forms[formname].f_indFirstName) && !isEmpty(document.forms[formname].f_indFirstName.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indFirstName like '%"+sqlEscape(document.forms[formname].f_indFirstName.value) + "%'";
			}
			if ((document.forms[formname].f_indCellPrefix) && !isEmpty(document.forms[formname].f_indCellPrefix.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indCellPrefix like '%"+sqlEscape(document.forms[formname].f_indCellPrefix.value)+ "%'";
			}
			if ((document.forms[formname].f_indCellNumber) && !isEmpty(document.forms[formname].f_indCellNumber.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indCellNumber like '%"+sqlEscape(document.forms[formname].f_indCellNumber.value)+ "%'";
			}
			if ((document.forms[formname].f_indEmail) && !isEmpty(document.forms[formname].f_indEmail.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indEmail like '%"+sqlEscape(document.forms[formname].f_indEmail.value)+ "%'";
			}
							
			break;
		case "frmRicercheQuestionari":
			if (document.forms[formname].f_id && !isEmpty(document.forms[formname].f_id.value)) {
				if (!isNaN(document.forms[formname].f_id.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "qsId="+ document.forms[formname].f_id.value;		
				}else
					document.forms[formname].f_id.value = "";					
			}
			if (document.forms[formname].f_desc && !isEmpty(document.forms[formname].f_desc.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "Q.qsDesc like '%"+sqlEscape(document.forms[formname].f_desc.value) + "%'";
			}
			if (document.forms[formname].f_gara && (document.forms[formname].f_gara.selectedIndex>0)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "Q.qsGara=" + document.forms[formname].f_gara.options[document.forms[formname].f_gara.selectedIndex].value;
			}			
			
			break;
		case "frmFCGara":
			if (document.forms[formname].f_nomegruppo && !isEmpty(document.forms[formname].f_nomegruppo.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "G.GroupDescription like '%"+sqlEscape(document.forms[formname].f_nomegruppo.value) + "%'";	
			}
			if (document.forms[formname].f_nomeutente && !isEmpty(document.forms[formname].f_nomeutente.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "(I.indFirstName like '%"+sqlEscape(document.forms[formname].f_nomeutente.value) + "%'";	
				cat += " OR " 
				cat += "I.indLastName like '%"+sqlEscape(document.forms[formname].f_nomeutente.value) + "%')";	
			}
			if (document.forms[formname].f_puntiRis && !isEmpty(document.forms[formname].f_puntiRis.value)) {
				if (!isNaN(document.forms[formname].f_puntiRis.value)) {
					//if(!isEmpty(cat))	cat += " AND ";
					//cat += "PuntiRisp="+ document.forms[formname].f_puntiRis.value;		
				}else
					document.forms[formname].f_puntiRis.value = "";					
			}			

			if (document.forms[formname].f_puntiQuest && !isEmpty(document.forms[formname].f_puntiQuest.value)) {
				if (!isNaN(document.forms[formname].f_puntiQuest.value)) {
					//if(!isEmpty(cat))	cat += " AND ";
					//cat += "PuntiRisp="+ document.forms[formname].f_puntiQuest.value;		
				}else
					document.forms[formname].f_puntiQuest.value = "";					
			}
						
			if (document.forms[formname].f_puntiTot && !isEmpty(document.forms[formname].f_puntiTot.value)) {
				if (!isNaN(document.forms[formname].f_puntiTot.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "ia.points = "+ document.forms[formname].f_puntiTot.value;		
				}else
					document.forms[formname].f_puntiCP.value = "";					
			}			

			if (document.forms[formname].f_pos && !isEmpty(document.forms[formname].f_pos.value)) {
				if (!isNaN(document.forms[formname].f_pos.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "position="+ document.forms[formname].f_pos.value;		
				}else
					document.forms[formname].f_pos.value = "";					
			}			
			
			break;
		case "frmRicercheRisposte":
			if (document.forms[formname].f_nomegruppo && !isEmpty(document.forms[formname].f_nomegruppo.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "P.GroupDescription like '%"+sqlEscape(document.forms[formname].f_nomegruppo.value) + "%'";	
			}
			if (document.forms[formname].f_nomeutente && !isEmpty(document.forms[formname].f_nomeutente.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "(P.indFirstName like '%"+sqlEscape(document.forms[formname].f_nomeutente.value) + "%'";	
				cat += " OR " 
				cat += "P.indLastName like '%"+sqlEscape(document.forms[formname].f_nomeutente.value) + "%')";	
			}
			if (document.forms[formname].f_risposta && !isEmpty(document.forms[formname].f_risposta.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "PA.paCode like '%"+sqlEscape(document.forms[formname].f_risposta.value) + "%'";	
			}			

			break;
		case "frmRicercheDomande":
			if (document.forms[formname].f_codice && !isEmpty(document.forms[formname].f_codice.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "pollCode like '%"+sqlEscape(document.forms[formname].f_codice.value) + "%'";	
			}
			/*
			if (document.forms[formname].f_gara && !isEmpty(document.forms[formname].f_gara.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "garaCode like '%"+sqlEscape(document.forms[formname].f_gara.value) + "%'";	
			}
			*/	
			if (document.forms[formname].f_gara && (document.forms[formname].f_gara.selectedIndex>0)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "P.pollGara=" + document.forms[formname].f_gara.options[document.forms[formname].f_gara.selectedIndex].value;
			}
								
			if (document.forms[formname].f_domandaCP && !isEmpty(document.forms[formname].f_domandaCP.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "pollDesc like '%"+sqlEscape(document.forms[formname].f_domandaCP.value) + "%'";	
			}			
			if (document.forms[formname].f_domandaMP && !isEmpty(document.forms[formname].f_domandaMP.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "pollSms like '%"+sqlEscape(document.forms[formname].f_domandaMP.value) + "%'";	
			}
			if (document.forms[formname].f_testo && !isEmpty(document.forms[formname].f_testo.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "pollTesto like '%"+sqlEscape(document.forms[formname].f_testo.value) + "%'";	
			}
			break;		
		case "frmRicercheGare":
			if (document.forms[formname].f_codice && !isEmpty(document.forms[formname].f_codice.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "GaraCode like '%"+sqlEscape(document.forms[formname].f_codice.value) + "%'";	
			}
			if (document.forms[formname].f_desc && !isEmpty(document.forms[formname].f_desc.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "GaraDesc like '%"+sqlEscape(document.forms[formname].f_desc.value) + "%'";	
			}	
			if (document.forms[formname].f_dadate && !isEmpty(document.forms[formname].f_dadate.value)) {
				if (isDate(document.forms[formname].f_dadate.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "GaraStartDate >= convert(datetime,'" + document.forms[formname].f_dadate.value + "',105)";
				}else
					document.forms[formname].f_dadate.value = "";
			}	
			if (document.forms[formname].f_adata && !isEmpty(document.forms[formname].f_adata.value)) {
				if (isDate(document.forms[formname].f_adata.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "GaraEndDate <= convert(datetime,'" + document.forms[formname].f_adata.value + "',105)";
				}else
					document.forms[formname].f_adata.value = "";
			}
			if (document.forms[formname].f_nPart && !isEmpty(document.forms[formname].f_nPart.value)) {
				if (!isNaN(document.forms[formname].f_nPart.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "(GaraNIndStart >= "+ document.forms[formname].f_nPart.value;
					cat += " OR GaraNIndEnd >= "+ document.forms[formname].f_nPart.value + ")";
				}else
					document.forms[formname].f_nPart.value = "";					
			}
			if (document.forms[formname].f_nPnt && !isEmpty(document.forms[formname].f_nPnt.value)) {
				if (!isNaN(document.forms[formname].f_nPart.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "GaraNIndPnt >= "+ document.forms[formname].f_nPnt.value;
				}else
					document.forms[formname].f_nPnt.value = "";
			}	
			if (document.forms[formname].f_status && (document.forms[formname].f_status.selectedIndex>0)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "GaraStatus=" + document.forms[formname].f_status.options[document.forms[formname].f_status.selectedIndex].value;
			}

			break;
		case "frmoallestimento":
			try
			{
				if (!isEmpty(document.forms[formname].f_allestimento.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "tao.id_allestimento = "+sqlEscape(document.forms[formname].f_allestimento.value) + " ";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_negozio.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "top_negozi.negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value) + "%'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_gruppo.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "top_negozi.gruppo like '%"+sqlEscape(document.forms[formname].f_gruppo.value)+ "%'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_citta.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "top_negozi.citta like '%"+sqlEscape(document.forms[formname].f_citta.value)+ "%'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_provincia.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "top_negozi.provincia='"+sqlEscape(document.forms[formname].f_provincia.value)+ "'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_regione.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "top_negozi.regione like '%"+sqlEscape(document.forms[formname].f_regione.value)+ "%'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_cap.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "top_negozi.nrcap like '%"+sqlEscape(document.forms[formname].f_cap.value)+ "%'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_indirizzo.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "top_negozi.indirizzo like '%"+sqlEscape(document.forms[formname].f_indirizzo.value)+ "%'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_stato.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "coalesce(tao.stato_ordine, tas.id_stato) like '%"+sqlEscape(document.forms[formname].f_stato.value)+ "%'";
				}
			} catch (e) {}
			//alert(cat);
			break;
		case "frmnegozi":
			/*
			for (var obj in document.forms[formname])
			{
				if (String(obj).startsWith("f_"))
				{
					alert("obj: " + obj + " =  "  + document.getElementById(obj).value);
				}
			}
			*/
			try
			{
				if (!isEmpty(document.forms[formname].f_negozio.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value) + "%'";
				}
			} catch (e) {}
			try
			{
				if (document.forms[formname].f_tipo!=null && !isEmpty(document.forms[formname].f_tipo.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "id_tipo="+sqlEscape(document.forms[formname].f_tipo.value)+ "";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_gruppo.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "gruppo like '%"+sqlEscape(document.forms[formname].f_gruppo.value)+ "%'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_citta.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "citta like '%"+sqlEscape(document.forms[formname].f_citta.value)+ "%'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_provincia.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "provincia='"+sqlEscape(document.forms[formname].f_provincia.value)+ "'";
				}
			} catch (e) {}
			try
			{
				if (!isEmpty(document.forms[formname].f_regione.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "regione like '%"+sqlEscape(document.forms[formname].f_regione.value)+ "%'";
				}
			} catch (e) {}
			try
			{
				if (document.forms[formname].f_contatto!=null && !isEmpty(document.forms[formname].f_contatto.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "(email like '%"+sqlEscape(document.forms[formname].f_contatto.value)+ "%' or tel_prefix + '/' + tel_number like '%"+sqlEscape(document.forms[formname].f_contatto.value)+ "%')";
				}
			} catch (e) {}
			try
			{
				if (document.forms[formname].f_agenzia!=null && !isEmpty(document.forms[formname].f_agenzia.value)) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "agenzia like '%"+sqlEscape(document.forms[formname].f_agenzia.value)+ "%'";
				}
			} catch (e) {}
			try
			{
				if (document.forms[formname].l_distributore.selectedIndex>0) {
					if(!isEmpty(cat))
						cat += " AND ";
					cat += "distributore="+document.forms[formname].l_distributore.options[document.forms[formname].l_distributore.selectedIndex].value;
				}
			} catch (e) {}
			try
			{
				if (document.forms[formname].f_allestito.selectedIndex>0) {
					if(!isEmpty(cat))
						cat += " AND ";
					if (document.getElementById("f_allestito").value == 1)
					{
						cat += "tao.stato_ordine = 70";
					}
					else
					{
						cat += "(tao.stato_ordine <> 70 or tao.stato_ordine is null)";
					}
				}
			} catch (e) {}
			try
			{
				if (document.forms[formname].f_foto.selectedIndex>0) {
					if(!isEmpty(cat))
						cat += " AND ";
					if (document.getElementById("f_foto").value == 1)
					{
						cat += "coalesce(tpa.foto_1, '') != ''";
					}
					else
					{
						cat += "coalesce(tpa.foto_1, '') = ''";
					}
				}
			} catch (e) {}
			//alert(cat);
			break;

		case "frmanagrafiche":
			if ((document.forms[formname].f_cognome != null) && !isEmpty(document.forms[formname].f_cognome.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indLastname like '%"+sqlEscape(document.forms[formname].f_cognome.value) + "%'";
			}
			if ((document.forms[formname].f_nome != null) && !isEmpty(document.forms[formname].f_nome.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indFirstname like '%"+sqlEscape(document.forms[formname].f_nome.value)+ "%'";
			}
			if ((document.forms[formname].f_citta != null) && !isEmpty(document.forms[formname].f_citta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des_citta_negozio like '%"+sqlEscape(document.forms[formname].f_citta.value)+ "%'";
			}
			if ((document.forms[formname].f_negozio != null) && !isEmpty(document.forms[formname].f_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des_negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value)+ "%'";
			}
			if ((document.forms[formname].f_agenzia != null) && !isEmpty(document.forms[formname].f_agenzia.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des_agenzia like '%"+sqlEscape(document.forms[formname].f_agenzia.value)+ "%'";
			}
			if ((document.forms[formname].f_tipo != null) && !isEmpty(document.forms[formname].f_tipo.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "tipo='"+sqlEscape(document.forms[formname].f_tipo.value)+ "'";
			}
			if ((document.forms[formname].f_groupDescription!=null) && !isEmpty(document.forms[formname].f_groupDescription.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "groupDescription like '%"+sqlEscape(document.forms[formname].f_groupDescription.value)+ "%'";
			}
			break;
		case "frmworkflow":
			if (typeof(document.forms[formname].f_agente)!="undefined" && !isEmpty(document.forms[formname].f_agente.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indLastName like '%"+sqlEscape(document.forms[formname].f_agente.value) + "%'";
			}
			if (typeof(document.forms[formname].f_status)!="undefined" && !isEmpty(document.forms[formname].f_status.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "w.status="+sqlEscape(document.forms[formname].f_status.value);
			}
			if (typeof(document.forms[formname].f_nnegozio)!="undefined" && !isEmpty(document.forms[formname].f_nnegozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "n.negozio like '%"+sqlEscape(document.forms[formname].f_nnegozio.value)+ "%'";
			}
			if (typeof(document.forms[formname].f_enegozio)!="undefined" && !isEmpty(document.forms[formname].f_enegozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "e.negozio like '%"+sqlEscape(document.forms[formname].f_enegozio.value)+ "%'";
			}
			break;
		case "frmricerchenegozi":
			if ((document.forms[formname].f_societa != null) && !isEmpty(document.forms[formname].f_societa.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des_societa like '%"+sqlEscape(document.forms[formname].f_societa.value) + "%'";
			}
			if (document.forms[formname].f_tipo!=null && !isEmpty(document.forms[formname].f_tipo.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "id_tipo="+sqlEscape(document.forms[formname].f_tipo.value)+ "";
			}
			if ((document.forms[formname].f_negozio != null) && !isEmpty(document.forms[formname].f_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value) + "%'";
			}
			if ((document.forms[formname].f_gruppo != null) && !isEmpty(document.forms[formname].f_gruppo.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "gruppo like '%"+sqlEscape(document.forms[formname].f_gruppo.value)+ "%'";
			}
			if ((document.forms[formname].f_citta != null) && !isEmpty(document.forms[formname].f_citta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "citta like '%"+sqlEscape(document.forms[formname].f_citta.value)+ "%'";
			}
			if ((document.forms[formname].f_provincia != null) && !isEmpty(document.forms[formname].f_provincia.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "provincia='"+sqlEscape(document.forms[formname].f_provincia.value)+ "'";
			}
			if ((document.forms[formname].f_regione != null) && !isEmpty(document.forms[formname].f_regione.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "regione like '%"+sqlEscape(document.forms[formname].f_regione.value)+ "%'";
			}
			if ((document.forms[formname].f_agenzia != null) && !isEmpty(document.forms[formname].f_agenzia.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "agenzia like '%"+sqlEscape(document.forms[formname].f_agenzia.value)+ "%'";
			}
			if ((document.forms[formname].l_distributore != null) && document.forms[formname].l_distributore.selectedIndex>0) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "distributore="+document.forms[formname].l_distributore.options[document.forms[formname].l_distributore.selectedIndex].value;
			}
			// nuovi controllo per gestire i campi nuovi desClasse - desClasseMKTG - desChannel - desCatena - desGruppoA
			if (!isEmpty(document.forms[formname].desClasse.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "desClasse like '%"+sqlEscape(document.forms[formname].desClasse.value) + "%'";
			}
			if (!isEmpty(document.forms[formname].desClasseMKTG.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "desClasseMKTG like '%"+sqlEscape(document.forms[formname].desClasseMKTG.value) + "%'";
			}
			if (!isEmpty(document.forms[formname].desChannel.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "desChannel like '%"+sqlEscape(document.forms[formname].desChannel.value) + "%'";
			}
			if (!isEmpty(document.forms[formname].desCatena.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "desCatena like '%"+sqlEscape(document.forms[formname].desCatena.value) + "%'";
			}
			if (!isEmpty(document.forms[formname].desGruppoA.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "desGruppoA like '%"+sqlEscape(document.forms[formname].desGruppoA.value) + "%'";
			}
			break;
		case "frmelencodistributori":
			if ((document.forms[formname].f_gamma != null) && !isEmpty(document.forms[formname].f_gamma.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "gamma = '"+sqlEscape(document.forms[formname].f_gamma.value) + "'";
			}
			if ((document.forms[formname].f_negozio != null) && !isEmpty(document.forms[formname].f_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value) + "%'";
			}
			if (document.forms[formname].f_tipo!=null && !isEmpty(document.forms[formname].f_tipo.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "tipo like '%"+sqlEscape(document.forms[formname].f_tipo.value)+ "%'";
			}
			if ((document.forms[formname].f_citta != null) && !isEmpty(document.forms[formname].f_citta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "citta like '%"+sqlEscape(document.forms[formname].f_citta.value)+ "%'";
			}
			if ((document.forms[formname].f_provincia != null) && !isEmpty(document.forms[formname].f_provincia.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "provincia='"+sqlEscape(document.forms[formname].f_provincia.value)+ "'";
			}
			if ((document.forms[formname].f_regione != null) && !isEmpty(document.forms[formname].f_regione.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "regione like '%"+sqlEscape(document.forms[formname].f_regione.value)+ "%'";
			}
			if ((document.forms[formname].f_contatti != null) && !isEmpty(document.forms[formname].f_contatti.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += " ( email like '%"+sqlEscape(document.forms[formname].f_contatti.value)+ "%' OR ";
				cat += "   telefono like '%"+sqlEscape(document.forms[formname].f_contatti.value)+ "%' ) ";
			}
			break;
		case "frmricercheanagrafiche":
			if ((document.forms[formname].f_societa != null) && !isEmpty(document.forms[formname].f_societa.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des_societa like '%"+sqlEscape(document.forms[formname].f_societa.value) + "%'";
			}
			if ((document.forms[formname].f_cognome != null) && !isEmpty(document.forms[formname].f_cognome.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indLastname like '%"+sqlEscape(document.forms[formname].f_cognome.value) + "%'";
			}
			if ((document.forms[formname].f_nome != null) && !isEmpty(document.forms[formname].f_nome.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indFirstname like '%"+sqlEscape(document.forms[formname].f_nome.value)+ "%'";
			}
			if ((document.forms[formname].f_citta != null) && !isEmpty(document.forms[formname].f_citta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des_citta_negozio like '%"+sqlEscape(document.forms[formname].f_citta.value)+ "%'";
			}
			if ((document.forms[formname].f_negozio != null) && !isEmpty(document.forms[formname].f_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des_negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value)+ "%'";
			}
			if ((document.forms[formname].f_agenzia != null) && !isEmpty(document.forms[formname].f_agenzia.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des_agenzia like '%"+sqlEscape(document.forms[formname].f_agenzia.value)+ "%'";
			}
			if ((document.forms[formname].f_tipo != null) && !isEmpty(document.forms[formname].f_tipo.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "tipo='"+sqlEscape(document.forms[formname].f_tipo.value)+ "'";
			}
			if ((document.forms[formname].f_groupDescription!=null) && !isEmpty(document.forms[formname].f_groupDescription.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "groupDescription like '%"+sqlEscape(document.forms[formname].f_groupDescription.value)+ "%'";
			}

		break;
		case "frmnegoziosearch":
			if ((document.forms[formname].f_negozio != null) && !isEmpty(document.forms[formname].f_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value) + "%'";
			}
			if ((document.forms[formname].f_citta != null) && !isEmpty(document.forms[formname].f_citta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "citta like '%"+sqlEscape(document.forms[formname].f_citta.value)+ "%'";
			}
			break;
		case "frmlogazioni":
			if ((document.forms[formname].f_descr != null) && !isEmpty(document.forms[formname].f_descr.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "top_descr like '%"+sqlEscape(document.forms[formname].f_descr.value) + "%'";
			}
			/*
			if ((document.forms[formname].f_data != null) && !isEmpty(document.forms[formname].f_data.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "lastvisit like '%"+sqlEscape(document.forms[formname].f_data.value) + "%'";
			}
			if ((document.forms[formname].f_counter != null) && !isEmpty(document.forms[formname].f_counter.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "counter='"+sqlEscape(document.forms[formname].f_counter.value)+ "'";
			}
			*/
			break;
		case "frmlogutenti":
			if ((document.forms[formname].f_nome != null) && !isEmpty(document.forms[formname].f_nome.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indFirstName like '%"+sqlEscape(document.forms[formname].f_nome.value) + "%'";
			}
			if ((document.forms[formname].f_cognome != null) && !isEmpty(document.forms[formname].f_cognome.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indLastName like '%"+sqlEscape(document.forms[formname].f_cognome.value) + "%'";
			}
			/*
			if ((document.forms[formname].f_data != null) && !isEmpty(document.forms[formname].f_data.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "lastvisit like '%"+sqlEscape(document.forms[formname].f_data.value) + "%'";
			}
			if ((document.forms[formname].f_counter != null) && !isEmpty(document.forms[formname].f_counter.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "counter='"+sqlEscape(document.forms[formname].f_counter.value)+ "'";
			}
			*/
			break;
		case "frmlogs":
			if ((document.forms[formname].f_descr != null) && !isEmpty(document.forms[formname].f_descr.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "top_descr like '%"+sqlEscape(document.forms[formname].f_descr.value) + "%'";
			}
			if ((document.forms[formname].f_user != null) && !isEmpty(document.forms[formname].f_user.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "EXT.indFirstName + ' ' + EXT.indLastName like '%"+sqlEscape(document.forms[formname].f_user.value) + "%'";
			}
			if ((document.forms[formname].f_utente != null) && !isEmpty(document.forms[formname].f_utente.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "ANAG.indFirstName + ' ' + ANAG.indLastName like '%"+sqlEscape(document.forms[formname].f_utente.value) + "%'";
			}
			if ((document.forms[formname].f_negozio != null) && !isEmpty(document.forms[formname].f_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "N.negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value) + "%'";
			}
			break;
		case "frmkits":
			if ((document.forms[formname].f_kit != null) && !isEmpty(document.forms[formname].f_kit.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "k.des like '%"+sqlEscape(document.forms[formname].f_kit.value) + "%'";
			}
			if (typeof(document.forms[formname].f_tipo)!="undefined" && !isEmpty(document.forms[formname].f_tipo.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "k.tipo = "+sqlEscape(document.forms[formname].f_tipo.value);
			}

			if ((document.forms[formname].f_da != null) && !isEmpty(document.forms[formname].f_da.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), dal, 103) like '%"+sqlEscape(document.forms[formname].f_da.value) + "%'";
			}

			if ((document.forms[formname].f_a != null) && !isEmpty(document.forms[formname].f_a.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), al, 103) like '%"+sqlEscape(document.forms[formname].f_a.value) + "%'";
			}
			if (typeof(document.forms[formname].f_totale_arrotondato)!="undefined" && !isEmpty(document.forms[formname].f_totale_arrotondato.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "totale_arrotondato="+sqlEscape(document.forms[formname].f_totale_arrotondato.value);
			}
			if (typeof(document.forms[formname].f_totale_restanti)!="undefined" && !isEmpty(document.forms[formname].f_totale_restanti.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "totale_restanti="+sqlEscape(document.forms[formname].f_totale_restanti.value);
			}
			if (typeof(document.forms[formname].f_totale_ordinati)!="undefined" && !isEmpty(document.forms[formname].f_totale_ordinati.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "(totale_arrotondato-totale_restanti)="+sqlEscape(document.forms[formname].f_totale_ordinati.value);
			}
			if (typeof(document.forms[formname].f_ordinati)!="undefined" && !isEmpty(document.forms[formname].f_ordinati.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "(k.totale-k.restanti)="+sqlEscape(document.forms[formname].f_ordinati.value);
			}
			if (typeof(document.forms[formname].f_totale)!="undefined" && !isEmpty(document.forms[formname].f_totale.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "totale="+sqlEscape(document.forms[formname].f_totale.value);
			}
			if (typeof(document.forms[formname].f_restanti)!="undefined" && !isEmpty(document.forms[formname].f_restanti.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "restanti="+sqlEscape(document.forms[formname].f_restanti.value);
			}
			break;
		case "frmkitsDistr":
			if ((document.forms[formname].f_kit != null) && !isEmpty(document.forms[formname].f_kit.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "k.des like '%"+sqlEscape(document.forms[formname].f_kit.value) + "%'";
			}
			if (typeof(document.forms[formname].f_tipo_negozio)!="undefined" && !isEmpty(document.forms[formname].f_tipo_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "k.tipo_negozio = "+sqlEscape(document.forms[formname].f_tipo_negozio.value);
			}

			if ((document.forms[formname].f_da != null) && !isEmpty(document.forms[formname].f_da.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), dal, 103) like '%"+sqlEscape(document.forms[formname].f_da.value) + "%'";
			}

			if ((document.forms[formname].f_a != null) && !isEmpty(document.forms[formname].f_a.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), al, 103) like '%"+sqlEscape(document.forms[formname].f_a.value) + "%'";
			}
			if (typeof(document.forms[formname].f_ordinati)!="undefined" && !isEmpty(document.forms[formname].f_ordinati.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "(k.totale-k.restanti)="+sqlEscape(document.forms[formname].f_ordinati.value);
			}
			if (typeof(document.forms[formname].f_totale)!="undefined" && !isEmpty(document.forms[formname].f_totale.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "totale="+sqlEscape(document.forms[formname].f_totale.value);
			}
			if (typeof(document.forms[formname].f_restanti)!="undefined" && !isEmpty(document.forms[formname].f_restanti.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "restanti="+sqlEscape(document.forms[formname].f_restanti.value);
			}
			break;
		case "frmkitsordine":
			if ((document.forms[formname].f_negozio != null) && !isEmpty(document.forms[formname].f_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value) + "%'";
			}
			if (typeof(document.forms[formname].f_agente)!="undefined" && !isEmpty(document.forms[formname].f_agente.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indFirstName + ' ' + indLastName like '%"+sqlEscape(document.forms[formname].f_agente.value) + "%'";
			}
			if ((document.forms[formname].f_data_c != null) && !isEmpty(document.forms[formname].f_data_c.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), o._creato, 103) like '%"+sqlEscape(document.forms[formname].f_data_c.value) + "%'";
			}
			if ((document.forms[formname].f_stato != null) && !isEmpty(document.forms[formname].f_stato.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "coalesce(o.status,'0')='"+sqlEscape(document.forms[formname].f_stato.value) + "'";
			}
			if ((document.forms[formname].f_data_m != null) && !isEmpty(document.forms[formname].f_data_m.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), o._modificato, 103) like '%"+sqlEscape(document.forms[formname].f_data_m.value) + "%'";
			}
			if ((document.forms[formname].f_qta != null) && !isEmpty(document.forms[formname].f_qta.value) && !isNaN(document.forms[formname].f_qta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "qta="+sqlEscape(document.forms[formname].f_qta.value);
			} else
				document.forms[formname].f_qta.value='';

			// ale: aggiunto filtro materiali aggiuntivi
			if ((document.forms[formname].f_materiale != null) && !isEmpty(document.forms[formname].f_materiale.value)) 
			{
				if(!isEmpty(cat))
					cat += " AND ";
				cat += " ( m1.des_materiale like '%" + sqlEscape(document.forms[formname].f_materiale.value) + "%' or";
				cat += "  m2.des_materiale like '%" + sqlEscape(document.forms[formname].f_materiale.value) + "%' or";
				cat += "  m3.des_materiale like '%" + sqlEscape(document.forms[formname].f_materiale.value) + "%')";
				//cat += "'"+sqlEscape(document.forms[formname].f_materiale.value)+"' in (m1.des_materiale,m2.des_materiale,m3.des_materiale)";
			} else
				document.forms[formname].f_materiale.value='';

			break;
		case "frmkitscorriere":
			if ((document.forms[formname].f_kit != null) && !isEmpty(document.forms[formname].f_kit.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des like '%"+sqlEscape(document.forms[formname].f_kit.value) + "%'";
			}
			if (typeof(document.forms[formname].f_tipo)!="undefined" && !isEmpty(document.forms[formname].f_tipo.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "tipo = "+sqlEscape(document.forms[formname].f_tipo.value);
			}
			if ((document.forms[formname].f_da != null) && !isEmpty(document.forms[formname].f_da.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), dal, 103) like '%"+sqlEscape(document.forms[formname].f_da.value) + "%'";
			}
			if ((document.forms[formname].f_a != null) && !isEmpty(document.forms[formname].f_a.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), al, 103) like '%"+sqlEscape(document.forms[formname].f_a.value) + "%'";
			}
			break;
		case "frmkitsordinecorriere":
			if (typeof(document.forms[formname].f_ades)!="undefined" && !isEmpty(document.forms[formname].f_ades.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indFirstName + ' ' + indLastName like '%"+sqlEscape(document.forms[formname].f_ades.value) + "%'";
			}
			if (typeof(document.forms[formname].f_acitta)!="undefined" && !isEmpty(document.forms[formname].f_acitta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indCity like '%"+sqlEscape(document.forms[formname].f_acitta.value) + "%'";
			}
			if (typeof(document.forms[formname].f_acap)!="undefined" && !isEmpty(document.forms[formname].f_acap.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indZip like '%"+sqlEscape(document.forms[formname].f_acap.value) + "%'";
			}
			if (typeof(document.forms[formname].f_aindirizzo)!="undefined" && !isEmpty(document.forms[formname].f_aindirizzo.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indAddress like '%"+sqlEscape(document.forms[formname].f_aindirizzo.value) + "%'";
			}
			if (typeof(document.forms[formname].f_ndes)!="undefined" && !isEmpty(document.forms[formname].f_ndes.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "negozio like '%"+sqlEscape(document.forms[formname].f_ndes.value) + "%'";
			}
			if (typeof(document.forms[formname].f_ncitta)!="undefined" && !isEmpty(document.forms[formname].f_ncitta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "capcitta like '%"+sqlEscape(document.forms[formname].f_ncitta.value) + "%'";
			}
			if (typeof(document.forms[formname].f_ncap)!="undefined" && !isEmpty(document.forms[formname].f_ncap.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "capcap like '%"+sqlEscape(document.forms[formname].f_ncap.value) + "%'";
			}
			if (typeof(document.forms[formname].f_nindirizzo)!="undefined" && !isEmpty(document.forms[formname].f_nindirizzo.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "indirizzo like '%"+sqlEscape(document.forms[formname].f_nindirizzo.value) + "%'";
			}
			if (typeof(document.forms[formname].f_status)!="undefined" && !isEmpty(document.forms[formname].f_status.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "coalesce(o.status,'0') = '"+sqlEscape(document.forms[formname].f_status.value) + "'";
			}
			/*
			if (!isEmpty(document.forms[formname].f_status.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				var mode = (document.forms[formname].f_status.value == '=') ? '=2' : ((document.forms[formname].f_status.value == '>') ? '>2' : '>=2');
				cat += "coalesce(o.status,'0')"+mode;
			}*/

			break;
		case "frmkitsnegozio":
			if (typeof(document.forms[formname].f_des_kit)!="undefined" && !isEmpty(document.forms[formname].f_des_kit.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des like '%"+sqlEscape(document.forms[formname].f_des_kit.value) + "%'";
			}

			if (typeof(document.forms[formname].f_tipo_kit)!="undefined" && !isEmpty(document.forms[formname].f_tipo_kit.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "tipo = "+sqlEscape(document.forms[formname].f_tipo_kit.value);
			}

			if (typeof(document.forms[formname].f_stato)!="undefined" && !isEmpty(document.forms[formname].f_stato.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "coalesce(status,'0')='"+sqlEscape(document.forms[formname].f_stato.value) + "'";
			}

			if ((document.forms[formname].f_qta != null) && !isEmpty(document.forms[formname].f_qta.value) && !isNaN(document.forms[formname].f_qta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "qta="+sqlEscape(document.forms[formname].f_qta.value);
			} else
				document.forms[formname].f_qta.value='';

			if ((document.forms[formname].f_data_c != null) && !isEmpty(document.forms[formname].f_data_c.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), _creato, 103) like '%"+sqlEscape(document.forms[formname].f_data_c.value) + "%'";
			}
			break;
		case "frmkitordinidistributore":
			if (typeof(document.forms[formname].f_des_kit)!="undefined" && !isEmpty(document.forms[formname].f_des_kit.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "des like '%"+sqlEscape(document.forms[formname].f_des_kit.value) + "%'";
			}

			if (typeof(document.forms[formname].f_tipo_negozio)!="undefined" && !isEmpty(document.forms[formname].f_tipo_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "tipo_negozio = "+sqlEscape(document.forms[formname].f_tipo_negozio.value);
			}

			if (typeof(document.forms[formname].f_stato)!="undefined" && !isEmpty(document.forms[formname].f_stato.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "case when TOP_KITS_ORDINE.id_kit is not null and TOP_KITS_ORDINE.id_agenzia is null then 'Ordinato' when TOP_KITS_ORDINE.id_kit is not null and TOP_KITS_ORDINE.id_agenzia is not null then 'Ordinato da Agente' when dbo.DateWOTime(TOP_KITS.al) < dbo.DateWOTime(getdate()) then 'Scaduto' else '' end='"+sqlEscape(document.forms[formname].f_stato.value) + "'";
			}

			if ((document.forms[formname].f_scadenza != null) && !isEmpty(document.forms[formname].f_scadenza.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "convert(varchar(10), TOP_KITS.al, 103) like '%"+sqlEscape(document.forms[formname].f_scadenza.value) + "%'";
			}
			break;
		case "frmAllFuoriclasse":
			if ((document.forms[formname].f_groupDescription) && !isEmpty(document.forms[formname].f_groupDescription.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "groupDescription like '%" + sqlEscape(document.forms[formname].f_groupDescription.value) + "%'";
			}
			if ((document.forms[formname].f_citta) && !isEmpty(document.forms[formname].f_citta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "citta like '%"+sqlEscape(document.forms[formname].f_citta.value)+ "%'";
			}
			if ((document.forms[formname].f_provincia) && !isEmpty(document.forms[formname].f_provincia.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "provincia='"+sqlEscape(document.forms[formname].f_provincia.value)+ "'";
			}
			if ((document.forms[formname].f_agente) && !isEmpty(document.forms[formname].f_agente.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "agenzia like '%"+sqlEscape(document.forms[formname].f_agente.value)+ "%'";
			}
			if ((document.forms[formname].f_classifica) && !isEmpty(document.forms[formname].f_classifica.value)){
				if(!isNaN(document.forms[formname].f_classifica.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
					cat += "dbo.getPosizioneClassificaCP(P.groupPoints)="+sqlEscape(document.forms[formname].f_classifica.value);
			} else
				document.forms[formname].f_classifica.value='';
			}		
			if ((document.forms[formname].f_groupPoints) && !isEmpty(document.forms[formname].f_groupPoints.value)) {
				if(!isNaN(document.forms[formname].f_groupPoints.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
					cat += "groupPoints="+sqlEscape(document.forms[formname].f_groupPoints.value);
				}else	
					document.forms[formname].f_groupPoints.value='';
			}			
			
			if ((document.forms[formname].f_membriMP) && !isEmpty(document.forms[formname].f_membriMP.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "membri like '%"+sqlEscape(document.forms[formname].f_membriMP.value) + "%'";	
			}
			if ((document.forms[formname].f_classificaMP) && !isEmpty(document.forms[formname].f_classificaMP.value)){
				if(!isNaN(document.forms[formname].f_classificaMP.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "Classifica="+sqlEscape(document.forms[formname].f_classificaMP.value);
			} else
					document.forms[formname].f_classificaMP.value='';
			}		
			if ((document.forms[formname].f_indPointsMP) && !isEmpty(document.forms[formname].f_indPointsMP.value)) {
				if(!isNaN(document.forms[formname].f_indPointsMP.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "indPoints="+sqlEscape(document.forms[formname].f_indPointsMP.value);
				}else
					document.forms[formname].f_indPointsMP.value='';
			}			
			break;
		case "frmMyFuoriclasse":
			if ((document.forms[formname].f_groupDescription) && !isEmpty(document.forms[formname].f_groupDescription.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "groupDescription like '%" + sqlEscape(document.forms[formname].f_groupDescription.value) + "%'";
			}
			if ((document.forms[formname].f_membri) && !isEmpty(document.forms[formname].f_membri.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "('||'+ dbo.getMembriGruppoCP(P.FCCPgroupId) +'||') like '%||%" + sqlEscape(document.forms[formname].f_membri.value) + "%||%'";
			}
			if ((document.forms[formname].f_negozio) && !isEmpty(document.forms[formname].f_negozio.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "negozio like '%"+sqlEscape(document.forms[formname].f_negozio.value) + "%'";
			}
			if ((document.forms[formname].f_citta) && !isEmpty(document.forms[formname].f_citta.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "citta like '%"+sqlEscape(document.forms[formname].f_citta.value)+ "%'";
			}
			if ((document.forms[formname].f_provincia) && !isEmpty(document.forms[formname].f_provincia.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "provincia='"+sqlEscape(document.forms[formname].f_provincia.value)+ "'";
			}
			if ((document.forms[formname].f_regione) && !isEmpty(document.forms[formname].f_regione.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "regione like '%"+sqlEscape(document.forms[formname].f_regione.value)+ "%'";
			}
			if ((document.forms[formname].f_classifica) && !isEmpty(document.forms[formname].f_classifica.value)){
				if(!isNaN(document.forms[formname].f_classifica.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
					cat += "dbo.getPosizioneClassificaCP(groupPoints)="+sqlEscape(document.forms[formname].f_classifica.value);
			} else
				document.forms[formname].f_classifica.value='';
			}		
			if ((document.forms[formname].f_groupPoints) && !isEmpty(document.forms[formname].f_groupPoints.value)) {
				if(!isNaN(document.forms[formname].f_groupPoints.value)) {
				if(!isEmpty(cat))
					cat += " AND ";
					cat += "groupPoints="+sqlEscape(document.forms[formname].f_groupPoints.value);
				}else	
					document.forms[formname].f_groupPoints.value='';
			}
				
				
			if ((document.forms[formname].f_membriMP) && !isEmpty(document.forms[formname].f_membriMP.value)) {
				if(!isEmpty(cat))	cat += " AND ";
				cat += "Membri like '%"+sqlEscape(document.forms[formname].f_membriMP.value) + "%'";	
			}
			if ((document.forms[formname].f_classificaMP) && !isEmpty(document.forms[formname].f_classificaMP.value)){
				if(!isNaN(document.forms[formname].f_classificaMP.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "Classifica="+sqlEscape(document.forms[formname].f_classificaMP.value);
			} else
					document.forms[formname].f_classificaMP.value='';
			}		
			if ((document.forms[formname].f_indPointsMP) && !isEmpty(document.forms[formname].f_indPointsMP.value)) {
				if(!isNaN(document.forms[formname].f_indPointsMP.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "indPoints="+sqlEscape(document.forms[formname].f_indPointsMP.value);
				}else
					document.forms[formname].f_indPointsMP.value='';
			}			
							
			break;
		case "frmnegoziultimavisita":
			var activeForm = document.forms[formname];		  
				  
			cat = FormatSqlText( cat, activeForm.promoter, "promoter" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}		  
			
			cat = FormatSqlText( cat, activeForm.channel, "channel" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.classevendite, "classevendite" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.negozio, "negozio" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			
			
			cat = FormatSqlText( cat, activeForm.catena, "catena" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.gruppo, "gruppo" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.citta, "citta" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.provincia, "provincia" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.regione, "regione" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			
			if ( activeForm.fc.value =="1" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "fc <> 0";
			}
			if ( activeForm.fc.value =="0" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "fc = 0";
			}      
			

			if ((activeForm.NumeroVisiteDa) && !isEmpty(activeForm.NumeroVisiteDa.value)) 
			{
			if ( !isNumber( activeForm.NumeroVisiteDa.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.NumeroVisiteDa.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.NumeroVisiteDa.value, "NumeroVisite", ">=" );		  
			}
			
			if ((activeForm.NumeroVisiteA) && !isEmpty(activeForm.NumeroVisiteA.value)) 
			{
			if ( !isNumber( activeForm.NumeroVisiteA.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.NumeroVisiteA.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.NumeroVisiteA.value, "NumeroVisite", "<=" );
			}


			if ((activeForm.DataVisitaDa) && !isEmpty(activeForm.DataVisitaDa.value)) 
			{
			if ( !isDate( activeForm.DataVisitaDa.value ))
			{
				alert( 'Impostare la data.' );
				activeForm.DataVisitaDa.focus();
				return false;
			}
			if(!isEmpty(cat))
				cat += " AND ";
			cat += " DataVisita >= '" + sqlEscape(FormatSqlDate( activeForm.DataVisitaDa.value)) + "'";
			}
			if ((activeForm.DataVisitaA) && !isEmpty(activeForm.DataVisitaA.value)) 
			{
			if ( !isDate( activeForm.DataVisitaA.value ))
			{
				alert( 'Impostare la data.' );
				activeForm.DataVisitaA.focus();
				return false;
			}
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "DataVisita <= '" + sqlEscape(FormatSqlDate( activeForm.DataVisitaA.value)) + "'";
			}
			
			if ((activeForm.OperatoreDa) && !isEmpty(activeForm.OperatoreDa.value)) 
			{
			if ( !isNumber( activeForm.OperatoreDa.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.OperatoreDa.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.OperatoreDa.value, "Operatore", ">=" );		  
			}
			
			if ((activeForm.OperatoreA) && !isEmpty(activeForm.OperatoreA.value)) 
			{
			if ( !isNumber( activeForm.OperatoreA.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.OperatoreA.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.OperatoreA.value, "Operatore", "<=" );
			}
			
			if ((activeForm.RetailDa) && !isEmpty(activeForm.RetailDa.value)) 
			{
			if ( !isNumber( activeForm.RetailDa.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.RetailDa.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.RetailDa, "Retail", ">=" );		  
			}
			
			if ((activeForm.RetailA) && !isEmpty(activeForm.RetailA.value)) 
			{
			if ( !isNumber( activeForm.RetailA.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.RetailA.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.RetailA, "Retail", "<=" );		  
			}
			
			if ((activeForm.AccessoriDa) && !isEmpty(activeForm.AccessoriDa.value)) 
			{
			if ( !isNumber( activeForm.AccessoriDa.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.AccessoriDa.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.AccessoriDa, "Accessorio", ">=" );		  
			}
			
			if ((activeForm.AccessoriA) && !isEmpty(activeForm.AccessoriA.value)) 
			{
			if ( !isNumber( activeForm.AccessoriA.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.AccessoriA.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.AccessoriA, "Accessorio", "<=" );		  
			}
				  
			
			
			if ( activeForm.EsposizionePrivileggiata.value =="1" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "EsposizionePrivileggiata <> 0";
			}
			if ( activeForm.EsposizionePrivileggiata.value =="0" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "EsposizionePrivileggiata = 0";
			}

			if ( activeForm.GiudizioEsposizione.value =="1" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "GiudizioEsposizioneBuono <> 0";
			}
			if ( activeForm.GiudizioEsposizione.value =="0" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "GiudizioEsposizioneBuono = 0";
			}
			
			if ( activeForm.Commento.value =="1" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "Commento <> '' and not Commento is null";
			}
			if ( activeForm.Commento.value =="0" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "Commento = '' or Commento is null";
			}


			if ( activeForm.NotePromo.value =="1" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "NotePromo <> '' and not NotePromo is null";
			}
			if ( activeForm.NotePromo.value =="0" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "NotePromo = '' or NotePromo is null";
			}
			
			activeForm.filter.value=cat;
			return true;
		break;
		
		case "frmnegoziultimavisitanac":
			var activeForm = document.forms[formname];		  
				  
			cat = FormatSqlText( cat, activeForm.promoter, "promoter" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}		  
			
			cat = FormatSqlText( cat, activeForm.channel, "channel" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.classevendite, "classevendite" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.negozio, "negozio" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}			
			
			cat = FormatSqlText( cat, activeForm.gruppo, "gruppo" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.citta, "citta" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.provincia, "provincia" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			cat = FormatSqlText( cat, activeForm.regione, "regione" );		  
			if( cat == "___invalid_data___" )
			{
			return false;
			}
			
			
			if ( activeForm.fc.value =="1" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "fc <> 0";
			}
			if ( activeForm.fc.value =="0" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "fc = 0";
			}      
			

			if ((activeForm.NumeroVisiteDa) && !isEmpty(activeForm.NumeroVisiteDa.value)) 
			{
			if ( !isNumber( activeForm.NumeroVisiteDa.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.NumeroVisiteDa.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.NumeroVisiteDa.value, "NumeroVisite", ">=" );		  
			}
			
			if ((activeForm.NumeroVisiteA) && !isEmpty(activeForm.NumeroVisiteA.value)) 
			{
			if ( !isNumber( activeForm.NumeroVisiteA.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.NumeroVisiteA.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.NumeroVisiteA.value, "NumeroVisite", "<=" );
			}


			if ((activeForm.DataVisitaDa) && !isEmpty(activeForm.DataVisitaDa.value)) 
			{
			if ( !isDate( activeForm.DataVisitaDa.value ))
			{
				alert( 'Impostare la data.' );
				activeForm.DataVisitaDa.focus();
				return false;
			}
			if(!isEmpty(cat))
				cat += " AND ";
			cat += " DataVisita >= '" + sqlEscape(FormatSqlDate( activeForm.DataVisitaDa.value)) + "'";
			}
			if ((activeForm.DataVisitaA) && !isEmpty(activeForm.DataVisitaA.value)) 
			{
			if ( !isDate( activeForm.DataVisitaA.value ))
			{
				alert( 'Impostare la data.' );
				activeForm.DataVisitaA.focus();
				return false;
			}
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "DataVisita <= '" + sqlEscape(FormatSqlDate( activeForm.DataVisitaA.value)) + "'";
			}
			
			if ((activeForm.OperatoreDa) && !isEmpty(activeForm.OperatoreDa.value)) 
			{
			if ( !isNumber( activeForm.OperatoreDa.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.OperatoreDa.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.OperatoreDa.value, "Operatore", ">=" );		  
			}
			
			if ((activeForm.OperatoreA) && !isEmpty(activeForm.OperatoreA.value)) 
			{
			if ( !isNumber( activeForm.OperatoreA.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.OperatoreA.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.OperatoreA.value, "Operatore", "<=" );
			}
			
			if ((activeForm.RetailDa) && !isEmpty(activeForm.RetailDa.value)) 
			{
			if ( !isNumber( activeForm.RetailDa.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.RetailDa.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.RetailDa, "Retail", ">=" );		  
			}
			
			if ((activeForm.RetailA) && !isEmpty(activeForm.RetailA.value)) 
			{
			if ( !isNumber( activeForm.RetailA.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.RetailA.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.RetailA, "Retail", "<=" );		  
			}
			
			if ((activeForm.AccessoriDa) && !isEmpty(activeForm.AccessoriDa.value)) 
			{
			if ( !isNumber( activeForm.AccessoriDa.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.AccessoriDa.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.AccessoriDa, "Accessorio", ">=" );		  
			}
			
			if ((activeForm.AccessoriA) && !isEmpty(activeForm.AccessoriA.value)) 
			{
			if ( !isNumber( activeForm.AccessoriA.value ))
			{
				alert( 'Valore non valido. Si accettano solo valori numerici.' );
				activeForm.AccessoriA.focus();
				return false;
			}
			cat = FormatSqlNumber( cat, activeForm.AccessoriA, "Accessorio", "<=" );		  
			}
				  
			
			
			if ( activeForm.EsposizionePrivileggiata.value =="1" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "EsposizionePrivileggiata <> 0";
			}
			if ( activeForm.EsposizionePrivileggiata.value =="0" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "EsposizionePrivileggiata = 0";
			}
			
			if ( activeForm.NotePromo.value =="1" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "NotePromo <> '' and not NotePromo is null";
			}
			if ( activeForm.NotePromo.value =="0" )
			{
			if(!isEmpty(cat))
				cat += " AND ";
			cat += "NotePromo = '' or NotePromo is null";
			}
			
			activeForm.filter.value=cat;
			return true;
		break;
		
		case "frmpromoternegozi":
		  var activeForm = document.forms[formname];
		  
		  if ((activeForm.negozio) && !isEmpty(activeForm.negozio.value)) 
		  {
			  if(!isEmpty(cat))
				  cat += " AND ";
			  cat += "negozio like '%" + sqlEscape(activeForm.negozio.value) + "%'";
		  }
      
		  if ((activeForm.ragionesociale) && !isEmpty(activeForm.ragionesociale.value)) 
		  {
			  if(!isEmpty(cat))
				  cat += " AND ";
			  cat += "ragionesociale like '%" + sqlEscape(activeForm.ragionesociale.value) + "%'";
		  }
	
		  if ((activeForm.catena) && !isEmpty(activeForm.catena.value)) 
		  {
			  if(!isEmpty(cat))
				  cat += " AND ";
			  cat += "catena like '%" + sqlEscape(activeForm.catena.value) + "%'";
		  }
		  
		  if ((activeForm.gruppo) && !isEmpty(activeForm.gruppo.value)) 
		  {
			  if(!isEmpty(cat))
				  cat += " AND ";
			  cat += "gruppo like '%" + sqlEscape(activeForm.gruppo.value) + "%'";
		  }
	
		  if ((activeForm.provinciaSigla) && !isEmpty(activeForm.provinciaSigla.value)) 
		  {
			  if(!isEmpty(cat))
				  cat += " AND ";
			  cat += "provinciaSigla like '%" + sqlEscape(activeForm.provinciaSigla.value) + "%'";
		  }
	
		  if ((activeForm.citta) && !isEmpty(activeForm.citta.value)) 
		  {
			  if(!isEmpty(cat))
				  cat += " AND ";
			  cat += "(citta like '%" + sqlEscape(activeForm.citta.value) + "%'";
			  cat += " OR ";
			  cat += "indirizzo like '%" + sqlEscape(activeForm.citta.value) + "%'";
			  cat += " OR ";
			  cat += "cap like '%" + sqlEscape(activeForm.citta.value) + "%')";
		  }
		  		  
		break;

		case "frmpromotervisiteelenco":
		{
		  var activeForm = document.forms[formname];
		  
			if ((activeForm.negozio) && !isEmpty(activeForm.negozio.value)) 
			{          
				if(!isEmpty(cat))
					cat += " AND ";
				
				cat += " (catena like '%" + sqlEscape(activeForm.negozio.value) + "%'";          
				cat += " OR ";
				cat += " centroCommerciale like '%" + sqlEscape(activeForm.negozio.value) + "%'";          
				cat += " OR ";
				cat += " negozio like '%" + sqlEscape(activeForm.negozio.value) + "%'";          
				cat += " OR ";
				cat += " citta like '%" + sqlEscape(activeForm.negozio.value) + "%'";
				cat += " OR ";
				cat += " indirizzo like '%" + sqlEscape(activeForm.negozio.value) + "%'";
				cat += " OR ";
				cat += " provinciaSigla like '%" + sqlEscape(activeForm.negozio.value) + "%')";            
			}
      
			if ((activeForm.DataDa) && !isEmpty(activeForm.DataDa.value)) 
			{
				if ( !isDate( activeForm.DataDa.value ))
				{
					alert( 'Impostare la data.' );
					activeForm.DataDa.focus();
					return false;
				}
				if(!isEmpty(cat))
					cat += " AND ";
				cat += " DataVisita >= '" + sqlEscape(FormatSqlDate( activeForm.DataDa.value)) + "'";
			}
			if ((activeForm.DataA) && !isEmpty(activeForm.DataA.value)) 
			{
				if ( !isDate( activeForm.DataA.value ))
				{
					alert( 'Impostare la data.' );
					activeForm.DataA.focus();
					return false;
				}
				if(!isEmpty(cat))
					cat += " AND ";
				cat += "DataVisita <= '" + sqlEscape(FormatSqlDate( activeForm.DataA.value)) + "'";
			}
			
			if ((activeForm.Promoter) && !isEmpty(activeForm.Promoter.value)) 
			{
			  if(!isEmpty(cat))
				  cat += " AND ";
			  cat += " (FirstName like '%" + sqlEscape(activeForm.Promoter.value) + "%' OR ";
			  cat += " LastName like '%" + sqlEscape(activeForm.Promoter.value) + "%')";
			}      
      
		  	activeForm.filter.value=cat;
		  	return true;
		}		
		break;
		case "frmcatelements":
		{
		  var activeForm = document.forms[formname];
        if ((activeForm.DataDa) && !isEmpty(activeForm.DataDa.value)) 
			{
			  if ( !isDate( activeForm.DataDa.value ))
			  {
				  alert( 'Impostare la data.' );
				  activeForm.DataDa.focus();
				  return false;
			  }
			  if(!isEmpty(cat))
				  cat += " AND ";
  				
			  cat += " Data >= '" + sqlEscape(FormatSqlDate( activeForm.DataDa.value)) + "'";
			}
			if ((activeForm.DataA) && !isEmpty(activeForm.DataA.value)) 
			{
			  if ( !isDate( activeForm.DataA.value ))
			  {
				  alert( 'Impostare la data.' );
				  activeForm.DataA.focus();
				  return false;
			  }
			  if(!isEmpty(cat))
				  cat += " AND ";
			  cat += "Data <= '" + sqlEscape(FormatSqlDate( activeForm.DataA.value)) + "'";
			}		  
			

            cat = FormatSqlText( cat, activeForm.titolo, "Titolo" );		  
			if( cat == "___invalid_data___" )
			{
			  return false;
			}
			
			cat = FormatSqlText( cat, activeForm.Fabstract, "Abstract" );		  
			if( cat == "___invalid_data___" )
			{
			  return false;
			}
			
			cat = FormatSqlText( cat, activeForm.allegato, "nomeAllegato" );		  
			if( cat == "___invalid_data___" )
			{
			  return false;
			}			      
			
			if ( activeForm.testo != null )
			{
			  cat = FormatSqlText( cat, activeForm.testo, "Testo" );		  
			  if( cat == "___invalid_data___" )
			  {
			    return false;
			  }
  						  			
			}

			if ( activeForm.visibile != null )
			{
			  if ( activeForm.visibile.value =="1" )
			  {
			    if(!isEmpty(cat))
				    cat += " AND ";
			    cat += "Visibile <> 0";
			  }
			  if ( activeForm.visibile.value =="0" )
			  {
			    if(!isEmpty(cat))
				    cat += " AND ";
			    cat += "Visibile = 0";
			  }
			}
		}
		break;
		case "frmcatlist":
		{
            var activeForm = document.forms[formname];
            cat = FormatSqlText( cat, activeForm.negozio, "negozio" );		  
            if( cat == "___invalid_data___" )
            {
                return false;
            }
            
            
            cat = FormatSqlText( cat, activeForm.indirizzo, "indirizzo" );		  
            if( cat == "___invalid_data___" )
            {
                return false;
            }
            
            
            cat = FormatSqlText( cat, activeForm.provincia, "provincia" );		  
            if( cat == "___invalid_data___" )
            {
                return false;
            }
            
            
            cat = FormatSqlText( cat, activeForm.capcap, "capcap" );		  
            if( cat == "___invalid_data___" )
            {
                return false;
            }
            
            
            cat = FormatSqlText( cat, activeForm.capcitta, "capcitta" );		  
            if( cat == "___invalid_data___" )
            {
                return false;
            }
            
            
            cat = FormatSqlText( cat, activeForm.telefono, "telefono" );		  
            if( cat == "___invalid_data___" )
            {
                return false;
            }
            
            
            cat = FormatSqlText( cat, activeForm.user, "user" );		  
            if( cat == "___invalid_data___" )
            {
                return false;
            }
		}
		break;
		case "frmrallestimento":
			if (document.forms[formname].f_dataordineda && !isEmpty(document.forms[formname].f_dataordineda.value)) {
				if (isDate(document.forms[formname].f_dataordineda.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "data_mod >= convert(datetime,'" + document.forms[formname].f_dataordineda.value + "',105)";
				}else
					document.forms[formname].f_dataordineda.value = "";
			}	
			if (document.forms[formname].f_dataordinea && !isEmpty(document.forms[formname].f_dataordinea.value)) {
				if (isDate(document.forms[formname].f_dataordinea.value)) {
					if(!isEmpty(cat))	cat += " AND ";
					cat += "data_mod <= convert(datetime,'" + document.forms[formname].f_dataordinea.value + "',105)";
				}else
					document.forms[formname].f_dataordinea.value = "";
			}

		default:
			break;
	}

	document.forms[formname].filter.value=cat;	
	//alert( document.forms[formname].filter.value );
}


function checkForm(formname) {
	var err = "";
	var warn = "";
	if (document.forms[formname].mode.value == "u" ||
		document.forms[formname].mode.value == "U" ||
		document.forms[formname].mode.value == "V" ||
		document.forms[formname].mode.value == "r" ) {
		switch (formname) {
			case "frmGara":
				var sCodice = document.forms[formname].GaraCode.value;
				var sDesc = document.forms[formname].GaraDesc.value;
				var sDal = document.forms[formname].GaraStartDate.value;
				var sAl = document.forms[formname].GaraEndDate.value;
				//var sStato = document.forms[formname].GaraStatus.value;

				if (isEmpty(sCodice))
					err += "Inserire il nome della gara.\r\n";
				if (isEmpty(sDal))
					err += "Inserire una data d'inizio.\r\n";
				else if (! isDate(sDal))	
					err += "Data d'inizio non valida.\r\n";
				//if (isEmpty(sAl))
				//	err += "Inserire una data di fine.\r\n";
				//else if (! isDate(sAl))
				//	err += "Data di fine non valida.\r\n";
				
				break;
				
			case "frmnegozio":
				var sEmail = document.forms[formname].email.value;
				var sTPre = document.forms[formname].telpre.value;
				var sTNr = document.forms[formname].telnr.value;
				var sFPre = document.forms[formname].faxpre.value;
				var sFNr = document.forms[formname].faxnr.value;
				var sCPre = document.forms[formname].cellpreresp.value;
				var sCNr = document.forms[formname].cellnrresp.value;
				var sTipo = eval(document.forms[formname].id_tipo.value);
				var sNome = document.forms[formname].negozio.value;
				var sVetrine = document.forms[formname].nr_vetrine.value;
				var sCasse = document.forms[formname].nr_casse.value;
				var sCTPre = document.forms[formname].cellpretitolare.value;
				var sCTNr = document.forms[formname].cellnrtitolare.value;
				var sCC = document.forms[formname].cc;
				var sCentroCommerciale = document.forms[formname].centrocommerciale.value;
				var sCitta = document.forms[formname].citta.value;
				var sCAPID = document.forms[formname].capid.value;

				if (sTipo != null && sTipo == 0)
						err += "Inserire una tipologia.\r\n";
				if (document.forms[formname].id_agenzia != null) {
					if (document.forms[formname].id_agenzia.value == 0) {
						err += "Specificare un'agenzia.\r\n";
					}
				}
				if (isEmpty(sNome))
					err += "Inserire il nome del negozio.\r\n";
				if (!isEmpty(sCitta) && isEmpty(sCAPID))
					err += "Selezionare la Città utilizzando la selezione guidata dal link \"seleziona\".\r\n";
				if (!isEmpty(sEmail) && !isEmail(sEmail))
					err += "Inserire un indirizzo valido di email.\r\n";
				if ((!isEmpty(sTPre) || !isEmpty(sTNr)) && (!isAllDigit(sTPre) || !isAllDigit(sTNr)) )
					err += "Il numero di telefono deve essere completo e numerico.\r\n";
				if ((!isEmpty(sFPre) || !isEmpty(sFNr)) && (!isAllDigit(sFPre) || !(sFNr)) )
					err += "Il numero di fax deve essere completo e numerico.\r\n";
				if ((!isEmpty(sCPre) || !isEmpty(sCNr)) && (!isAllDigit(sCPre) || !isAllDigit(sCNr)) )
					err += "Il numero di cellulare del responsabile deve essere completo e numerico.\r\n";
				if ((!isEmpty(sCTPre) || !isEmpty(sCTNr)) && (!isAllDigit(sCTPre) || !isAllDigit(sCTNr)) )
					err += "Il numero di cellulare del titolare deve essere completo e numerico.\r\n";
				if (!isEmpty(sVetrine) && !isAllDigit(sVetrine))
					err += "Il numero di vetrine deve essere numerico.\r\n";
				if (!isEmpty(sCasse) && !isAllDigit(sCasse) )
					err += "Il numero di casse deve essere numerico.\r\n";
				if (whichIsChecked(sCC) == "1" && isEmpty(sCentroCommerciale))
					err += "Avendo specificato che il negozio è presso un centro commerciale,\nè necessario inserirne il nome.\r\n";
				if (whichIsChecked(sCC) == "0" && !isEmpty(sCentroCommerciale))
					warn += "Avendo specificato che il negozio NON è presso un centro commerciale,\nla descrizione Centro Commerciale verrà cancellata.\r\nSi desidera procedere?\r\n";
				if (!isEmpty(sCentroCommerciale) && whichIsChecked(sCC) == "") {
					sCC[0].checked;
				}
				break;

			case "frmworkflow":
				var warnmsg = document.forms[formname].warn.value;
				var id_elimina = document.forms[formname].id_elimina.value;
				if (warnmsg == "1" && id_elimina == "0")
					err += "L'agenzia ha raggiunto il limite di Negozi Top.\nE' necessario specificare un negozio Top da declassare in sostituzione del negozio appena inserito.";
				break;

			case "frmnegozioins":
				var sNomeI = document.forms[formname].negozio.value;
				document.forms[formname].citta_des.value=document.forms[formname].citta.value;
				var sCAPIDI = document.forms[formname].capid.value;
				if (isEmpty(sNomeI))
					err += "Inserire il nome del negozio.\r\n";
				if (isEmpty(sCAPIDI))
					err += "Selezionare la Città utilizzando la selezione guidata dal link \"seleziona\".\r\n";
				break;

			case "frmkit":
			case "frminskit":
				var sDes = document.forms[formname].des.value;
				var sTipo1 = document.forms[formname].tipo.value;
				var sDal = document.forms[formname].dal.value;
				var sAl = document.forms[formname].al.value;
				var sTotale = document.forms[formname].totale.value;
				var bDal = false;
				var bAl = false;
				if (isEmpty(sDes))
					err += "Inserire la descrizione del kit.\r\n";
				if (isEmpty(sTipo1))
					err += "Selezionare il tipo kit.\r\n";
				if (isEmpty(sDal) || (!isEmpty(sDal) && !isDate(sDal)))
					err += "Il campo \"disponibile dal\" deve essere una data valida.\r\n";
				else
					bDal = true;
				if (isEmpty(sAl) || (!isEmpty(sAl) && !isDate(sAl)))
					err += "Il campo \"disponibile al\" deve essere una data valida.\r\n";
				else
					bAl = true;
				if (bDal && bAl && !dal_al (sDal, sAl))
					err += "La data \"disponibile dal\" deve essere antecedente la data \"disponibile al\".\r\n";
				if (isEmpty(sTotale))
					err += "Inserire il numero totale di kit disponibile.\r\n";
				var tot = 0;
				var i;
				var till = document.forms[formname].count.value;
				for (i = 0; i < till; i++) {
					tot = tot + eval ("Number(document.forms[formname]." + eval("document.forms[formname].societa_" + i + ".value") + ".value)");
				}
				if (tot > sTotale)
					err += "Il numero totale di kit assegnati alle società eccede il numero disponibile.\r\n";
				if (tot < sTotale)
					warn += "Il numero totale di kit assegnati alle società è INFERIORE al numero disponibile.\r\nSi desidera continuare?";
				break;

			case "frmkitdistr":
				var sDes = document.forms[formname].des.value;
				//var sTipoNegozio = document.forms[formname].tipo_negozio.value;
				var sDal = document.forms[formname].dal.value;
				var sAl = document.forms[formname].al.value;
				var sTotale = document.forms[formname].totale.value;
				var bDal = false;
				var bAl = false;
				if (isEmpty(sDes))
					err += "Inserire la descrizione del kit.\r\n";
				//if (isEmpty(sTipoNegozio))
				//	err += "Tipo negozio obbligatorio.\r\n";
				if (isEmpty(sDal) || (!isEmpty(sDal) && !isDate(sDal)))
					err += "Il campo \"disponibile dal\" deve essere una data valida.\r\n";
				else
					bDal = true;
				if (isEmpty(sAl) || (!isEmpty(sAl) && !isDate(sAl)))
					err += "Il campo \"disponibile al\" deve essere una data valida.\r\n";
				else
					bAl = true;
				if (bDal && bAl && !dal_al (sDal, sAl))
					err += "La data \"disponibile dal\" deve essere antecedente la data \"disponibile al\".\r\n";
				if (isEmpty(sTotale))
					err += "Inserire il numero totale di kit disponibile.\r\n";
				var tot = 0;
				var i;
				var till = document.forms[formname].count.value;
				/*
				for (i = 0; i < till; i++) {
					tot = tot + eval ("Number(document.forms[formname]." + eval("document.forms[formname].societa_" + i + ".value") + ".value)");
				}
				if (tot > sTotale)
					err += "Il numero totale di kit assegnati alle società eccede il numero disponibile.\r\n";
				if (tot < sTotale)
					warn += "Il numero totale di kit assegnati alle società è INFERIORE al numero disponibile.\r\nSi desidera continuare?";
				*/
				break;

			case "frmkitsocieta":
				if (!ricalcola())
					err += "Il numero totale di kit assegnato eccede il numero totale disponibile.\r\n";
				document.forms[formname].calc.disabled = false;
				break;

			case "frmkitsordine":

				var tot_ordine = 0;

				var a_insert = new Array();

				var a_delete = new Array();

				var a_qta = new Array();

				var a_exist = new Array()



				var checked_disbled = document.forms[formname].checked_disabled.value;

				var kits_restanti = document.forms[formname].kits_restanti.value;

	

				if(document.forms[formname].ordine.length){

				// MULTIPLO -----------------------------------------------------

					var ordini = document.forms[formname].ordine.length;

					for (i = 0; i < ordini; i++) {

						if (document.forms[formname].ordine[i].checked) {

							if(isNaN(document.forms[formname].qta[i].value) || (document.forms[formname].qta[i].value=="")) {

								err = "Una o più quantità non sono specificate o sono errate!";

								break;

							} else

								tot_ordine += Number(document.forms[formname].qta[i].value);

							//a_insert.push(document.forms[formname].ordine[i].value);

							arrayPush(a_insert,document.forms[formname].ordine[i].value);

							arrayPush(a_qta,document.forms[formname].qta[i].value);

						}

					}



				}else{

				// SINGOLO ------------------------------------------------------

					if (document.forms[formname].ordine.checked) {

						if(isNaN(document.forms[formname].qta.value) || (document.forms[formname].qta.value=="")) {

							err = "Una o più quantità non sono specificate o sono errate!";

						} else {

							tot_ordine += Number(document.forms[formname].qta.value);

							arrayPush(a_insert,document.forms[formname].ordine.value);

							arrayPush(a_qta,document.forms[formname].qta.value);

							//alert("qta--");

						}

					}

				}



				document.forms[formname].kits_qta.value = a_qta.toString();





				if (kits_restanti < (tot_ordine - checked_disbled))

					err = "Non è possibile prenotare più di " + kits_restanti + " kit.\nTogliere " + ((tot_ordine - checked_disbled) - kits_restanti) + " prenotazioni.";

				break;
			case "frmkitsordinecorriere":
				if (document.forms[formname].mode.value == 'V') {
					if (String(document.forms[formname].evadi.length) == "undefined" )
						document.forms[formname].evadi.checked=true;
					else
					{
						for (i=0; i < document.forms[formname].evadi.length; i++)
							document.forms[formname].evadi[i].checked = true;
					}
				}
				break;
		}

		if (err.length > 0)
		{
			alert(err);
			return false;
		}
		if (warn.length > 0)
		{
			if( confirm(warn) ) {
				if (typeof(document.forms[formname].centrocommerciale)!="undefined" )
					document.forms[formname].centrocommerciale.value = "";
				return true;
			} else
				return false;
		}
		return true;
	}

	if (document.forms[formname].mode.value == "d" ) {
		var szconfirm = "";
		switch (formname) {
			case "frmnegozio":
				szconfirm = "Sei sicuro di voler eliminare il negozio?";
			break;
			case "frmworkflow":
				szconfirm = "Sei sicuro di voler eliminare il workflow?";
			break;
			case "frmkit":
				szconfirm = "Sei sicuro di voler eliminare il kit?";
			break;
		}
		if (szconfirm != "") {
			if (!confirm (szconfirm))
				return false;
		}
		return true;
	} else
		return true;
}

function selCity(formname) {
	if (formname!="undefined" && formname!="")
		formname="?formname="+formname;
	var newWindow = window.open("selCAP.asp"+formname, "Citta", "toolbar=no,directories=no,menubar=no,scrollbars=yes,width=700,height=400",false);
}

function nav(formname,offset) {
	document.forms[formname].offset.value=offset;
}

function select(formname, idvalue, value, iddescr, descr) {
	window.opener.document.forms[formname].elements[idvalue].value = value;
	window.opener.document.forms[formname].elements[iddescr].value = descr;
	window.close();
}

function setSorting (formname, order, sort) {
	document.forms[formname].order.value=order;
	document.forms[formname].sort.value=sort;
	document.forms[formname].submit();
}

function selPopup(params,title,w,h) {
	if (!isEmpty(params)) { params = "?"+params; }
	var newWindow = window.open("default_popup.asp"+params, title, "toolbar=no,directories=no,menubar=no,scrollbars=yes,width="+w+",height="+h,false);
}

function ricalcola(dis) {
	var tot = 0;
	var i;
	var till = document.forms['frmkitsocieta'].count.value;
	for (i = 0; i < till; i++) {
		tot = tot + eval ("Number(document.forms['frmkitsocieta'].tot_" + i + ".value)");
	}
	if ((document.forms['frmkitsocieta'].sum_totale.value - tot) < 0) {
		if (dis) {
			alert ("Non si hanno kit a disposizione a sufficienza.\nRidurre il numero impostato.");
			eval("document.forms['frmkitsocieta']."+dis+".select();");
		}
		return false;
	} else {
		document.forms['frmkitsocieta'].calc.disabled = false;
		document.forms['frmkitsocieta'].calc.value = Math.round (document.forms['frmkitsocieta'].sum_totale.value - tot);
		document.forms['frmkitsocieta'].calc.disabled = true;
		return true;
	}
}

function recopy() {
	var tot = 0;
	var i;
	var till = document.forms['frmkitsocieta'].count.value;
	for (i = 0; i < till; i++) {
		eval("document.forms['frmkitsocieta'].tot_" + i + ".value = Math.floor( document.forms['frmkitsocieta'].tot_auto_" + i + ".value)");
	}
	ricalcola();
}

function in_array(arrObj, element) {
	var isin = false;
	for (var i = 1; i < arrObj.length; i++) {
		if (arrObj[i] == element) {
			isin = true;
			break;
		}
	}
	return isin;
}

function dal_al (dal, al) {
	var jdate_dal = new Date();
	jdate_dal.setDate(Number(dal.substr(0,2)));
	jdate_dal.setMonth(Number(dal.substr(3,2))-1);
	jdate_dal.setFullYear(Number(dal.substr(6,4)));
	var jdate_al = new Date();
	jdate_al.setDate(Number(al.substr(0,2)));
	jdate_al.setMonth(Number(al.substr(3,2))-1);
	jdate_al.setFullYear(Number(al.substr(6,4)));
	return (jdate_dal.getTime() <= jdate_al.getTime());
}

function chooseDate(szForm,szInput,bTime)
{
	var szDate, objNewWindow;
	var leftPos = 0, topPos = 0, leftMax, topMax;

	if(String(bTime) == "undefined" || String(bTime) == "")
		bTime = 1;

	szDate = eval("document."+szForm+"." + szInput).value;
	if (!isDate(szDate)){
		szDate = "";
		eval("document."+szForm+"." + szInput).value = "";
	}
	try {
		leftPos = event.screenX;
		topPos = event.screenY;
	} catch(exception) {}

	leftMax = screen.availWidth - 140;
	//140 = dimensioni originali(130) + i bordi
	topMax = screen.availHeight - 247;
	//247 = dimensioni originali(218) + i bordi e la barra superiore
	if(leftPos > leftMax)	leftPos = leftMax;
	if(topPos > topMax)		topPos = topMax;
	objNewWindow = window.open("../commonlib/calendar.asp?szForm="+szForm+"&szInput=" + szInput + "&szDate=" + szDate +"&bWithHours=" + bTime, "Calendario", "status=no, toolbar=no, directories=no, menubar=no, scrollbars=no, top=" + topPos + ", left=" + leftPos + ",width=130,height=218,resizable=no,titlebar=no", false);
}

function arrayPush(array, value) {
	var next_index = array.length;
	array[next_index] = value;
}

function isNumber(str)
{
  var numdecs = 0;
  for (i = 0; i < str.length; i++)
  {
    var mychar = str.charAt(i);
    if ((mychar >= "0" && mychar <= "9") || mychar == ".")
    {
      if (mychar == ".")
      numdecs++;
    }
    else 
    {
      return false;
    }
  }

  if (numdecs > 1)
  {
    return false;
  }
  
  return true;
} 

function PromoterCheckDataVisita( formname)
{      
  var activeform = document.forms[ formname ];  
  var strdate = new String();
  strdate = activeform.DataVisitaDD.value + "/" + activeform.DataVisitaMM.value  + "/" + activeform.DataVisitaYYYY.value;  
  
  if ( !isDate( strdate ))
  {
    alert( 'Impostare la data.' );
    activeform.DataVisitaDD.focus();
    return false;
  }
  /*
  var dateNow = new Date();
  var theDate = new Date();
  theDate = Date(strdate);
  
  alert( "theDate="+Date(theDate).valueOf() );
  alert( "dateNow="+dateNow.valueOf());
  if ( theDate.valueOf() > dateNow.valueOf() )
  {
    var result = confirm( "Attenzione: La data inserita è una data futura.\nConferma la correttezza della data?" );
    if (!result)
    {
        activeform.DataVisitaDD.focus();
        return false;
    }
  }
  
  if ( dateNow.getFullYear() < theDate.getFullYear() )
  {
    var result = confirm( "Attenzione: La data inserita è una data futura.\nConferma la correttezza della data?" );
    if (!result)
    {
        activeform.DataVisitaDD.focus();
        return false;
    }
  }
  else
  {
    if ( dateNow.getMonth() < theDate.getMonth() )
    {
        var result = confirm( "Attenzione: La data inserita è una data futura.\nConferma la correttezza della data?" );
        if (!result)
        {
            activeform.DataVisitaDD.focus();
            return false;
        }
    }
    else
    {
        if ( dateNow.getDay() < theDate.getDay() )
        {
            var result = confirm( "Attenzione: La data inserita è una data futura.\nConferma la correttezza della data?" );
            if (!result)
            {
                activeform.DataVisitaDD.focus();
                return false;
            }
        }
    }
  }
  */
  //alert( activeform.promo );
  
  
  if ( activeform.promo != "" )
  {
    var invalidChars = new Array(':', '"');
    var message = new String();
    var s = new String();
    s = activeform.promo.value;
  
    for( i = 0; i < invalidChars.length; i++ )
    {
      if ( message != '' )
      {
        message += ', ';
      }
      message += '< ' + invalidChars[ i ] + ' >';
    }
    //alert( message );
    
    for(i = 0; i < invalidChars.length; i++ )
    { 
      for(j=0; j < s.length; j++)
      {
        //alert( s[j] + " " + invalidChars[ i ]);
        if ( s[j] == invalidChars[ i ] )
        {
            alert( 'Attenzione: Non è consentito inserire i seguenti caratteri: ' + message );
            activeform.promo.focus();
            return false;
        }      
      }
    }    
    
  }  
  
  if ( ! isNumber( activeform.retail.value ) )
  {
    alert( 'Inserisci le Referenze Prodotti Retail.' );
    activeform.retail.focus();
    return false;
  }
  
  if ( ! isNumber( activeform.operatore.value ) )
  {
    alert( 'Inserisci le Referenze Prodotti Operatore.' );
    activeform.operatore.focus();
    return false;
  }
  
  if ( ! isNumber( activeform.accessori.value ) )
  {
    alert( 'Inserisci le Referenze Prodotti Accessorio.' );
    activeform.accessori.focus();
    return false;
  }
  
  activeform.mode.value = "save";
  activeform.submit();
  return true;
}

function PromoterCheckDataVisita2( formname )
{  
    
  var activeform = document.forms[ formname ];  
  /*
  var strdate = new String();
  strdate = activeform.DataVisitaDD.value + "/" + activeform.DataVisitaMM.value  + "/" + activeform.DataVisitaYYYY.value;  
  
  if ( !isDate( strdate ))
  {
    alert( 'Impostare la data.' );
    activeform.DataVisitaDD.focus();
    return false;
  }
  
  if ( ! isNumber( activeform.retail.value ) )
  {
    alert( 'Inserisci le Referenze Prodotti Retail.' );
    activeform.retail.focus();
    return false;
  }
  
  if ( ! isNumber( activeform.operatore.value ) )
  {
    alert( 'Inserisci le Referenze Prodotti Operatore.' );
    activeform.operatore.focus();
    return false;
  }
  
  if ( ! isNumber( activeform.accessori.value ) )
  {
    alert( 'Inserisci le Referenze Prodotti Accessorio.' );
    activeform.accessori.focus();
    return false;
  }*/
  
  activeform.mode.value = "saveclose";
  activeform.submit();
  return true;
}

function PromoterGotoUpdateVisita( formname )
{
  var activeform = document.forms[ formname ]; 
  activeform.action.value = "promotervisitemod";
  activeform.mode.value = "";
  activeform.submit();
  return true;
}

function PromoterCheckRilevazioniDuplicate( formname, prefix, rilevazione, idRilevazioneSelezionata )
{
  var tags = document.getElementsByTagName( "select" );
  for( i = 0; i < tags.length; i++ )
  {
    s = tags[ i ].id;    
    if( s.indexOf( "rilevazione" + prefix, 0 ) > -1 )
    {
      if( tags[ i ].value == rilevazione && tags[ i ].value != idRilevazioneSelezionata )
      {        
        alert( "Attenzione. Il prodotto che si sta tentando di inserire è già presente nella lista." );
        return false;
      }      
    }    
  }
  
  return true;
}

function PromoterAggiungiRilevazione( formname, prefix )
{
  var activeform = document.forms[ formname ];
  var rilevazione = document.getElementById( "rilevazioneAdd" + prefix );
  var valore = document.getElementById( "valoreAdd" + prefix );  
   
  if ( rilevazione.value == "0" )
  {
    alert( "Seleziona il tipo di rilevazione." );
    rilevazione.focus();    
    return false;
  }
  
  
  if ( valore.value == "" )
  {
    alert( "Inserisci valore della rilevazione." );
    valore.focus();
    return false;
  }
  
  if ( PromoterCheckRilevazioniDuplicate( formname, prefix, rilevazione.value, 0 ) )
  {
    activeform.TipoRilevazione.value = prefix;
    activeform.mode.value = "insrilevazione";
    activeform.submit();
    return true;
  }
  return false;
}

function PromoterModificaRilevazione( formname, idVisitaRilevazione, prefix, idRilevazioneSelezionata )
{
  var activeform = document.forms[ formname ];
  var rilevazioneInput = document.getElementById( "rilevazioneAdd" + prefix );
  var valoreInput = document.getElementById( "valoreAdd" + prefix );
  var rilevazione = document.getElementById( "rilevazione" + prefix + idVisitaRilevazione );
  var valore = document.getElementById( "valore" + prefix + idVisitaRilevazione );   
  
  if ( rilevazione.value == "0" )
  {
    alert( "Seleziona il tipo di rilevazione." );
    rilevazione.focus();
    return false;
  }  
  
  if ( valore.value == "" )
  {
    alert( "Inserisci valore della rilevazione." );
    valore.focus();
    return false;
  }
  
  if ( PromoterCheckRilevazioniDuplicate( formname, prefix, rilevazione.value, idRilevazioneSelezionata ) )
  {
    rilevazioneInput.value = rilevazione.value;
    valoreInput.value = valore.value;
    activeform.idVisitaRilevazione.value = idVisitaRilevazione;
    activeform.TipoRilevazione.value = prefix;
    activeform.mode.value = "modrilevazione";  
    activeform.submit();
    return true;
  }
  return false;
}

function PromoterRimuoviRilevazione( formname, idVisitaRilevazione )
{
  var activeform = document.forms[ formname ];
  
  if ( confirm( "Cancellare la rilevazione?" ) )
  {
    activeform.idVisitaRilevazione.value = idVisitaRilevazione;
    activeform.mode.value = "cancrilevazione";  
    activeform.submit();
  }
  return true;
}



function CheckDataCATElement(formname)
{
  var activeform = document.forms[ formname ];
  alert( activeform );
  var sdate = activeform.dataDay.value + "/" + activeform.dataMonth.value + "/" + activeform.dataYear.value;
  if (!isDate(sdate)) 
  {
    alert( "La data non è valida." );
    activeform.dataDay.focus();
    return false;
  }
  
  if ( activeform.titolo.value = "" )
  {
    alert( "Inserire il titolo." );
    activeform.titolo.focus();
    return false;
  }
  
  if ( activeform.testo.value = "" )
  {
    alert( "Inserire il testo." );
    activeform.testo.focus();
    return false;
  }
  
  if ( activeform.fabstract.value = "" )
  {
    alert( "Inserire l' Abstract." );
    activeform.fabstract.focus();
    return false;
  }
  
  
  return true;
}


function CheckCATData( formname, action )
{
    var activeform = document.forms[ formname ];
    
    if ( activeform.negozio != null )
    {
        activeform.negozio.value = Trim(activeform.negozio.value);
        if ( activeform.negozio.value == "" )
        {
            alert( "Inserire il nome del negozio." );
            activeform.negozio.focus();
            return false;
        }
    }
    
    if ( activeform.regione != null )
    {        
        if ( activeform.regione.value == "-1" )
        {
            alert( "Seleziona la regione." );
            activeform.regione.focus();
            return false;
        }
    }
    
    if ( activeform.provincia != null )
    {
        if ( activeform.provincia.value == "-1" )
        {
            alert( "Seleziona la provincia." );
            activeform.provincia.focus();
            return false;
        }
    }
    
    if ( activeform.citta != null )
    {
        if ( activeform.citta.value == "-1" )
        {
            alert( "Seleziona la citta." );
            activeform.citta.focus();
            return false;
        }
    }
    
    if ( activeform.cap != null )
    {
        if ( activeform.cap.value == "-1" )
        {
            alert( "Seleziona il CAP." );
            activeform.cap.focus();
            return false;
        }
    }
        
    if ( activeform.telefono != null )
    {
        activeform.telefono.value = Trim(activeform.telefono.value);
        if ( activeform.telefono.value == "" )
        {
            alert( "Inserire il telefono." );
            activeform.telefono.focus();
            return false;
        }
    }
    
    activeform.action = action;
    activeform.mode.value = "s";
    activeform.submit();    
}

function CheckCATData2( formname, action )
{
    var activeform = document.forms[ formname ];
    
    if ( activeform.negozio != null )
    {
        activeform.negozio.value = Trim(activeform.negozio.value);
        if ( activeform.negozio.value == "" )
        {
            alert( "Inserire il nome del negozio." );
            activeform.negozio.focus();
            return false;
        }
    }
    
    if ( activeform.regione != null )
    {        
        if ( activeform.regione.value == "-1" )
        {
            alert( "Seleziona la regione." );
            activeform.regione.focus();
            return false;
        }
    }
    
    if ( activeform.provincia != null )
    {
        if ( activeform.provincia.value == "-1" )
        {
            alert( "Seleziona la provincia." );
            activeform.provincia.focus();
            return false;
        }
    }
    
    if ( activeform.citta != null )
    {
        if ( activeform.citta.value == "-1" )
        {
            alert( "Seleziona la citta." );
            activeform.citta.focus();
            return false;
        }
    }
    
    if ( activeform.cap != null )
    {
        if ( activeform.cap.value == "-1" )
        {
            alert( "Seleziona il CAP." );
            activeform.cap.focus();
            return false;
        }
    }

    if ( activeform.indirizzo != null )
    {
        activeform.indirizzo.value = Trim(activeform.indirizzo.value);
        if ( activeform.indirizzo.value == "" )
        {
            alert( "Inserire l'indirizzo." );
            activeform.indirizzo.focus();
            return false;
        }
    }
            
    if ( activeform.telefono != null )
    {
        activeform.telefono.value = Trim(activeform.telefono.value);
        if ( activeform.telefono.value == "" )
        {
            alert( "Inserire il telefono." );
            activeform.telefono.focus();
            return false;
        }
    }
    
    activeform.action = action;
    activeform.mode.value = "s";
    activeform.submit();    
}


function CheckCATElement( formname, action, tipoelemento )
{
    var activeform = document.forms[ formname ];

    var sdate = activeform.dataDay.value + "/" + activeform.dataMonth.value + "/" + activeform.dataYear.value;
    if (!isDate(sdate)) 
    {
        alert( "La data non è valida." );
        activeform.dataDay.focus();
        return false;
    }
    
    if ( activeform.titolo != null )
    {
        activeform.titolo.value = Trim(activeform.titolo.value);
        if ( activeform.titolo.value == "" )
        {
            alert( "Inserire il titolo." );
            activeform.titolo.focus();
            return false;
        }
    }
    
    if ( activeform.testo != null )
    {
        activeform.testo.value = Trim(activeform.testo.value);
        if ( activeform.testo.value == "" )
        {
            alert( "Inserire il testo." );
            activeform.testo.focus();
            return false;
        }
    }
    
    if ( activeform.fabstract != null )
    {
        activeform.fabstract.value = Trim(activeform.fabstract.value);
        if ( activeform.fabstract.value == "" )
        {
            alert( "Inserire l' Abstract." );
            activeform.fabstract.focus();
            return false;
        }
    }
    
    if( tipoelemento=="schede" || tipoelemento=="manuali")
    {
        if(activeform.nomeAllegato != null )
        {
            if ( activeform.nomeAllegato.value == "" )
            {
                alert( "Selezionare l'allegato." );
                activeform.nomeAllegato.focus();
                return false;
            }
        }
    }
    activeform.action.value = action;
    activeform.mode.value = "s";
    activeform.submit();
}

function MyConfirm( msg, link, target )
{
    if ( confirm( msg ) )
    {
        window.open(link, target );
    }
}


function Trim(TRIM_VALUE)
{
    if(TRIM_VALUE.length < 1)
    {
        return"";
    }
    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
    if(TRIM_VALUE=="")
    {
        return "";
    }
    else
    {
        return TRIM_VALUE;
    }
} //End Function

function RTrim(VALUE)
{
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if(v_length < 0)
    {
        return"";
    }
    var iTemp = v_length -1;

    while(iTemp > -1)
    {
        if(VALUE.charAt(iTemp) == w_space){
    }
    else
    {
        strTemp = VALUE.substring(0,iTemp +1);
        break;
    }
    iTemp = iTemp-1;

    } //End While
    return strTemp;

} //End Function

function LTrim(VALUE)
{
    var w_space = String.fromCharCode(32);
    if(v_length < 1)
    {
        return"";
    }
    var v_length = VALUE.length;
    var strTemp = "";

    var iTemp = 0;

    while(iTemp < v_length)
    {
        if(VALUE.charAt(iTemp) == w_space)
        {
        }
        else
        {
            strTemp = VALUE.substring(iTemp,v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    return strTemp;
} //End Function

function VerifyPhoto()
{
	var esp = document.getElementById('esposizione');
	var photo = document.getElementById('nomeImmagine');

	if((esp.checked) && (photo.value == ""))
	{
		alert('Per abilitare il campo Esposiziome è necessario selezionare prima una foto.');
		esp.checked = false;
	}

}