function responseScript(response) {
  var script = response.extractScripts();
          var myReturnedValues = script.map(function(script) {
              return eval(script);
              }); 
}

function printPartOfPage(elementId, hidden_field){

    var hidden = document.getElementById(hidden_field);
    
    var printContent = document.getElementById(elementId);
    var windowUrl = 'about:blank';
    var uniqueName = new Date();
    var windowName = 'Print' + uniqueName.getTime();
    var printWindow = window.open(windowUrl, windowName, '');
    
    printWindow.document.write(hidden.innerHTML);
    printWindow.document.write(printContent.innerHTML);
    printWindow.document.close();
    printWindow.focus();
    printWindow.print();
    printWindow.close();
}

function prepare(form_id){
    var formblock;
    
    formblock = document.getElementById(form_id);
    forminputs = formblock.getElementsByTagName('input');
    
    return forminputs;
}

function select_all(formname, name, value){

    forminputs = prepare(formname);
    
    for (i = 0; i < forminputs.length; i++) {
        // regex here to check name attribute   
        
        var regex = new RegExp(name, "i");
        if (regex.test(forminputs[i].getAttribute('name'))) {
        
            if (value == '1') {
                forminputs[i].checked = true;
            }
            else {
                forminputs[i].checked = false;
            }
        }
    }
}

function showConfirmationBox(msg){
    var r = confirm(msg);
    if (r == true) {
        return true;
    }
    else {
        return false;
    }
}

function change_rule_options(options_value){
    row_min_points = $('min_points');
    row_correct_answer = $('min_correct_answers');
    
    if (options_value == "min_points") {
        row_min_points.style.display = '';
        row_correct_answer.style.display = 'none';
    }
    else 
        if (options_value == "min_correct_answers") {
            row_min_points.style.display = 'none';
            row_correct_answer.style.display = '';
        }
        else {
            row_min_points.style.display = 'none';
            row_correct_answer.style.display = 'none';
        }
}

function get_state_list(country, type, state){
    if (typeof state == 'undefined') {
        var state = "0";
    }
    if (country == "select") {
        return;
    }
    
    var divArticle = $("state_list");
    divArticle.innerHTML = 'Loading...';
    ajax_url = website_url + 'getStatesList.php';
    new Ajax.Request(ajax_url, {
        method: 'post',
        parameters: {
            country: country,
            type: type,
            state: state
        },
        onSuccess: function(transport){
            var response = transport.responseText;
            divArticle.innerHTML = response;
        }
    });
}

/*
 function get_chat_lines(myform)
 {
 
 var target_user_name=document.bad_chats.target_username.value;
 var divArticle = $("chat_report");
 var divArticle1 = $("chat_report_1");
 divArticle.innerHTML = 'Loading...';
 ajax_url = 'getBadChatLines.php';
 new Ajax.Request(ajax_url,
 {
 method: 'post',
 parameters: {target_user_name:target_user_name},
 onSuccess: function(transport)
 {
 var response = transport.responseText;
 divArticle.innerHTML = response;
 divArticle1.innerHTML = response;
 }
 }
 );
 }
 */
/*
 function get_chat_lines(restricted)
 {
 
 var target_user_name=document.bad_chats.target_username.value;
 
 var divArticle1 = $("chat_report_1");
 var divArticle3 = $("chat_report");
 var divArticle2 = $("admin_chat");
 divArticle1.innerHTML = 'Loading...';
 divArticle3.innerHTML = 'Loading...';
 ajax_url = 'getBadChatLines.php';
 new Ajax.Request(ajax_url,
 {
 method: 'post',
 parameters: {target_user_name:target_user_name, restricted:1},
 onSuccess: function(transport)
 {
 var response = transport.responseText;
 divArticle1.innerHTML = response;
 divArticle3.innerHTML = response;
 }
 }
 );
 
 divArticle2.innerHTML = 'Loading...';
 ajax_url = 'getBadChatLines.php';
 new Ajax.Request(ajax_url,
 {
 method: 'post',
 parameters: {target_user_name:target_user_name, restricted:0},
 onSuccess: function(transport)
 {
 var response = transport.responseText;
 divArticle2.innerHTML = response;
 }
 }
 );
 }*/
