function popup_mappa(url,res,scrol,men,sta,loc,tool) {
    var numero
    urllen=url.length-4;
    numero=Math.round(Math.random()*100);
    winname="pippo"+numero.toString();
    var lar = 1000; // larghezza finestra
    var alt = 750; // altezza finestra (la pop-up apre sempre al centro schermo)
    var offx = Math.floor((screen.width-lar)/2);
    var offy = Math.floor((screen.height-alt)/2);

    msg=open(url,"Mappa","fullscreen=no,left="+offx+",top="+offy+",toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width="+lar+",height="+alt);
}



var newwindow;
function poptastic(url)
{
	newwindow=window.open(url,'name','height=300,width=450,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
}

function popup_studio_prestazioni(url)
{
    var lar = 850; // larghezza finestra
    var alt = 400; // altezza finestra (la pop-up apre sempre al centro schermo)
    var offx = Math.floor((screen.width-lar)/2);
    var offy = Math.floor((screen.height-alt)/2);
    newwindow=window.open(url,'name',"width="+lar+",height="+alt+",scrollbars=yes,left="+offx+",top="+offy);
    if (window.focus) {newwindow.focus()}
}

function puliscicampo(field) {
    if (field.value=='Inserisci il cognome del medico...' || field.value=='Inserisci la prestazione medica...')
    {
        field.value='';
    }
}

function controlla_prestazioni_filter_form()
{
    $("#prestazione-autocomplete" ).autocomplete( "close" );
    if ($("#prestazione-autocomplete" ).val()=='')
    {
        alert('Per continuare inserisci una prestazione');
        return false;
    }
    else
    {
        if ($('#spec_ID').val()<=0)
        {
            $('#finestra_prestazione').load('/frontend/scegli_prestazione/'+escape($("#prestazione-autocomplete" ).val()));
            try
            {
                $('#finestra_prestazione').dialog( "option" , 'title' , 'Hai cercato "'+$("#prestazione-autocomplete" ).val()+'". Forse cercavi...');
                $('#finestra_prestazione').dialog('open');
            }
            catch (e)
            {
                document.getElementById('finestra_prestazione').parentNode.style.display = '';
            }
            return false;
        }
        else if ($('#ID_provincia').val()<=0)
        {
            $('#finestra_provincia').load('/frontend/scegli_provincia/'+$('#spec_ID').val()+'/'+$('#tipo_spec').val());
            try
            {
                $('#finestra_provincia').dialog( "option" , 'title' , $("#prestazione-autocomplete" ).val()+' > Scegli una provincia');
                $('#finestra_provincia').dialog('open');
            }
            catch (e)
            {
                document.getElementById('finestra_provincia').parentNode.style.display = '';
            }
            return false;
        }
        else
        {
            return true
        }
    }
}

function setVar(id,tipo,nome)
{
    document.getElementById('spec_ID').value=id;
    document.getElementById('tipo_spec').value=tipo;
    document.getElementById('prestazione-autocomplete').value=nome;
    $( "#prestazione-autocomplete" ).autocomplete('close');
    $('#prestazione').remove();
    $('#finestra_prestazione').dialog('close');
    controlla_prestazioni_filter_form();
}

function form_submit(id)
{
    document.getElementById('ID_provincia').value=id;
    $('#provincia').remove();
    $('#finestra_provincia').dialog('close');
    $('#form-ricerca-prestazioni-home').submit();
}

function setAllVar(id,tipo,prov)
{
    document.getElementById('spec_ID').value=id;
    document.getElementById('tipo_spec').value=tipo;
    document.getElementById('ID_provincia').value=prov;
    $('#form-ricerca-prestazioni-home').submit();
}

function reload(nome)
{
    var ID_prest=document.getElementById('ID_prest').value;
    var ID_prov=document.getElementById('ID_prov').value;
    var ID_spec=document.getElementById('ID_spec').value;
    location.href="/attivita/"+ID_spec+"/"+ID_prest+"/"+ID_prov+"/1/"+nome;
}

function setta_sort (sort)
{
    document.getElementById('sort').value = sort;
    document.filtra_ordina.submit();
}

function setta_domicilio ()
{
    if(document.getElementById('cb_domicilio').checked)
    {
        document.getElementById('page').value = 1;
        document.getElementById('filter_data').value = 1;
        document.filtra_ordina.submit();
    }
    else
    {
        $("#filter_data").val('0');
        document.filtra_ordina.submit();
    }
}

function abilita_filtro()
{
    document.filtra_ordina.action=document.location.hash;
    document.getElementById('page').value = 1;
    document.getElementById('filter_data').value = 1;
    document.filtra_ordina.submit();
}

function pulisci_date()
{
    $("#datainizio").val($('#default_start_date').val());
    $("#datafine").val($('#default_end_date').val());
    $("#orainizio").val('');
    $("#orafine").val('');
}

function controlla_cb(cb)
{
    if(document.getElementById(cb).checked)
    {
        $('#hid_'+cb).val(1);
        if (cb=='cb_ore_data')
        {
            pulisci_date();
        }
        else if(cb=='cb_zone_data')
        {
            $('#cb_domicilio').attr('disabled','disabled');
        }
    }
    else
    {
        if (cb=='cb_ore_data')
        {
            $("#datainizio").val('');
            $("#datafine").val('');
            $("#orainizio").val('');
            $("#orafine").val('');
        }
        else if(cb=='cb_zone_data')
        {
            document.getElementById('dropdown_zone').selectedIndex=0;
        }
        else if(cb=='cb_price')
        {
            document.getElementById('fascia_prezzo').selectedIndex=0;
        }
        $('#hid_'+cb).val('0');
        document.filtra_ordina.action=document.location.hash;
        document.filtra_ordina.submit();
    }
}

function setta_pagina(url){
     $('#filtra_ordina').attr('action', url);
     $('#filtra_ordina').submit();
}

//run the currently selected effect
function show(id){
        //get effect type from
        var selectedEffect = 'blind';

        //most effect types need no options passed by default
        var options = {};
        //check if it's scale or size - they need options explicitly set
        if(selectedEffect == 'scale'){  options = {percent: 100}; }
        else if(selectedEffect == 'size'){ options = { to: {width: 280,height: 185} }; }

        //$(".prezzo").hide();
        //run the effect
        if( $("#prezzo"+id).is(':hidden') ) {
             $("#prezzo"+id).show();
        }else{
            $("#prezzo"+id).show();
        }


}

function hide(id){
   var options = {};
   $("#prezzo"+id+":visible").hide();
}

function finestra(url,title,width,height)
{
    $('#finestra').load(url);
    try
    {
        $('#finestra').dialog( "option" , 'title' , title);
        if (width!= undefined) {
            $('#finestra').dialog( "option" , 'width' , width);
        }
        if (height!= undefined) {
            $('#finestra').dialog( "option" , 'height' , height);
        }
        $('#finestra').dialog('open');
    }
    catch (e)
    {
        document.getElementById('finestra').parentNode.style.display = '';
    }
    return false;
}

function finestra_medico(id,title)
{
    $('#finestra').html(($('#'+id).html()));
    try{
        $('#finestra').dialog( "option" , 'title' , title);
        $('#finestra').dialog('open');
    }catch (e){
        document.getElementById('finestra').parentNode.style.display = '';

    }
}

function finestra_riepilogo_studio(id_studio,top)
{
    $('#finestra_riepilogo_studio').load('/frontend/lista_prestazioni_studio_popup/'+id_studio);
    try
    {
        if(top)
        {
            $('#finestra_riepilogo_studio').dialog( "option" , 'position' , ["center",410]);
        }
        $('#finestra_riepilogo_studio').dialog('open');
    }
    catch (e)
    {
        document.getElementById('finestra_riepilogo_studio').parentNode.style.display = '';
    }
    return false;
}

function submit_filter_form(field)
{
    switch(field.id)
    {
        case 'dropListSpec':$('#dropListProv').val(0);$('#dropListPrest').val(0);break
        case 'dropListProv':$('#dropListPrest').val(0);break
    }
    if(document.location.hash=='#mappa')
    {
        $('#hidden_hash').val('#mappa');
    }
    field.form.submit();
}

function reload_provincie_filter_form (id_spec){
        $.ajax(
        {
            url: "/frontend/create_xml_provincie_form",
            datatype: "xml",
            type: "post",
            data: {
                    id_spec: id_spec,
                    specializzazioni:'specializzazioni'
            },

            success: function(data)
            {
                var options = '<option value="0">Scegli città...</option>';
                $("row", data).map(function()
                {
                    var row_data = ($("cell", this));
                    options += '<option value="' + row_data[0].childNodes[0].nodeValue + '">' + row_data[1].childNodes[0].nodeValue + '</option>';
                })
                $("select#dropListProv").html(options);
                $("select#dropListProv").removeAttr('disabled');

                options = '<option value="0">Scegli prestazione...</option>';
                $("select#dropListPrest").html(options);
                $("select#dropListPrest").attr('disabled','disabled');
            }
        });
}

function reload_prestazioni_filter_form (id_spec,id_prov){
        $.ajax(
        {
            url: "/frontend/create_xml_prestazioni_form",
            datatype: "xml",
            type: "post",
            data: {
                    id_spec:id_spec,
                    id_prov: id_prov
            },

            success: function(data)
            {
                var options = '<option value="0">Scegli prestazione...</option>';
                $("row", data).map(function()
                {
                    var row_data = ($("cell", this));
                    options += '<option value="' + row_data[0].childNodes[0].nodeValue + '">' + row_data[1].childNodes[0].nodeValue + '</option>';
                })
                $("select#dropListPrest").html(options);
                $("select#dropListPrest").removeAttr('disabled');
            }
        });
}

function controlla_filtri_filter_form()
{
    if ($('#dropListSpec').val()<=0 && $('#dropListProv').val()<=0 && $('#dropListPrest').val()<=0)
    {
        alert('Per continuare devi inserire almeno un campo');
        return false;
    }
    return true;
}

function disdici (ID)
{
    if (confirm('Sicuro di voler disdire la prenotazione?'))
    {
        location.href = '/disdici_prenotazione/'+ID;
    }
}

function recupera (ID)
{
    location.href = '/conferma_prenotazione/'+ID;
}

function controlla_conferma_prenotazione()
{
    if (document.getElementById('box').checked==true)
    {
        var nome=document.getElementById('nome').value;
        nome=nome.replace(/^\s+|\s+$/g,"");
        var cognome=document.getElementById('cognome').value;
        cognome=cognome.replace(/^\s+|\s+$/g,"");
        var data_nascita=document.getElementById('data_nascita_utente').value;
        data_nascita=data_nascita.replace(/^\s+|\s+$/g,"");
        if (nome=='' || cognome=='' || data_nascita=='')
        {
            document.getElementById('nome').value='';
            document.getElementById('cognome').value='';
            document.getElementById('data_nascita_utente').value='';
            alert('Se si vuole prenotare per un\'altra persona questi campi sono obbligatori');

            return false
        }
        else
            return true
    }
    else
        return true
}

function reload_faq(id)
{
    location.href = '/faq/'+id;
}

function controlla_questionario(frm) {
    var rv = false;
    for (var i=0;i<frm.elements.length;i++){
        if (frm.elements[i].type && frm.elements[i].type.toLowerCase() =="checkbox" && frm.elements[i].checked) rv=true;
    }
    if (!rv){
        alert("Almeno uno dei campi \"Sulla base di quale criterio hai scelto il medico\" devono essere spuntati!");
    }
    return rv;
}

function controlla_domicilio()
{
        var telefono=document.getElementById('telefono').value;
        telefono=telefono.replace(/^\s+|\s+$/g,"");
        var provincia=document.getElementById('provincia').value;
        var citta=document.getElementById('citta').value;
        citta=citta.replace(/^\s+|\s+$/g,"");
        var indirizzo=document.getElementById('indirizzo').value;
        indirizzo=indirizzo.replace(/^\s+|\s+$/g,"");
        if (telefono=='')
        {
            alert('Il campo telefono è obbligatorio');
            return false
        }
        if (provincia=='--')
        {
            alert('Il campo provincia è obbligatorio');
            return false
        }
        if (citta==0 || citta=='--')
        {
            alert('Il campo città è obbligatorio');
            return false
        }
        if (indirizzo=='')
        {
            alert('Il campo indirizzo è obbligatorio');
            return false
        }

        if (controlla())
            return true
        else
            return false

}

function cambiacitta()
{
    var ID = $("#provincia").val();
    if (ID > 0){
        $.ajax(
        {
            url: "/frontend/create_xml_citta",
            datatype: "xml",
            type: "post",
            data: {
                    ID_provincia: ID,
                    sidx:'nome'
            },

            success: function(data)
            {
                var options = '<option value="0">--</option>';
                $("row", data).map(function()
                {
                    var row_data = ($("cell", this));
                    options += '<option value="' + row_data[0].childNodes[0].nodeValue + '">' + row_data[1].childNodes[0].nodeValue + '</option>';
                })
                $("#citta").html(options);
            }
        });
    }else{
        var options = '<option value="0">--</option>';
        $("#citta").html(options);
    }
}

function change_tab(tab_id,id_spec,id_prov,id_prest)
{
    if (tab_id=='tab-mappa' && $('#cb_domicilio').attr("checked"))
    {
        alert('Nella mappa non si possono vedere i medici che effettuano prestazioni a domicilio');
        setTimeout(function() {document.location.hash="";},200);
    }
    else
    {
        $('*').find('.my-tab-active').removeClass('my-tab-active');
        $('#'+tab_id).addClass('my-tab-active');
        if (tab_id=='tab-lista')
        {
            $('#sc4').hide();
            if($('#cb_zone_data').attr("checked"))
            {
                $('#sc3').show();
            }
            $('#num_medici').show();
            $('#num_studi_mappa').html('');
            $('#div-mappa').remove();
            $('.span_filter').show();
            $('.span_filter_mappa').hide();
            $('.li_sort').show();
            $('.li_sort_mappa').hide();
            $('.medico').show();
            $('#paginatediv').show();
        }
        else
        {
            var data={'id_spec':id_spec,'id_prov':id_prov,'id_prest':id_prest,'fascia-prezzo':$('#fascia_prezzo').val(),'datainizio':$('#datainizio').val(),'datafine':$('#datafine').val(),'orainizio':$('#filterorainizio').val(),'orafine':$('#filterorafine').val()};
            $.ajax({
                type: 'POST',
                data: data,
                url: '/frontend/load_mappa_medici',
                success: function(html){
                    $('#sc3').hide();
                    if($('#cb_price').attr("checked"))
                    {
                        $('#sc4').show();
                    }
                    $('#num_medici').hide();
                    $('#div-mappa').remove();
                    $('.span_filter').hide();
                    $('.span_filter_mappa').show();
                    $('.li_sort').hide();
                    $('.li_sort_mappa').css('display','inline');
                    $('.medico').hide();
                    $('#paginatediv').hide();
                    $('#ordinamento-medici').parent().append(html);
                    $( ".feedback-medico-accordion" ).accordion({
                        collapsible: true,
                        active:false,
                        autoHeight: false
                    });
                    $('a.ajax-lightbox').fancybox({
                        autoDimensions: false,
                        width: 560,
                        height: 'auto',
                        padding: 30
                    });
                    return false;
                }
            });
        }
    }
}

