﻿function f_ValidateBasket(status)
{
    var m;
    
    if(status=='User') m=f_ValidateBasketUser();
    else if(status=='Ship') m=f_ValidateBasketShip();
    else if(status=='Invoice') m=f_ValidateBasketInvoice();
        
    if(!m) return true;
    alert(m)
    return false;
}
function f_ValidateBasketShip(){
    if(fTrim(document.getElementById('fid_shipcognome').value)=='') return 'Il campo Cognome è obbligatorio';
    if(fTrim(document.getElementById('fid_shipnome').value)=='') return 'Il campo Nome è obbligatorio';
    if(fTrim(document.getElementById('fid_shipindirizzo').value)=='') return 'Il campo Indirizzo è obbligatorio';
    if(document.getElementById('fid_shipstato').selectedIndex==0) return 'Il campo Stato è obbligatorio';
    if(fTrim(document.getElementById('fid_shipcitta').value)=='') return 'Il campo Città è obbligatorio';
    if(fTrim(document.getElementById('fid_shipcap').value)=='') return 'Il campo Cap è obbligatorio';
    if(fTrim(document.getElementById('fid_shipprovincia').value)=='') return 'Il campo Provincia è obbligatorio';
    return null;
}
function f_ValidateBasketInvoice(){
    
    if(document.getElementById('fid_invoicetype').selectedIndex==0) return null;
    
    if(fTrim(document.getElementById('fid_invoiceintestazione').value)=='') return 'Il campo Intestazione è obbligatorio';
    if(fTrim(document.getElementById('fid_invoiceindirizzo').value)=='') return 'Il campo Indirizzo è obbligatorio';
    if(document.getElementById('fid_invoicestato').selectedIndex==0) return 'Il campo Stato è obbligatorio';
    if(fTrim(document.getElementById('fid_invoicecitta').value)=='') return 'Il campo Città è obbligatorio';
    if(fTrim(document.getElementById('fid_invoicecap').value)=='') return 'Il campo Cap è obbligatorio';
    if(fTrim(document.getElementById('fid_invoiceprovincia').value)=='') return 'Il campo Provincia è obbligatorio';
    if(fTrim(document.getElementById('fid_invoicepartitaiva').value)=='') return 'Il campo Partita Iva è obbligatorio';
    if(fTrim(document.getElementById('fid_invoicecodicefiscale').value)=='') return 'Il campo CodiceFiscale è obbligatorio';
    return null;
}
function f_ValidateBasketUser(){
    
    if(document.getElementById('fid_isnewuser').value=='1'){
        if(!f_CheckEmail(document.getElementById('fid_newuseremail').value)) return 'Email non valida'
        if(fTrim(document.getElementById('fid_newuserpassword').value).length<6) return 'La password deve contenere almeno 6 caratteri'
        if(document.getElementById('fid_newuseremail').value!=document.getElementById('fid_newuseremailc').value) return 'I campi Email e Conferma Email non corrispondono';
        if(document.getElementById('fid_newuserpassword').value!=document.getElementById('fid_newuserpasswordc').value) return 'I campi Password e Conferma Password non corrispondono';
        if(fTrim(document.getElementById('fid_newusercognome').value)=='') return 'Il campo Cognome è obbligatorio';
        if(fTrim(document.getElementById('fid_newusernome').value)=='') return 'Il campo Nome è obbligatorio';
        if(fTrim(document.getElementById('fid_newuserindirizzo').value)=='') return 'Il campo Indirizzo è obbligatorio';
        if(document.getElementById('fid_newuserstato').selectedIndex==0) return 'Il campo Stato è obbligatorio';
        if(fTrim(document.getElementById('fid_newusercitta').value)=='') return 'Il campo Città è obbligatorio';
        if(fTrim(document.getElementById('fid_newusercap').value)=='') return 'Il campo Cap è obbligatorio';
        if(fTrim(document.getElementById('fid_newuserprovincia').value)=='') return 'Il campo Provincia è obbligatorio';
        if(fTrim(document.getElementById('fid_newusertelefono').value)=='') return 'Il campo Telefono è obbligatorio';
    }
    else{
        if(fTrim(document.getElementById('fid_registereduseremail').value)=='') return 'Il campo Email è obbligatorio'
        if(fTrim(document.getElementById('fid_registereduserpassword').value)=='') return 'Il campo Password è obbligatorio'
    }

   return null;
        
}
function f_ValidatePasswordRequest()
{
    if(!f_CheckEmail(document.getElementById('fid_email').value)) 
    {
        alert('Email non valida');
        return false;
    }
    return true;
}
function fTrim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}
function f_CheckEmail(email) {



    var valid = "y";



    if (email != "") {

        if (email.length<7 || email.indexOf("@.") != -1 || email.indexOf("-.") != -1 || email.indexOf("_.") != -1 ||

                email.indexOf("..") != -1 || email.indexOf("._") != -1 || email.indexOf(".-") != -1 || 

                email.indexOf(".@") != -1 || email.indexOf("@-") != -1 || email.indexOf("@_") != -1 ||

                email.indexOf("@") != email.lastIndexOf("@") || email.indexOf("@") == -1 || email.indexOf(".") == -1 ||

                (email.length - (email.lastIndexOf(".") + 1)) < 2) 

            return false;

        else {

            a = "abcdefghijklmnopqrstuvwxyz0123456789@-_.";

            b = 0;

            while (b < email.length) {

                if (a.indexOf(email.charAt(b)) == -1) return false;

                b = b + 1;

            }

        }

    }



    rx = /^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.[a-z][a-z]$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.aero$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.biz$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.coop$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.com$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.edu$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.gov$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.info$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.int$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.mil$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.museum$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.name$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.net$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.org$|^[a-z0-9\._]*[a-z0-9_]@[a-z0-9][a-z0-9\-\.]*[a-z0-9]\.pro$/i;

    if (rx.test(email)) {

        rx = /^[a-z0-9_]/i;

        if((rx.test(email))) return true;

    }

    

    return false;

}
