/*
- ===============================================================
- FileUploadWizard.js
- Script for uploading files through a dynamic questionnaire.
- This is the code to accompany .
- ===============================================================
- /  
var fuwTesting = false;
var fuwDefaultTextboxLength = 60;
var fuwDefaultTextarea;
var fuwDefaultTextareaLines = 3;  
// ================================================================
// Constructor function of global fuw (= File Upload Wizard) object
// ================================================================
function fuwGlobal {  
// Loading the accompanying .css
mw.loader.load( mw.config.get('wgServer') + mw.config.get('wgScriptPath') +
'/index.php?title=MediaWiki:FileUploadWizard.css&action=raw&ctype=text/css',
'text/css' );  
// see if user is logged in, autoconfirmed, experienced etc.
this.getUserStatus;  
fuwSetVisible('warningLoggedOut', (this.userStatus == 'anon'));
fuwSetVisible('warningNotConfirmed', (this.userStatus == 'notAutoconfirmed'));
if ((this.userStatus == 'anon') || (this.userStatus == 'notAutoconfirmed')) {
return;
}
fuwSetVisible('fuwStartScriptLink', false);  
// create the form element to wrap the main ScriptForm area
// containing input elements of Step2 and Step3
var frm = fuwGet('fuwScriptForm');
if (! frm) {
frm = document.createElement('form');
frm.id = "fuwScriptForm";
var area = fuwGet('placeholderScriptForm');
var parent = area.parentNode;
parent.insertBefore(frm, area);
parent.removeChild(area);
frm.appendChild(area);
}
this.ScriptForm = frm;  
// create the TargetForm element that contains the filename
// input box, together with hidden input controls.
// This is the form that is actually submitted to the api.php.
frm = fuwGet('TargetForm');
if (! frm) {
frm = document.createElement('form');
frm.id = "TargetForm";
frm.method = "post";
frm.enctype = "multipart/form-data";
// "enctype" doesn't work properly on IE; need "encoding" instead:
frm.encoding = "multipart/form-data";
// we'll submit via api.php, not index.php, mainly because that
// allows us to use a proper edit summary different from the page content
frm.action = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';  
// However, since api.php sends back a response page that humans won't want to read,
// we'll have to channel that response away and discard it. We'll use a hidden iframe
// for that purpose.
// Unfortunately, it doesn't seem possible to submit file upload content through an
// Xmlhtml object via Ajax.  
frm.target = "TargetIFrame";
//testing:
//frm.target = "_blank";
var area = fuwGet('placeholderTargetForm');
var parent = area.parentNode;
parent.insertBefore(frm, area);
parent.removeChild(area);
frm.appendChild(area);
}
this.TargetForm = frm;  
// For the testing version, create a third form that will display
// the contents to be submitted, at the bottom of the page
if (fuwTesting) {
frm = fuwGet('fuwTestForm');
if (! frm) {
frm = document.createElement('form');
frm.id = "fuwTestForm";
var area = fuwGet('placeholderTestForm');
var parent = area.parentNode;
parent.insertBefore(frm, area);
parent.removeChild(area);
frm.appendChild(area);
}
this.TestForm = frm;
}  
// objects to hold cached results during validation and processing
this.opts = { };
this.warn = { };  
// create the input filename box
var filebox = document.createElement('input');
filebox.id = 'file';
filebox.name = 'file';
filebox.type = 'file';
filebox.size = fuwDefaultTextboxLength;
filebox.onchange = fuwValidateFile;
filebox.accept = 'image/png,image/jpeg,image/gif,image/svg+xml,image/tiff,image/x-xcf,application/pdf,image/vnd.djvu,audio/ogg,video/ogg,audio/rtp-midi';
fuwAppendInput('file', filebox);  
// create hidden controls for sending the remaining API parameters:
fuwMakeHiddenfield('action', 'upload', 'apiAction');
fuwMakeHiddenfield('format', 'xml', 'apiFormat');
fuwMakeHiddenfield('filename', '', 'apiFilename');
fuwMakeHiddenfield('text', '', 'apiText');
fuwMakeHiddenfield('comment', '', 'apiComment');
fuwMakeHiddenfield('token', mw.user.tokens.get('csrfToken'), 'apiToken');
fuwMakeHiddenfield('ignorewarnings', 1, 'apiIgnorewarnings');
fuwMakeHiddenfield('watch', 1, 'apiWatch');  
if (fuwTesting) {
fuwMakeHiddenfield('title', mw.config.get('wgPageName') + "/sandbox", 'SandboxTitle');
fuwMakeHiddenfield('token', mw.user.tokens.get('csrfToken'), 'SandboxToken');
fuwMakeHiddenfield('recreate', 1, 'SandboxRecreate');
}  
// create a hidden IFrame to send the api.php response to
var ifr = document.createElement('iframe');
ifr.id = "TargetIFrame";
ifr.name = "TargetIFrame";
//ifr.setAttribute('style', 'float:;width:;height:;');
ifr.style.display = "";
ifr.src = "";  
fuwAppendInput('TargetIFrame', ifr);  
if (fuwTesting) {  
// create the sandbox submit button
btn = document.createElement('input');
btn.id = 'SandboxButton';
btn.value = 'Sandbox';
btn.name = 'Sandbox';
btn.disabled = true;
btn.type = 'button';
btn.style.;
btn.onclick = fuwSubmitSandbox;
fuwAppendInput('SandboxButton', btn);  
}  
// create the real submit button
btn = document.createElement('input');
btn.id = "SubmitButton";
btn.value = "Upload";
btn.name = "Upload";
btn.disabled = true;
btn.type = "button";
btn.onclick = fuwSubmitUpload;
btn.style.;
fuwAppendInput('SubmitButton', btn);  
// create the Commons submit button
btn = document.createElement('input');
btn.id = "CommonsButton";
btn.value = "Upload on Commons";
btn.name = "Upload_on_Commons";
btn.disabled = true;
btn.type = "button";
btn.onclick = fuwSubmitCommons;
btn.style.;
fuwAppendInput('CommonsButton', btn);  
// create reset buttons
for (i = 1; i<=2; i++) {
btn = document.createElement('input');
btn.id = 'ResetButton' + i;
btn.value = "Reset form";
btn.name = "Reset form";
btn.type = "button";
btn.onclick = fuwReset;
btn.style.;
fuwAppendInput('ResetButton' + i, btn);
}  
// names of radio button fields
var optionRadioButtons = {
// top-level copyright status choice
'FreeOrNonFree' : ['OptionFree','OptionNonFree','OptionNoGood'],
// main subsections under OptionFree
'FreeOptions' : ['OptionOwnWork', 'OptionThirdParty', 'OptionFreeWebsite',
'OptionPDOld', 'OptionPDOther'],
// main subsections under OptionNonFree
'NonFreeOptions': ['OptionNFSubject','OptionNF3D','OptionNFExcerpt',
'OptionNFCover','OptionNFLogo','OptionNFPortrait',
'OptionNFMisc'],
// response options inside warningFileExists
'FileExistsOptions':
['NoOverwrite','OverwriteSame','OverwriteDifferent'],
// choice of evidence in OptionThirdParty subsection
'ThirdPartyEvidenceOptions' :
['ThirdPartyEvidenceOptionLink',
'ThirdPartyEvidenceOptionOTRS',
'ThirdPartyEvidenceOptionOTRSForthcoming',
'ThirdPartyEvidenceOptionNone'],
// choice of PD status in OptionPDOld subsection
'PDOldOptions' : ['PDUS1923','PDURAA','PDFormality','PDOldOther'],
// choice of PD status in OptionPDOther subsection
'PDOtherOptions': ['PDOtherUSGov','PDOtherOfficial','PDOtherSimple',
'PDOtherOther'],
// whether target article is wholly or only partly dedicated to discussing non-free work:
'NFSubjectCheck': ['NFSubjectCheckDedicated','NFSubjectCheckDiscussed'],
'NF3DCheck' : ['NF3DCheckDedicated','NF3DCheckDiscussed'],
// choice about copyright status of photograph in OptionNF3D
'NF3DOptions' : ['NF3DOptionFree','NF3DOptionSame']
};
for (var group in optionRadioButtons) {
var op = optionRadioButtons[group];
for (i=0; i<op.length; i++) {
fuwMakeRadiobutton(group, op[i]);
}
}
this.ScriptForm.NoOverwrite.checked = true;  
// input fields that trigger special
// onchange event handlers for validation:
fuwMakeTextfield('InputName', fuwValidateFilename);
fuwMakeTextfield('NFArticle', fuwValidateNFArticle);  
// names of input fields that trigger normal
// validation event handler
var activeTextfields = [
'Artist3D','Country3D',
'Date','OwnWorkCreation','OwnWorkPublication',
'Author','Source',
'Permission','ThirdPartyOtherLicense',
'ThirdPartyEvidenceLink','ThirdPartyOTRSTicket',
'FreeWebsiteOtherLicense',