function get_package_amount(package_id){
    var divArticle = $("order_amounts");
    divArticle.innerHTML = 'Loading...';
    ajax_url = 'getPackageName.php';
    new Ajax.Request(ajax_url, {
        method: 'post',
        parameters: {
            'package': package_id
        },
        onSuccess: function(transport){
            var response = transport.responseText;
            divArticle.innerHTML = response;
        }
    });
}



function reload_captcha(element){
    url = website_url + 'kcaptcha/index.php?' + Math.round(Math.random() * 10000 + 1);
    document.getElementById(element).src = url;
}

function view_top_rankers(){
    r = Math.random();
    //alert("hi");
    var url = website_url + "top_rankers.php?r=" + r;
    ajaxwin = dhtmlwindow.open("ajaxbox", "ajax", url, "Top 100 Users", "width=240px,height=400px,left=100px,top=20px,resize=1,scrolling=1");
}

function show_next(){
    //alert("hi show next");
    //document.getElementById("quiz_next_button").style.display = '';
    timer_condition = true;
    submitForm();
    
}

function checkNumberOfOptions1(correct_answer,option_count)
{
    var ccounter = 0;
    
    var ques_answer =document.getElementById('ques_answer').value;
    if(document.getElementById('ques_answer').value)
    {
        ccounter = ccounter+1;
    }

    global_ccounter = ccounter;
    document.getElementById('ques_answer_hidden').value = ques_answer;
    if(ques_answer!='')
    {
        option_cond = true;
        document.getElementById('ques_answer_hidden').value = ques_answer;
        document.getElementById('ques_answer').disabled=true;
    }
    else
    {
        option_cond = false;

        if(document.getElementById('loading_text').style.display == '')
        {
            document.getElementById('select_more_options').style.display='none';
        }
        else if(document.getElementById('loading_text').style.display == 'none')
        {
            document.getElementById('select_more_options').style.display='';
        }
    //alert(select_more_options);

    }
    //alert(option_cond);
    //    alert('called');
    submitForm();
    return false;
}


function checkNumberOfOptions(correct_answer_count,option_count)
{
  var ccounter = 0;
//alert(correct_answer_count+" "+option_count);
 for(var i=0;i<option_count;i++)
 {
 var temp = "select_"+i;
 //alert(temp); 
	 if(document.getElementById(temp).checked)
	 {
	 	ccounter = ccounter+1;
	 }
	 
 }

global_ccounter = ccounter;
if(ccounter == correct_answer_count)
{
option_cond = true;
for(var j=0;j<option_count;j++)
 {
 var temp1 = "select_"+j;
	 if(document.getElementById(temp1).checked == false)
	 {
	 	document.getElementById(temp1).disabled=true;
	 }
	
	 
 }
}
else if(ccounter > correct_answer_count)
{
option_cond = false;

for(var k=0;k<option_count;k++)
 {
 var temp2 = "select_"+k;
	 if(document.getElementById(temp2).disabled == true)
	 {
	 if(document.getElementById(temp2).checked == true)
	 {
	 	document.getElementById(temp2).checked=false;
	 	document.getElementById(chk)
	 }
	 }
	
	 
 }
}
else
{
option_cond = false;

if(document.getElementById('loading_text').style.display == '')
{
document.getElementById('select_more_options').style.display='none';
}
else if(document.getElementById('loading_text').style.display == 'none')
{
	document.getElementById('select_more_options').style.display='';	
}
//alert(select_more_options);

}
//alert(option_cond);
submitForm();
}


function submitForm() {
//alert("option :"+option_cond+"\ntimer "+timer_condition);
if((option_cond == true) && (timer_condition == true) && submit_form_flag == false)
{
document.getElementById('select_more_options').style.display='none';
document.getElementById('loading_text').style.display='';
//document.getElementById("quiz_next_button").style.display = '';
$('quiz').submit();
submit_form_flag = true;
}



}


//


// script for Time ------------------------------------------------------------------------------
function cdtime(container, seconds_to_countdown){

    if (!document.getElementById || !document.getElementById(container)) {
    
        return
    }
    this.container = document.getElementById(container)
    this.elapsedTime = 0
    //this.seconds_to_countdown=new Date(seconds_to_countdown)
    this.seconds_to_countdown = seconds_to_countdown
    this.timesup = false
    this.updateTime()
}

