$(document).ready(function(){

        var id_block = $('#id_block').attr('value');    // id dell'utente della pagina visualizzata
        var go = $('#go').attr('value');    // id dell'utente della pagina visualizzata

        // attacca unn loader gif ad un determinato div
        function preloader(div){
            var page;
            page =  $.ajax({
            url: '/pigreco/cmsstuff/modules/userpages/loader.php',
            async: false
            }).responseText;
            $(div).html(page);
		};

        // carica una determinata pagina esterna in un determinato div
        function loadPageToDiv(urlPage, div){
            var page;
            page =  $.ajax({
            url: urlPage,
            async: false,
            cache: false
            }).responseText;
            $(div).html(page);
        }




        function loadSurvey(id_block, go){
            urlPage = '/pigreco/cmsstuff/modules/survey/survey.php';
            div = '#survey';

            // preloader
            preloader(div);
            var page;
            page =  $.ajax({
            type: 'POST',
            url: urlPage,
            data: 'id_block='+ id_block +'&action='+ go,
            async: false,
            cache: false
            }).responseText;
            $(div).html(page);
            $(div).show('blind','',500);
        }

        loadSurvey(id_block, go);

        $('.resultSurveyButton').live('click', function() {
            urlPage = '/pigreco/cmsstuff/modules/survey/results.php';
            div = '#survey';

            // preloader
            preloader(div);
            var page;
            page =  $.ajax({
            type: 'POST',
            url: urlPage,
            data: 'id_block='+ id_block,
            async: false,
            cache: false
            }).responseText;
            $(div).html(page);
            $(div).show('blind','',500);
        });

        $('.returnToSurveyButton').live('click', function() {
            urlPage = '/pigreco/cmsstuff/modules/survey/survey.php',
            div = '#survey';

            // preloader
            preloader(div);
            var page;
            page =  $.ajax({
            type: 'POST',
            url: urlPage,
            data: 'id_block='+ id_block,
            async: false,
            cache: false
            }).responseText;
            $(div).html(page);
            $(div).show('blind','',500);
        });


        $('#surveyForm').ajaxForm({
            url: '/pigreco/cmsstuff/modules/survey/survey.php',
            success: function(data) {
                // visualizza normalmente il set di domande
                if(data != ''){
                    $('#survey').html(data);
                    //alert(data);
//                  $('#button').html("<input type=\"submit\" name=\"Submit\" value=\"Start2\" />");
                }else{
                    // il sondaggio è concluso
                    $('#button').html("<input type=\"submit\" name=\"Submit\" value=\"Termina\" />");
                }
            }
        });
	});



    // pre-submit callback
function showRequest(formData, jqForm, options) {
    // formData is an array; here we use $.param to convert it to a string to display it
    // but the form plugin does this for you automatically when it submits the data
    var queryString = $.param(formData);

    // jqForm is a jQuery object encapsulating the form element.  To access the
    // DOM element for the form do this:
    // var formElement = jqForm[0];

    alert('About to submit: \n\n' + queryString);

    // here we could return false to prevent the form from being submitted;
    // returning anything other than false will allow the form submit to continue
    return true;
}

// post-submit callback
function showResponse(responseText, statusText, xhr, $form)  {
    // for normal html responses, the first argument to the success callback
    // is the XMLHttpRequest object's responseText property

    // if the ajaxForm method was passed an Options Object with the dataType
    // property set to 'xml' then the first argument to the success callback
    // is the XMLHttpRequest object's responseXML property

    // if the ajaxForm method was passed an Options Object with the dataType
    // property set to 'json' then the first argument to the success callback
    // is the json data object returned by the server

    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
        '\n\nThe output div should have already been updated with the responseText.');
} 
