<!-- Aggiunge 3 prototipi di manipolazione stringhe per il trim sinistro, trim destro e trim sinistro+destro !-->

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

<!-- Funzione che controlla se la data inserita è ben formattata ! -->

function controllo_data(stringa, ignora) {
		
	stringa = stringa.trim();
		
	var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
	var espressione2 = /^[0-9]{2}\/[0-9]{2}\/[0-9]{2}$/;
	
	if (ignora==undefined || ignora==null) { 
		ignora=0;
	}
		
	if (ignora==1) { 
			
		if (stringa=="") return true;
			
	}
		
				
	if (!espressione.test(stringa) && !espressione2.test(stringa))	{ 
	   
	    return false;
	    
	}
	
	else if (espressione.test(stringa)) { // BEGIN IF controllo data AAAA
		
			
			anno = parseInt(stringa.substr(6),10);
			mese = parseInt(stringa.substr(3, 2),10);
			giorno = parseInt(stringa.substr(0, 2),10);
		
			var data=new Date(anno, mese-1, giorno);
			
			if(data.getFullYear() == anno && data.getMonth()+1==mese && data.getDate()==giorno)	{
				return true;
			} else {
				return false;
			 }
			 
	} // END IF controllo data AAAA
		
	else if (espressione2.test(stringa)) { // BEGIN IF controllo data AA
	
		anno2 = parseInt(stringa.substr(6), 10);
		mese2 = parseInt(stringa.substr(3,2),10);
		giorno2 = parseInt(stringa.substr(0,2),10);
		
		var data2=new Date(anno2,mese2-1,giorno2);
		if (data2.getYear() == anno2 && data2.getMonth()+1==mese2 && data2.getDate()==giorno2) {
			return true;
		} else return false;
		
	} // END IF controllo data AA
	
	else return false;
		

} // END function


function update_chars(campotesto,campocaratteri) {
		var temp;
		
		temp = 1000 - campotesto.value.length;
		
		if (temp<0) {
			campocaratteri.value = 0;
			return false;
		}
		
		campocaratteri.value = temp;
	
}


function popup_localita() {
// pop up ricerca localita
         apri_popup("http://www.iltaccodibacco.it/ric_localita.php",380,200);
}

function popup_utenti() {
// pop up ricerca localita
         
}



function aggiorna_ricerca(at) {

	var trovato;
	var i_delete;
	
	//at = at+1;
	/*
	if (document.form_ricerca.str_attributi.value !="") {
	} else {
		vett_attrib = new Array();
		
	}*/
		
	vett_attrib = document.form_ricerca.str_attributi.value.split(",");

	
	//alert(at);
	/*
	vett_attrib[vett_attrib.lenght]
	campo.value = coprenza.join(",");
	coprenza = campo.value.split(",");
	*/
	
	trovato = false;
	vett_attrib_nuovo = Array();
	
	for(i=0;i<vett_attrib.length;i++) {
			
		if (vett_attrib[i]==at) {
			trovato = true;
			i_delete = i;
		} else {
		  vett_attrib_nuovo[i]=vett_attrib[i];
        }
			
	}
	
	
	if (trovato) {
	// non funziona su IE per MAC
	   //unset(vett_attrib[i_delete]);
		//vett_attrib.splice(i_delete,1);
	} else {
	   vett_attrib_nuovo[vett_attrib.length] = at;
		//vett_attrib.splice(0,0,at);
	
	}
	
	
	document.form_ricerca.str_attributi.value = vett_attrib_nuovo.join(",");
	document.form_ricerca.submit();
	
	//alert(document.form_ricerca.str_attributi.value+"");
	
} // aggiorna_ricerca



function apri_popup(url,dimx,dimy){
  var id;

  xpos = Math.round(screen.width / 2) - (dimx/2);
  ypos = Math.round(screen.height / 2)-(dimy/2);

 if((ind = navigator.appVersion.indexOf("MSIE")) > -1 ) {
    id = window.open(url,"_blank","status=1,scrollbars=1,width="+dimx+",height="+dimy+",left="+xpos+",top="+ypos);
 } else
    id = window.open(url,"_blank","scrollbars=1,width="+dimx+",height="+dimy+",screenX="+xpos+",screenY="+ypos);

 return id;   
}





