﻿//----------------------------------------------------------------------------------
function RitornaZero(Formato, Decimali){
    var Valore="";
    
    switch(Formato){
            case 1: //Euro
                Valore="€ 0,";
                for (n=1; n<(Decimali+1); n++){
                    Valore=Valore+"0";
                }
                break;
            case 2: //Percentuale     
                Valore="0,";
                for (n=1; n<(Decimali+1); n++){
                    Valore=Valore+"0";
                }
                Valore=Valore+"%";
                break;
        }
    return Valore;       
}

function IsNumber(Valore){
    for (n=0; n<Valore.length; n++){
        if(isNaN(Valore.substr(n,1))){
            return false;
        }
    }
    return true;
}

//FORMATTA VALORE NUMERICO --------------------------------------------------------
function ControllaNumero(ctrNome,Formato,Decimali){
    var tmp=document.forms["form1"].elements[ctrNome].value;
    var Valore="";
    var str="";
    var num=false;
    
    if (tmp==""){
        Valore=RitornaZero(Formato, Decimali);
        document.forms["form1"].elements[ctrNome].value=Valore;
        return false;
    }
    
    //Controlla la presenza di caratteri estranei
    for (n=0; n<tmp.length; n++){
        var str=tmp.substr(n,1)
        if (isNaN(str)){
            if (str!="€" && str!="%" && str!="," && str!="." && str!="-"){
                Valore=RitornaZero(Formato, Decimali);
                document.forms["form1"].elements[ctrNome].value=Valore;
                alert("Valore inserito non valido.");
                return false; 
            }
        }
        else{
            num=true;
        }
    }
    
    //Controlla se sono presenti numeri
    if (num==false){
        Valore=RitornaZero(Formato, Decimali);
        document.forms["form1"].elements[ctrNome].value=Valore;
        alert("Valore inserito non valido.");
        return false; 
    }
    
    //Verifica presenza di un punto
    var Punto=0;
    var Virgola=0;
    
    for (n=0; n<tmp.length; n++){
        if (tmp.substr(n,1)=="."){
            Punto++;
        }    
        if (tmp.substr(n,1)==","){
            Virgola=1;
        }
    }

    if (Virgola==0 && Punto==1){
        for (n=0; n<tmp.length; n++){
            if (tmp.substr(n,1)=="."){
                Valore=Valore+",";
            }
            else{
                Valore=Valore+tmp.substr(n,1);
            }
        }
        tmp=Valore;        
    }
    
        
    //Controlla numeri
    Valore="";
    var Virgola=false;
    for (n=tmp.length; n>=0; n--){
        str=tmp.substr(n,1);
        if (!isNaN(str)){
            Valore=Valore+str;
        }
        else{
            var ok=false;
            switch(str){
                case ",":
                    if (Virgola==false){
                        Virgola=true;
                        Valore=Valore+".";
                    }
                    ok=true;
                    break;
                    
                case "-":
                    Valore=Valore+"-";
                    ok=true;
                    break;  
            }
            if (!ok){
                Valore=RitornaZero(Formato, Decimali);
                document.forms["form1"].elements[ctrNome].value=Valore;
                alert("Valore inserito non valido.");
                return false;     
            }    
        }       
    }
    
    var Numero="";
    for (n=Valore.length; n>=0; n--){
        Numero=Numero+Valore.substr(n,1);
    }    
    
    //Verifica risultato e arrotonda decimali
    if (isNaN(Numero)){
        document.forms["form1"].elements[ctrNome].value=Valore;
        alert("Valore inserito non valido.");
        return false;
    }
    var b=Math.pow(10,Decimali)
    var flt=parseFloat(Numero);
    var arrot=Math.round(flt*b)/b;

    //Converte in stringa    
    Valore=arrot.toString();
    if (Valore.indexOf(".") != -1){
        Valore=Valore.replace(".",",");
    }
    else{
        //Aggiunge virgola se manca
        if (Formato<3){
            Valore=Valore+",";
            for (n=1; n<=Decimali; n++){
                Valore=Valore+"0";
            }
        }
    }    
        
    var conta=0;
    var inizia=false;
    var dec=""
    str="";
    for (n=Valore.length; n>=0; n--){
        if (inizia==true){
            str=str+Valore.substr(n,1);
            conta++;
            if (conta==3){
                if (n!=0){
                    if (Valore.substr(n-1,1)!="-"){
                        str=str+".";    
                    }
                }
                conta=0;
            }
        }
        else{
            dec=dec+Valore.substr(n,1);
        }
        if (Valore.substr(n,1)==",") inizia=true;
    }
    Valore="";
    for (n=str.length; n>=0; n--){
        Valore=Valore+str.substr(n,1);
    }
    
    for (n=dec.length; n>=0; n--){
        Valore=Valore+dec.substr(n,1);
    }

    switch(Formato){
        case 1: //Euro
            Valore="€ " + Valore;
            break;
        case 2: //Precentuale
            Valore=Valore+"%";    
            break;
    }
    document.forms["form1"].elements[ctrNome].value=Valore;
    return true;
}

function ConverteInNumero(Valore,Decimali){
    var Numero="";
    for (n=0; n<Valore.length; n++){
        if (!isNaN(Valore.substr(n,1))){
            Numero=Numero+Valore.substr(n,1);
        }
        else{
            if (Valore.substr(n,1)==","){
                Numero=Numero+"."
            }
        }
    }
    var b=Math.pow(10,Decimali);
    var flt=parseFloat(Numero);
    var arrot=Math.round(flt*b)/b;
    return arrot;
}

function PuntoInVirgola(Valore){
    var strTmp="";
    
    for (n=0; n<Valore.length;n++){
        if (Valore.substr(n,1)=="."){
            strTmp=strTmp+",";
        }
        else{
            strTmp=strTmp+Valore.substr(n,1);
        }
    }
    return strTmp;
}
//-------------------------------

function ConverteInFloat(Valore){
    if (Valore.indexOf("€") != -1){
        Valore=Valore.replace("€"," ");
    }
    if (Valore.indexOf(",") != -1){
        Valore=Valore.replace(",",".");
    }
    var flt=parseFloat(Valore);
    return flt;
}

function ConverteInString(Valore, Euro){
    var flt=Valore.toFixed(2);
    var nbr=flt.toString();
    if (nbr.indexOf(".") != -1){
        nbr=nbr.replace(".",",");
    }
    if(Euro){
        var str="€ " + nbr;
    }    
    else{
        var str=nbr;
    }
    return str;
}
//--------------------------------

function CheckQta(min,obj){
    var strVal=new String
    var strNum=new String;
    
    strVal=document.getElementById(obj).value;
    strNum=strVal.replace(",",".");
    
    var n=new Number(strNum);
    if (isNaN(n)){    
        alert ("Attenzione, Il valore inserito non è valido.");
        document.getElementById(obj).value=min;
        return;
    }
    if (n<min){
        alert ("Attenzione, La quantità inserita è inferiore alla quantità minima ordinabile.");
        document.getElementById(obj).value=min;
        return;
    }
}
function ChangeQta(id,obj){
    qta=document.form1.elements[obj].value;
    document.form1.Comando.value="14 idc="+id+";qta="+qta+";";
    document.form1.submit();
}

