// $Id: subquestions.js 9692 2011-01-15 21:31:10Z c_schmitz $
var labelcache=[];
$(document).ready(function(){
removeCSRFDivs();
$(document).on("click","#editsubquestionsform :submit", function() {//Validate duplicate before try to submit: surely some other javascript elsewhere
return code_duplicates_check();
});
$('.tab-page:first .answertable tbody').sortable({
containment:'parent',
start:startmove,
update:aftermove,
distance:3});
$('.btnaddanswer').click(addinput);
$('.btndelanswer').click(deleteinput);
$('#labelsetbrowser').dialog({
autoOpen: false,
modal: true,
width:800,
title: lsbrowsertitle});
$('#quickadd').dialog({
autoOpen: false,
modal: true,
width:600,
title: quickaddtitle,
open: function( event, ui ) {
$('textarea', this).show(); // IE 8 hack
},
beforeClose: function( event, ui ) {
$('textarea', this).hide(); // IE 8 hack
}
});
$('.btnlsbrowser').click(lsbrowser);
$('#btncancel').click(function(){
$('#labelsetbrowser').dialog('close');
});
$('#btnlsreplace').click(transferlabels);
$('#btnlsinsert').click(transferlabels);
$('#btnqacancel').click(function(){
$('#quickadd').dialog('close');
});
$('#btnqareplace').click(quickaddlabels);
$('#btnqainsert').click(quickaddlabels);
$('#labelsets').click(lspreview);
$('#languagefilter').click(lsbrowser);
$('.btnquickadd').click(quickadddialog);
$('#saveaslabel').dialog({ autoOpen: false,
modal: true,
width: 300,
title: saveaslabletitle});
$('.bthsaveaslabel').click(getlabel);
$('#btnlacancel').click(function(){
$('#saveaslabel').dialog('close');
});
$('input[name=savelabeloption]:radio').click(setlabel);
flag = [false, false];
$('#btnsave').click(savelabel);
updaterowproperties();
});
function deleteinput()
{
// 1.) Check if there is at least one answe
countanswers=$(this).closest("tbody").children("tr").length;//Maybe use class is better
if (countanswers>1)
{
// 2.) Remove the table row
scale_id=removechars($(this).closest('table').attr('id'));
index = Number($(this).closest('tr').parent().children().index($(this).closest('tr')))+1;
languages=langs.split(';');
var x;
for (x in languages)
{
tablerow=$('#answertable_'+languages[x]+'_'+scale_id+' tbody tr:nth-child('+index+')');
if (x==0) {
tablerow.fadeTo(400, 0, function(){
$(this).remove();
updaterowproperties();
});
}
else {
tablerow.remove();
}
}
if ($(tablerow).is('[id]'))
{
rowinfo=$(tablerow).attr('id').split('_');
$('#deletedqids').val($('#deletedqids').val()+' '+rowinfo[2]);
}
}
else
{
$.blockUI({message:"
"+strCantDeleteLastAnswer+"
"});
setTimeout(jQuery.unblockUI,1000);
}
updaterowproperties();
}
function addinput()
{
var sID=$('input[name=sid]').val();
var gID=$('input[name=gid]').val();
var qID=$('input[name=qid]').val();
scale_id=removechars($(this).closest('table').attr('id'));
newposition = Number($(this).closest('tr').parent().children().index($(this).closest('tr')))+1;
languages=langs.split(';');
sNextCode=getNextCode($(this).parent().parent().find('.code').val());
while ($(this).parent().parent().parent().find('input[value="'+sNextCode+'"]').length>0 && sNextCode!=$(this).parent().parent().find('.code').val())
{
sNextCode=getNextCode(sNextCode);
}
for (x in languages)
{
tablerow=$('#answertable_'+languages[x]+'_'+scale_id+' tbody tr:nth-child('+newposition+')');
var randomid='new'+Math.floor(Math.random()*111111)
if (x==0) {
inserthtml='
'
}
else
{
inserthtml='
'+htmlspecialchars(sNextCode)+'
'
}
tablerow.after(inserthtml);
tablerow.next().find('.btnaddanswer').click(addinput);
tablerow.next().find('.btndelanswer').click(deleteinput);
tablerow.next().find('.code').blur(updatecodes);
}
$('.row_'+newposition).fadeIn('slow');
$('.row_'+newposition).show(); //Workaround : IE does not show with fadeIn only
$('.tab-page:first .answertable tbody').sortable('refresh');
updaterowproperties();
}
function startmove(event,ui)
{
oldindex = Number($(ui.item[0]).parent().children().index(ui.item[0]))+1;
}
function aftermove(event,ui)
{
// But first we have change the sortorder in translations, too
var newindex = Number($(ui.item[0]).parent().children().index(ui.item[0]))+1;
info=$(ui.item[0]).closest('table').attr('id').split("_");
languages=langs.split(';');
var x;
for (x in languages)
{
if (x>0) {
tablerow=$('#tabpage_'+languages[x]+' tbody tr:nth-child('+newindex+')');
tablebody=$('#tabpage_'+languages[x]).find('tbody');
if (newindex option').size()>0)
{
$('#labelsets').selectOptions(remind);
lspreview();
$('#btnlsreplace').removeClass('ui-state-disabled');
$('#btnlsinsert').removeClass('ui-state-disabled');
$('#btnlsreplace').removeProp('disabled');
$('#btnlsinsert').removeProp('disabled');
}
else
{
$("#labelsetpreview").html("
"+strNoLabelSet+"
");
$('#btnlsreplace').addClass('ui-state-disabled');
$('#btnlsinsert').addClass('ui-state-disabled');
$('#btnlsreplace').prop( "disabled", true );
$('#btnlsinsert').prop( "disabled", true );
}
});
}
// previews the labels in a label set after selecting it in the select box
function lspreview()
{
if ($('#labelsets > option').size()==0)
{
return;
}
var lsid=$('#labelsets').val();
surveyid=$('input[name=sid]').val();
// check if this label set is already cached
if (!isset(labelcache[lsid]))
{
$.ajax({
url: lsdetailurl,
dataType: 'json',
data: {lid:lsid, sid:surveyid},
cache: true,
success: function(json){
//$("#labelsetpreview").tabs('destroy');
$("#labelsetpreview").empty();
var tabindex='';
var tabbody='';
for ( x in json)
{
language=json[x];
for (y in language)
{
tabindex=tabindex+'
";
lsrows=language[y][0];
tablerows='';
var highlight=true;
for (z in lsrows)
{
highlight=!highlight;
tabbody=tabbody+'
'+lsrows[z].code+'
'+lsrows[z].title+'
';
}
tabbody=tabbody+'
'+strcode+'
'+strlabel+'
';
}
}
$("#labelsetpreview").append('
'+tabindex+'
'+tabbody);
labelcache[lsid]='
'+tabindex+'
'+tabbody;
$("#labelsetpreview").tabs();
$("#labelsetpreview").tabs( "refresh" );
}}
);
}
else
{
//$("#labelsetpreview").tabs('destroy');
$("#labelsetpreview").empty();
$("#labelsetpreview").append(labelcache[lsid]);
$("#labelsetpreview").tabs();
$("#labelsetpreview").tabs( "refresh" );
}
}
/**
* This is a debug function
* similar to var_dump in PHP
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;
//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j \"" + value + "\"\n";
}
}
} else { //Stings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
function transferlabels()
{
var sID=$('input[name=sid]').val();
var gID=$('input[name=gid]').val();
var qID=$('input[name=qid]').val();
surveyid=$('input[name=sid]').val();
if ($(this).attr('id')=='btnlsreplace')
{
var lsreplace=true;
}
else
{
var lsreplace=false;
}
if (lsreplace)
{
$('.answertable:eq('+scale_id+') tbody tr').each(function(){
aRowInfo=this.id.split('_');
$('#deletedqids').val($('#deletedqids').val()+' '+aRowInfo[2]);
});
}
var lsid=$('#labelsets').val();
$.ajax({
url: lsdetailurl,
dataType: 'json',
data: {lid:lsid, sid:surveyid},
cache: true,
success: function(json){
languages=langs.split(';');
var x;
var defaultdata_labels = null;
for (x in languages)
{
lang_x_found_in_label=false;
var tablerows='';
var y;
for (y in json)
{
language=json[y];
var lsrows = new Array();
//defaultdata=language[languages[0]][0];
for (z in language)
{
if (z==languages[0])
{
defaultdata_labels=language[languages[0]];
}
if (z==languages[x])
{
lang_x_found_in_label = true;
lsrows=language[z][0];
}
var k;
for (k in lsrows)
{
var randomid='new'+Math.floor(Math.random()*111111)
if (x==0)
{
tablerows=tablerows+'
'
}
else
{
tablerows=tablerows+'
'+htmlspecialchars(lsrows[k].code)+'
'
}
}
}
}
if (lang_x_found_in_label === false)
{
lsrows=defaultdata_labels[0];
k=0;
for (k in lsrows)
{
tablerows=tablerows+'
'+htmlspecialchars(lsrows[k].code)+'
'
}
}
if (lsreplace) {
$('#answertable_'+languages[x]+'_'+scale_id+' tbody').empty();
}
$('#answertable_'+languages[x]+'_'+scale_id+' tbody').append(tablerows);
// Unbind any previous events
$('#answertable_'+languages[x]+'_'+scale_id+' .btnaddanswer').unbind('click');
$('#answertable_'+languages[x]+'_'+scale_id+' .btndelanswer').unbind('click');
$('#answertable_'+languages[x]+'_'+scale_id+' .answer').unbind('focus');
$('#answertable_'+languages[x]+'_'+scale_id+' .btnaddanswer').click(addinput);
$('#answertable_'+languages[x]+'_'+scale_id+' .btndelanswer').click(deleteinput);
}
$('#labelsetbrowser').dialog('close');
$('.tab-page:first .answertable tbody').sortable('refresh');
updaterowproperties();
}}
);
}
function quickaddlabels()
{
var sID=$('input[name=sid]').val();
var gID=$('input[name=gid]').val();
var qID=$('input[name=qid]').val();
if ($(this).attr('id')=='btnqareplace')
{
var lsreplace=true;
}
else
{
var lsreplace=false;
}
if (lsreplace)
{
$('.answertable:eq('+scale_id+') tbody tr').each(function(){
aRowInfo=this.id.split('_');
$('#deletedqids').val($('#deletedqids').val()+' '+aRowInfo[2]);
});
}
languages=langs.split(';');
for (x in languages)
{
lsrows=$('#quickaddarea').val().split("\n");
if (lsrows[0].indexOf("\t")==-1)
{
separatorchar=';'
}
else
{
separatorchar="\t";
}
tablerows='';
for (k in lsrows)
{
thisrow=lsrows[k].splitCSV(separatorchar);
if (thisrow.length<=languages.length)
{
thisrow.unshift(parseInt(k)+1);
}
else
{
thisrow[0]=thisrow[0].replace(/[^A-Za-z0-9]/g, "").substr(0,20);
}
var randomid='new'+Math.floor(Math.random()*111111)
if (typeof thisrow[parseInt(x)+1]=='undefined')
{
thisrow[parseInt(x)+1]=thisrow[1];
}
if (x==0)
{
tablerows=tablerows+'
'
}
else
{
tablerows=tablerows+'
'
}
}
if (lsreplace) {
$('#answertable_'+languages[x]+'_'+scale_id+' tbody').empty();
}
$('#answertable_'+languages[x]+'_'+scale_id+' tbody').append(tablerows);
// Unbind any previous events
$('#answertable_'+languages[x]+'_'+scale_id+' .btnaddanswer').unbind('click');
$('#answertable_'+languages[x]+'_'+scale_id+' .btndelanswer').unbind('click');
$('#answertable_'+languages[x]+'_'+scale_id+' .answer').unbind('focus');
$('#answertable_'+languages[x]+'_'+scale_id+' .btnaddanswer').click(addinput);
$('#answertable_'+languages[x]+'_'+scale_id+' .btndelanswer').click(deleteinput);
}
$('#quickadd').dialog('close');
$('#quickaddarea').val('');
$('.answertable tbody').sortable('refresh');
updaterowproperties();
}
function getlabel()
{
var answer_table = $(this).parent().children().eq(0);
scale_id=removechars($(this).attr('id'));
$('#saveaslabel').dialog('open');
updaterowproperties();
}
function setlabel()
{
switch($(this).attr('id'))
{
case 'newlabel':
if(!flag[0]){
$('#lasets').parent().remove();
$(this).parent().after('