function SymError() {
    return true;
}

window.onerror = SymError;

var SymRealWinOpen = window.open;


function SymWinOpen(url, name, attributes) {
    return (new Object());
}
window.open = SymWinOpen;


function datediff(per, d1, d2) {
    var d = (d2.getTime() - d1.getTime()) / 1000;
    switch (per) {
        case "yyyy": d /= 12;
        case "m": d *= 12 * 7 / 365.25;
        case "ww": d /= 7;
        case "d": d /= 24;
        case "h": d /= 60;
        case "n": d /= 60;
    }
    return Math.round(d);
}


function check_Data(strG, strM, strA) {
    //CONTROLLO DATA :Se data creata con new Data e' != da quella inserita ==> la data e' scorretta
    var err = '';

    strdata = (strG.length == 1 ? "0" + strG : strG) + "/" + (strM.length == 1 ? "0" + strM : strM) + "/" + strA;
    strdata19 = (strG.length == 1 ? "0" + strG : strG) + "/" + (strM.length == 1 ? "0" + strM : strM) + "/" + "19" + strA ;
    strdata20 = (strG.length == 1 ? "0" + strG : strG) + "/" + (strM.length == 1 ? "0" + strM : strM) + "/" + "20" + strA ;
    data = new Date(parseInt(strA, 10), parseInt(strM, 10) - 1, parseInt(strG, 10));
    var daa = data.getFullYear().toString();
    var dmm = (data.getMonth() + 1).toString();
    dmm = dmm.length == 1 ? "0" + dmm : dmm //se mese ha solo una cifra aggiungo lo 0
    var dgg = data.getDate().toString();
    dgg = dgg.length == 1 ? "0" + dgg : dgg //se giorno ha solo una cifra aggiungo lo 0
    //gestione anno completo
    var dataConf = dgg + "/" + dmm + "/" + daa;
    
    if ((dataConf != strdata) && (dataConf != strdata19) && (dataConf != strdata20)) {
        return -1;
    }
    return data;
}


function check_periodo(strDG, strDM, strDA, strAG, strAM, strAA) {
    var da, a;
    if ((da = check_Data(strDG, strDM, strDA)) != -1) {
        if ((a = check_Data(strAG, strAM, strAA)) != -1) {
            if (datediff("dd", da, a) <= 0) {
                return document.getElementById('ErrPeriodo').value;
            }
            else {
                return 1;
            }
        }
        else {
            return "La data \"Al\" non è una data valida.";
        }
    }
    else {
        return "La data \"Dal\" non è una data valida.";
    }
}


