﻿function aprifin(nome, titolo, larghezza, altezza, ridim, scrolla) {
    centroX = (screen.width - larghezza) / 2
    centroY = (screen.height - altezza) / 2
    afin = window.open(nome, titolo, "width=" + larghezza + ",height=" + altezza + ",resizable=" + ridim + ",scrollbars=" + scrolla + ",screenX=" + centroX + ",left=" + centroX + ",screenY=" + centroY + ",top=" + centroY);
    afin.focus();
}
function AddBookmark(type, id) {
    $.post('/Account/AddBookmark', { type: type, id: id }, function (data) {

        if (data.result) {
            $('.add-bookmark').remove();
            alert("Elemento aggiunto ai bookmarks");
        }
        else {
            alert(data.ErrorMessage);
        }
    }, 'json');
}
function CancelBookmark(id) {
    $.post('/Account/RemoveBookmark', { id: id }, function (data) {

        if (data.result) {
            $('#item-' + id).remove();
        }
        else {
            alert(data.ErrorMessage);
        }
    }, 'json');
}
String.prototype.totalReplace = function (find, replace) {
    var temp_array = this.split(find);
    return temp_array.join(replace);
};
function ValidateISBN() {

    /*var ISBNstr = $('#RegistrationCode').val().totalReplace('T600', '');
    var ISBNstr = $('#RegistrationCode').val().totalReplace('t600', '');*/
    var ISBNstr = $('#RegistrationCode').val();
    $.post('/Account/ValidateISBN', { ISBNstr: ISBNstr }, function (data) {

        if (data.result) {
            $('.ISBNerror').remove();
            EnableRegistrationForm();
        }
        else {
            if (!$('.ISBNerror').is(':visible')){
                $('#RegistrationCode').after('<span class="field-validation-error ISBNerror">' + data.ErrorMessage + '</span>');
                InitializeRegistrationForm();
            }
        }
    }, 'json');

}
function InitializeRegistrationForm() {
    $("form input").attr("disabled", "disabled");
    $("form select").attr("disabled", "disabled");
    $("#RegistrationCode").removeAttr("disabled");
}
function EnableRegistrationForm() {
    $("form input").removeAttr("disabled");
    $("form select").removeAttr("disabled");
}

//general Loader 
function createLoader(obj, fun, bgColor) {
    var div = $('<div />');
    $(div).addClass('generalLoader');
    $(div).moveTo($(obj));
    $(div).css('width', $(obj).outerWidth());
    $(div).css('height', $(obj).outerHeight());
    $(div).css('opacity', 0);
    if (bgColor != undefined) {
        $(div).css('background-color', bgColor);
    }
    var loaderId = $(obj).attr('id') + '-Loader';
    $(div).attr('id', loaderId);
    $(document.body).append(div);
    $('#' + loaderId).animate({
        opacity: 0.7
    }, 200, function () {
        if (typeof (fun) == "function") fun();
    });
    return loaderId;
}
//fine general loader

