function __cnv_convert_date(typeconversion)
{
 var res=new Date();
 var opttypeconversion=((typeof typeconversion=="undefined") ? "3" : String(typeconversion).replace("undefined","3"));
 if (opttypeconversion=="3")
 	{
	 var gg=String(this).split("/");//trim().match(/\d+/g)
	 if ( (gg.length==3) && (!(isNaN(gg[0]))) && (!(isNaN(gg[1]))) && (!(isNaN(gg[2]))) )
	 	{
	 	if(gg[2].length==1) gg[2]="0"+gg[2];
	 	gg[2]=(gg[2].length==2?"20":"")+gg[2]; 
     	try {res= new Date(gg[2],gg[1]-1,gg[0]);	 } catch (e) { res= new Date(); }
		}
	else
		{
     	res= new Date();	 		
		}
	} 
 else if (opttypeconversion=="112")
 	{
	 // FORMATO ISO
	 var yy=parseInt(String(this).substr(0,4));
	 var mm=(String(this).substr(5,2));
	 if (mm.substr(0,1)=="0") mm=mm.substr(1)	 
	 var gg=(String(this).substr(6,2));
	 if (gg.substr(0,1)=="0") gg=gg.substr(1)
	 mm=parseInt(mm);
	 gg=parseInt(gg);
	 
   	 try {res= new Date(yy,mm,gg);	 } catch (e) { res= new Date(); }
	}
 else
 	{
	 res= new Date();
	}
return res;	
}


function __pt_Date_isHappyDay()
{
var datadacheck=new Date(this.getTime());
var res=0;
var gg=datadacheck.getDate();
var mm=datadacheck.getMonth()+1;
var weekday = datadacheck.getUTCDay()
res=(((weekday==6)||(weekday==5))) ? 1 : res; // DOM-SAB
res=(((gg==1)||(gg==6))&&((mm==1))) ? 1 : res; // 1 e 6 Gennaio
res=((gg==25)&&((mm==4))) ? 1 : res; // 25 Aprile
res=((gg==1)&&((mm==1)||(mm==5))) ? 1 : res; // 1 Gennaio - Maggio
res=((gg==2)&&((mm==6))) ? 1 : res; // 2 Giugno
res=((gg==15)&&((mm==8))) ? 1 : res; // 15 Agosto
res=((gg==1)&&((mm==11))) ? 1 : res; // 1 Novembre
res=(((gg==8)||(gg==25)||(gg==26))&&((mm==12))) ? 1 : res; // 8 - 25 - 26 Dicembre
return res;
}

function __pt_Date_getFisrtWeekDay()
{
var datadaver=new Date(this.getTime());
var newdate=new Date(this.getTime());
var addday=0;
addday=newdate.isHappyDay()
if (addday>0) 
	{
	newdate=datadaver.dateadd(addday,"D");
	newdate=newdate.getFisrtWeekDay();
	}
return newdate;
}

function __pt_str_formatnumber(dec,sepd,sepm) {
  var s=this.toString();
  try
  	{
	
	
		  //return s;
		  var neg="";
		  var sint="";
		  var strdec="";  
		  var d=( ((typeof dec=="undefined") || (dec=="") || isNaN(dec)) ? 2 : Number(dec) );
		  var strsepd=( ((typeof sepd=="undefined") || (sepd=="")) ? "," : sepd );
		  var strsepm=( ((typeof sepm=="undefined") || (sepm=="")) ? "." : sepm );
		  // registra segno negativo se presente
		  if (String(s).substr(0,1)=="-") {neg="-";s=String(s).substr(1);} 
		  // migliaia
		  if ( String(s).indexOf(strsepd)< 0 ) {
		  		s+=strsepd;
				for (var i=0;i<d;i++) {s+="0"}			
				}
		  if ( String(s).indexOf(strsepm)< 0 )// non formattato migliaia
		  	{
		  	for (var i=(String(s).indexOf(strsepd)+1?String(s).indexOf(strsepd):String(s).length)-3;i>0;i-=3)
				   s=String(s).substring(0,i)+strsepm+String(s).substring(i,String(s).length);
			}
			// già formattato migliaia
			// parte intera
		   sint=String(s).substr(0,s.lastIndexOf(strsepd));
		   pos=String(s).lastIndexOf(strsepd);
		   if(pos== -1) //impossibile aggiunti all'inizio
			{
			 for (var i=0;i<d;i++) {strdec+="0"}
			 strdec=strsepd+strdec;
			 }
		   else
			{
			strdec=String(s).substr(pos,d+1)
			var pos=Number(strdec.length);
			for (var i=pos;i<=d;i++) {strdec+="0"}
			}
		return neg+sint+strdec;				
	}
  catch(e)
  	{
	return s;
	}
 
 }