function MM_validateForm() { //v3.0
    //Per validazione email uso un espressione regolare
    var dag, dam, daa, ag, am, aa;
    var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
    var i, cp, p, q, nm, test, num, min, max, errors = '', args = MM_validateForm.arguments;
    for (i = 0; i < (args.length - 3); i += 4) {
        test = args[i + 2];
        val = MM_findObj(args[i]);
        lblerror = args[i + 3];
        if (val) {
            document.getElementById(lblerror).style.visibility = 'hidden';
            nm = args[i + 1];if ((val = val.value) != "") {
                if (test.indexOf('isPeriod') != -1) {
                    //Controllo date arrivo partenza
                    dag = val;
                    var adag = dag.split("/");
                    if (adag.length < 3) {
                        errors += '- ' + nm + ' non è valida.\n';
                        document.getElementById(lblerror).style.visibility = 'visible';
                        document.getElementById(lblerror).innerHTML = nm + ' ' + document.getElementById('ErrData').value;
                        break;
                    } else {
                        dag = adag[0];
                        dam = adag[1];
                        daa = adag[2];
                        nm = args[i + 5];
                        ag = MM_findObj(args[i + 4]);
                        if (!(ag && (ag = ag.value) != "")) {
                            errors += '- ' + nm + ' ' + convertHtmlEntitiesToCharacters(' è richiesto.') + '\n';
                            document.getElementById(lblerror).style.visibility = 'visible';
                            document.getElementById(lblerror).innerHTML = nm + ' ' + document.getElementById('Obbligatorio').value;
                            break;
                        } else {
                            var aag = ag.split("/");
                            if (aag.length < 3) {

                                errors += '- ' + nm + ' non valida.\n';
                                document.getElementById(lblerror).style.visibility = 'visible';
                                document.getElementById(lblerror).innerHTML = nm + ' ' + document.getElementById('ErrData').value;
                                break;
                                
                            } else {
                                ag = aag[0];
                                am = aag[1];
                                aa = aag[2];
                                if ((cp = check_periodo(dag, dam, daa, ag, am, aa)) != 1) {
                                    errors += cp + '\n';
                                    document.getElementById(lblerror).style.visibility = 'visible';
                                    document.getElementById(lblerror).innerHTML = cp;
                                    break;
                                }
                            }
                        }
                    }

                    i += 4;
                    continue;
                }
                if (test.indexOf('isData') != -1) {
                    //Controllo date arrivo partenza
                    dag = val;
                    var adag = dag.split("/");
                    if (adag.length < 3) {
                        errors += '- ' + nm + ' non è valida.\n';
                        document.getElementById(lblerror).style.visibility = 'visible';
                        document.getElementById(lblerror).innerHTML = nm + ' ' + document.getElementById('ErrData').value;
                        break;
                    } else {
                        dag = adag[0];
                        dam = adag[1];
                        daa = adag[2];
                        if ((cp = check_Data(dag, dam, daa)) == -1) {
                            errors += '- ' + nm + ' non valida.\n';
                            document.getElementById(lblerror).style.visibility = 'visible';
                            document.getElementById(lblerror).innerHTML = nm + ' ' + document.getElementById('ErrData').value;
                            break;
                        }
                    }
                    continue;
                }
                if (test.indexOf('isEmail') != -1) {
                    if (!email_reg_exp.test(val)) {
                        errors += '- ' + nm + ' ' + convertHtmlEntitiesToCharacters(' deve contenere un indirizzo e-mail valido.') + '\n';
                        document.getElementById(lblerror).style.visibility = 'visible';
                        document.getElementById(lblerror).innerHTML = nm + ' ' + document.getElementById('ErrEmail').value;
                        break;
                    }
                    continue;
                } else if (test != 'R') {
                    num = parseFloat(val);
                    if (val != '' + num) {
                        errors += '- ' + nm + ' ' + convertHtmlEntitiesToCharacters(' deve contenere un numero.') + '\n';
                        document.getElementById(lblerror).style.visibility = 'visible';
                        document.getElementById(lblerror).innerHTML = nm + ' ' + document.getElementById('ErrNum').value;
                        break;
                    }
                }
            } else if (test.charAt(0) == 'R') {
                errors += '- ' + nm + ' ' + convertHtmlEntitiesToCharacters(' è richiesto.') + '\n';
                document.getElementById(lblerror).style.visibility = 'visible';
                document.getElementById(lblerror).innerHTML = nm + ' ' + document.getElementById('Obbligatorio').value;
                break;
            }

        }
    } 
    document.MM_returnValue = (errors == '');
}

function mostra(str) {
    preno = MM_findObj("divp");
    preno.style.display = str;
}

function MM_openBrWindow(theURL, winName, features) { //v2.0
    window.open(theURL, winName, features);
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3) if ((obj = MM_findObj(args[i])) != null) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}

function P7_Snap() { //v2.63 by PVII
    var x, y, ox, bx, oy, p, tx, a, b, k, d, da, e, el, tw, q0, xx, yy, w1, pa = 'px', args = P7_Snap.arguments; a = parseInt(a);
    if (document.layers || window.opera) { pa = ''; } for (k = 0; k < (args.length); k += 4) {
        if ((g = MM_findObj(args[k])) != null) {
            if ((el = MM_findObj(args[k + 1])) != null) {
                a = parseInt(args[k + 2]); b = parseInt(args[k + 3]); x = 0; y = 0; ox = 0; oy = 0; p = ""; tx = 1;
                da = "document.all['" + args[k] + "']"; if (document.getElementById) {
                    d = "document.getElementsByName('" + args[k] + "')[0]"; if (!eval(d)) {
                        d = "document.getElementById('" + args[k] + "')"; if (!eval(d)) { d = da; } 
                    }
                } else if (document.all) { d = da; } if (document.all || document.getElementById) {
                    while (tx == 1) {
                        p += ".offsetParent"; if (eval(d + p)) {
                            x += parseInt(eval(d + p + ".offsetLeft")); y += parseInt(eval(d + p + ".offsetTop"));
                        } else { tx = 0; } 
                    } ox = parseInt(g.offsetLeft); oy = parseInt(g.offsetTop); tw = x + ox + y + oy;
                    if (tw == 0 || (navigator.appVersion.indexOf("MSIE 4") > -1 && navigator.appVersion.indexOf("Mac") > -1)) {
                        ox = 0; oy = 0; if (g.style.left) { x = parseInt(g.style.left); y = parseInt(g.style.top); } else {
                            w1 = parseInt(el.style.width); bx = (a < 0) ? -5 - w1 : -10; a = (Math.abs(a) < 1000) ? 0 : a; b = (Math.abs(b) < 1000) ? 0 : b;
                            x = document.body.scrollLeft + event.clientX + bx; y = document.body.scrollTop + event.clientY;
                        } 
                    }
                } else if (document.layers) {
                    x = g.x; y = g.y; q0 = document.layers, dd = ""; for (var s = 0; s < q0.length; s++) {
                        dd = 'document.' + q0[s].name; if (eval(dd + '.document.' + args[k])) {
                            x += eval(dd + '.left'); y += eval(dd + '.top');
                            break;
                        } 
                    } 
                } e = (document.layers) ? el : el.style; xx = parseInt(x + ox + a), yy = parseInt(y + oy + b);
                if (navigator.appVersion.indexOf("MSIE 5") > -1 && navigator.appVersion.indexOf("Mac") > -1) {
                    xx += parseInt(document.body.leftMargin); yy += parseInt(document.body.topMargin);
                }
                e.left = xx + pa; e.top = yy + pa;
            } 
        } 
    }
}