function menuExploder(idd) {
    


    
    temp = 0;
    
   
    for (i = 1; i < 19; i++) {
        //alert(document.getElementById("sub" + i).style.display)

        if (document.getElementById("sub" + i).style.display == 'block') 
            {
             
                temp = i;
            }
        }
        
        if (idd != temp){
        if (temp != 0)
          
        { $("#sub" + temp).slideUp(); }

        
         $("#sub" + idd).slideDown(); }


 }
 function ValidateExercise() {
     $('.ok-answer').remove();
     $('.ko-answer').remove();
     $.each($('.news-aggiornamento-wide'), function (i, item) {
         if ($(this).find('form input:checked').val() == 'True') {
             $(this).find('.titolo').append('<span class="ok-answer">corretto</span>');
         }
         else {
             $(this).find('.titolo').append('<span class="ko-answer">sbagliato</span>');
         }
     });
     $('.btn-reset').show();
     $('.btn-validate').hide();
 }
 function ResetExercise() {
     $('.ok-answer').remove();
     $('.ko-answer').remove();
     $("form input").removeAttr("checked");
     $('.btn-reset').hide();
     $('.btn-validate').show();
 }
 function ShowExerciseSolution() {
     $.each($('.news-aggiornamento-wide form input'), function (i, item) {
         if ($(this).val() == 'True') {
             $(this).attr("checked", "checked");
         }
     });
     ValidateExercise();
 }

 function SearchSite() {
     var SearchSiteText = $('#SearchSiteText').val();
     if (SearchSiteText != 'Cerca nel sito' && SearchSiteText != '') {
         location.href = "/home/SearchResults/" + SearchSiteText;
     }
     else {
         alert('Testo di ricerca non valido');
     }
 }

 function ValidateStaticExercise() {
     $('.ok-answer').remove();
     $('.ko-answer').remove();

     var count = 0;

     $.each($('.news-aggiornamento-wide'), function (i, item) {
         $.each($(item).find('form input'), function (i, item2) {
             if ($(item2).is(':checked')) {
                 if ($(item2).val() == 'True') {
                     $(item2).parent().append('<span class="ok-answer">Risposta corretta</span>');
                     count++;
                 }
                 else {
                     $(item2).parent().append('<span class="ko-answer">Risposta sbagliata</span>');
                     $(item).find('span').show();
                 }
             }
             else if ($(item2).val() == 'False') {
                 $(item2).parent().hide();
             }
             else {
                 var temp = $(item2).parent().clone();
                 $(item2).parent().remove();
                 $(temp).find('input').remove();
                 $(temp).prepend('<span class="msg-answer">La risposta corretta è: </span>');
                 $(item).find('form').append(temp);
             }
         });
     });
     $('.btn-reset').show();
     $('.btn-validate').hide();
     $('h2').text('Risultati della Simulazione Prova Preselettiva');

     var p = $('<p/>');
     p.addClass('focus-test')
     if (count == 10) {
         p.html('<strong>Hai ottenuto un punteggio di ' + count + '/' + $('.news-aggiornamento-wide').length + '.</strong><br /><br />Per la preparazione al concorso' +
        ' per dirigenti scolastici ti segnaliamo il volume di Giuseppe Colosio, Direttore Generale USR Lombardia<br />"<strong>LE NORME DELL\'ISTRUZIONE</strong>" (maggio 2011).<br /><br /><a href="/home/BuyBook" target="_blank">Acquista subito</a></strong>');
     }
     else {
         p.html('<strong>Hai ottenuto un punteggio di ' + count + '/' + $('.news-aggiornamento-wide').length + ' rispondendo correttamente a ' + count +
        ' domande su ' + $('.news-aggiornamento-wide').length + '.<br />Verifica di seguito le soluzioni corrette.</strong><br /><br />Per la preparazione al concorso' +
        ' per dirigenti scolastici ti segnaliamo il volume di Giuseppe Colosio, Direttore Generale USR Lombardia<br />"<strong>LE NORME DELL\'ISTRUZIONE</strong>" (maggio 2011).<br /><br /><a href="/home/BuyBook" target="_blank">Acquista subito</a></strong>');
     }
     $('h2').after(p);
     $(window).scrollTop(0);
 }


 function togglePastEvent(idd) {

     evento = $(idd).parent();
     
     evento = $(evento).find('div.passato');
     
 if (evento.css('display')=='none')
 {
     evento.slideDown();
     
 }
 else{
     evento.slideUp();
     
 }

}

function toggleGallery(idd) {

    gg = $(idd).parent();

    gg = $(gg).find('div.gallery');

    if (gg.css('display') == 'none') {
        gg.slideDown();

    }
    else {
        gg.slideUp();

    }

}

function explodeText(corpo) {
    obj = $(corpo).parent().parent();
    //alert(obj.text());
    v = $(obj).find('div.body');

    if ($(v).height() == 5) {
        $(v).css('overflow', 'auto');
        $(v).css('height', 'auto');
        h = $(v).height();
        $(v).css('overflow', 'hidden');
        $(v).css('height', '5px');

        //alert(h);
        //$(v).css('overflow', 'visible');
        //$(v).slideDown();
        //$(v).css('height', 'auto');
        $(v).animate({ 'height': h + 'px' });
        $(corpo).text('Comprimi');
    }
    else {
        $(v).animate({ 'height': '5px' });
        $(corpo).text('Leggi tutto').fadeIn();
         }


}
function showSiaeField() {
    $('.siae-link').fadeOut(200, function () { $('.siae-code').fadeIn(200); });
}
function hideSiaeField() {
    $('.siae-code').fadeOut(200, function () { $('.siae-link').fadeIn(200); });
}

function checkSIAE() {

    $('#fieldSiae a').fadeOut();
    $('#fieldSiae .ServiceField').fadeIn();
}

function toggleAnswer(qst){

    $ans = $(qst).parent().children('.faqsAnswer');
   

if ($ans.css('display')=='block')
{$ans.slideUp();}
else {
$('.faqsAnswer').slideUp();
$ans.slideDown();}

}

function isEmail(str) {
var at = "@"
var dot = "."
var lat = str.indexOf(at)
var lstr = str.length
var ldot = str.indexOf(dot)
if (str.indexOf(at) == -1) {
return false
}
if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
return false
}
if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
return false
}
if (str.indexOf(at, (lat + 1)) != -1) {
return false
}
if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
return false
}
if (str.indexOf(dot, (lat + 2)) == -1) {
return false
}
if (str.indexOf(" ") != -1) {
return false
}
return true
}


$(document).ready(function () {
    if ($('.box-newsletter').is(':visible')) {
        $('.box-newsletter .but').click(function () {
            if (isEmail($('.box-newsletter .txt').val())) {
                SubscribeNewsletter($('.box-newsletter .txt').val());
                return false;
            }
            else {
                alert('Formato email errato!');
            }
            
        });
        
    }

});

function SubscribeNewsletter(strEmail) {

    
        var EmailRecipe = strEmail;
        $.post('/Home/SubscribeNewsletter', { EmailRecipe: EmailRecipe }, function (data) {

            if (data.result) {

                alert(data.ErrorMessage);
                $('.box-newsletter').fadeOut(200);

            }
            else {
                alert(data.ErrorMessage);
            }
        }, 'json');

}

