/* global mw, $, CiteTB, citeTemplate, citeErrorCheck */

if ( window.CiteTB === undefined ) {
 window.CiteTB = {
 "Templates" : {}, // All templates
 "Options" : {}, // Global options
 "UserOptions" : {}, // User options
 "DefaultOptions" : {}, // Script defaults
 "ErrorChecks" : {} // Error check functions

if (typeof mw.usability == 'undefined') {
 mw.usability = {};
 mw.usability.getMsg = function(m) { return mw.messages.get(m); }
 mw.usability.addMessages = function(msgs) { mw.messages.set(msgs); };

// Object for cite templates
window.citeTemplate = function  {
 // Properties
 this.templatename = templatename; // The template name - "cite web", "cite book", etc.
 this.shortform = shortform; // A short form, used for the dropdown box
 this.basic = basicfields; // Basic fields - author, title, publisher...
 // Less common - quote, archiveurl - should be everything the template supports minus the basic ones
 this.extra = expandedfields;

 this.incrementables = {};

 // Add it to the list
 CiteTB.Templates[this.templatename] = this;
 // Methods
 this.makeFormInner = function {
 var i=0;
 var trs = [];
 var autofills = []
 for (i=0; i<fields.length; i++) {
 var fieldobj = fields[i];
 var field = labelfield = fieldobj.field;
 var ad = false;
 if (incrsetup && fieldobj.increment_group) {
 		field = fieldobj.field.replace;
 		labelfield = fieldobj.field.replace;
 		if (this.incrementables[fieldobj.increment_group] && !this.incrementables[fieldobj.increment_group].setup) {
 			// The object has been created, but not fully initialized
 			this.incrementables[fieldobj.increment_group]['fields'].push(fieldobj)
 			// Object not yet created
 			this.incrementables[fieldobj.increment_group] = {
 				'fields': [fieldobj],
 				'val':1,
 				'setup': false
 			// Fully initialized from a previous invocation of this function, just resetting the number
 			this.incrementables[fieldobj.increment_group].val = 1
 	// Adding a new row
 	incrval = this.incrementables[fieldobj.increment_group].val;
 	field = fieldobj.field.replace;
 	labelfield = fieldobj.field.replace;
 if ($.inArray(field, CiteTB.getOption('autodate fields')) != -1 ) {
 im = $('').attr;
 im.attr('alt', mw.usability.getMsg('cite-insert-date')).attr('title', mw.usability.getMsg('cite-insert-date'));
 var ad = $('').attr;
 ad.append(im);
 ad.attr('id', 'cite-date-'+CiteTB.escStr(this.shortform)+'-'+field);
 $( document ).on( 'click', '#cite-date-'+CiteTB.escStr(this.shortform)+'-'+field, CiteTB.fillAccessdate );
	 
 if (fieldobj.autofillid) {
 var autotype = fieldobj.autofillid;
 im = $('').attr;
 im.attr('alt', mw.usability.getMsg('cite-autofill-alt')).attr('title', mw.usability.getMsg('cite-autofill-alt'));
 var ad = $('').attr;
 ad.append(im);
 ad.attr('id', 'cite-auto-'+CiteTB.escStr(this.shortform)+'-'+field+'-'+autotype);
 autofills.push('#cite-auto-'+CiteTB.escStr(this.shortform)+'-'+field+'-'+autotype);
 
 if (fieldobj.increment_button) {
 var incrtype = fieldobj.increment_group;
 im = $('').attr;
 im.attr('alt', mw.usability.getMsg('cite-increment-alt')).attr('title', mw.usability.getMsg('cite-increment-alt'));
 var ad = $('').attr;
 ad.append(im);
 ad.attr('id', 'cite-incr-'+CiteTB.escStr(this.shortform)+'-'+incrtype);
	 
 var display = mw.usability.getMsg('cite-'+labelfield+'-label');
 if (typeof display !== 'string') {
 	if (fieldobj.label) {
 		display = CiteTB.fixStr(fieldobj.label);
 		display = CiteTB.fixStr(labelfield)
 var tooltip = fieldobj.tooltip ? $('') : false;
 
 var input = '';
 if (ad) {
 input = $('');
 input = $('');
 input.attr('id', 'cite-'+CiteTB.escStr(this.shortform)+'-'+field);
	 if (fieldobj.autofillprop) {
	 	classname = 'cite-'+CiteTB.escStr(this.shortform)+'-'+fieldobj.autofillprop
	 if (fieldobj.increment_group) {
	 	input.addClass('cite-'+CiteTB.escStr(this.shortform)+'-incr-'+fieldobj.increment_group);
	 	classname+='-'+this.incrementables[fieldobj.increment_group].val.toString;
	 input.addClass(classname);
 var label = $('');
 label.attr('for', 'cite-'+CiteTB.escStr(this.shortform)+'-'+field).text(display);
 if (tooltip) {
 label.append(tooltip);
 var style = 'text-align:right; width:20%;';
 if (i%2 == 1) {
 style += ' padding-left:1em;';
 var tr = $('');
 var td1 = $('').attr;
 td1.append(label);
 tr.append(td1);
 var td2 = $('');
 td2.append(input);
 if (ad) {
 td2.append(ad);
 tr.append(td2);
 if (i%2 == 0) {
 trs.push(tr);
 var needsetup = false;
 for (g in this.incrementables) {
 	if (!this.incrementables[g].setup) {
 		needsetup = true;
 		$( document ).on( 'click', '#cite-incr-'+CiteTB.escStr(this.shortform)+'-'+g, CiteTB.incrementFields );
 	this.incrementables[g].setup = true;
 if (needsetup) {
 	for(i=0; i<autofills.length; i++) {
 		$( document ).on;
 return trs;
 
 
 // gives a little bit of HTML so the open form can be identified
 this.getInitial = function {
 var hidden = $('');
 hidden.val(this.templatename);
 return hidden;
 
 // makes the form used in the dialog boxes
 this.getForm = function {
 var main = $("");
 var form1 = $('');
 var i=0;
 var trs = this.makeFormInner;
 for (var i=0; i<trs.length; i++) {
 form1.append(trs[i]);
 
 var form2 = $('');
 trs = this.makeFormInner;
 for (var i=0; i<trs.length; i++) {
 form2.append(trs[i]);
 main.append(form1).append(form2);
 
 var form3 = $('');
 var tr = $('');
 var td1 = $('');
 var label1 = $('');
 label1.attr('for', "cite-"+CiteTB.escStr(this.shortform)+'-name').text(mw.usability.getMsg('cite-name-label'));
 td1.append(label1);
 var td2 = $('');
 var input1 = $('');
 input1.attr('id', 'cite-'+CiteTB.escStr(this.shortform)+'-name');
 td2.append(input1);
 var td3 = $('');
 var label2 = $('');
 label2.attr('for', 'cite-'+CiteTB.escStr(this.shortform)+'-group').text(mw.usability.getMsg('cite-group-label'));
 td3.append(label2);
 var td4 = $('');
 var input2 = $('');
 input2.attr('id', 'cite-'+CiteTB.escStr(this.shortform)+'-group');
 td4.append(input2);
 tr.append(td1).append(td2).append(td3).append(td4);
 form3.append(tr);
 main.append(form3);
 var extras = $('');
 extras.append('');
 var hidden = $('');
 hidden.val(this.templatename);
 extras.append(hidden);
 var span1 = $('');
 span1.text(mw.usability.getMsg('cite-raw-preview'));
 extras.append(span1).append('');
 var span2 = $('');
 span2.text(mw.usability.getMsg('cite-parsed-label'));
 extras.append(span2).append('');
 var link = $('');
 link.text(mw.usability.getMsg('cite-form-parse'));
 extras.append(link); 
 main.append(extras);
 
 return main;

/* Class for error checks
 FIXME: DOCS OUT OF DATE
 type - type of error check - current options:
 The strings returned by the function should be valid HTML
 
 func - The function described above
 testname - Name of the error check, must not contain spaces
 desc - A short description of the test

window.citeErrorCheck = function (obj) {
 this.obj = obj
 CiteTB.ErrorChecks[this.obj.testname] = this;
 
 this.run = function {
 var errors = [];
 switch(this.obj['type']) {
 case "refcheck":
 CiteTB.loadRefs;
 for(var i=0; i<CiteTB.mainRefList.length; i++) {
 var e = this.obj.func(CiteTB.mainRefList[i]);
 if (e) {
 errors.push(e);
 break;
 case "reflist":
 CiteTB.loadRefs;
 errors = this.obj.func(CiteTB.mainRefList);
 break;
 case "search":
 var func = this.obj.func
 CiteTB.getPageText(function(text) {
 errors = func(text);
 break;
 return errors;
 
 this.getRow = function {
 var row = $("");
 var check = $("");
 check.attr;
 var label = $("").html(mw.usability.getMsg(this.obj.desc));
 label.attr; 
 row.append(check).append(' &ndash; ').append(label);
 return row;