function apri(url, nome, attributi) {
    interno = window.open(url, nome, attributi)
}

function newWindow(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen) {
    var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
    var int_windowTop = (screen.height - a_int_windowHeight) / 2;
    var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
    var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
    if (parseInt(navigator.appVersion) >= 4) {
        obj_window.window.focus();
    }
}

function convertHtmlEntitiesToCharacters(theStr) {
    var newDiv = document.createElement(newDiv);
    newDiv.innerHTML = theStr;
    return newDiv.innerHTML;

}
function alertj(str) {
    alert(convertHtmlEntitiesToCharacters(str));
}

function PrivacyChecked() {
    var buttonchecked = false;
    e = document.getElementsByName("personali1")
    b = document.getElementById("Invia")
    if (e[0].checked == true) {
        b.disabled = false;

    }
    else if (e[1].checked == true) {
        //Error privacy
    alertj(document.getElementById('ErrPrivacy2').value);
        b.disabled = true;
    }
} // function PrivacyChecked()


function verifica(tipo) {
    document.MM_returnValue = false;
    if (Left(document.getElementById('risorsa').value,1) == 'P') {

        MM_validateForm('nome', document.getElementById('LNome').innerHTML, 'R', 'EDatiPersonali',
                'cognome', document.getElementById('LCognome').innerHTML, 'R', 'EDatiPersonali',
                //'prov', document.getElementById('LProvincia').innerHTML, 'R', 'EDatiPersonali',
				'indirizzo', document.getElementById('LIndirizzo').innerHTML, 'R', 'EDatiPersonali',
				'tel', document.getElementById('LTelefono').innerHTML, 'R', 'EDatiPersonali',
				'citta', document.getElementById('LCitta').innerHTML, 'R', 'EDatiPersonali',
				'cap', document.getElementById('LCap').innerHTML, 'R', 'EDatiPersonali',
				'email', document.getElementById('LEmail').innerHTML, 'RisEmail', 'EDatiPersonali',
				'npersone', document.getElementById('LNumeroPersone').innerHTML, 'RisNum', 'EDatiPreno',
				'dal_giorno', document.getElementById('LDal').innerHTML, 'RisPeriod', 'EDatiPreno',
				'al_giorno', document.getElementById('LAl').innerHTML, 'RisPeriod', 'EDatiPreno',
				'lung', document.getElementById('Llung').innerHTML, 'R', 'EDatiPreno',
				'larg', document.getElementById('Llarg').innerHTML, 'R', 'EDatiPreno');
    } else {
    MM_validateForm('nome', document.getElementById('LNome').innerHTML, 'R', 'EDatiPersonali',
                'cognome', document.getElementById('LCognome').innerHTML, 'R', 'EDatiPersonali',
				'indirizzo', document.getElementById('LIndirizzo').innerHTML, 'R', 'EDatiPersonali',
				'tel', document.getElementById('LTelefono').innerHTML, 'R', 'EDatiPersonali',
				'citta', document.getElementById('LCitta').innerHTML, 'R', 'EDatiPersonali',
				'cap', document.getElementById('LCap').innerHTML, 'R', 'EDatiPersonali',
				'email', document.getElementById('LEmail').innerHTML, 'RisEmail', 'EDatiPersonali',
				'npersone', document.getElementById('LNumeroPersone').innerHTML, 'RisNum', 'EDatiPreno',
				'dal_giorno', document.getElementById('LDal').innerHTML, 'RisPeriod', 'EDatiPreno',
				'al_giorno', document.getElementById('LAl').innerHTML, 'RisPeriod', 'EDatiPreno');
    }

    if (document.MM_returnValue) {
        e = document.getElementsByName("personali1");
        b = document.getElementById("Invia");
        if (e[0].checked != true) {
            alertj(document.getElementById('ErrPrivacy').value);
            b.disabled = true;
            document.MM_returnValue = false;
        }

    }

    if (document.MM_returnValue) {
        tipo.action = "Conferma_preno.aspx";
        tipo.submit();
    }
}

