// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function submitform()
{
    document.forms['editform'].onsubmit();
}
function toggleprofiles()
{
    new Ajax.Request('/admin/ajax_permission_template', {
        asynchronous:true,
        evalScripts:true,
        onComplete:function(request){
            Element.hide('form-indicator')
        },
        onLoading:function(request){
            Element.show('form-indicator')
        },
        parameters:Form.serialize(document.getElementById('templates_list'))
    });
}
function submitcustomer()
{
    document.forms["customerform"].template.value = document.forms["templates_list"].id.value;
    document.forms["customerform"].onsubmit();
}
function editcustomer()
{
    submitcustomer();
}
function savenewcategory()
{
    document.forms["new-category-form"].newcategory.value = document.forms["answertype-form"].new_category.value;
    document.forms["new-category-form"].onsubmit();
}
function saveanswertype()
{
    document.forms["answertype-form"].onsubmit();
}
function toggle_answer_types()
{
    document.forms['toggle-answertypes'].id.value = document.getElementById('select_main_type').value;
    document.forms['toggle-answertypes'].onsubmit();
}
function question(surveyid, position, f)
{
    f.survey_id.value=surveyid;
    f.position.value=position;
    f.onsubmit();
}
function copyinsert(surveyid, questionid, f)
{
    f.survey_id.value=surveyid;
    f.question_id.value=questionid;
    f.onsubmit();
}
function getElementsByStyleClass(className) {
    var all = document.all ;
    var elements = new Array();
    for (var e = 0; e < all.length; e++)
        if (all[e].className == className)
            elements[elements.length] = all[e];
    return elements;
}
function upload_file(type){
    document.forms["edit-question"].audio_upload.value=type;
    document.forms["edit-question"].submit();
}
function submitsettings()
{
    document.forms["user_survey_settings-form"].submit();
}
function save_new_survey_category()
{
    document.forms["new-survey_category-form"].new_survey_category.value = document.forms["user_survey_settings-form"].new_survey_category_input.value;
    document.forms["new-survey_category-form"].onsubmit();
}
function cancel_new_survey_category()
{
    document.forms["cancel-survey_category-form"].onsubmit();
}
function call_annotation_style_save()
{
    document.forms["call_annotation_style_save_form"].onsubmit();
}
function question_annotation_style_save()
{
    document.forms["question_annotation_style_save_form"].onsubmit();
}
var question_count = 0
function add_question_from_library(question_id,text)
{
    question_count++
    if (question_count == 1) {
        title = document.getElementById('save-library-title');
        title.innerHTML = 'The following questions will be saved to the end of your survey';
        button = document.getElementById("add-btn");
        button.style.display = "block";
    }
    html = $('question-list').innerHTML
    html = html + '<p id="question_p_' + question_id + '">' + text 
    html = html + '<input type="hidden" name="question[' + question_count + ']" value="' + question_id + '" />'
    html = html + '</p>'
    $('question-list').innerHTML = html
}
function remove_library_question(question_id) 
{
    question_count--
    if (question_count == 0) {
        title = document.getElementById('save-library-title');
        title.innerHTML = 'Browse the question banks below and tick the boxes next to the questions you want to add to your survey';
        button = document.getElementById("add-btn");
        button.style.display = "none";
    }			
    Element.remove('question_p_' + question_id)
    // Rename the hidden input fields so the numbering stays correct
    var children = $('question-list').getElementsByTagName('p')
    for (var i = 0; i < children.length; i++) 
    {
        var p = children.item(i)
        var input = p.getElementsByTagName('input').item(0)
        input.name = 'question[' + (i+1) + ']'
    }
}

function check_whisper_answer() {
    if($('whisper_answer').getValue()) {
        $('question_whisper_answer_id').show();
    } else {
        $('question_whisper_answer_id').hide();
    }
}

function playUrl(url) {
    url = "/audio/player.html?url=" + url;
    
    if(!$('player')) {
        var div = new Element('iframe', {
            id: 'player',
            style: 'width: 1px; height: 1px;',
            name:'player',
            src: url
        });
        document.body.appendChild(div);
    } else {
        frames['player'].location.href = "about:blank";
        frames['player'].location.href = url;
    }
}

function stopAudio() {
    if($('player')) {
        frames['player'].location.href = "about:blank";
    }
}

function soundComplete() {
    $$('.preview').each(function(p) {
        if(p['reset']) p['reset']();
    });
}

function playPreview(text, button) {
    // Stop any playing audio
    stopAudio();
    // Reset any preview buttons
    soundComplete();
    
    var url = "/survey/preview";
                            
    button = $(button);
    var original_onclick = button.onclick;
    button.reset = function() {
        this.innerHTML = 'PREVIEW'; 
        this.onclick = original_onclick;
        this.reset = null;
    }
    button.hide();

    var indicator = new Element("img", {
        src: '/images/indicator.gif'
    });
    button.insert({
        after: indicator
    });
                            
    new Ajax.Request(
        url,
        {
            parameters: 'text=' + text,
            onSuccess: function(transport) {
                button.innerHTML = 'STOP';
                button.onclick = function() {
                    stopAudio();
                    this.reset();
                };
            },
            onFailure: function(transport) {
                stopAudio();
                button.reset();
            },
            onComplete: function(transport) {
                indicator.remove();
                button.show();
            }
        });
                            
    return false;
}