cdtime.prototype.updateTime = function(){
    var thisobj = this
    this.elapsedTime++
    setTimeout(function(){
        thisobj.updateTime()
    }, 1000) //update time every second
}

cdtime.prototype.displaycountdown = function(baseunit, functionref){

    this.baseunit = baseunit
    this.formatresults = functionref
    this.showresults()
}

cdtime.prototype.showresults = function(){

    var thisobj = this
    
    var timediff = this.seconds_to_countdown - this.elapsedTime //difference btw target date and current date, in seconds
    if (timediff < 0) { //if time is up
        this.timesup = true
        this.container.innerHTML = this.formatresults()
        return;
    }
    var oneMinute = 60 //minute unit in seconds
    var oneHour = 60 * 60 //hour unit in seconds
    var oneDay = 60 * 60 * 24 //day unit in seconds
    var dayfield = Math.floor(timediff / oneDay)
    var hourfield = Math.floor((timediff - dayfield * oneDay) / oneHour)
    var minutefield = Math.floor((timediff - dayfield * oneDay - hourfield * oneHour) / oneMinute)
    var secondfield = Math.floor((timediff - dayfield * oneDay - hourfield * oneHour - minutefield * oneMinute))
    if (this.baseunit == "hours") { //if base unit is hours, set "hourfield" to be topmost level
        hourfield = dayfield * 24 + hourfield
        dayfield = "n/a"
    }
    else 
        if (this.baseunit == "minutes") { //if base unit is minutes, set "minutefield" to be topmost level
            minutefield = dayfield * 24 * 60 + hourfield * 60 + minutefield
            dayfield = hourfield = "n/a"
        }
        else 
            if (this.baseunit == "seconds") { //if base unit is seconds, set "secondfield" to be topmost level
                var secondfield = timediff
                dayfield = hourfield = minutefield = "n/a"
            }
    this.container.innerHTML = this.formatresults(dayfield, hourfield, minutefield, secondfield)
    ti = setTimeout(function(){
        thisobj.showresults()
    }, 1000) //update results every second
}

/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////

//Create your own custom format function to pass into cdtime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left

//The values of these arguments may change depending on the "baseunit" parameter of cdtime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc


function formatresults(){

    if (this.timesup == false) {//if target date/time not yet met
        var displaystring = "";
        
        if (arguments[0] != "n/a" && arguments[0] != 0) {
            displaystring = displaystring + arguments[0] + " Tage ";
        }
        if (arguments[1] != "n/a") {
            if (arguments[1] < 10) {
                displaystring = displaystring + "0";
            }
            displaystring = displaystring + arguments[1] + " Stunden ";
        }
        if (arguments[2] != "n/a") {
            if (arguments[2] < 10) {
                displaystring = displaystring + "0";
            }
            displaystring = displaystring + arguments[2] + " Min ";
        }
        if (arguments[3] != "n/a") {
            if (arguments[3] < 10) {
                displaystring = displaystring + "0";
            }
            displaystring = displaystring + arguments[3] + " Sek ";
        }
        
    }
    else { //else if target date/time met
        var displaystring = display_string;
    }
    
    return displaystring;
}

function formatresultsquiz(){
    if (this.timesup == false) {//if target date/time not yet met
        var displaystring = "";
        
        if (arguments[0] != "n/a" && arguments[0] != 0) {
            displaystring = displaystring + arguments[0] + " days ";
        }
        if (arguments[1] != "n/a") {
            if (arguments[1] < 10) {
                displaystring = displaystring + "0";
            }
            displaystring = displaystring + arguments[1] + " : ";
        }
        if (arguments[2] != "n/a") {
            if (arguments[2] < 10) {
                displaystring = displaystring + "0";
            }
            displaystring = displaystring + arguments[2] + " : ";
        }
        if (arguments[3] != "n/a") {
            if (arguments[3] < 10) {
                displaystring = displaystring + "0";
            }
            displaystring = displaystring + arguments[3] + "";
        }
    }
    else { //else if target date/time met
        //clearTimeout(ti);
        
        $('quiz_timer').style.display = 'none';
        $('time_expired').style.display = '';
       
    }
    return displaystring;
}

