filtro_spazi = /^\s*$/;
filtro_alfanum = /^[A-z0-9àèéòùì_\-]+$/;
filtro_digit = /^\d+$/;
filtro_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
filtro_float = /^\d+[\,\.]\d\d?$|^\d+$/;
filtro_data = /^\d\d\/\d\d\/\d\d\d\d$/;
filtro_mese = /^0?[1-9]$|^1[0-2]$/;
filtro_anno = /^1[89]|^2\d\d\d$/;
filtri = new Array(filtro_alfanum//0
					, filtro_digit//1
					, filtro_mail//2
					, filtro_float//3
					, filtro_mese//4
					, filtro_anno//5
                    , filtro_data //6
                    );

var c_INDICE_FILTRO_DATA = 6;

var c_OBBLIGATORIO_SI = true;
var c_OBBLIGATORIO_NO = false;

// questa funzione può essere applicata sui field di tipo text, textarea e select 
function check_input(oggetto, descrizione, indice_filtri, obbligatorio) {
	if(oggetto) {
		if(oggetto.type && /^select/.test(oggetto.type)) {
			if(obbligatorio && (oggetto.selectedIndex < 0 || filtro_spazi.test(oggetto.options[oggetto.selectedIndex].value))) {
				alert("PER FAVORE, SELEZIONARE IL CAMPO "+descrizione);
				oggetto.focus();
				return false;
			}
	    } else {
			if(filtro_spazi.test(oggetto.value)) {
			    if (obbligatorio) {
			        oggetto.focus();
					alert("PER FAVORE, COMPLETARE IL CAMPO "+descrizione);  					
					return false;
				}
			} else if(indice_filtri>=0) {
			    if (!filtri[indice_filtri].test(oggetto.value)) {
			        oggetto.focus();
			        alert("ATTENZIONE, E' STATO INSERITO UN VALORE NON VALIDO IN " + descrizione);
			        return false;
			    }
		    }
	    }
		return true;
	} else
		return true;
}

function euroconv(valore) {
	var temp = valore.toString().replace(".",",");
	var arraytemp = temp.split(",");
	temp = "€ " + temp;
	if(arraytemp.length == 1) return temp + ",00";
	else if(arraytemp[1].toString().length == 1) return temp + "0";
	else return temp;
}

function convertiData(strData) {		// ritorna: YYYY/MM/GG
	return strData.split("\/")[2] + "\/" + strData.split("\/")[1] + "\/" + strData.split("\/")[0];
}

function full_screen(){
	if (typeof(screen.availWidth)!="undefined") {
		window.resizeTo(screen.availWidth, screen.availHeight);
		window.moveTo(0,0);
	}
}

var marked_row = new Array;
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
}

function openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function faiFuoco(dove){
	dove.focus();
}
//FUNZIONE PER CONTROLLARE LA DATA INSERITA
function checkDate(inputStr,gField){
  //gField = document.ins_data.date_ins;
  //var inputStr = document.ins_data.date_ins.value;
  //converte i delimitatori - in /
  while(inputStr.indexOf("-") != -1){
    inputStr = inputStr.replace("-","/");
    //inputStr = replaceString(inputStr,"-","/");
  }
  var delim1 = inputStr.indexOf("/");
  var delim2 = inputStr.lastIndexOf("/");

  if(delim1 != -1 && delim1 == delim2){
    //c'e' un solo delimitatore, quindi formato non accettabile
    alert("La data inserita non è in un formato accettabile.\n\nPuoi inserire date nei seguenti formati: gg/mm/aaaa, gg-mm-aaaa");
    gField.value= "";
    gField.focus();
    //gField.select();
    return false;
  }
  if(delim1 != -1){
    //ci sono i delimitatori: estraggo i valori che compongono la data
    var dd = parseInt(inputStr.substring(0,delim1),10);
    var mm = parseInt(inputStr.substring(delim1 + 1,delim2),10);
    var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10);
  }else{
    //non ci sono i delimitatori
    var dd = parseInt(inputStr.substring(0,2),10);
    var mm = parseInt(inputStr.substring(2,4),10);
    var yyyy = parseInt(inputStr.substring(4,inputStr.length),10);
  }
  if(isNaN(dd) || isNaN(mm) || isNaN(yyyy)){
    alert("La data inserita non è in un formato accettabile.\n\nPuoi inserire date nei seguenti formati: gg/mm/aaaa, gg-mm-aaaa");
    gField.value= "";
    gField.focus();
    //gField.select();
    return false;
  }
  if(mm < 1 || mm > 12){
    alert("Il mese deve essere compreso tra 1 e 12");
    //gField.value= "";
    gField.focus();
    gField.select();
    return false;
  }
  if(dd < 1 || dd > 31){
    alert("I mesi non possono avere più di 31 giorni in funzione del mese inserito.");
    //gField.value= "";
    gField.focus();
    gField.select();
    return false;
  }
  if(yyyy < 100){
    if(yyyy >= 90){
      yyyy += 1900
    }else{
      yyyy += 2000
    }
  }
  if(!checkMonthLength(mm,dd)){
    gField.focus();
    gField.select();
    return false;
  }
  if(mm == 2){
    if(!checkLeapMonth(mm,dd,yyyy)){
    gField.focus();
    gField.select();
    return false;
    }
  }
  
  if(dd < "10"){
    dd = "0"+dd;
  }
  if(mm < "10"){
    mm = "0"+mm;
  }
  if(gField.name == "ins_data_intervento"){
    if(!checkTodayDate(mm,dd,yyyy)){
      gField.focus();
      gField.select();
      return false;
    }
  }
  gField.value = (dd + "/" + mm + "/" + yyyy);
  //alert(gField.name);
  return true;
}

