var pattern = /^(img|image|dsc|picture|pic)?(\\s*|\\_*|[a-z]{,3})?\\d+$/i;
var auto = name.match(pattern);  
window.fuw.warn.BadFilename = (tooShort || auto);
return !tooShort && !auto;
}  
// =======================================================
// check if file extensions match between local filename
// and target filename input box. Automatically append
// appropriate extension to target filename if they don't.
// =======================================================
function fuwCheckFileExtension {
var nameBox = window.fuw.ScriptForm.InputName;
var name = $.trim(nameBox.value);
var fileBox = window.fuw.TargetForm.file;
var file = fileBox.value;  
// cancel check if no filename has been provided yet
if (!file || !name) return true;  
var extensions = /.+\.(png|gif|jpg|jpeg|xcf|pdf|mid|ogg|ogv|svg|djvu|tiff|tif|oga)$/i;
var mimetypes = {
"png" : "image/png",
"gif" : "image/gif",
"jpg" : "image/jpeg",
"jpeg" : "image/jpeg",
"xcf" : "image/x-xcf",
"pdf" : "application/pdf",
"mid" : "audio/rtp-midi",
"ogg" : "audio/ogg",
"ogv" : "video/ogg",
"svg" : "image/svg+xml",
"djvu" : "image/vnd.djvu",
"tiff" : "image/tiff",
"tif" : "image/tiff",
"oga" : "video/ogg"
};  
var found = extensions.exec(file);
var fileExt = found ? found[1].toLowerCase : "";
found = extensions.exec(name);
var nameExt = found ? found[1].toLowerCase : "";
var mime = mimetypes[fileExt];  
if (fileExt && mime && (mimetypes[nameExt] != mime)) {
nameBox.value = name.replace(/\.?$/, ('.' + fileExt));
}
return true;
}  
// ============================================================
// Check if a file under the chosen name already exists,
// either locally or on Commons.
// Store results in the fuw.warn object, so warnings will
// be displayed on the next fuwUpdateOptions call
// ============================================================
function fuwCheckFileExists {
// this is an asynchronous AJAX function.
// results won't yet be present when this function returns.  
var nameBox = window.fuw.ScriptForm.InputName;
var name = $.trim(nameBox.value);  
// using the jQuery wrapper for the Ajax functionality:
$.ajax({
url : mw.util.wikiScript( 'api' ),
type : 'GET',
dataType: 'xml',
traditional : true,
data: {
format: 'xml',
action: 'query',
titles: 'File:' + name,
prop : 'imageinfo',
iiprop: 'url|user',
iiurlwidth: 120
},
success: function(resp) {
// callback function, called when API query has succeeded:
// see if the request has returned info from an existing image:
var foundlist = resp.getElementsByTagName('ii');
var exists = (foundlist.length >= 1);
var isCommons = false;
if (exists) {  
// extract description data from http response.
// see https://www.mediawiki.org/wiki/API:Properties#imageinfo_.2F_ii
// for structure of API response
var foundImg = foundlist[0];
isCommons = (foundImg.parentNode.parentNode.getAttribute('imagerepository')=='shared');  
// need this data for creating our own image link
var width = foundImg.getAttribute('thumbwidth');
var height = foundImg.getAttribute('thumbheight');
var thumbURL = foundImg.getAttribute('thumburl');
var lastUser = foundImg.getAttribute('user');
var descURL = foundImg.getAttribute('descriptionurl');  
// API returns link to local description page even for Commons images.
// However, we want a direct link to Commons.
if (isCommons) {
descURL = descURL.replace(/en\.wikipedia\.org/, "commons.wikimedia.org");
descURL = descURL.replace(/\/\/secure\.wikimedia\.org\/wikipedia\/en/, "commons.wikimedia.org");
}  
// build the image info into the warning section of our page:
thumbDiv = fuwGet('warningImageThumb');
if (thumbDiv) {  
// make all links point to description page:
var thumbA = thumbDiv.getElementsByTagName('a');
for (i = 0; i<thumbA.length; i++) {
thumbA[i].setAttribute('href', descURL);
}
// insert the image itself:
var thumbImg = thumbDiv.getElementsByTagName('img');
if (thumbImg.length > 0) {
thumbImg = thumbImg[0];
thumbImg.setAttribute('src', thumbURL);
thumbImg.setAttribute('width', width);
thumbImg.setAttribute('height', height);
}
// insert the name of the last uploader:
var thumbSpan = fuwGet('existingImageUploader');
// TO DO: turn this into a proper link
if (thumbSpan) thumbSpan.innerHTML = lastUser;
}  
}
warn = window.fuw.warn;
warn.ImageOnCommons = exists && isCommons;
warn.ImageExists = exists && !isCommons;  
fuwUpdateOptions;
}
});
}  
// ===========================================================
// onchange event handler for the local filename box
// ===========================================================
fuwValidateFile = function {
fuwCheckFileExtension;
fuwUpdateOptions;
}  
// ===========================================================
// onchange event handler for the name input box
// ===========================================================
fuwValidateFilename = function {
fuwCleanFilename;
if (
fuwCheckLegalFilename &&
fuwCheckPoorFilename &&
fuwCheckFileExtension) {
// after fuwCheckFileExists,
// fuwUpdateOptions will be triggered
// by the callback function after Ajax completion
fuwCheckFileExists;
}
else {
// if there's been no Ajax call.
fuwUpdateOptions;
}
};  
// ==========================================================
// function fuwValidateNFArticle
// ==========================================================
// This is the validation routine for the obligatory
// article-to-be-used-in field for non-free files. It queries
// api.php about the target article through an Ajax call.
// It will store error info in the fuw.warn object,
// triggering the following error on the next updateOptions:
// * warningNFArticleNotFound : target page doesn't exist.
// * warningNFArticleNotMainspace : target is not an article.
// * warningNFArticleDab : target is a disambiguation page.
// Redirects will automatically be substituted.
// ==========================================================
fuwValidateNFArticle = function {  
var nameBox = window.fuw.ScriptForm.NFArticle;
oldname = name = nameBox.value;  
// cleanup article name:
// automatically fix accidentally added ... and
// regularize underscores
name = $.trim(name);
name = name.replace(/(^\[\[)|(\]\]$)/g, "");
// automatically fix article names entered as full urls:
name = name.replace(/^https?:\/\/en\.wikipedia\.org\/wiki\//, "");
name = name.replace(/^https?:\/\/en\.wikipedia\.org\/w\/index\.php\?title=/, "");
name = name.replace(/_/g, " ");
if (name != oldname) nameBox.value = name;  
// do nothing more if field was blank
if (!name) return;  
// using the jQuery wrapper for the Ajax functionality:
$.ajax({
url : mw.util.wikiScript( 'api' ),
type : 'GET',
dataType: 'xml',
traditional : true,
data: {
format: 'xml',
action: 'query',
titles: name,
prop : 'info|categories|links'
},
success: function(resp) {
// callback function, called when API query has succeeded:
var errorType = 0;
var pg = resp.getElementsByTagName('page')[0];
var title = pg.getAttribute('title');
var target = title;
if (pg.getAttribute('missing') != null) {
// no page found under this title.
errorType = 1;
}
else {
var userspace = false;
var ns = pg.getAttribute('ns');
var rd = pg.getAttribute('redirect');
if (ns != 0) {
// not a mainspace page!
errorType = 2;  
// try to detect if the target might be a user space draft:
if (title.match(new RegExp("User( talk)?:" + mw.config.get('wgUserName')))) {
userspace = true;
}
}
else if (rd != null) {
// redirect page
// API returns an empty redirect="" attribute if
// the page is a redirect
var targets = pg.getElementsByTagName('pl');
for (i=0; i<targets.length; i++) {
var link = targets[i];
if (link.getAttribute('ns')==0) {
target = link.getAttribute('title');
errorType = 3;
break;
}
}
}
else {
// check for disambiguation categories
var cats = pg.getElementsByTagName('cl');
for (i=0; i<cats.length; i++) {
var cat = cats[i];
if (cat.getAttribute('title') == "Category:All disambiguation pages") {
errorType = 4;
break;
}
}
}
}
warn = window.fuw.warn;
warn.NFArticleNotFound = (errorType==1);
warn.NFArticleNotMainspace = (errorType==2);