function checkXMLHTTP(xmlhttp) {
    xmlhttp = false;
    try {
        return xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest !== 'undefined') {
        return xmlhttp = new XMLHttpRequest();
    }
}

function more_view(number, total_quiz) {
    var baseUrl = document.getElementById('base_url').value;
    var xmlhttp4;
    xmlhttp4 = checkXMLHTTP(xmlhttp4);
    var url = baseUrl + 'select-more-quiz';
    var formdata = new FormData();
    formdata.append("number", number);
    xmlhttp4.addEventListener("load", function () {
        var str = xmlhttp4.responseText.split("######");
        document.getElementById('latest_quiz').innerHTML = JsonPersingfromData(str[1], str[0], total_quiz);
        if ((number > total_quiz) || (number === total_quiz)) {
            document.getElementById('moreL').style.display = 'none';
        }
    }, false);
    xmlhttp4.addEventListener("error", function () {
        alert("not upload");
    }, false);
    xmlhttp4.addEventListener("abort", function () {
        alert("Aborted");
    }, false);
    xmlhttp4.open("POST", url);
    xmlhttp4.send(formdata);
}

function JsonPersingfromData(jsonInfo, number, total_quiz) {
    var vals = document.getElementById('select_val').value;
    var valArray = vals.split(",");
    var quizListArray = JSON.parse(jsonInfo);
    var output = '';

    for (var i = 0; i < quizListArray.length; i++) {
        output += '<div class="checkbox checkbox-success">';
        output += '<ul style="padding-left: 10px;">';
        if (valArray.indexOf(quizListArray[i].topic_id) > -1) {
            output += '<input id="checkbox2" name="checkbox2[]" class="styled" onclick="CategorywiseQuiz()" value="' + quizListArray[i].topic_id + '" type="checkbox" checked>';
        } else {
            output += '<input id="checkbox2" name="checkbox2[]" class="styled" onclick="CategorywiseQuiz()" value="' + quizListArray[i].topic_id + '" type="checkbox" >';
        }
        output += '<label for="checkbox1">' + quizListArray[i].topic_name + '</label><br/></ul></div>';
    }
    output += '<li id="moreL">';
    output += '<a href="javascript:;" onclick=" more_view(' + number + ', ' + total_quiz +'); " style="padding-left: 40px">';
    output += 'More....';
    output += '</a> </li> ';
    return output;
}


function CategorywiseQuiz() {
    var xmlhttp2;
    xmlhttp2 = checkXMLHTTP(xmlhttp2);
    var vals = [];
    $(document).ready(function () {
        $('input:checkbox[name="checkbox2[]"]').each(function () {
            if (this.checked) {
                vals.push(this.value);
            }
        });
    });

    var topic_id = vals;
    document.getElementById('select_val').value = vals;
    var baseUrl = document.getElementById('base_url').value;
    var url = baseUrl + 'select-topic-wise-quiz';

    var formdata = new FormData();
    formdata.append("topic_id", topic_id);
    xmlhttp2.addEventListener("load", function () {
        var str = xmlhttp2.responseText.split("#########");
        document.getElementById('cat_quiz').innerHTML = str[0];
        document.getElementById('cat_quiz_latest').innerHTML = str[1];
    }, false);
    xmlhttp2.open("POST", url);
    xmlhttp2.send(formdata);
}

function quizPagination(url, quiz_type, page_number) {
    var xmlhttp2;
    xmlhttp2 = checkXMLHTTP(xmlhttp2);
    var vals = [];
    $(document).ready(function () {
        $('input:checkbox[name="checkbox2[]"]').each(function () {
            if (this.checked) {
                vals.push(this.value);
            }
        });
    });
    var topic_id = vals;
    var formdata = new FormData();
    formdata.append("quiz_type", quiz_type);
    formdata.append("page_number", page_number);
    formdata.append("topic_id", topic_id);
    xmlhttp2.addEventListener("load", function () {
        if (quiz_type === 'popular') {
            document.getElementById('cat_quiz').innerHTML = xmlhttp2.responseText;
        }
        if (quiz_type === 'latest') {
            document.getElementById('cat_quiz_latest').innerHTML = xmlhttp2.responseText;
        }
        $('html, body').animate({scrollTop: 0}, 'slow');
    }, false);
    xmlhttp2.open("POST", url);
    xmlhttp2.send(formdata);
}