//FUNZIONE PER CONTROLLARE IL MESE INSERITO PER VALORI TROPPO ALTI
function checkMonthLength(mm,dd){
  var months = new Array("","Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");
  if((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30){
    alert(months[mm] + " ha soltanto 30 giorni.");
    return false;
  }else if(dd > 31){
    alert(months[mm] + " ha soltanto 31 giorni.");
    return false;
  }
  return true;
}

//FUNZIONE PER CONTROLLARE LA LUNGHEZZA DEL MESE DI FEBBRAIO
function checkLeapMonth(mm,dd,yyyy){
  if(yyyy % 4 > 0 && dd > 28){
    alert("Febbraio dell'anno " + yyyy + " ha soltanto 28 giorni.");
    return false;
  }else if(dd > 29){
    alert("Febbraio dell'anno " + yyyy + " ha soltanto 29 giorni.");
    return false;
  }
  return true;
}

//FUNZIONE PER CONTROLLARE CHE LA DATA INTERVENTO NON SIA SUCCESSIVA AD OGGI

function checkTodayDate(mm,dd,yyyy) {
	todayDate =new Date();
	var thisMonth = todayDate.getMonth();
	var thisYear = todayDate.getFullYear();
	var thisDay = todayDate.getDate();
	/*
	if(thisYear < 100){
	 if(thisYear >= 90){
	   thisYear += 1900
	 }else{
	   thisYear += 2000
	 }
	}
	*/
	thisMonth = thisMonth+1;
	if(thisMonth < "10"){
	 thisMonth = "0"+thisMonth;
	}
	if(thisDay < "10"){
	 thisDay = "0"+thisDay;
	}
	if(yyyy > thisYear){
	 alert("LA DATA INTERVENTO NON PUO' ESSERE MAGGIORE DELLA DATA DI OGGI");
	 return false;
	}else if(yyyy == thisYear){
	 if(mm > thisMonth){
	   alert("LA DATA INTERVENTO NON PUO' ESSERE MAGGIORE DELLA DATA DI OGGI");
	   return false;
	 }else if(mm == thisMonth){
	   if(dd > thisDay){
	     alert("LA DATA INTERVENTO NON PUO' ESSERE MAGGIORE DELLA DATA DI OGGI");
	     return false;
	   }
	 }
	}
	return true;
}

function safeSubmit(theForm) {
	for(i = 0; i < document.links.length; i++) {
		document.links[i].href = "#";
	}
	for(i = 0; i < theForm.elements.length; i++) {
		if(theForm.elements[i].type == "button" || theForm.elements[i].type == "image" || theForm.elements[i].type == "submit") {
			theForm.elements[i].disabled = true;
		}
	}
	theForm.submit();
}
