/**
  Replace ',' with '.'. It's used before a checkField on a numeric to
  accept comma as decimal separator and display .
*/
function replaceComma(field) {
   if (field.value.length != 0)
      field.value = field.value.replace(/,/,".");
}

function replaceCommaVal(val) {
   return val.replace(/,/,".");
}
/**
  Replace '.' with ','. It's used before a checkField on a numeric to
  accept comma as decimal separator and display ,
*/
function replaceDot(field) {
   if (field.value.length != 0)
      field.value = field.value.replace(/\./,",");
}
/**
* field: reference to html object
* label: String that describe the html object
* check mandatory field. The method uses the label to compose error message
*/
function checkMandatoryLabel(field,label)
{
	if((field.type=='select-one')||(field.type=='select-multiple'))
	{
		if((field.selectedIndex==-1)||(field.options[field.selectedIndex].value=="")||(field.options[field.selectedIndex]==null))
		{
			alert("Campo obbligatorio non selezionato: " + label);
			field.focus();
			return false;
		}
		return true;
	}
	if((field.type=='text')||(field.type=='textarea')||(field.type=='hidden')||(field.type=='file')||(field.type=='password'))
	{
		if(field.value=="")
		{
			alert("Campo obbligatorio: "+ label);
			if(field.type!='hidden')
				field.focus();
			return false;
		}
		return true;
	}
	alert(field.type);
	return false;
}

function checkMandatoryNoLabel(field)
{
	if((field.type=='select-one')||(field.type=='select-multiple'))
	{
		if((field.selectedIndex==-1)||(field.options[field.selectedIndex].value=="")||(field.options[field.selectedIndex]==null))
		{
                  return false;
		}
		return true;
	}
	if((field.type=='text')||(field.type=='textarea')||(field.type=='hidden')||(field.type=='file'))
	{
		if(field.value=="")
		{
			return false;
		}
		return true;
	}
	return false;
}
/**
* field: reference to html object
* check mandatory field. The method uses the the name of html object to compose error message
*/
function checkMandatory(field)
{
        return checkMandatoryLabel(field,field.name);
}

