var numforms = 0;
var wikEdAutoUpdateUrl;
var citeUserDateFormat;
var refTagURL;
var defaultRefTagURL = '//reftag.appspot.com/';
var refToolDebug;  
String.prototype.trim = function {
return this.replace(/^\s+|\s+$/g,"");
};  
function refbuttons {
if (mw.toolbar && document.getElementById('toolbar') ) {
var button = document.createElement('a');
button.href = "javascript:easyCiteMain";
button.title = "Insert Citation";
button.className = "mw-toolbar-editbutton";
var buttonimage = document.createElement('img');
buttonimage.src = "//upload.wikimedia.org/wikipedia/commons/e/ea/Button_easy_cite.png";
buttonimage.alt = "Insert Citation";
button.appendChild(buttonimage);
var toolbar = document.getElementById('toolbar');
if (navigator.userAgent.indexOf('MSIE') == -1) {
toolbar.appendChild(button);
var citemain = document.createElement('div');
citemain.style.display = '';
citemain.style.margin = '0 0 ';
citemain.style.clear = 'both';
citemain.style.;
citemain.setAttribute('Id', 'citeselect');
citemain.appendChild( addOption("citeWeb", "Web") );
citemain.appendChild( addOption("citeNews", "News") );
citemain.appendChild( addOption("citeBook", "Book") );
citemain.appendChild( addOption("citeJournal", "Journal") );
citemain.appendChild( addOption("citeNamedRef", "Named references") );
citemain.appendChild( addOption("dispErrors", "Error check") );
citemain.appendChild( addOption("showMore", "More") );
citemain.appendChild( addOption("hideInitial", "Cancel") );  
var citemore = document.createElement('div');
citemore.style.display = '';
citemore.setAttribute('Id', 'citemore');
citemore.appendChild( addOption("citeEncyclopedia", "Encyclopedia") );
citemore.appendChild( addOption("citePressRelease", "Press release") );
citemore.appendChild( addOption("citeMap", "Map") );
citemore.appendChild( addOption("showRefSectionOptions", "Ref Section") );
citemain.appendChild(citemore);  
$( citemain ).insertAfter( $( toolbar ) );
}
else {
toolbar.appendChild(button);
var citeselect = document.createElement('div');
citeselect.id = 'citeselect';
citeselect.style.display = "";
citeselect.innerHTML = '<input type="button" value="Web" onclick="citeWeb" />'+
'<input type="button" value="News" onclick="citeNews" />'+
'<input type="button" value="Book" onclick="citeBook" />'+
'<input type="button" value="Journal" onclick="citeJournal" />'+
'<input type="button" value="Named references" onclick="citeNamedRef" />'+
'<input type="button" value="Error check" onclick="dispErrors" />'+
'<input type="button" value="More" onclick="showMore" />'+
'<input type="button" value="Cancel" onclick="hideInitial" />'+
'<div id="citemore">\
<input type="button" value="Encyclopedia" onclick="citeEncyclopedia" />\
<input type="button" value="Press release" onclick="citePressRelease" />\
<input type="button" value="Map" onclick="citeMap" />\
<input type="button" value="Ref Section" onclick="showRefSectionOptions" />\
</div>';
document.getElementById('editform').insertBefore(citeselect, document.getElementById('editform').firstChild ) ;
}
var reftoolformarea = document.createElement( 'span' );
reftoolformarea.id = 'refToolFormArea';
document.getElementById('citeselect').appendChild( reftoolformarea );
}
if (typeof EditTools == "object") {
var placeholder = document.getElementById("editpage-specialchars");
EditTools.createEditTools (placeholder);
}
}  
function addOption(script, text) {
var option = document.createElement('input');
option.setAttribute('type', 'button');
option.setAttribute('onclick', script);
option.setAttribute("value", text);
return option;
}  
function hideInitial {
document.getElementById('citeselect').style.display = '';
oldFormHide;
}  
function oldFormHide {
//if (numforms != 0) {
// document.getElementById('citediv'+numforms).style.display = '';
//}
document.getElementById('refToolFormArea').innerHTML = '';
if (document.getElementById('errorform') !== null) {
document.getElementById('citeselect').removeChild(document.getElementById('errorform'));
}
}  
function easyCiteMain {
document.getElementById('citeselect').style.display = '';
document.getElementById('citemore').style.display = '';
}  
function showMore {
document.getElementById('citemore').style.display = '';
}  
var months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
var citeGlobalDateFormat = "<date> <monthname> <year>";
function getTime {
var datestr = '';
if (citeUserDateFormat) {
datestr = citeUserDateFormat;
} else {
datestr = citeGlobalDateFormat;
}
var DT = new Date;
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;
datestr = datestr.replace('<date>', date);
datestr = datestr.replace('<month>', month);
datestr = datestr.replace('<zdate>', zdate);
datestr = datestr.replace('<zmonth>', zmonth);
datestr = datestr.replace('<monthname>', months[DT.getUTCMonth]);
datestr = datestr.replace('<year>', DT.getUTCFullYear.toString);
return (datestr);
}  
function lastNameToRefname {
//Note: This only works if field 'refname' comes after field 'last', but it always does
var lastName;
var fields = document.getElementById('citediv'+numforms).getElementsByTagName('input');
for (var i=0; i<fields.length; i++) {
if(fields[i].id == 'last') {
lastName = fields[i].value;
}
else if(fields[i].id == 'refname') {
fields[i].value = lastName.replace(/^\s*(.*?)\s*$/, "$1");
return;
}
}
}  
function setAccessDateToday {
document.getElementById('accessdate').value = getTime;
/* var fields = document.getElementById('citediv'+numforms).getElementsByTagName('input');
for (var i=0; i<fields.length; i++) {
if(fields[i].id == 'accessdate') {
fields[i].value = getTime;
return;
}
}
- /
}  
function getLastName(authornum) {
if (/\S/.test(document.getElementById('last'+authornum).value)) {
return document.getElementById('last'+authornum).value;
}
else {
var author = document.getElementById('author'+authornum).value;
var match = /(\S+)\s*$/.exec(author);
if (match) {
return match[1];
}
else {
return '';
}
}
}  
function makeRefname {
var refname;
if (document.getElementById('last1')) {
refname = document.getElementById('last1').value + document.getElementById('last2').value;
}
else{
refname = document.getElementById('last').value;
}
if (/\S/.test(refname)) {
var date = document.getElementById('date').value;
var match = /[0-9]{4}/.exec(date);
if (match) {
refname += match[0];
}
}
else {
refname = document.getElementById('title').value;
}
document.getElementById('refname').value = refname;
}  
function citeWeb {
citeNewsWeb("cite web");
}
function citeNews {
citeNewsWeb("cite news");
}  
function citeNewsWeb(templatename) {
oldFormHide;
var template = templatename;
var legend;
if (template == "cite web") {
legend = "Cite web source";
} else {
legend = "Cite news source";
}
var newtime = getTime;
numforms++;
var form = '<div id="citediv'+numforms+'">'+
'<fieldset><legend>'+legend+'</legend>'+
'<table cellspacing="5">'+
'<input type="hidden" value="'+template+'" id="template">'+
'<tr><td><label for="url">&nbsp;URL: </label></td>'+
'<td><input type="image" src="//upload.wikimedia.org/wikipedia/commons/6/62/Arrow_out_condensed.png" tabindex=1 alt="Fetch" title="' + ((template == 'cite news') ? 'For a New York Times article URL, fetch article data and fill in the fields. For other URLs, fetch the page title.' : 'Fetch the page title. Irrelevant parts often have to be removed manually afterwards.') + '" onClick="pullURL; return false;"><span><input type="text" tabindex=1 id="url"></span></td>'+
'<td><label for="title">&nbsp;Title: </label></td>'+
'<td><input type="text" tabindex=1 id="title"></td></tr>'+
'<tr><td><label for="last">&nbsp;Last name: </label></td>'+
'<td><input type="text" tabindex=1 id="last"></td>'+