﻿function f_Round(n, decimalpositions) {
    var i = n * Math.pow(10, decimalpositions);
    i = Math.round(i);
    return i / Math.pow(10, decimalpositions);
}
function f_PscSend() {

    var x = f_PscTotali();
    if (x) {
        alert(x);
        return false;
    }

    if(!confirm(f_PscText(20))) return false;

    return true;
}
function f_PscTotali() {

    var el = document.getElementById('fid_psctotali');
    var el2 = document.getElementById('fid_psctotali2');

    var x = f_PscParseValues();
    if (x) { el.innerHTML = '&nbsp;'; el2.innerHTML = x; }
    else { el.innerHTML = f_PscTextValues(); el2.innerHTML = '&nbsp;' }

    el = document.getElementById('fid_PasTitMaxLL');
    for (i = 0; i < document.PscMateriali.length; i++) 
    {
        if(el) el.innerText = f_Round(document.PscMateriali[i].Larghezza / document.Psc.Umc, 0) + 'X' + f_Round(document.PscMateriali[i].Lunghezza / document.Psc.Umc, 0);
        document.PscMateriali[i].TextControlI.innerHTML = 'max ' + f_Round(document.PscMateriali[i].Larghezza / document.Psc.Umc, 0) + ' x ' + f_Round(document.PscMateriali[i].Lunghezza / document.Psc.Umc, 0) + ' (' + document.Psc.Um + ')';
    }
    

    return x;
}
function f_PscText(k) {
    if(document.PscTexts.length-1<k) return '???';
    var t = document.PscTexts[k];
    if (t) return t;
    return '???';
}
function f_PscParseLarghezzaLunghezza() {

    var lum = document.Psc.Materiale.Lunghezza,
        lam = document.Psc.Materiale.Larghezza,
        lu = document.Psc.Lunghezza,
        la = document.Psc.Larghezza;
        
    //Non ci sta
    if (!(lu <= lum && la <= lam || lu <= lam && la <= lum))
        return  f_PscText(0);

    //Ci sta in un senso solo
    if (!(lu <= lum && la <= lam && lu <= lam && la <= lum)) {
        if (lu <= lam && la <= lum) {
            document.Psc.Lunghezza = la;
            document.Psc.Larghezza = lu;
            document.Note = f_PscText(1);
        }
        return;
    }

    //Ci sta in entrambi i sensi
    if (lum >= lam && lu <= la || lum <= lam && lu >= la) return;

    document.Psc.Lunghezza = la;
    document.Psc.Larghezza = lu;
    document.Note = f_PscText(2); 

    return;    
}
function f_PscParseValues() {

    document.Psc = new Object();

    var u = document.getElementById('fid_PscUni').selectedIndex;
    document.Psc.Um = u == 0 ? 'cm' : 'inch'
    document.Psc.Umc = u == 0 ? 1 : 2.54

    document.Note = '';

    document.Psc.Materiale = f_Psc_GetMateriale();
    if (!document.Psc.Materiale) return f_PscText(3);

    SetAddonsMateriale();
    SetColoriMateriale();

    document.Psc.Larghezza = document.Psc.Umc * parseInt(document.getElementById('fid_PscLarghezza').value);
    if (!document.Psc.Larghezza) return f_PscText(4);
    document.getElementById('fid_PscLarghezza').value = document.Psc.Larghezza;

    document.Psc.Lunghezza = document.Psc.Umc * parseInt(document.getElementById('fid_PscLunghezza').value);
    if (!document.Psc.Lunghezza) return f_PscText(5);
    document.getElementById('fid_PscLunghezza').value = document.Psc.Lunghezza;

    document.Psc.Addons = f_Psc_GetAddons();

    return f_PscParseLarghezzaLunghezza();
}
function f_PscRowValues(v1, v2) {
    return '<tr><td class="cPscTotLabel">' + v1 + ':</td><td>' + v2 + '</td></tr>';
}
function f_PscTextValues() {

    var i, x = '<table>' +
        f_PscRowValues('Larghezza' , document.Psc.Larghezza + ' cm' ) +
        f_PscRowValues('Lunghezza', document.Psc.Lunghezza + ' cm') +
        f_PscRowValues('Materiale' , document.Psc.Materiale.Text);

    if (document.Psc.Materiale.Colore)
        x += f_PscRowValues('Colore' , document.Psc.Materiale.Colore.Text );

    if (document.PscAddons)
        for (i = 0; i < document.Psc.Addons.length; i++)
            x += f_PscRowValues('Addon' , document.Psc.Addons[i].Text);

    x += "</table>";

    var p = f_Psc_GetCosto();

    x += "<div class=\"cPscCosto\">" + f_PscText(6) + ": " + p + "€</div>";

    if (document.Note) x += '<div class="cPscNote">' + document.Note + '</div>';
    return x;
}
function f_Psc_GetCosto() {
    var m= document.Psc.Materiale;
    var la = document.Psc.Larghezza, lu = document.Psc.Lunghezza;
    var f = m.Formula.replace('@A', la).replace('@U', lu);
    var p = eval(f);
    if (p < m.PrezzoMinimo) p = m.PrezzoMinimo;

    var i, pp = p;
    if (document.Psc.Addons)
        for (i = 0; i < document.Psc.Addons.length; i++)
            p += document.Psc.Addons[i].Prezzo + pp * document.Psc.Addons[i].PrezzoPercentuale / 100;

    return f_Round(p,2);
}
function f_Psc_GetMateriale() {

    var i;
    for (i = 0; i < document.PscMateriali.length; i++)
        if (document.PscMateriali[i].Control.checked)
            return document.PscMateriali[i];

    return null;
}
function f_Psc_GetMaterialeById(id) {

    var i;
    for (i = 0; i < document.PscMateriali.length; i++)
        if (document.PscMateriali[i].Id == id)
            return document.PscMateriali[i];
    return null;
}
function f_Psc_GetAddons() {

    var i, ads = new Array();
    if (!document.PscAddons) return;
    for (i = 0; i < document.PscAddons.length; i++)
        if (document.PscAddons[i].Control.checked)
            ads[ads.length] = document.PscAddons[i];

    return ads;
}
function IsAddonsMateriale(add) {
    if(!document.Psc.Materiale.Addons) return false;

    var i;
    for (i = 0; i < document.Psc.Materiale.Addons.length; i++)
        if ( document.Psc.Materiale.Addons[i]==add) 
            return true;
    return false;
}
function IsColoreMateriale(col) {
    if (!document.Psc.Materiale.Colori) return false;

    var i;
    for (i = 0; i < document.Psc.Materiale.Colori.length; i++)
        if (document.Psc.Materiale.Colori[i] == col)
            return true;
    return false;
}
function SetAddonsMateriale() {

    if (!document.PscAddons) return;

    var v, i;

    for (i = 0; i < document.PscAddons.length; i++) 
       if(!IsAddonsMateriale(document.PscAddons[i]))
       {
            document.PscAddons[i].Control.style.display="none";
            document.PscAddons[i].Control.checked = false;
            document.PscAddons[i].TextControl.style.color = '#aaa';
            document.PscAddons[i].TextControlI.style.color = '#aaa';
       }
       else
       {
           document.PscAddons[i].Control.style.display = "inline";
           document.PscAddons[i].TextControl.style.color = '#000';
           document.PscAddons[i].TextControlI.style.color = '#000';
       }

}
function SetColoriMateriale() {

    var nc = true, sel = document.getElementById('fid_PscSelCol'), opt, v=0, k=0, i;

    if (sel.selectedIndex >= 0)
        v = sel.options[sel.selectedIndex].value;
    while (sel.options.length > 0)
        sel.remove(0);

    if (document.PscColori) {
        for (i = 0; i < document.PscColori.length; i++)
            if (IsColoreMateriale(document.PscColori[i])) {
                opt = new Option(document.PscColori[i].Text, document.PscColori[i].Id);
                sel.options[k] = opt;
                if (document.PscColori[i].Id == v) v = k;
                k++;
            }

        nc = k==0;
        if(k>0) sel.selectedIndex=v;
    }

    document.getElementById('fid_PscNoCol').style.display = nc ? 'block' : 'none';
    document.getElementById('fid_PscColContainer').style.display = !nc ? 'block' : 'none';
}
function f_Psc_GetAddonById(id) {

    var i;
    if (!document.PscAddons) return null;
    for (i = 0; i < document.PscAddons.length; i++)
        if (document.PscAddons[i].Id==id)
            return document.PscAddons[i];
    return null;
}
function f_Psc_GetColoreById(id) {

    var i;
    if (!document.PscColori) return null;
    for (i = 0; i < document.PscColori.length; i++)
        if (document.PscColori[i].Id == id)
            return document.PscColori[i];
    return null;
}
function f_PscPrepareMisura(id, lung, larg, formula, pmin) {
    
    if (!document.PscMateriali) document.PscMateriali = new Array();
    var m = new Object();
    
    m.Id = id;
    m.Larghezza = larg;
    m.PrezzoMinimo = pmin;
    m.Lunghezza = lung;
    m.Control = document.getElementById('fid_PscMat' + id);
    m.TextControlI = document.getElementById('fid_PscMatText' + id + 'I');
    m.Text = document.getElementById('fid_PscMatText' + id).innerHTML;
    m.Formula = formula;

    document.PscMateriali[document.PscMateriali.length] = m;
}
function f_PscPrepareAddon(id, prezzo, prezzop) {

    if (!document.PscAddons) document.PscAddons = new Array();
    var m = new Object();

    m.Id = id;
    m.Prezzo = prezzo;
    m.PrezzoPercentuale = prezzop;
    m.Control = document.getElementById('fid_PscAdd' + id);
    m.TextControl = document.getElementById('fid_PscAddText' + id);
    m.TextControlI = document.getElementById('fid_PscAddText' + id + 'I');
    m.Text = m.TextControl.innerHTML;
    
    document.PscAddons[document.PscAddons.length] = m;
}
function f_PscPrepareColore(id, desc) {

    if (!document.PscColori) document.PscColori = new Array();
    var m = new Object();

    m.Id = id;
    m.Text = desc;

    document.PscColori[document.PscColori.length] = m;
}
function f_PscPrepareAddonMateriale(mat, add) {
    mat = f_Psc_GetMaterialeById(mat);
    add = f_Psc_GetAddonById(add);
    if (!mat.Addons) mat.Addons = new Array();
    mat.Addons[mat.Addons.length] = add;
}
function f_PscPrepareColoreMateriale(mat, col) {
    mat = f_Psc_GetMaterialeById(mat);
    col = f_Psc_GetColoreById(col);
    if (!mat.Colori) mat.Colori = new Array();
    mat.Colori[mat.Colori.length] = col;
}
