}
};  
// FORM DIALOG FUNCTIONS
// Add new incrementable fields
CiteTB.incrementFields = function {
template = CiteTB.getOpenTemplate;
var currentrow = $(this).parents('tr')[0];
$(this).prev.css('width', '100%')
$(this).detach;
var elemid = $(this).attr('id');
var res = /^cite\-incr\-(.*?)\-(.*)$/.exec(elemid);
group = res[2];
increments = template.incrementables[group];
fields = increments.fields;
incrval = increments.val+1;
template.incrementables[group].val += 1;
trs = template.makeFormInner(fields, false);
trs.reverse;
for (var i=0; i<trs.length; i++) {
$(currentrow).after(trs[i]);
}
};  
// fill the accessdate param with the current date
CiteTB.fillAccessdate = function {
var elemid = $(this).attr('id');
var res = /^cite\-date\-(.*?)\-(.*)$/.exec(elemid);
var id = res[1];
var field = res[2];
var DT = new Date;
var datestr = CiteTB.formatDate(DT);
$('#cite-'+id+'-'+field).val(datestr);
return false;
};  
CiteTB.formatDate = function(DT, useday, usemonth) {
if (typeof useday == "undefined") {
useday = true;
}
if (typeof usemonth == "undefined") {
usemonth = true;
}
var datestr = CiteTB.getOption('date format');
var zmonth = '';
var month = DT.getUTCMonth+1;
if (month < 10) {
zmonth = "0"+month.toString;
} else {
zmonth = month.toString;
}
month = month.toString;
var zdate = '';
var date = DT.getUTCDate;
if (date < 10) {
zdate = "0"+date.toString;
} else {
zdate = date.toString;
}
date = date.toString;
if (useday) {
datestr = datestr.replace('<date>', date);
datestr = datestr.replace('<zdate>', zdate);
} else {
datestr = datestr.replace('<date>', '');
datestr = datestr.replace('<zdate>', '');
}
if (usemonth) {
datestr = datestr.replace('<month>', month);
datestr = datestr.replace('<zmonth>', zmonth);
datestr = datestr.replace('<monthname>', CiteTB.getOption('months')[DT.getUTCMonth]);
} else {
datestr = datestr.replace('<month>', '');
datestr = datestr.replace('<zmonth>', '');
datestr = datestr.replace('<monthname>', '');
}
datestr = datestr.replace('<year>', DT.getUTCFullYear.toString);
return datestr.replace(/^[ \/\-\,\.]*(.*?)[ \/\-\,\.]*$/g, "$1"); // Cleanup any dangling spaces or connectors that might result from omitting date/month
};  
// Function called after the ref list is loaded, to actually set the contents of the named ref dialog
// Until the list is loaded, its just a "Loading" placeholder
CiteTB.setupNamedRefs = function {
var names = [], i;
for( i=0; i<CiteTB.mainRefList.length; i++) {
if (!CiteTB.mainRefList[i].shorttag && CiteTB.mainRefList[i].refname) {
names.push(CiteTB.mainRefList[i]);
}
}
var stuff = $('<div>');
$('#citetoolbar-namedrefs').html( stuff );
if (names.length === 0) {
stuff.html(mw.usability.getMsg('cite-no-namedrefs'));
} else {
stuff.html(mw.usability.getMsg('cite-namedrefs-intro'));
var select = $('<select id="cite-namedref-select">');
select.append($('<option value="" />').text(mw.usability.getMsg('cite-named-refs-dropdown')));
for(i=0; i<names.length; i++) {
select.append($('<option />').text(names[i].refname));
}
stuff.after(select);
select.before('
');
var prevlabel = $('<div id="cite-nref-preview-label" />').html(mw.usability.getMsg('cite-raw-preview'));
select.after(prevlabel);
prevlabel.before("  
");
prevlabel.after('<div id="cite-namedref-preview" />');
var parselabel = $('<span id="cite-parsed-label" />').html(mw.usability.getMsg('cite-parsed-label'));
$('#cite-namedref-preview').after(parselabel);
parselabel.after('<div id="cite-namedref-parsed" />');
var link = $('<a href="#" id="cite-nref-parse" />');
link.html(mw.usability.getMsg('cite-form-parse'));
$('#cite-namedref-parsed').after(link);  
$("#cite-namedref-select").on( 'change', CiteTB.namedRefSelectClick);
$('#cite-nref-parse').on( 'click', CiteTB.nrefParseClick);
}
};  
// Function to get the errorcheck form HTML
CiteTB.setupErrorCheck = function {
var form = $('<div id="cite-errorcheck-heading" />').html(mw.usability.getMsg('cite-errorcheck-heading'));
var ul = $("<ul id='cite-errcheck-list' />");
var test;
for (var t in CiteTB.ErrorChecks) {
test = CiteTB.ErrorChecks[t];
ul.append(test.getRow);
}
form.append(ul);
$('#citetoolbar-errorcheck').html(form);
};  
// Callback function for parsed preview
CiteTB.fillNrefPreview = function(parsed) {
$('#cite-parsed-label').show;
$('#cite-namedref-parsed').html(parsed);
};  
// Click handler for the named-ref parsed preview
CiteTB.nrefParseClick = function {
var choice = $("#cite-namedref-select").val;
if (choice === '') {
$('#cite-parsed-label').hide;
$('#cite-namedref-parsed').text('');
return false;
}
$('#cite-nref-parse').hide;
for( var i=0; i<CiteTB.mainRefList.length; i++) {
if (!CiteTB.mainRefList[i].shorttag && CiteTB.mainRefList[i].refname == choice) {
CiteTB.parse(CiteTB.mainRefList[i].content, CiteTB.fillNrefPreview);
return false;
}
}
};  
// Click handler for the named-ref dropdown
CiteTB.lastnamedrefchoice = '';
CiteTB.namedRefSelectClick = function {
var choice = $("#cite-namedref-select").val;
if (CiteTB.lastnamedrefchoice == choice) {
return;
}
CiteTB.lastnamedrefchoice = choice;
$('#cite-parsed-label').hide;
$('#cite-namedref-parsed').text('');
if (choice === '') {
$('#cite-nref-preview-label').hide;
$('#cite-namedref-preview').text('');
$('#cite-nref-parse').hide;
return;
}
for( var i=0; i<CiteTB.mainRefList.length; i++) {
if (!CiteTB.mainRefList[i].shorttag && CiteTB.mainRefList[i].refname == choice) {
$('#cite-nref-preview-label').show;
$('#cite-namedref-preview').text(CiteTB.mainRefList[i].content);
if (CiteTB.getOption('autoparse')) {
CiteTB.nrefParseClick;
} else {
$('#cite-nref-parse').show;
}
}
}
};  
// callback function for parsed preview
CiteTB.fillTemplatePreview = function(text) {
var template = CiteTB.getOpenTemplate;
var div = $("#citetoolbar-"+CiteTB.escStr(template.shortform));
div.find('.cite-prev-parsed-label').show;
div.find('.cite-preview-parsed').html(text);
};  
// Click handler for template parsed preview
CiteTB.prevParseClick = function {
var ref = CiteTB.getRef(true, false);
var template = CiteTB.getOpenTemplate;
var div = $("#citetoolbar-"+CiteTB.escStr(template.shortform));
div.find('.cite-prev-parse').hide;
CiteTB.parse(ref, CiteTB.fillTemplatePreview);
};  
// Show/hide the extra fields in the dialog box
CiteTB.showHideExtra = function {
var template = CiteTB.getOpenTemplate;
var div = $("#citetoolbar-"+CiteTB.escStr(template.shortform));
var setting = div.find(".cite-form-status").val;
if ( setting === 'closed' ) {
div.find(".cite-form-status").val('open');
div.find('.cite-extra-fields').show(1, function {
// jQuery adds "display:block", which screws things up
div.find('.cite-extra-fields').attr('style', 'width:100%; background-color:transparent;');
});
} else {
div.find(".cite-form-status").val('closed');
div.find('.cite-extra-fields').hide;
}
};  
// Resets form fields and previews
// Resets form fields and previews
CiteTB.resetForm = function {
var template = CiteTB.getOpenTemplate;
var div = $("#citetoolbar-"+CiteTB.escStr(template.shortform));
div.html(template.getForm);
};  
// STRING UTILITY FUNCTIONS
// Returns a string quoted as necessary for name/group attributes
CiteTB.getQuotedString = function(s) {
var sq = /\'/.test(s); // single quotes
var dq = /\"/.test(s); // double quotes
if (!sq && !dq) { // Always quotes for non-latin alphabet
return '"'+s+'"';
} else if (!dq) { // Can use double quotes
return '"'+s+'"';
} else if (!sq) { // Can use single quotes
return "'"+s+"'";
} else { // Has double and single quotes
s = s.replace(/\"/g, '\'');
return '"'+s+'"';
}
};  
// Fix up strings for output - capitalize first char, replace underscores with spaces
CiteTB.fixStr = function(s) {
s = s.slice(0,1).toUpperCase + s.slice(1);
s = s.replace('_',' ');
return s;
};  
// Escape spaces and quotes for use in HTML classes/ids
CiteTB.escStr = function(s) {
return s.replace(' ', '-').replace("'", "\'").replace('"', '\"');
};  
// MISC FUNCTIONS
// Determine which template form is open, and get the template object for it