//funzione che mi apre pop-up (vedi: privacy)
function openwindow2(Url, Wid, Hei){
window.open (Url, '', 'toolbar=no,status=yes,menubar=no,scrollbars=yes,fullscreen=no,resizable=no,width='+Wid+',height='+Hei);
}

//funzione che mi permette di aggiornare la pagina madre (funzione usata in dettaglio.asp)
function chiudifinestra(page,idutente){
window.opener.location.href= page + '?id='+ idutente +'&mode=mod';
window.close();
}

//funzione che mi permette di centrare la popup, esempio su Pannello Controllo Portfolio
function resizeWin(Larghezza,Altezza)
{
var sizeH= Altezza;
var sizeW= Larghezza;
var posH=0;
var posW=0;

if (sizeH<=screen.height)
sizeH=30+sizeH;
if (sizeW<=screen.width)
sizeW=30+sizeW
self.resizeTo(sizeW,sizeH);
if (((screen.height-sizeH)/2)>0)
posW=((screen.height-sizeH)/2);
if (((screen.width-sizeW)/2)>0)
posH=((screen.width-sizeW)/2);
self.moveTo(posW,posH);
}


//questa funzione mi serve per controllare che i campi segnalati siano compilati
//in caso contrario mi da errore..un'esempio del funzionamento
function checkfield(objForm)
{
if(!objForm.requiredFields)
return true
objForm.requiredFields = objForm.requiredFields.toLowerCase()
for(i=0;i<objForm.elements.length;i++)
{
if((","+ objForm.requiredFields).indexOf(","+ objForm.elements[i].name.toLowerCase())==-1)
{
continue;
}
switch(objForm.elements[i].type)
{
case "hidden":
if(objForm.elements[i].value.match(/^\s*$/))
{
alert("Il campo \'"+ objForm.elements[i].name + "\' è un campo obbligatorio !")
return false;
}
break;

case "textarea":
case "text":
case "password":
if(objForm.elements[i].value.match(/^\s*$/))
{
alert("Il campo \'"+ objForm.elements[i].name + "\' è un campo obbligatorio !")
objForm.elements[i].focus()
return false;
}
break;

case "select-one":
if(objForm.elements[i].selectedIndex==-1 || !objForm.elements[i].options[objForm.elements[i].selectedIndex].value || objForm.elements[i].selectedIndex==0)
{
alert("Il campo \'"+ objForm.elements[i].name + "\' è un campo obbligatorio!")
objForm.elements[i].focus()
return false;
}
break;
}
}
return true;
}


function WM_toggle(id){
if (document.all){
if(document.all[id].style.display == 'none'){
document.all[id].style.display = '';
} else {
document.all[id].style.display = 'none';
}
return false;
} else if (document.getElementById){
if(document.getElementById(id).style.display == 'none'){
document.getElementById(id).style.display = 'block';
} else {
document.getElementById(id).style.display = 'none';
}
return false;
}
}



function creafattura(){
//var tasso_iva = 20;
var imponibile = 0;
var tot_fattura = 0;
var sconto = 0;
var tot_sconto = 0;
var iva = 0;
var importo_a = document.theform.importo_a.value;
var importo_b = document.theform.importo_b.value;
var importo_c = document.theform.importo_c.value;
var sconto = document.theform.Sconto.options[document.theform.Sconto.options.selectedIndex].value
var straniero = document.theform.straniero.value;

if(document.theform.desc_a.value=="")
document.theform.importo_a.value= 0;

if(document.theform.desc_b.value=="")
document.theform.importo_b.value= 0;

if(document.theform.desc_c.value=="")
document.theform.importo_c.value= 0;

if(document.theform.importo_a.value=="")
document.theform.importo_a.value= 0;

if(document.theform.importo_b.value=="")
document.theform.importo_b.value= 0;

if(document.theform.importo_c.value=="")
document.theform.importo_c.value= 0;

somma=parseFloat(document.theform.importo_a.value) + parseFloat(document.theform.importo_b.value) + parseFloat(document.theform.importo_c.value);

document.theform.tot_sconto.value = Math.round(somma * sconto)/100;

document.theform.imponibile.value = somma - parseFloat(document.theform.tot_sconto.value);

if (straniero==""){
var tasso_iva = document.theform.tasso_iva.options[document.theform.tasso_iva.options.selectedIndex].value;
document.theform.iva.value = Math.round(document.theform.imponibile.value * tasso_iva)/100;
document.theform.tot_fattura.value = parseFloat(document.theform.iva.value) + parseFloat(document.theform.imponibile.value);
}

if (straniero=="on"){
var tasso_iva = 0;
document.theform.iva.value = "0";
document.theform.tot_fattura.value = document.theform.imponibile.value;
}

}