function verificagruppo(tipo) {

    i = 1;

    MM_validateForm('Nome', document.getElementById('LNome').innerHTML, 'R', 'ENome',
                'Cognome', document.getElementById('LCognome').innerHTML, 'R', 'ECognome',
				'Provincia', document.getElementById('LProvincia').innerHTML, 'R', 'EProvincia',
				'Indirizzo', document.getElementById('LIndirizzo').innerHTML, 'R', 'EIndirizzo',
				'Cap', document.getElementById('LCap').innerHTML, 'R', 'ECap',
				'Localita', document.getElementById('LLocalita').innerHTML, 'R', 'ELocalita',
				'Email', document.getElementById('LEmail').innerHTML, 'RisEmail', 'EEmail',
				'Telefono', document.getElementById('LTelefono').innerHTML, 'R', 'ETelefono',
				'LuogoNascita', document.getElementById('LLuogoNascita').innerHTML, 'R', 'ELuogoNascita',
				'DataNascita', document.getElementById('LDataNascita').innerHTML, 'RisData', 'EDataNascita',
				'NumeroDocumento', document.getElementById('LDocumentoNumero').innerHTML, 'R', 'ENumeroDocumento',
				'DataDocumento', document.getElementById('LDocumentoData').innerHTML, 'RisData', 'EDataDocumento',
				'AutoritaRilascio', document.getElementById('LRilasciato').innerHTML, 'R', 'EAutoritaRilascio'
				);
				
        if (document.MM_returnValue) {
            while (document.getElementById("GNome_" + i + "_")) {
                MM_validateForm('GNome_' + i + '_', i + '° '+ document.getElementById('LNome').innerHTML, 'R','EGruppo',
                                'GCognome_' + i + '_', i + '° ' + document.getElementById('LCognome').innerHTML, 'R', 'EGruppo',
                                'GDataNascita_' + i + '_', i + '° ' + document.getElementById('LDataNascita').innerHTML, 'RisData', 'EGruppo',
                                'GLuogoNascita_' + i + '_', i + '° ' + document.getElementById('LLuogoNascita').innerHTML, 'R', 'EGruppo',
                                'GStato_' + i + '_', i + '° ' + document.getElementById('LStato').innerHTML, 'R', 'EGruppo',
                                'GNazionalita_' + i + '_', i + '° ' + document.getElementById('LNazionalita').innerHTML, 'R', 'EGruppo'
                                );
                i = i + 1;
                if (document.MM_returnValue) {
                    continue; 
                }
                else {
                    break;
                }
            }
        }

        if (document.MM_returnValue) {
            tipo.action = "RegistraGruppo.aspx";
            tipo.submit();
        }

}

function apri(url, nome, attributi) {
    interno = window.open(url, nome, attributi)
}

$(function() {

    $("#dal_giorno").datepicker({ dateFormat: 'dd/mm/yy' });
    $("#al_giorno").datepicker({ dateFormat: 'dd/mm/yy' });
    if (document.getElementById('CFAA')) {
        $("#dal_giorno").datepicker('option', 'minDate', new Date(document.getElementById('CFAA').value, document.getElementById('CFAM').value - 1, document.getElementById('CFAD').value));
        $("#dal_giorno").datepicker('option', 'maxDate', new Date(document.getElementById('CFCA').value, document.getElementById('CFCM').value - 1, document.getElementById('CFCD').value));
        $("#al_giorno").datepicker('option', 'minDate', new Date(document.getElementById('CFAA').value, document.getElementById('CFAM').value - 1, document.getElementById('CFAD').value));
        $("#al_giorno").datepicker('option', 'maxDate', new Date(document.getElementById('CFCA').value, document.getElementById('CFCM').value - 1, document.getElementById('CFCD').value));
    }
}
);

function SettaForm(scelta) {
    if (scelta == true) {
        document.getElementById('dvPreno').style.visibility = 'hidden';
        document.getElementById('dvPreno').style.height = '0%';
        document.getElementById('dal_giorno').value = "";
        document.getElementById('al_giorno').value = "";
        
    }
    else {
        document.getElementById('dvPreno').style.visibility = 'visible';
        document.getElementById('dvPreno').style.height = '100%';
    
    }
}

function TipoVeicolo() {
    if (Left(document.getElementById('risorsa').value, 1) != 'P') {
        document.getElementById('dvveicolo').style.visibility = 'hidden';
        document.getElementById('dvveicolo').style.height = '0px';

    }
    else {
        document.getElementById('dvveicolo').style.visibility = 'visible';
        document.getElementById('dvveicolo').style.height = '100%';

    }
}


function AnnullaPreno() {
    form = document.getElementById("ConfermaPreno");
    form.action = "Prenotazione.aspx";
    form.submit();

}

function ismaxlength(obj) {
    var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : "";
    if (obj.getAttribute && obj.value.length > mlength)
        obj.value = obj.value.substring(0, mlength);
}


function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}

function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}