/**
* field: reference to html object
* tipo:  the type of data to check (number, numberInt,numberComma, date)
* label: String that describe the html object
* check if the value inserted in html object is of the type tipo
*       The method uses the label to compose error message
*/
function checkFieldLabel(field,tipo,campo)
{
// NB: if the field is empty no check is done
        var data = field.value;
	if (data.length == 0) return (true);
	if(tipo=='number' || tipo=='numberComma' || tipo=='numberInt')
	{
// Check for numeric                
		var numStr="-0123456789.";
                if (tipo=='numberComma') 
		    numStr="-0123456789,";
		if (tipo=='numberInt') 
		    numStr="-0123456789";
		var thisChar;
		var counter=0;
		for (var i=0; i < data.length; i++)
		{
			thisChar=data.substring(i, i+1);
			if (numStr.indexOf(thisChar) != -1) counter ++;
		}
		if (counter == data.length) 
			return(true);
		else
		        if (tipo=='numberInt')
			   alert("Campo non numerico intero: "+ campo);
			else
			   alert("Campo non numerico: "+ campo);
			field.focus();
			field.select();	
			return(false);
	}
	if(tipo=='date')
	{
// Check for date in the format dd/mm/yyyy or dd-mm-yyyy
		// Declare Variables
  		var year = 0;
		var month = 0;
		var day = 0;
		var leap = false;
		var temp;
		var flag = false;
		var pattern = /^([0-9-])/;
	
		// Check String length
	
        	if ((data.length > 10) || (data.length < 8))
		{
 			alert("Data errata: "+ campo);
			field.focus();
			field.select();
			return(false);
		}
		// Convert String into D-M-Y

		array = new Array(); 

		for (var i = 0; i<data.length;i++) 
		{ 
			temp=data.substring(i, i+1,0);
			array[i]=temp;
			flag = pattern.test(array[i]);
			if (flag==false)
			{
 				alert("Data errata: "+ campo);
				field.focus();
				field.select();
				return(false);	
			}	
			flag=false;	
		}	 

        	var i = 0;
		var c = 0;
		var temp=data.toString();
		var len = temp.length; 
        	for (i = 0; i < len; i++)
			if ((c = temp.charAt(i)) >= '0' && c <= '9')
				day = day * 10 + parseInt(c) - '0';
			else
				break;
		for (i++; i < len; i++)
			if ((c = temp.charAt(i)) >= '0' && c <= '9')
				month = month * 10 + parseInt(c) - '0';
			else
				break;
		for (i++; i < len; i++)
			if ((c = temp.charAt(i)) >= '0' && c <= '9')
				year = year * 10 + parseInt(c) - '0';
			else
				break;
// Check to see if leap year	
		if (year % 4 == 0)
			leap = true;

// Check year
		if (year >2050 || year < 1900)
		{
			alert("Data errata: "+ campo);
			field.focus();
			field.select();
			return(false);
		}
// Check Month
		if (month < 01 || month > 12)
		{
			alert("Data errata: "+ campo);
			field.focus();
			field.select();
			return(false);
		}
//Check Days
		if (leap == true)
		{
			if (month == 02)
			{
		
				if ((day < 01 )||( day > 29))
				{
					return(false);
				}
			}
		}
		if ((month == 02)&&(leap==false))
		{
			if ((day < 01 )||( day > 28))
			{
				alert("Data errata: "+ campo);
				field.focus();
				field.select();
				return(false);
			}
		}
		if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
		{
			if (day < 1 || day > 30)
			{
				alert("Data errata: "+ campo);
				field.focus();
				field.select();
				return(false);
			}
		}
		if ((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
		   (month == 8)||(month == 10) || (month == 12))
		{
			if (day < 1 || day > 31)
			{
				alert("Data errata: "+ campo);
				field.focus();
				field.select();
				return(false);
			}
		}	

//                field.value = "" + day + "/" + month + "/" + year	
		return(true);
  	}
}

/**
* field: reference to html object
* tipo:  the type of data to check (number, date)
* check if the value inserted in html object is of the type tipo
*       The method uses the name of html object to compose error message
*/
function checkField(field,tipo)
{
        return ckeckFieldLabel(field,tipo,field.name);
}

/**
*  Printing the content of a page
*/
function printDocument(Obj){
	if (confirm("Confirm the print?")) {
	    Obj.style.display='none';
	    window.print();
	    Obj.style.display='';
	}
}

/**
  date1: is a field html containing a date (start date)
  date2: is a field html containing a date (end date)
  return: true if date2 is greater or equal than date1
          false otherwise
*/
function dateCompare(date1,date2, msg) {
	
		var sDay,eDay,sMonth,eMonth,sYear,eYear;
		var eday, emonth, eyear, sday, smonth, syear;
		var endDate = new String();
		var startDate = new String();
		if (date2.value.length < 10) {
			if(date2.value.length == 8) {
				eday = date2.value.substring(0,1);
				emonth = date2.value.substring(2,3);
				eyear = date2.value.substring(4);
				endDate = "0"+eday+"-0"+emonth+"-"+eyear;
			}
			else if(date2.value.length == 9) {
				if(date2.value.substring(1,2) == "-") {
					eday = date2.value.substring(0,1);
					emonth = date2.value.substring(2,4);
					eyear = date2.value.substring(5);
					endDate = "0"+eday+"-"+emonth+"-"+eyear;
				}
				if(date2.value.substring(2,3) == "-") {
					eday = date2.value.substring(0,2);
					emonth = date2.value.substring(3,4);
					eyear = date2.value.substring(5);
					endDate = eday+"-0"+emonth+"-"+eyear;
				}
			}
		
		} 
		else if (date2.value.length == 10) {
			endDate = date2.value;
		}
		if(date1.value.length < 10) {
			if(date1.value.length==8) {
				sday = date1.value.substring(0,1);
				smonth = date1.value.substring(2,3);
				syear = date1.value.substring(4);
				startDate = "0"+sday+"-0"+smonth+"-"+syear;
			}
			else if(date1.value.length==9) {
				if(date1.value.substring(1,2) == "-") {
					sday = date1.value.substring(0,1);
					smonth = date1.value.substring(2,4);
					syear = date1.value.substring(5);
					startDate = "0"+sday+"-"+smonth+"-"+syear;
				}
				if(date1.value.substring(2,3) == "-") {
					sday = date1.value.substring(0,2);
					smonth = date1.value.substring(3,4);
					syear = date1.value.substring(5);
					startDate = sday+"-0"+smonth+"-"+syear;
				}
			}
		

		}

		else if (date1.value.length == 10) {
			startDate = date1.value;
	 	}
		

		if (date1.value.length != 0) {
			sDay = startDate.substring(0,2);
			sMonth = startDate.substring(3,5);
			sYear = startDate.substring(6);
		}   

		if (date2.value.length != 0) {
			eDay = endDate.substring(0,2);
			eMonth = endDate.substring(3,5);
			eYear = endDate.substring(6);
		}
		
		if (date2.value.length == 0 || date1.value.length == 0)
			return true;
		if (sYear < eYear)
			return true;
		if (sYear > eYear) {
			alert(msg);         
			return false;
		}
		if (sMonth < eMonth) 
			return true;
		if (sMonth > eMonth) {
			alert(msg);         
			return false;
		}
		if (sDay <= eDay)
			return true;
		if (sDay > eDay) {
			alert(msg);         
			return false;
		}		
}  


/**
  date1: is a field html containing a date (start date)
  date2: is a field html containing a date (end date)
  return: true if date1 opeRel than date2 
          false otherwise
          Consider date only YYYYMM
*/
function dateCompareYYMM(date1,opRel,date2) {
	
		var sDay,eDay,sMonth,eMonth,sYear,eYear;
		var eday, emonth, eyear, sday, smonth, syear;
		var endDate = new String();
		var startDate = new String();
		if (date2.value.length < 10) {
			if(date2.value.length == 8) {
				eday = date2.value.substring(0,1);
				emonth = date2.value.substring(2,3);
				eyear = date2.value.substring(4);
				endDate = "0"+eday+"-0"+emonth+"-"+eyear;
			}
			else if(date2.value.length == 9) {
				if(date2.value.substring(1,2) == "-") {
					eday = date2.value.substring(0,1);
					emonth = date2.value.substring(2,4);
					eyear = date2.value.substring(5);
					endDate = "0"+eday+"-"+emonth+"-"+eyear;
				}
				if(date2.value.substring(2,3) == "-") {
					eday = date2.value.substring(0,2);
					emonth = date2.value.substring(3,4);
					eyear = date2.value.substring(5);
					endDate = eday+"-0"+emonth+"-"+eyear;
				}
			}
		
		} 
		else if (date2.value.length == 10) {
			endDate = date2.value;
		}
		if(date1.value.length < 10) {
			if(date1.value.length==8) {
				sday = date1.value.substring(0,1);
				smonth = date1.value.substring(2,3);
				syear = date1.value.substring(4);
				startDate = "0"+sday+"-0"+smonth+"-"+syear;
			}
			else if(date1.value.length==9) {
				if(date1.value.substring(1,2) == "-") {
					sday = date1.value.substring(0,1);
					smonth = date1.value.substring(2,4);
					syear = date1.value.substring(5);
					startDate = "0"+sday+"-"+smonth+"-"+syear;
				}
				if(date1.value.substring(2,3) == "-") {
					sday = date1.value.substring(0,2);
					smonth = date1.value.substring(3,4);
					syear = date1.value.substring(5);
					startDate = sday+"-0"+smonth+"-"+syear;
				}
			}		
		} else if (date1.value.length == 10) {
			startDate = date1.value;
	 	}	
		if (date1.value.length != 0) {
			sDay = startDate.substring(0,2);
			sMonth = startDate.substring(3,5);
			sYear = startDate.substring(6);
		}   

		if (date2.value.length != 0) {
			eDay = endDate.substring(0,2);
			eMonth = endDate.substring(3,5);
			eYear = endDate.substring(6);
		}
		
		if (date2.value.length == 0 || date1.value.length == 0)
			return true;
			    
		eDateYyMm=eYear+eMonth;
		sDateYyMm=sYear+sMonth;
		if (opRel==">=") {
		    if (sDateYyMm >= eDateYyMm) {
		       return true;
		    } else{
		       return false; 
		    }
		}	
		if (opRel=="<=") {
		    if (sDateYyMm <= eDateYyMm) {
		       return true;
		    } else{
		       return false; 
		    }
		}
		if (opRel=="<") {
		    if (sDateYyMm < eDateYyMm) {
		       return true;
		    } else{
		       return false; 
		    }
		}	
		
		if (opRel==">") {
		    if (sDateYyMm > eDateYyMm) {		    
		       return true;
		    } else{
		       return false; 
		    }
		}	
		if (opRel=="=") {
		    if (sDateYyMm == eDateYyMm) {
		       return true;
		    } else{
		       return false; 
		    }
		}	
		

} 
	/** Return true if the field is in the range (check also if numeric)
            field: reference to html object
	    tipo:  the type of data to check (number, numberInt,numberComma)
	    inf: minimum of the range
	    sup: maximum of the range
            label: String that describe the html object
	*/
	function checkRange(field,tipo,inf,sup,label) {
	    if (!checkFieldLabel(field,tipo,label)) return false;
	    val=replaceCommaVal(field.value);
	    if (val < inf || val > sup) {
		alert("The field " + label + " must be between "+inf +" and " + sup);
		field.focus();
		return false;
            }
	    return true;
	
	}
/*
   call a calendar to view and select a date from
   Usually is linked with onClick() event
*/
	function OpenCalendar(obj, wpath, delimitatore, lastVal)
	{
		if(wpath!="") wpath +="/";
		if(lastVal==null) lastVal="";
		var x = window.showModalDialog(wpath + 'Calendar.htm',delimitatore,'dialogWidth:263px;dialogHeight:190px;dialogTop:' + (window.event.y + 50) + ';dialogLeft:' + (window.event.x + 50));
		if(x==null) x = lastVal;	
		// Sostituisce la barra con trattino	
		x = x.replace(/\//,"-");
		x = x.replace(/\//,"-");
		obj.value = x;
	}

/*
	convert the field into lower case
*/
	function toLowerField(field) {
		document.forms[0].field.value = document.forms[0].field.value.toLowerCase();
	}

/*
	convert the field into upper case
*/
	function toUpperField(field) {
		document.forms[0].field.value = document.forms[0].field.value.toUpperCase();
	}

function replace_apice(valore){

    valore = valore.replace(/_apice_/g, "'");
    return valore;
}