//tania 29-08-2016
function activequiz(url, quiz_id) {
    var xmlhttp8;
    xmlhttp8 = checkXMLHTTP(xmlhttp8);
    var formdata = new FormData();
    formdata.append("quiz_id", quiz_id);
    xmlhttp8.addEventListener("load", function () {
        location.reload();
    }, false);
    xmlhttp8.addEventListener("error", function () {
        alert("not working");
    }, false);
    xmlhttp8.addEventListener("abort", function () {
        alert("Aborted");
    }, false);
    xmlhttp8.open("POST", url);
    xmlhttp8.send(formdata);
}

function inactivequiz(url, quiz_id) {
    var xmlhttp8;
    xmlhttp8 = checkXMLHTTP(xmlhttp8);
    var formdata = new FormData();
    formdata.append("quiz_id", quiz_id);
    xmlhttp8.addEventListener("load", function () {
        location.reload();
    }, false);
    xmlhttp8.addEventListener("error", function () {
        alert("not working");
    }, false);
    xmlhttp8.addEventListener("abort", function () {
        alert("Aborted");
    }, false);
    xmlhttp8.open("POST", url);
    xmlhttp8.send(formdata);
}

function deletequiz(url, quiz_id) {
    var xmlhttp8;
    xmlhttp8 = checkXMLHTTP(xmlhttp8);
    var formdata = new FormData();
    formdata.append("quiz_id", quiz_id);
    xmlhttp8.addEventListener("load", function () {
        location.reload();
    }, false);
    xmlhttp8.addEventListener("error", function () {
        alert("not working");
    }, false);
    xmlhttp8.addEventListener("abort", function () {
        alert("Aborted");
    }, false);
    xmlhttp8.open("POST", url);
    xmlhttp8.send(formdata);
}


/*------------ this block of code used for view answer----------*/
// tania date 02-09-2016
$("a").click(function () {
    $("#correct_answer").show("slow");
    $(":radio").each(function () {
        var radioName = $(this).attr("name"); //Get radio name
        $(":radio[name='" + radioName + "']:not(:checked)").attr("disabled", true); //Disable all unchecked radios with the same name
    });
});
// tania date 02-09-2016
$(document).on('click', ':radio', function () {
    $("#correct_answer").show();
    var divnumber = $("#correct_answer").children().length;
    var radioName = $(this).attr("name"); //Get radio name
    var select_answer = $("input:checked").val();
    var correct_answer = $("input[name='hidden_value']").val();
    $(":radio[name='" + radioName + "']:not(:checked)").attr("disabled", true); //Disable all unchecked radios with the same name
    if (divnumber === 2) {
        if (select_answer !== correct_answer) {
            $('#correct_answer').prepend('<div style="color:red" id="wrong_ans" class="col-sm-3">Your answer is wrong</div>');
        } else {
            $('#answer_text').hide();
            $('#correct_answer').prepend('<div class="col-sm-3 align-left">Your Answer is Correct: </div>');
        }
    }
});

$(document).on('click', 'a[id^="multiple_choice_pagination_link"]', function () {
    var currentPage = this.id.replace('multiple_choice_pagination_link', '');
    var url = $('#baseUrl').val() + 'multiple-choice-pagination-link';    
    var quizUrl = $('#quizUrl').val();
    $.post(url,
            {
                current_page: currentPage,
                quiz_url: quizUrl
            },
            function (data) {
                $('#mcq').html(data);
            });
});

$(document).on('click', 'a[id^="true_false_pagination_link"]', function () {
    var currentPage = this.id.replace('true_false_pagination_link', '');
    var url = $('#baseUrl').val() + 'true-false-pagination-link';    
    var quizUrl = $('#quizUrl').val();
    $.post(url,
            {
                current_page: currentPage,
                quiz_url: quizUrl
            },
            function (data) {
                $('#true_false').html(data);
            });
});
/*------------------- end ---------------------------*/
