
function FbQuizSubmit(quizId)
 {
   try
   {
        FB_RequireFeatures(["CanvasUtil", "Api"], function() 
        {
                FB.Facebook.init("236741c949ca741e2fe76fa15243d47f", "/xd_receiver.htm");
                //Login Dialog
                FB.Connect.requireSession(function(){
                    var session = FB.Facebook.apiClient.get_session();
                    var url = 'FbQuiz/SendQuiz' + '?fb_sig_added=1&quizid=' + quizId + '&userId=' + session['uid']
                    for(var key in session) url += '&'+'fb_sig_'+key+'='+session[key];
                    
                    //Check to see if the current logged in user already has granted extended permissions to the application
                    FB.Facebook.apiClient.users_hasAppPermission("publish_stream", 
                    function(check_result){
                     
                            if(check_result == 0)
                            {
                                //Display the Extended Permissions Dialog if the user does not already have the permission
                                 //Extended Permissions Dialog
                                FB.Connect.showPermissionDialog("publish_stream", //permission
                                function(perm_result){
                                //alert(perm_result);
                                if(perm_result != null)
                                {
                                    //Call the ajax post to send quiz to controller for submission
                                    QuizSubmitPost(url);
                                }
                                
                                }, //callback
                                false, //showProfileSelector
                                null   //profileSelectorIds              
                                );
                               
                                               
                            }
                            else if( check_result == 1)
                            {
                                //alert(check_result);
                                //Call the ajax post to send quiz to controller for submission
                                QuizSubmitPost(url);
                            }
                        }
                    );
                    
                   
                    
                    });
        });
    }
    catch(e)
    {
        //TODO: Add code to load mobile window with facebook error message.
        alert("Unable to connect to facebook. Please try again.");
    }
    
    //return quizConfirm;
}

function QuizSubmitPost(quizUrl)
{
    var quiz = $.toJSON(quizUrl);
    $.ajax({
                url: quizUrl,
                type: 'POST',
                data: quiz,
                dataType: 'JSON',
                contentType: "application/json; charset=utf-8",
                success: function(data)
                {
                  // passSelectedDateData(data);
                }
            });
            

}

function passSelectedDateData(data)
{
   alert(data.ReturnCode);
}