//visualizzazione calendario in popup
function show_calendar(str_target, lang, str_datetime){
var lang = lang;
if (lang=="en"){
var arr_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var week_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
}
else{
var arr_months = ["Gennaio", "Febbario", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"];
var week_days = ["Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"];
}

var n_weekstart = 0; // day week starts from (normally 0 or 1)

var dt_datetime = ((str_datetime == null || str_datetime == "") ? new Date() : str2dt(str_datetime));
 
  var dt_prev_month = new Date(dt_datetime);
  dt_prev_month.setMonth(dt_datetime.getMonth() - 1);
 
  var dt_next_month = new Date(dt_datetime);
  dt_next_month.setMonth(dt_datetime.getMonth() + 1);
 
  var dt_firstday = new Date(dt_datetime);
  dt_firstday.setDate(1);
  dt_firstday.setDate(1 - (7 + dt_firstday.getDay() - n_weekstart) % 7);
 
  var dt_lastday = new Date(dt_next_month);
  dt_lastday.setDate(0);
 
  // html generation (feel free to tune it for your particular application)
  // print calendar header
  var str_buffer = new String("<html>\n" +
                              "<head>\n" +
                              " <title>Calendar</title>\n" +
					"<LINK rel=\"stylesheet\" href=\"font.css\" type=\"text/css\">\n" +
                              "</head>\n" +
                              "<body>\n" +
                              "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n<Tbody>\n" +
                              "<tr><td bgcolor=\"#FFFFFF\">\n" +
                              "<table cellspacing=\"1\" cellpadding=\"2\" border=\"0\" width=\"100%\">\n" +
                              "<tr>\n <td bgcolor=\"#c00000\"><a href=\"javascript:window.opener.show_calendar('" +
                              str_target +
					"', '"+ lang +"', '" +
                              dt2dtstr(dt_prev_month) +
                              " ' + document.cal.time.value);\">" +
                              "<img src=\"img/freccia_sx2.gif\" width=\"10\" height=\"9\" border=\"0\"" +
                              " alt=\"" +
                              arr_months[dt_datetime.getMonth()-1] +
                              "\"></a></td>\n" +
                              " <td bgcolor=\"#c00000\" colspan=\"5\" align=\"center\" valign=\"middle\">" +
                              "<font color=\"white\" class=\"corpo\">" +
                              arr_months[dt_datetime.getMonth()] +
                              " " +
                              dt_datetime.getFullYear() +
                              "</font></td>\n" +
                              " <td bgcolor=\"#c00000\" align=\"right\"><a href=\"javascript:window.opener.show_calendar('" +
                              str_target +
 					"', '"+ lang +"', '" +
                              dt2dtstr(dt_next_month) +
                              " ' + document.cal.time.value);\">" +
                              "<img src=\"img/freccia_dx2.gif\" width=\"10\" height=\"9\" border=\"0\"" +
                              " alt=\"" +
                              arr_months[dt_datetime.getMonth()+1] +
                              "\"></a></td>\n</tr>\n");
 
  var dt_current_day = new Date(dt_firstday);
 
  // print weekdays titles
  str_buffer += "<tr>\n";
 
  for ( var n = 0 ; n < 7 ; n++)
    str_buffer += " <td bgcolor=\"#CCCCCC\">" +
    "<font color=\"white\" class=\"corpo\">" +
    week_days[(n_weekstart + n) % 7] +
    "</font></td>\n";
 
  // print calendar table
  str_buffer += "</tr>\n";
  while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
         dt_current_day.getMonth() == dt_firstday.getMonth())
    {
      // print row heder
      str_buffer += "<tr>\n";
 
      for ( var n_current_wday = 0 ; n_current_wday < 7 ; n_current_wday++)
        {
          if (dt_current_day.getDate() == dt_datetime.getDate() &&
              dt_current_day.getMonth() == dt_datetime.getMonth())
              // print current date
              str_buffer += " <td bgcolor=\"#FF0000\" align=\"right\">";
          else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
                   // weekend days
                   str_buffer += " <td bgcolor=\"#E0ECE8\" align=\"right\">";
          else // print working days of current month
               str_buffer += " <td bgcolor=\"white\" align=\"right\">";
 
          if (dt_current_day.getMonth() == dt_datetime.getMonth())
              // print days of current month
              str_buffer += "<a href=\"javascript:window.opener." +
                            str_target +
                            ".value='" + dt2dtstr(dt_current_day) + "'; window.close();\">" +
                            "<font color=\"black\" class=\"corpo\">";
          else // print days of other months
               str_buffer += "<a href=\"javascript:window.opener." +
                             str_target +
                             ".value='" + dt2dtstr(dt_current_day) + "'; window.close();\">" +
                             "<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
 
          str_buffer += dt_current_day.getDate() + "</font></a></td>\n";
          dt_current_day.setDate(dt_current_day.getDate() + 1);
        }
 
      // print row footer
      str_buffer += "</tr>\n";
    }
 
  // print calendar footer
  str_buffer += "<form name=\"cal\" style=\"margin:0px;\">\n<tr bgcolor=\"#c00000\"><td colspan=\"7\" height=\"15\" class=\"corpo2\" align=\"center\" valign=\"middle\">" +
                "<input type=\"hidden\" name=\"time\" value=\""+dt2tmstr(dt_datetime) +
                "\"><a href=\"javascript:window.close();\"><font color=\"#ffffff\">Close Window</font></a>\n" +
		    "</td></tr>\n</form>\n" +
                "</Tbody>\n</table>\n" +
                "</td>\n</tr>\n</Tbody>\n</table>\n" +
                "</body>\n" +
                "</html>\n";

 
  var vWinCal = window.open("", "Calendario", "width=250,height=190,status=no,resizable=yes,top=200,left=200");
  vWinCal.opener = self;
  var calc_doc = vWinCal.document;
  calc_doc.write(str_buffer);
  calc_doc.close();
}
 
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt(str_datetime)
{
  var re_date = /^(\d{1,2})\/(\d{1,2})\/(\d{4})\s+(\d{1,2})\:(\d{1,2})\:(\d{1,2})$/;
 
  if (!re_date.exec(str_datetime)) return(alert("Invalid Datetime format: " + str_datetime));
 
  return(new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
}

function dt2dtstr(dt_datetime){
//var dt = dt_datetime.getDate() + "-";

var day = dt_datetime.getDate();
var month = dt_datetime.getMonth() + 1;
var dt = ((day < 10) ? "0" + day : day) + "/";
dt += ((month < 10) ? "0" + month : month);
dt += "/" + dt_datetime.getFullYear();
return(dt);
}
 
function dt2tmstr(dt_datetime){
var dt = dt_datetime.getHours() + ":";
dt += ((dt_datetime.getMinutes() < 10) ? "0" + dt_datetime.getMinutes() : dt_datetime.getMinutes()) + ":";
dt += ((dt_datetime.getSeconds() < 10) ? "0" + dt_datetime.getSeconds() : dt_datetime.getSeconds());
return(dt);
}


//le prossime due funzioni si riferiscono alla fatturazione.
function insertanno(){
document.theform.n_anno.value = document.theform.data_fattura.value.substr(8,2);
}
function avviso(n){
if (n==1) alert("Attenzione !! Se Lasci vuoto il campo, il sistema non sarà più in grado di riabbinare la numerazione...\n\nSe modifichi il numero controlla bene che non vada in contrasto con le altre fatture...");
if (n==2) alert("Attenzione !! Se modifichi la data, controlla che sia in ordine cronologico...");
}


//funzione per contare i caratteri e fermare inserimento nella textarea
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) {
field.value = field.value.substring(0, maxlimit);
} else {
countfield.value = maxlimit - field.value.length;
}
}

