function dataOggi() {
	var currentTime = new Date();
	var day = currentTime.getDate().toString();
	var month = (currentTime.getMonth() + 1).toString();
	if (day.length == 1) day = "0" + day;
	if (month.length == 1) month = "0" + month;
	return day + "-" + month + "-" + currentTime.getFullYear();
}

function formattaTestoHTML(el, tag) {
	var selectedText = document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.selectionEnd);
	if(selectedText!='') {
		var newText='<'+tag+'>'+selectedText+'</'+tag+'>';
		el.value=el.value.replace(selectedText,newText);
	}
} 

function trim(stringa){
    while (stringa.substring(0,1) == ' ' || stringa.substring(0,1) == '\n'){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' ' || stringa.substring(stringa.length-1, stringa.length) == '\n'){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

function controllomail(mail){
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
	return !espressione.test(mail);
}

function selezionaElemento(dropDown, testoElemento) {
	for (i=0; i<dropDown.options.length; i++) {	
		if (dropDown.options[i].value == testoElemento)
			dropDown.selectedIndex = i;
	}
}

function onKeyNumeric(e) {
	return  (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode == 8 || e.keyCode == 9 || e.keyCode == 37 || e.keyCode==39 || e.keyCode == 110 || e.keyCode == 188;
}

function onKeyNumericInt(e) {
	return  (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode == 8 || e.keyCode == 9 || e.keyCode == 37 || e.keyCode==39;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function calcolaIvaETotale(campoIvaEuro, campoTotale, campoImporto, ivaPerc) {
	var ivaPercDef = "";
	var importo = document.getElementById(campoImporto).value;
	if (importo == "") importo = "0";
	importo = importo.replace(",",".");
	var ivaPerc = document.getElementById(ivaPerc);
	if ( (ivaPerc == undefined) || (ivaPerc == null) )
		ivaPercDef = "20";
	else
		ivaPercDef = ivaPerc.value;
	var ivaEuro = document.getElementById(campoIvaEuro);
	var totale = document.getElementById(campoTotale);
	var iva = (importo/100)*ivaPercDef;
	var totImporto = parseFloat(importo)+iva;
	iva = iva.toFixed(2);
	totImporto = totImporto.toFixed(2);
	totImporto = totImporto.replace(".",",");
	iva = iva.replace(".",",");
	ivaEuro.innerHTML = formattaEuro(iva, true) + "&nbsp;&#8364;&nbsp;";
	totale.innerHTML = "Totale:&nbsp; " + formattaEuro(totImporto, true) + "&nbsp;&#8364;&nbsp;";
}

function HelpTip(keyword, dir, height){
	var el=document.getElementById('helper');
	el.innerHTML="<img src='../../img/icone/loader2.gif'>";
	TagToTip("helper",BGCOLOR, '#FFFFEE',BORDERWIDTH,'1', BORDERCOLOR, '#999999', SHADOWCOLOR,'#999999', SHADOWWIDTH,'20', ABOVE, true, STICKY, true, CLICKCLOSE, true, HEIGHT, 0);
	var ajax = assegnaXMLHttpRequest();
    if(ajax) {
    ajax.open("get", "../../manuale/HelpTip.php?keyword="+keyword+"&height="+height, true);
	ajax.setRequestHeader("connection", "close");
	ajax.onreadystatechange = function() {
    if(ajax.readyState === readyState.COMPLETATO) {
        if(statusText[ajax.status] === "OK"){
          var r = ajax.responseText;
		  if(r!="0"){
			var dati=r;
		  	el.innerHTML="<div>"+dati+"</div>";
			TagToTip("helper",BGCOLOR, '#FFFFEE',BORDERWIDTH,'1', BORDERCOLOR, '#999999', SHADOWCOLOR,'#999999', SHADOWWIDTH,'20', ABOVE, true, STICKY, true, CLICKCLOSE, true, HEIGHT, 0);
		  } else {
			  
		  }
		}
		else {
          el.innerHTML = "Impossibile accedere all'help";
        }
      } 
    }
    ajax.send(null);
  }
}

function nascondiAvviso(){
	var el=document.getElementById('avviso');
	el.style.display='none';
}

function verificaiban(verificaabicab){
	var iban="";
	var els=new Array();
	for(i=1;i<=6;i++){
		var el=document.getElementById("iban"+i);
		els[i]=el;
		iban+=el.value;
		if(el.value=='') return false;
	}
	var el=document.getElementById("erroreiban");
	el.style.display="block";
	el.style.padding="3px";
	if(iban.length!=27){
		el.className='box_esitonegativo';
		//el.style.background="#FFCCAA";
		el.innerHTML="<div style='padding-left:45px; padding-right:20px;'>Lunghezza IBAN errata!</div>";
		return;
	}
	el.style.display="none";
	if(verificaabicab){
	el.style.display="block"
	//el.style.background="#EEFFFF";
	el.innerHTML="<img src='../../img/icone/loader2.gif'> Caricamento...";
	
	var ajax = assegnaXMLHttpRequest();
    if(ajax) {
    ajax.open("get", "../../includes/abicab.php?abi="+els[4].value+"&cab="+els[5].value, true);
	ajax.setRequestHeader("connection", "close");
	ajax.onreadystatechange = function() {
    if(ajax.readyState === readyState.COMPLETATO) {
        if(statusText[ajax.status] === "OK"){
          var r = ajax.responseText;
		   if(r=="0"){
			//el.style.background="#FFCCAA";
			el.className='';
			el.className='box_esitonegativo';
		  	el.innerHTML="<div style='padding-left:45px; padding-right:20px;'>Codici <b>ABI</b> o <b>CAB</b> Errati!</div>";
		  } else if(r=="1"){
		  	el.className='';
			el.className='box_esitonegativo';
		  	el.innerHTML="<div style='padding-left:45px; padding-right:20px;'>Non &egrave; stato possibile verificare la correttezza del codice iban</div>";
		  } else {
			var dati=r.split("--");
			el.className='';
		  	el.innerHTML="<div class='box_suggerimenti'>"+dati[0]+"</div>";
		  }
		}
		else {
          el.innerHTML = "Impossibile verificare la correttezza dell'IBAN";
        }
      } 
    }
    ajax.send(null);
  }
 }
}



// Formato data in ingresso: gg/mm/aaaa
function formattaDataPerGetDate(myDate) {
	var myDateArray = myDate.split("/");
	return myDateArray[1]+"/"+myDateArray[0]+"/"+myDateArray[2];
}

function formattaEuro(numeroParam, vediZeri) {
	
	var numero = numeroParam.toString();
	var parteIntera = "";
	var parteDecimale = "";
	var meno = "";

	if (numero < 0) {
		numero = numero*-1;
		meno = "-";
	}
	
	numero = numero.replace(",",".");
	
	if (numero.indexOf(".") < 0) {
		parteIntera = numero;
		if (vediZeri) parteDecimale = ",00";
	} else {
		numeroArray = numero.split(".");
		parteIntera = numeroArray[0];
		parteDecimale = ","+numeroArray[1];
	}
	
	var numLen = parteIntera.length;
	var tempLeftPart;
	var tempRightPart;
	var contaCifre = 0;
	for(var i=numLen; i>0; i--) {
		if (contaCifre == 3) {
			tempLeftPart = parteIntera.substring(0, i);
			tempRightPart = parteIntera.substring(i, parteIntera.length);
			parteIntera = tempLeftPart+"."+tempRightPart;
			contaCifre = 0;
		}
		contaCifre++;
	}
	
	if (parteDecimale.length == 2)
		parteDecimale += "0";
	else if (parteDecimale.length >= 3)
		parteDecimale = parteDecimale.substring(0, 3);
		
	return meno+parteIntera+parteDecimale;
}
function chiudi(){
	Tip('', FIX, [0,0],FOLLOWMOUSE, false, DURATION, 2, WIDTH, 0, HEIGHT,0);
}