//validation of take_quiz form
function validate_form(thisform){
    checkboxes = thisform.elements;
    count = checkboxes.length;
    flag = false;
    for (i = 0; i < count; i++) {
        if (checkboxes[i].checked) {
            flag = true;
            break;
        }
    }
    if (flag == true) {
        return true;
    }
    else {
        alert(err_select_option);
        return false;
    }
}

function showLoadingMessage(){
    myJsProgressBarHandler.setPercentage('displaybar', '100');
    //var hide_id = document.getElementById('submit_button');
    var show_id = document.getElementById('loading');
    show = $(show_id);
    //hide = $(hide_id);
    //hide.style.display = "none";
    show.style.display = "block";
    
    //document.getElementById('submit').value = "" ;
}

function ShowHidedElement(show_id, hide_id, status){ //status 1 = hide & 0 = display
    if (show_id != '') 
        show = $(show_id);
    
    if (hide_id != '') 
        hide = $(hide_id);
    
    if (status == 1) {
        if (show_id != '') 
            show.style.display = "none";
        
        if (hide_id != '') 
            hide.style.display = "block";
    }
    else {
        if (show_id != '') 
            show.style.display = "block";
        
        if (hide_id != '') 
            hide.style.display = "none";
    }
}

function prototype_window(){
    win3_1 = new Window('3_1', {
        className: "alphacube",
        title: "Sample3",
        width: 200,
        height: 200,
        top: 270,
        left: 400,
        destroyOnClose: true
    });
    win3_1.getContent().innerHTML = "Loading";
    win3_1.show();
    new Ajax.Request('top_rankers_1.php', {
        method: 'post',
        onSuccess: function(transport){
            var response_txt = transport.responseText;
            win3_1.getContent().innerHTML = response_txt;
            
        }
    });
    
    
}

function unescapeHTML(text){
    var div = document.createElement('div');
    div.innerHTML = text;
    return div.innerHTML;
}

function ajax_call_close(){
    //alert ("Hi before unload _)");
    var iX = window.document.body.offsetWidth - window.event.clientX;
    var iY = window.event.clientY;
    alert(iX + ":" + iY);
    if (iX <= 30 && iY < 0) {
    
        //window.onbeforeunload = ajax_call_close();
        
        ajax_url = website_url + 'logout.php';
        new Ajax.Request(ajax_url, {
            method: 'post',
            parameters: {},
            onSuccess: function(transport){
                //var response = transport.responseText;
                //divArticle.innerHTML = response;
            }
        });
    }
}


function getStateListPool(country, type, state){
    if (typeof state == 'undefined') {
        var state = "0";
    }
    if (country == "Select") {
        return;
    }
    
    var divArticle = $("state_list");
    divArticle.innerHTML = 'Loading...';
    ajax_url = website_url + 'get_state_list_pool.php';
    new Ajax.Request(ajax_url, {
        method: 'post',
        parameters: {
            country: country,
            type: type,
            state: state
        },
        onSuccess: function(transport){
            var response = transport.responseText;
            divArticle.innerHTML = response;
        }
    });
}

function getDistrictListPool(state, type, district){
    if (typeof district == 'undefined') {
        var district = "0";
    }
    if (state == "select") {
        return;
    }
    
    var divArticle = $("district_list");
    divArticle.innerHTML = 'Loading...';
    ajax_url = website_url + 'get_district_list_pool.php';
    new Ajax.Request(ajax_url, {
        method: 'post',
        parameters: {
            state: state,
            type: type,
            district: district
        },
        onSuccess: function(transport){
            var response = transport.responseText;
            divArticle.innerHTML = response;
        }
    });
}

function emptySchoolType(temp){
    var a = document.getElementById(temp);
    a.value = '';
    
}

function popupWindow(url, title, width, height){
    /*window.location="#top";
     my_window = window.open(url,title,'width='+ width +',height='+height);
     my_window.moveTo(300,300);
     */	
	
    var win = new Window({
        className: "alphacube",
        title: title,
        top: 0,
        left: 0,
        width: width,
        height: height,
        url: url,
        showEffectOptions: {
            duration: 1.5
        }
    })
	win.showCenter();
    win.show();
}