function __pt_str_isdate() { 
 var check=false;
 if (this.toString()=="") return false;
 var gg=String(this).trim().match(/\d+/g);
 if ((gg==null)||(gg.length!=3)) return false;
 gg[0]=(gg[0].length==1?"0":"")+gg[0]; 
 gg[1]=(gg[1].length==1?"0":"")+gg[1]; 
 gg[2]=(gg[2].length==2?"20":"")+gg[2]; 
 if(gg[2].length!=4) return check;
 try
 	{
	 var datatest=new Date(gg[2],gg[1]-1,gg[0]);
	 check=true;
	}
catch(e)
	{
	check=false;
	}
 if (!(check)) return check;
 return ((Number(gg[0])==datatest.getDate())&&(Number(gg[1])-1==datatest.getMonth())&&(String(Number(gg[2])).substr(String(Number(gg[2])).length-2)==String(datatest.getYear()).substr(String(datatest.getYear()).length-2)))?true:false;
}
function __pt_str_trim() {
 return this.toString().replace(/^\s*/, "").replace(/\s*$/, "");
}
function __pt_str_formatdate() {
 //data formato [g]g/[m]m/[yy]yy, da stringa, formatto aggiungendo gli 0 e millennio
 var gg=this.trim().match(/\d+/g);
 if(gg[2].length==1) gg[2]="0"+gg[2];
 gg[2]=(gg[2].length==2?"20":"")+gg[2]; 
 return new Date(gg[2],gg[1]-1,gg[0]).formatdate(2,0,0);
}
function __pt_str_codXcifre(nchar,ocar,leftToRight) {
 var str="";
 var car=(( typeof ocar =="undefined" ) ? ("0") : (ocar) );
 for(var x=0;x<eval(parseInt(nchar)-this.length);x++) str+=car;
 return ( (( typeof leftToRight =="undefined" )||(!leftToRight)) ? (str+this) : (this+str) );
}
function __pt_date_formatdate(formato,orario,oshortY,oseparatore,oseparatoreH) {
 //d=data in formato JScript, es. Wed Oct 10 12:02:49 UTC+0200 2001
 //formato=1 => mm/gg/aaaa (per cookie JS)
 //formato=0 => standard Date di JavaScript
 //formato=2 => gg/mm/aaaa
 //orario=1 => mette anche orario in formato hh:mm
 //shortY=1 => anno di 2 cifre, altrimenti di 4
 // 4 SQL FORMAT
 var separatore=((typeof oseparatore)=="undefined" ? "" : oseparatore);
 var separatoreH=((typeof oseparatoreH)=="undefined" ? "" : oseparatoreH);
 var separatoreGen=(((separatore!="")||(separatoreH!="")) ? " " : "");
 var shortY=((typeof oshortY)=="undefined" ? "1" : String(oshortY));
 if(String("0123456").indexOf(String(formato))==-1) formato=2; //def 
 //data=new Date(data);
 switch(String(formato)) {
  case "1": return String(eval(this.getMonth()+1)).codXcifre(2,0)+"/"+String(this.getDate()).codXcifre(2,0)+( (shortY!="2") ? "/" : "")+(shortY=="1"?String(this.getYear()).substr(String(this.getYear()).length-2): ((shortY!="2") ? this.getYear() : "") )+(String(orario)=="1"?" "+String(this.getHours()).codXcifre(2,0)+":"+String(this.getMinutes()).codXcifre(2,0):"");
  case "2": return String(this.getDate()).codXcifre(2,0)+"/"+String(eval(this.getMonth()+1)).codXcifre(2,0)+( (shortY!="2") ? "/" : "")+(shortY=="1"?String(this.getYear()).substr(String(this.getYear()).length-2): ((shortY!="2") ? this.getYear() : "") )+(String(orario)=="1"?" "+String(this.getHours()).codXcifre(2,0)+":"+String(this.getMinutes()).codXcifre(2,0):"");
  case "3": return (String(orario)=="1"?" "+String(this.getHours()).codXcifre(2,0)+":"+String(this.getMinutes()).codXcifre(2,0)+":"+String(this.getSeconds()).codXcifre(2,0) :"");  
  case "4": return String(this.getFullYear()).codXcifre(4,0)+String(eval(this.getMonth()+1)).codXcifre(2,0)+String(this.getDate()).codXcifre(2,0)
  case "5": return String(this.getFullYear()).codXcifre(4,0)+separatore+String(eval(this.getMonth()+1)).codXcifre(2,0)+separatore+String(this.getDate()).codXcifre(2,0)
  case "6": return String(this.getFullYear()).codXcifre(4,0)+separatore+String(eval(this.getMonth()+1)).codXcifre(2,0)+separatore+String(this.getDate()).codXcifre(2,0)+(String(orario)=="1"? separatoreGen+String(this.getHours()).codXcifre(2,0)+separatoreH+String(this.getMinutes()).codXcifre(2,0):"");
  case "0": return this;
  default: return "#ERR_FORMAT_NOT_FOUND";
 }
} 
function __pt_date_formatdate__old(formato,orario,shortY) {
 //d=data in formato JScript, es. Wed Oct 10 12:02:49 UTC+0200 2001
 //formato=1 => mm/gg/aaaa (per cookie JS)
 //formato=0 => standard Date di JavaScript
 //formato=2 => gg/mm/aaaa
 //orario=1 => mette anche orario in formato hh:mm
 //shortY=1 => anno di 2 cifre, altrimenti di 4
 if(String("012").indexOf(String(formato))==-1) formato=2; //def 
 //data=new Date(data);
 switch(String(formato)) {
  case "1": return String(eval(this.getMonth()+1)).codXcifre(2,0)+"/"+String(this.getDate()).codXcifre(2,0)+"/"+(String(shortY)=="1"?String(this.getYear()).substr(String(this.getYear()).length-2):this.getYear())+(String(orario)=="1"?" "+String(this.getHours()).codXcifre(2,0)+":"+String(this.getMinutes()).codXcifre(2,0):"");
  case "2": return String(this.getDate()).codXcifre(2,0)+"/"+String(eval(this.getMonth()+1)).codXcifre(2,0)+"/"+(String(shortY)=="1"?String(this.getYear()).substr(String(this.getYear()).length-2):this.getYear())+(String(orario)=="1"?" "+String(this.getHours()).codXcifre(2,0)+":"+String(this.getMinutes()).codXcifre(2,0):"");
  case "0": return this;
  default: return "#ERR_FORMAT_NOT_FOUND";
 }
} 
function __pt_str_isnumeric(dec,separatore) {
 if ((this.toString()==null)||(this=="")) return true;
 //SQL CEVEDALE USA . COME SEPARATORE
 //lato client uso invece ,
 //dec=0 NON considera decimali, altrimenti li considera
 //separatore: se non lo indico prende il . altrimenti usa quello indicato come separatore
 //se è negativo rimuovo simbolo
 var s=this;
 if (s.substr(0,1)=="-") s=String(s).substr(1);
 if ((dec!="0")&&((String("1234567890").indexOf(String(s).substr(0,1))==-1)||(String("1234567890").indexOf(String(s).substr(String(s).length-1,1))==-1))) return false;
 separatore=(String(separatore)=="undefined")?".":separatore;
 var filtronumber="1234567890"+((dec=="0")?"":separatore);
 s=String(s).replace(/,|\./g,separatore);
 var x;
 for (x=0;x<s.length;x++) 
  if (filtronumber.indexOf(s.substr(x,1))==-1) return false;
 if (dec!="0")
  if ((s.indexOf(separatore)!=-1)&&((s.substr(s.indexOf(separatore)+1).indexOf(separatore)!=-1))) return false;
 return true;
}
function __pt_bool_convert_b_to_s() {
 //se boolean=true restituisce 1, altrimenti 0
 return Number(this.valueOf()?1:0);
} 
function __pt_str_lenInt(min,max) {
 //true se la lunghezza della stringa è compresa tra min e max (inclusi)
 return (this.length>=min&&this.length<=max);
}
function __pt_str_formatsql() {
 return this.valueOf().replace(/'/g,"`");
} 
function __pt_str_acapo() {
 return this.valueOf().replace(/\n/g,"<br>").replace(/\r/g,"");
} 
function __pt_str_acapo2br() {
 //return this;
 return this.valueOf().replace(/<br>/g,"\n");
} 
function __pt_str_isSingolaEmail() {
 return this.match(/^[a-z0-9.\-_]+\@[a-z0-9.\-]+\.[a-z]{2,4}$/gi)?true:false;
}
function __pt_str_isEmail() {
 var __email_fornita=this.split(";");
 for(var __x=0;__x<__email_fornita.length;__x++)
 if((__email_fornita[__x]!="")&&((!String(__email_fornita[__x]).isSingolaEmail()))) return false;
 return true;
}
function __pt_date_dateadd_js(time,what) {
 //time=unità di misura da sommare
 //what: implementato per ora solo dateadd di giorni (D) e ultimo giorno del mese ("UGM") e anno
 switch(String(what).toUpperCase()) {
  case "DAY":
  case "D": {
   var data=new Date(this.getTime()+1000*60*60*24*parseInt(time));
   break;}
  case "UGM": {//ultimo giorno del mese rispetto alla data passata
   var data=new Date(this.getTime());
   var data2=new Date(this.getTime());
   for (var x=0;x<parseInt(time);x++) {
    while (data.getMonth()==data2.getMonth()) 
     data=dateadd_js(1,"D");
    data2=new Date(data.getTime());
   } //for per i mesi
    data=dateadd_js(-1,"D");
    break;}
   case "YEAR": {
     var data=new Date(this.getTime()+1000*60*60*24*365*parseInt(time));
     break;}	
     default: var data=this;
 } //switch tra il tipo di dateadd richiesto
 return data;
} 

function __pt_date_datediff_js(unita,dataobj) {
 //date: formato data JavaScript; se non la passo diventa data odierna
 //unita: per ora solo Day e Secondi
 var divisore=1;
 switch(String(unita).toUpperCase()) {
  case "D": {
   divisore=1000*60*60*24;
   break;}
  case "S":{
   divisore=1000;
   break;}
  case "N":{
   divisore=1000*60;
   break;}   
  default: return "Non implementato";
 } //switch per datediff
 var data1=((String(dataobj)=="undefined")||(dataobj==""))?new Date():new Date(dataobj);
 var data2=(new Date(this.getTime()));

 return Math.round((data1.getTime()-data2.getTime())/divisore)*(-1);
} 

function __round_number(dec)
{
var odec=((typeof dec)=="undefined" ? 3 : dec);
var divis=1;
for (var i=1;i<=(odec-1);i++) divis=divis*10;
var result=Math.round(this.valueOf()*divis)/divis;
return result;
}

function carica_prototype() {
 String.prototype.formatnumber=__pt_str_formatnumber;
 String.prototype.isdate=__pt_str_isdate;
 String.prototype.codXcifre=__pt_str_codXcifre; //passare n° di caratteri, carattere di completamento
 String.prototype.formatdate=__pt_str_formatdate;
 String.prototype.trim=__pt_str_trim;
 String.prototype.isnumeric=__pt_str_isnumeric;//passare decimali e separatore
 String.prototype.lenInt=__pt_str_lenInt; //passare minimo e massimo
 String.prototype.format_sql=__pt_str_formatsql;
 String.prototype.acapo=__pt_str_acapo; //converte a capo con <br> 
 String.prototype.acapo2br=__pt_str_acapo2br; //converte <br> con \n
 String.prototype.isSingolaEmail=__pt_str_isSingolaEmail; //non tiene conto di e-mail multiple
 String.prototype.isEmail=__pt_str_isEmail; //tiene conto di e-mail multiple, separate da ; 
 String.prototype.toDate = __cnv_convert_date;// 
  
 Date.prototype.formatdate=__pt_date_formatdate; //passare formato, orario, shortY; usato anche da formatdate String
 Date.prototype.dateadd=__pt_date_dateadd_js; //passare unità da sommare e tipo
 Date.prototype.datediff= __pt_date_datediff_js; //passare unità da sommare e tipo 
 Date.prototype.isHappyDay=__pt_Date_isHappyDay; // restitusice se è un giorno festivo 1, feriale 0
 Date.prototype.getFisrtWeekDay=__pt_Date_getFisrtWeekDay; // restituisce il primo giorno feriale utile dalla data
 
 Boolean.prototype.B2N=__pt_bool_convert_b_to_s;
 
 Number.prototype.round = __round_number;// 
}
void carica_prototype();