function isTime(timeStr){

	if(timeStr.length== 0){

		return true;

	}

	//time must be in the 24:00 format

	 if (timeStr.length < 4 || timeStr.length > 5)return false;

    if (timeStr.length == 4) { thetimeStr = '0'+timeStr }

	else { thetimeStr = timeStr }

	if(!/^[0-9]{2}\:[0-9]{2}$/.test(thetimeStr))

		return false

	var strHour=thetimeStr.substring(0,2)

	var strMin=thetimeStr.substring(3,5)

	var strTime=strHour+strMin

	var delimiter=thetimeStr.substring(2,3)

	if (strHour>maxHours || strMin>60  || delimiter!=':' || strTime>2400){

		return false

	}



return true

}


function isValidEmail(emailStr){
		
	emailStr = emailStr.trim();
	
	if(emailStr.length== 0){

		return true;

	}

	if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(emailStr)) {

		return false
	}

	return true	

}

function controlla_data(formcontrollo) {
/***
Controlla se la data è in formato accettabile da MSsql.
**/

    var giorni_mese= new Array (0,31,28,31,30,31,30,31,31,30,31,30,31);


    today = new Date()

    controllo = formcontrollo;

    Stringa = controllo.value;

    if (Stringa =="") return true;

    Day = new Date(controllo.value)



    if (isNaN(Day))  {
              return false;
    }


    //***Controlla il formato **************************


    if(Stringa.indexOf('/') == Stringa.indexOf('-')){
                return false;
    }


    // suddivide la stringa a seconda del separatore
	
    Data = Stringa.split("/");





    if (Data[0]==Stringa.split("/")) Data=Stringa.split("-");

    //controlla se è un numero

    if (isNaN(Data[0]) || isNaN(Data[1]) || isNaN(Data[2])) {
                 return false;
    }

    //controlla la lunghezza delle stringhe

    for(i=0;i<2;i++) {
            if(Data[i].length>4 || Data[i].length<1) {

                return false;
            }

            
    }



    if( (Data[2].length != 4)&&(Data[2].length != 2) ) {

                return false;
            }



    //Termina se il formato non è esatto.******

    // Trasforma Le stringhe in interi

    mese = parseFloat(Data[1],10);

    giorno = parseInt(Data[0],10);

    anno = parseInt(Data[2],10);

    // Controlla se l'anno è bisestile

    if( anno % 4 == 0) {

        giorni_mese[2]=29;
    }

    // controlla se la data è accettabile

    if(!( mese>0 && mese<13) ) {

            return false;
    }


    if( giorno > giorni_mese[mese] ) {

            return false;
    }
    return true;

}


function apricalendario(nome_formcontrollo) {

/**************************************************************************
La funzione apre in  una nuova finestra il file indip/calendario.php
e ritorna la data selezionata al TextBox indicato in nome controllo.
L'argomento:

  nome_controllo contiene il nome del TEXT BOX il cui valore sar? modificato.

Il controllo deve essere contenuto nel forms[0].
*****************************************************************************/
 xpos=Math.round(screen.width/2)-350/2;
 ypos=Math.round(screen.height/2)-400/2;


         if (self.campo)   {
                           if (typeof(self.campo)== "string") {
                                if (self.campo.indexOf("opener.document."))  {
                                         alert ("Possibile ridefinizione variabile globale 'campo'.")
                                         return;
                                }

                           } else {
                                   alert("Possibile ridefinizione variabile globale 'campo'.");
                                   return;
                           }
         }



         var cal = window.open("calendario.php", "calendariowin", "width=320, height=250, resizable=no, screenX="+xpos+",screenY="+ypos);
         self.campo = "opener.document." + nome_formcontrollo;

}
