Jump to content

User:CenPop/CenPop.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**<nowiki>
 * Note that this script will only run on the 'Project:CenPop/Script' page.
 * This script is based on the AWB script by Joeytje50 which in turn is based on the downloadable AutoWikiBrowser.
 * 
 * @licence
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 * @version 1.1
 * @author Jacob Alperin-Sheriff, Joeytje50
 */
 
window.CenPop = {allowed:true,
	loadedScripts:{"i18n":false,"CenPopExternal":false,"cenparse":false,"CenPopLayout":false},
	done_mwapi:false
}; //The main global object for the script.

var state_map={"Alabama":"AL","Alaska":"AK","American Samoa":"AS", "Arizona":"AZ","Arkansas":"AR",
	       "California":"CA","Colorado":"CO","Connecticut":"CT","Delaware":"DE",
	       "District of Columbia": "DC", "Florida": "FL","Georgia": "GA", "Guam":"GU", "Hawaii": "HI",
	       "Idaho":"ID","Illinois":"IL","Indiana":"IN","Iowa":"IA","Kansas": "KS", "Kentucky": "KY",
	       "Louisiana": "LA", "Maine": "ME", "Maryland": "MD", "Massachusetts":"MA", "Michigan": "MI",
               "Minnesota": "MN", "Mississippi": "MS", "Missouri": "MO", "Montana": "MT", "Nebraska": "NE",
	       "Nevada": "NV", "New Hampshire": "NH", "New Jersey": "NJ", "New Mexico": "NM", "New York": "NY",
	       "North Carolina": "NC", "North Dakota": "ND", "Northern Mariana Islands":"MP", "Ohio": "OH",
               "Oklahoma": "OK", "Oregon": "OR", "Pennsylvania": "PA","Puerto Rico":"PR",
	       "Rhode Island": "RI", "South Carolina": "SC", "South Dakota": "SD","Tennessee": "TN",
	       "Texas": "TX", "Utah": "UT","Virgin Islands":"VI", "Vermont": "VT", "Virginia": "VA",
	       "Washington": "WA", "West Virginia": "WV","Wisconsin": "WI", "Wyoming": "WY",
	      };

var reverse_state_map={},reverse_province_map={};
for(let x in state_map)     reverse_state_map[state_map[x]]=x;

/***** Initial Loading Scripts Functions *****/
CenPop.scriptsDoneLoading=function() {
	var x;
	console.log("CenPop.loadedScripts="+JSON.stringify(CenPop.loadedScripts)+", CenPop.done_mwapi="+CenPop.done_mwapi);
	for(x in CenPop.loadedScripts) if(!CenPop.loadedScripts[x]) return false;
	return CenPop.done_mwapi;
};
CenPop.onloadscript=function(script,success,response,url) {
	var match=url.match(/User:[^\/]*\/([^\.]+)/);
	if(match && CenPop.loadedScripts[match[1]]!==undefined) CenPop.loadedScripts[match[1]]=true;
	if(CenPop.scriptsDoneLoading()) CenPop.init();
};

/***** User verification *****/

;(function() {
	var curr_script;
	if (mw.config.get('wgCanonicalNamespace')+':'+mw.config.get('wgTitle') !== 'Project:CenPop/Script' || CenPop.allowed === false || mw.config.get('wgUserName') === null) {
		CenPop.allowed = false;
		return;
	}
	mw.loader.load('//wiki.riteme.site/w/index.php?title=User:DemocraticLuntz/CenPop.css&action=raw&ctype=text/css',"text/css");
	mw.loader.load('mediawiki.action.history.diff');
 
 for(curr_script in CenPop.loadedScripts) {
 	let temp_url='//wiki.riteme.site/w/index.php?title=User:DemocraticLuntz/'+curr_script+'.js&action=raw&ctype=text/javascript';
 	console.log("temp_url="+temp_url);
 	$.getScript(temp_url, function () { 
 	CenPop.onloadscript(arguments[0],arguments[1],arguments[2],temp_url); });
 }
//	//RegEx Typo Fixing
	$.getScript('//wiki.riteme.site/w/index.php?title=User:Joeytje50/RETF.js&action=raw&ctype=text/javascript',
		function() { $('#refreshRETF').click(RETF.load); });
	CenPop.mw_Api=new mw.Api();
	(CenPop.mw_Api).get({action: 'query',titles: 'Project:AutoWikiBrowser/CheckPage',
		prop: 'revisions',meta: 'userinfo|siteinfo',rvprop: 'content',rvlimit: 1,
		uiprop: 'groups',siprop: 'namespaces',indexpageids: true,format: 'json',
	}).done(function(response) {
		if(response.error) {
			alert('API error: ' + response.error.info);
			CenPop = false; //preventing further access. No verification => no access.
			return;
		}
		var term_map={ns:response.query.namespaces,sysop:false,username:mw.config.get('wgUserName'),bot:true,allowed:true,file_list:[],
			upload_list:[],modified_pop:false,modified_info:false,added_popest_ref:false,added_pop:false,loaded_gazetteer:false,
			loaded_pop:false,add_count:0,most_recent_census:0,most_recent_census_pop:0,gotNewestContent:false,mod_count:0,
			mod_info_count:0,geo_info_count:0,in_get:false};
		for(let x in term_map) CenPop[x]=term_map[x];
		CenPop.done_mwapi=true;
		if(CenPop.scriptsDoneLoading()) CenPop.init();

		//if(CenPop.messages.en&&CenPop.loadedParse&&CenPop.loadedExternal)  CenPop.init();
	}).fail(function(xhr, error) {
		alert(CenPop.msg('verify-error') + '\n' + error);
		CenPop = false; //preventing further access. No verification => no access.
	}); 
})();
 
/***** Global object/variables *****/
 
var objs = ['page', 'api', 'convert', 'fn', 'pl', 'messages', 'setup', 'settings', 'ns'];
for (var i=0;i<objs.length;i++) {
	CenPop[objs[i]] = {};
}
CenPop.lang = mw.config.get('wgUserLanguage');
CenPop.isStopped = true;
CenPop.tooltip = window.tooltipAccessKeyPrefix || '';



 
/***** API functions *****/
 
//Main template for API calls
CenPop.api.call = function(data, callback, onerror) {
	data.format = 'json';
	if (data.action !== 'query') data.bot = true;
	$.ajax({
		data: data,
		dataType: 'json',
		url: mw.config.get('wgScriptPath') + '/api.php',
		type: 'POST',
		success: function(response) {

			if (response.error) {
				console.error("response.error.info=",response.error.info,", data=",data);
				alert('API error: ' + response.error.info);
				CenPop.stop();
			} else {
				callback(response);
			}
		},
		error: function(xhr, error) {
			alert('AJAX error: ' + error+", data="+JSON.stringify(data));
			CenPop.stop();
			if (onerror) onerror();
		}
	});
};

/* Get an edit token (new update to Mediawiki circa 2021), to use before submitting */
CenPop.api.token = function(callback) {
	CenPop.status('token');
	var data = {'action': 'query',
		'meta':'tokens' };
	CenPop.api.call(data, function(response) {
		if(response && response.query && response.query.tokens && response.query.tokens.csrftoken) {
			CenPop.page.edittoken=response.query.tokens.csrftoken;
			callback();
		}
		else {
			console.error("Error getting tokens, response=",response);
		}
	});
}

 
//Get page diff, and process it for more interactivity
CenPop.api.diff = function(callback) {
	CenPop.status('diff');
	var editBoxInput = $('#editBoxArea').val();
//	var redirects = $('input.redirects:checked').val()==='follow'?'redirects':'inprop';
	var data = {
		'action': 'query',
		'prop': 'info|revisions',
		'indexpageids': true,
		'titles': CenPop.page.name,
		'rvlimit': '1',
		'rvdifftotext': editBoxInput
	};
	data[redirects] = 'redirect';
	CenPop.api.call(data, function(response) {
		var pageExists = response.query.pageids[0] !== '-1';
		var diff;
		if (pageExists) {
			var diffpage = response.query.pages[response.query.pageids[0]];
			diff = diffpage.revisions[0].diff['*'];
			if (diff === '') {
				diff = '<h2>'+CenPop.msg('no-changes-made')+'</h2>';
			} else {
				diff = '<table class="diff">'+
					'<colgroup>'+
						'<col class="diff-marker">'+
						'<col class="diff-content">'+
						'<col class="diff-marker">'+
						'<col class="diff-content">'+
					'</colgroup>'+
					'<tbody>'+diff+'</tbody></table>';
			}
		} else {
			diff = '<span style="font-weight:bold;color:red;">'+CenPop.msg('page-not-exists')+'</span>';
		}
		$('#resultWindow').html(diff);
		$('.diff-lineno').each(function() {
			$(this).parent().attr('data-line',parseInt($(this).html().match(/\d+/)[0])-1).addClass('lineheader');
		});
		$('table.diff tr').each(function() { //add data-line attribute to every line, relative to the previous one. Used for click event.
			if (!$(this).next().is('[data-line]') && !$(this).next().has('td.diff-deletedline + td.diff-empty')) {
				$(this).next().attr('data-line',parseInt($(this).data('line'))+1);
			} else if ($(this).next().has('td.diff-deletedline + td.diff-empty')) {
				$(this).next().attr('data-line',$(this).data('line')); //copy over current data-line for deleted lines to prevent them from messing up counting.
			}
		});
		CenPop.status('done', true);
		if (typeof(callback) === 'function') {
			callback();
		}
	});
};
 
/** Retrieve page contents/info, process them, and update population data.
 * 
 * Then we want to process the data.
 * Specifically, we want to 
 * 1) Check to see if the page already contains either of the two templates used for historical population data
 *   If so, we need to potentially update the template with the given data.
 * 
 * 
 * 2) If not:
 *     a) If we find the ==Demographics== or == Demographics == section tags
 * 	        i) Replace in page content 
 * 			ii) Submit update
 * 	   b) Add to manual list of pages */
CenPop.api.get = function(pagename) {
	CenPop.pageCount();
	if (CenPop.isStopped) {
		return CenPop.stop();
	}
	if (pagename === '#PRE-PARSE-STOP') {
		var curval = $('#articleList').val();
		//$('#articleList').val(curval.substr(curval.indexOf('\n') + 1));
		$('#preparse').prop('checked', false);
		CenPop.stop();
		return;
	}
	var data = {
		'action': 'query',
		'prop': 'info|revisions|templates',
		'intoken': 'edit|protect|move',
		'tltemplates': 'Template:US Census population|Template:Historical populations|Template:Disambiguation|Template:Geodis',
		'titles': pagename,
		'rvprop': 'content|timestamp|ids',
		'rvlimit': '1',
		'indexpageids': true,
		'meta': 'userinfo',
		'uiprop': 'hasmsg'
	};
	//.log("data="+JSON.stringify(data));
	data.redirects = true;
	CenPop.status('load-page');
	CenPop.api.call(data, function(response) {
		console.warn("Response=",response);
		var has_uscensuspop=false;
		if (response.query.userinfo.hasOwnProperty('messages')) {
			var view = mw.config.get('wgScriptPath') + '?title=Special:MyTalk';
			var viewNew = view + '&diff=cur';
			CenPop.status(
				'<span style="color:red;font-weight:bold;">'+
					CenPop.msg('status-newmsg', 
						'<a href="'+view+'" target="_blank">'+CenPop.msg('status-talklink')+'</a>',
						'<a href="'+viewNew+'" target="_blank">'+CenPop.msg('status-difflink')+'</a>')+
				'</span>', true);
			alert(CenPop.msg('new-message'));
			CenPop.stop();
			return;
		}
		
		CenPop.create_page(response.query,pagename); /* Create details for the page */
		has_us_census_pop=false;
		if(!CenPop.page.exists && CenPop.place_type)
		{
			/* TODO: the page is altogether missing, so we should try a different format */
			CenPop.place_type=false; 
			/* Call it again without the place type */
			var top_list=CenPop.pop_page.list[CenPop.pop_page.curr_pos].replace(/"/g,'').split(',');
			CenPop.page.name = CenPop.format_page_query(top_list,$('#geoAdding').val(),
			CenPop.place_type,false);
			CenPop.api.get(CenPop.page.name);
			return;
		}
		else if(!CenPop.page.exists)
		{
			var curr_art_val = $('#articleList').val();
			$('#articleList').val(curr_art_val+'\n'+pagename);
		 	setTimeout(CenPop.next, Math.max(+$('#throttle').val() || 0, 2) * 1000);
			return;
		}
		if(CenPop.page.templates)
		{
			for(var i=0;i<CenPop.page.templates.length; i++)
			{
				var curr_title=CenPop.page.templates[i].title;
				if(curr_title === 'Template:US Census population')
				{	
					has_uscensuspop=true; 
					CenPop.page.has_template=true;
				}
				else if(curr_title === 'Template:Historical populations') { CenPop.page.has_template=true; }
				else if(curr_title === 'Template:Disambiguation' || curr_title === 'Template:Geodis')
				{
					/* TODO: In this case we ought to search the disambiguation page for the 
						correct place name */
					CenPop.page.disambig=true;	
				}
			}
		}
		var curr_art_val = $('#articleList').val();
		if (response.query.redirects) {
			CenPop.page.name = response.query.redirects[0].to;
		}
		if(CenPop.page.disambig)
		{
			var curr_geo = $('#geoAdding').val();

			if(CenPop.in_get || curr_geo === 'county')
			{
				CenPop.in_get=false;
				$('#articleList').val(curr_art_val+'\n'+pagename);
				setTimeout(CenPop.next, Math.max(+$('#throttle').val() || 0, 2) * 1000);
				/* prevent infinite loops */
				return;
			}
			/* This is a disambiguation page.
			   Try with county, then return */
			var top_list=CenPop.pop_page.list[CenPop.pop_page.curr_pos].replace(/"/g,'').split(',');
			//CenPop.pop_page.curr_place=top_list;
		/*	var place_name = top_list[0].split('\"')[1];
			var county_name = top_list[1].split('\"')[1];
			var state_name = top_list[2].split('\"')[1];
			var full_name = place_name + ", " + county_name +", " + state_name;*/
			CenPop.page.name = CenPop.format_page_query(top_list,curr_geo,
				CenPop.place_type,true);
			CenPop.in_get=true;
			CenPop.api.get(CenPop.page.name);
			return;
		}
		else if(CenPop.in_get) CenPop.in_get=false;
		/* Check for template */
		if(CenPop.page.has_template) CenPop.page.newContent='';
		else {
			//alert(CenPop.page.content);
			var newContent = CenPop.add_pop(CenPop.page.content);
			if(newContent!=='') CenPop.page.newContent=newContent;
			else CenPop.page.newContent='';
		}
		var temp_ret='';
		if(CenPop.page.newContent==='') {
			console.log("Here");
			if(has_uscensuspop)
			{
				CenPop.debug("Before add_to_template","global");

				temp_ret=CenPop.add_to_template(CenPop.page.content);
				CenPop.debug("After add_to_template","global");
			//	alert('temp_ret='+temp_ret);
				if(temp_ret!=='')
				{
					CenPop.debug("temp_ret!==''",'global');
					CenPop.check_modified_pop(temp_ret);
				}
				else
				{
					CenPop.debug("temp_ret=''",'global');
					$('#articleList').val(curr_art_val+'\n'+pagename);
		 			setTimeout(CenPop.next, Math.max(+$('#throttle').val() || 0, 2) * 1000);
					return;
				}
			}
			else
			{
				temp_ret=CenPop.add_to_histpop_template(CenPop.page.content);
				if(temp_ret!=='') CenPop.check_modified_pop(temp_ret);
				else
				{
					$('#articleList').val(curr_art_val+'\n'+CenPop.page.name);
		 			setTimeout(CenPop.next, Math.max(+$('#throttle').val() || 0, 2) * 1000);
					return;
				}
			}
		}
		else
		{
			$('#editBoxArea').val(CenPop.page.newContent);
			$('#diffLen').html(CenPop.page.newContent.length-CenPop.page.content.length);
			if($('#autosave').prop('checked') !== true) $('.editbutton').prop('disabled', false);
			CenPop.added_pop=true;
		}
		if ($('#autosave').prop('checked')) {
			
			CenPop.debug("autosave checked","global");
					//timeout will take #throttle's value * 1000, if it's a number above 0. Currently defaults to 0.
			setTimeout(CenPop.api.submit, Math.max(+$('#throttle').val() || 0, 0) * 1000);
		}
	});
};

/**
 * Upload the ith file 
 */
CenPop.api.upload_ith = function(i, curr_Api) {
	var curr_promise, new_filename;
	var fips_code;
	var upload_text_begin='== Summary ==\nLocation of ';
	var upload_text_end='Own work, based on concept by Arkyan, using '+
	'Census Bureau Tiger GIS data and a custom fork of Kartography adapted for '+
	'this purpose, available at {{URL|https://github.com/jacobmas/kartograph.py}}\n'+
	'== Licensing ==\n{{self|cc-by-sa-4.0}}\n';
	curr_file=CenPop.file_list[i];
	if(curr_file != null)
	{
		my_match=curr_file.name.toString().match(/^(.+)_(County|Parish)_(.+)_(Incorporated_and_Unincorporated_areas)_(.+)_(Highlighted)_([0-9]+)/);
		place_match=my_match[5].replace(/_/g,' ');
		fips_code=''+my_match[7].slice(0,2)+'-'+my_match[7].substr(2);
		upload_text=upload_text_begin+place_match+' in '+my_match[1].replace(/_/g,' ')+' '+my_match[2];
		upload_text=upload_text+', '+my_match[3].replace(/_/g, ' ')+'.\n\nFIPS code: '+fips_code+'\n\n'+upload_text_end+'\n';
		/* Easiest way to ensure uniqueness is to leave the FIPS code in the file name */
		cty_match=curr_file.name.toString().match(/.+_(County|Parish)/g);
		for(str in cty_match)
		{
			upload_text+='[[Category:Maps of '+str.replace(/_/g,' ')+', '+my_match[3]+']]\n';
		}
		CenPop.api.getExternal(fips_code, my_match[5].replace(/_/g, ' ')+', '+my_match[3].replace(/_/g, ' '),0,true,null)
		.then(function() 
		{
			//if CenPop.
			new_filename=my_match[1]+'_'+my_match[2]+'_'+my_match[3]+'_'+my_match[4]+'_'+CenPop.page.name.split(',')[0]+'_'+my_match[6]+'.svg';
			//new_filename=curr_file.name.toString()
		
			CenPop.debug('upload_text='+upload_text,'upload');
			CenPop.debug("new_filename="+new_filename+", about to set timeout for upload", 'upload');
			setTimeout(function()
			{
				data={
					ignorewarnings: true,
					text: upload_text,
					filename: new_filename
				};
				curr_Api.upload(curr_file,data)
				.then(function() {
					CenPop.status("Done uploading file "+i);
					CenPop.debug("Done uploading "+i,'upload');
					if(++i < CenPop.file_list.length) 
						CenPop.api.upload_ith(i, curr_Api); 
					else
						CenPop.done_uploads();
				}, 
				function() 
				{ 
					CenPop.status("Failed/warned at uploading "+i,'upload'); 
					CenPop.debug("Failed/warned at uploading "+i,'upload'); 
					if(++i < CenPop.file_list.length) 
						CenPop.api.upload_ith(i, curr_Api); 
					else
						CenPop.done_uploads();				
				}, 
				function() 
				{
					CenPop.debug("Made progress with "+i+", at state"+this.state(),'upload');
				});
				
			}, Math.max(+$('#uploadthrottle').val() || 0, 1) * 1000);
		}, function() { 
					if(++i < CenPop.file_list.length) 
						CenPop.api.upload_ith(i, curr_Api); 
					else
						CenPop.done_uploads();		
		});
		
	}
}
/* Called once list of uploads has finished */
CenPop.done_uploads = function() {
						$('#inputfile').prop('disabled',false);
					$('#uploadthrottle').prop('disabled',false);
					$('#uploadButton').prop('disabled',false);
					$('#inputFile').files=CenPop.file_list=[]; /* Shoddy coding but javascript handles garbage */
};

CenPop.api.upload = function() {
	$('#uploadButton').prop('disabled',true);
	$('#uploadthrottle').prop('disabled',true);
	$('#inputfile').prop('disabled',true);
	var curr_api=new mw.ForeignApi( 'https://commons.wikimedia.org/w/api.php' );
	CenPop.debug('curr_api='+curr_api,'upload');
	CenPop.status('upload');
	if(CenPop.file_list.length>0)
		CenPop.api.upload_ith(0,curr_api);

	//CenPop.status('done');
};

//Some functions with self-explanatory names:
CenPop.api.submit=function(gotNewestContent=false) {
	CenPop.api.token(submit_func);
	/* Internal function to call after token is gotten */
	function submit_func() {
		console.log("In submit_func");
		$('.editbutton').prop('disabled', true);
		var summary = $('#summary').val(),printed=false;
		if(CenPop.added_pop && (printed=true)) summary+='Added historical population, ';
		if(CenPop.modified_pop && (printed=true)) summary+='Modified historical population, ';
		if(CenPop.modified_info && (printed=true)) summary += 'infobox, ';
		if(CenPop.converted_geobox && (printed=true)) summary += 'Geobox->Infobox, ';
		summary=summary.replace(/,\s*$/,"")+' '+CenPop.messages.en['via-field'];
		var data = {'title': CenPop.page.name,'summary': summary,'action': 'edit',
			'basetimestamp': CenPop.page.revisions ? CenPop.page.revisions[0].timestamp : '',
			'token': CenPop.page.edittoken,'text': $('#editBoxArea').val()};
		if((!CenPop.gotNewestContent && CenPop.page.content !== CenPop.page.newContent) || 
		(CenPop.gotNewestContent && CenPop.page.newestContent !== CenPop.page.content) || (CenPop.page.content!==$('#editBoxArea').val())) {
			CenPop.status('submit');
			CenPop.api.call(data, function(response) {
				if(CenPop.added_pop && !(CenPop.added_pop=false)) $('#newPages').html(++CenPop.add_count);
				if(CenPop.modified_pop && typeof response.edit.newrevid !== 'undefined' && 
				!(CenPop.modified_pop=false)) $('#modPages').html(++CenPop.mod_count);
				if(CenPop.modified_info && typeof response.edit.newrevid !== 'undefined' && 
				!(CenPop.modified_info=false)) $('#modInfoPages').html(++CenPop.mod_info_count);
				if(CenPop.converted_geobox && typeof response.edit.newrevid !== 'undefined' &&
				!(CenPop.converted_geobox=false)) $('#geoInfoPages').html(++CenPop.geo_info_count);
				CenPop.log('edit', response.edit.title, response.edit.newrevid);
				CenPop.status('done', true);
				CenPop.gotNewestContent=false;
				CenPop.next();
			});
		}
		else {
			/* Update status and move on */
			CenPop.log('null-edit', CenPop.page.name, '');
			CenPop.status('done', true);
			CenPop.gotNewestContent=false;
			CenPop.next();
		}
	}
};

/**
 * Checks if we modified the population, changes the color of count if file has shrunk
 * (TODO: this part broken thanks to new modifications the script makes later)
 */
CenPop.check_modified_pop = function(temp_ret)
{
	CenPop.page.newContent=temp_ret;
	if(CenPop.page.content !== CenPop.page.newContent)
		CenPop.modified_pop=true;
	CenPop.updateEditBox();
};
/* Update the edit box area with new content */
CenPop.updateEditBox = function()
{
	$('#editBoxArea').val(CenPop.page.newContent);
	if($('#autosave').prop('checked') !== true) $('.editbutton').prop('disabled', false);
	var temp_diff=CenPop.page.newContent.length-CenPop.page.content.length;
	$('#diffLen').html(temp_diff);
	if(temp_diff<0) { $('#diffLen').css("color", "red"); }
	else { $('#diffLen').css("color","black");	}			
};

CenPop.api.preview = function() {
	CenPop.status('preview');
	CenPop.api.call({
		'title': CenPop.page.name,
		'action': 'parse',
		'text': $('#editBoxArea').val()
	}, function(response) {
		$('#resultWindow').html(response.parse.text['*']);
		$('#resultWindow div.previewnote').remove();
		CenPop.status('done', true);
	});
};
CenPop.api.move = function() {
	CenPop.status('move');
	var topage = $('#moveTo').val().replace(/\$x/gi, CenPop.page.pagevar);
	var summary = $('#summary').val();
	summary += ' (via CenPop script)';
	var data = {
		'action':'move',
		'from': CenPop.page.name,
		'to': topage,
		'token': CenPop.page.movetoken,
		'reason': summary,
		'ignorewarnings': 'yes'
	};
	if ($('#moveTalk').prop('checked')) data.movetalk = true;
	if ($('#moveSubpage').prop('checked')) data.movesubpages = true;
	if ($('#suppressRedir').prop('checked')) data.noredirect = true;
	CenPop.api.call(data, function(response) {
		CenPop.log('move', response.move.from, reponse.move.to);
		CenPop.status('done', true);
		if (!$('#moveTo').val().match(/\$x/i)) $('#moveTo').val('')[0].focus(); //clear entered move-to pagename if it's not based on the pagevar
		CenPop.next(topage);
	});
};
 
/***** Main other functions *****/
 
//Show status message
CenPop.status = function(action, done) {
	$('#summary').prop('disabled', !done); //Disable box when not done (so busy loading). re-enable when done loading.
	var status = CenPop.msg('status-'+action);
	if (status === false) return;
	var spinImg = '<img src="//upload.wikimedia.org/wikipedia/commons/d/de/Ajax-loader.gif" width="15" height="15" alt="'+CenPop.msg('status-alt')+'"/>';
	if (status) if (!done) status += ' ' + spinImg;
	else status = action;
	$('#status').html(status);
	CenPop.pageCount();
	return action=='done';
};
 
CenPop.pageCount = function() {
	if (CenPop.allowed === false||!$('#articleList').length) return;
	$('#articleList').val(($('#articleList').val()||'').replace(/(^[ \t]*$\n)*/gm, ''));
	CenPop.list = $('#articleList').val().split('\n');
	var count = CenPop.list.length;
	if (count === 1 && CenPop.list[0] === '') count = 0;
//	$('#totPages').html(count);
};
 
//Adds a line to the logs tab.
CenPop.log = function(action, page, info) {
	var d = new Date(),pagee = encodeURIComponent(page),extraInfo = '', actionStat = '';
	switch (action) {
		case 'edit':
			if (typeof info === 'undefined') {
				action = 'null-edit';
				actionStat = 'nullEdits';
				extraInfo = '';
			} else {
				extraInfo = ' (<a target="_blank" href="/index.php?title='+pagee+'&diff='+info+'">diff</a>)';
				actionStat = 'pagesSaved';
			}
			break;
		case 'move':
			extraInfo = ' to <a target="_blank" href="/wiki/'+encodeURIComponent(info)+'" title="'+info+'">'+info+'</a>';
			break;
		case 'null-edit':
			action = 'null-edit';
			actionStat = 'nullEdits';
			extraInfo = '';
			break;
	}
	actionStat = '#' + (actionStat);
	$(actionStat).html(+$(actionStat).html() + 1);
	$('#actionlog tbody')
		.append('<tr><td>'+(d.getHours())+':'+(d.getMinutes())+':'+(d.getSeconds())+'</td>'+
			'<th>'+action+'</th>'+
			'<td><a target="_blank" href="/wiki/'+pagee+'" title="'+page+'">'+page+'</a>'+ extraInfo +'</td></tr>')
		.parents('.CenPoptabc').scrollTop($('#actionlog tbody').parents('.CenPoptabc')[0].scrollHeight);
};
// Adds a console message if we've set to debug that area 
CenPop.debug = function(message, tag='global')
{
	if(tag === 'global' || $('#debug-'+tag).prop('checked')) console.log(message);
};

/**
 * Create the CenPop.page object to aid in processing
 */
 CenPop.create_page=function(query,pagename) {
 	console.warn("CenPop.create_page, query=",query);
 		CenPop.page = query.pages[query.pageids[0]];
		CenPop.page.name = pagename;
	 	var varOffset = CenPop.list[0].indexOf('|') !== -1 ? CenPop.list[0].indexOf('|') + 1 : 0;
	 	CenPop.page.pagevar = CenPop.list[0].substr(varOffset);
		CenPop.page.content = CenPop.page.revisions ? CenPop.page.revisions[0]['*'] : '';
		CenPop.page.exists = !query.pages["-1"];
		CenPop.page.deletedrevs = query.deletedrevs;
		CenPop.page.watched = CenPop.page.hasOwnProperty('watched');
		CenPop.page.official_url=CenPop.get_official_url(CenPop.page.content);
		console.log("## CenPop.page.official_url="+CenPop.page.official_url);
		CenPop.page.has_template=false;
		CenPop.page.disambig=false;	
 };
 
 CenPop.get_official_url=function(content) {
 	var external_lst=CenPop.get_externallinks(content),i,curr_url,match,desc;
 	var state=$("#stateAdding option:selected").text();
 	console.log("state="+state);
 	console.log("## external_lst="+external_lst);
 	for(i=0;i<external_lst.length&&i<1;i++) {
 		if(/\.archive\.org/.test(external_lst[i])) break;
 		if(match=external_lst[i].match(/\{\{(?:Official Website|URL)\|([^\}]+)/i)) return match[1];
 		else if((match=external_lst[i].match(/\[(http[^\s]*)\s([^\]]*)/))) {
 			if(/official/i.test(external_lst[i])) return match[1];
 			desc=match[2].replace(/(^|[^A-Za-z]{1})(City|Town|Village|Borough)([^A-Za-z]{1}|$)/ig,"")
 			.replace(/(^|[^A-Za-z]{1})(the|of|website|homepage)([^A-Za-z]{1}|$)/ig,"")
 			.replace(new RegExp(", "+state),"").trim();
 			console.log("## desc="+desc+", CenPop.page.name="+CenPop.page.title+", match[1]="+match[1]);
 			if(CenPop.page.title.indexOf(desc)!==-1 && !/unofficial/i.test(desc)) return match[1];
 		}
 	}
 	return "";
 };
 
 /* Get the external links section to search for an official URL */
 CenPop.get_externallinks=function(content) {
 	var ext_regex=/\=\=\s*External Links\s*\=\=\s*\n(?:\{\{.*\n)*((?:\*.*\n)*)/i;
 	var match=content.match(ext_regex),ret=[],i,match2;
 	if(match) console.log("match="+JSON.stringify(match));
 	if(match && (match2=match[1].match(/.+/))) {
	  ret.push(match2[0].replace(/^\*\s*/,"")); 	
 	}
 		//for(i=1;i<match.length;i++) if(match[i]!==undefined) 
 	return ret;

 };
 
 


/* New next working with Census Bureau */
CenPop.next = function(nextPage) {
	CenPop.pop_page.curr_pos+=1
	var curr_geo=$('#geoAdding').val();
	var estorcensus=$('input[name="estorcensus"]:checked').val()
	console.log("estorcensus=",estorcensus);
	if(CenPop.pop_page.list.length<=CenPop.pop_page.curr_pos)
	{
		CenPop.status('Finished list!');
		CenPop.stop();
		return;
	}
	if($('input[name="load_from"]:checked').val()==='file') {
		var top_list=CenPop.pop_page.list[CenPop.pop_page.curr_pos].replace(/"/g,'').split(',');
		CenPop.place_type=true;
		var full_name=CenPop.format_page_query(top_list,curr_geo,
			CenPop.place_type,false);
		var place_name, county_name, state_name, full_name;
		CenPop.pop_page.curr_place=top_list;
		CenPop.page.name = full_name;
		CenPop.pageCount();
		CenPop.api.get(CenPop.page.name);
	}
	else if($('input[name="load_from"]:checked').val()==='census')
	{
		var county_fips=$('#county_fips').val();
		if(curr_geo === 'cousub' && parseInt(CenPop.pop_page.list[CenPop.pop_page.curr_pos][3])>county_fips)
		{
			/* Update county fips then make a call to loadGazetteer */
			$('#county_fips').val(CenPop.pop_page.list[CenPop.pop_page.curr_pos][3]);
			CenPop.api.loadGazetteer($('.estYearText').val(), $('#stateAdding').val(),
		$('#geoAdding').val(), function() { CenPop.api.callExternal(); });
			return;
		}
		else if($('#geoAdding').val() === 'incplace'&& CenPop.pop_page.list[CenPop.pop_page.curr_pos].length>1&&
		/\(CDP\)/.test(CenPop.pop_page.list[CenPop.pop_page.curr_pos][1])&&estorcensus==="Estimate") {
			CenPop.next();
			return;
		}
		else
		{
			CenPop.api.callExternal(estorcensus);
			return;
		}

	}
};

/* Make the call to CenPop.api.getExternal (if necessary after reloading new gazetteer data) */
CenPop.api.callExternal = function(estorcensus)
{
	var curr_entity = CenPop.pop_page.list[CenPop.pop_page.curr_pos], curr_fips='';
	CenPop.pop_page.curr_place = curr_entity;
	CenPop.pageCount();
	/* Send geoname too if necessary */
	curr_fips=''+curr_entity[2]+'-'+curr_entity[3]+($('#geoAdding').val() === 'cousub'?'-'+curr_entity[4]:'');
	console.log("Current FIPS: '"+curr_fips);
	CenPop.api.getExternal(curr_fips, curr_entity[1],0,false, null, estorcensus);
};
 
//Stop everything, reset inputs and editor
CenPop.stop = function() {
	$('#stopbutton,'+
	  '.editbutton,'+
	  '.CenPoptabc[data-tab="1"] .editbutton,'+
	  '.CenPoptabc[data-tab="4"] button').prop('disabled', true);
	 $('#runButton').prop('disabled',true);
	$('#startbutton,#articleList,'+
	  '#replacesPopup button,'+
	  '#replacesPopup input,'+
	  '.CenPoptabc input, select').prop('disabled', false);
	$('input[name="load_from"]').prop('disabled',false);
	$('#load_from_div').prop('disabled',false);
	CenPop.isStopped = true;
};
 
//Start AutoWikiBrowsing; this is where we should load in the population
CenPop.start = function() {
	CenPop.isStopped = false;
		var estorcensus=$('input[name="estorcensus"]:checked').val()
console.log("estorcensus=",estorcensus);
	/* Disable the fields to avoid wacky race conditions */
	$('#startbutton').prop('disabled',true);
	$('.CenPoptabc[data-tab="1"] button, #replacesPopup button, #replacesPopup input, select').prop('disabled', true);
	$('.estYearText, #stateAdding, #geoAdding, #min_fips').prop('disabled',true);
	$('input[name="load_from"]').prop('disabled',true);
		$('#load_from_div').prop('disabled',true);
		
	//console.log('load_from='+$('input[name="load_from"]:checked').val());
	if($('input[name="load_from"]:checked').val()==='census')
	{
		CenPop.api.loadPopExternal($('.estYearText').val(), $('#stateAdding').val(),
	estorcensus,
		$('#geoAdding').val(), function() {
				console.log("Done loading PopExternal");
				CenPop.loaded_pop=true;
				if(CenPop.loaded_pop && CenPop.loaded_gazetteer) {
					CenPop.on_load_external();
					CenPop.loaded_pop=CenPop.loaded_gazetteer=false;
				}
			});
				CenPop.api.loadGazetteer($('.estYearText').val(), $('#stateAdding').val(),
		$('#geoAdding').val(), function() {
				console.log("Done loading Gazetteer");
				CenPop.loaded_gazetteer=true;
				if(CenPop.loaded_pop && CenPop.loaded_gazetteer)
				{
					CenPop.on_load_external();
					CenPop.loaded_pop=CenPop.loaded_gazetteer=false;
				}
			});
	}
	else if($('input[name="load_from"]:checked').val()==='file') {
		CenPop.api.loadPop($('.loadFileText').val(), $('.hasFirstHeader').prop('checked'),$('.estYearText').val(),
	 function() {
				/* 	Enable run, edit, etc. buttons upon completion */
				$('#stopbutton, #runButton, .editbutton, .CenPoptabc[data-tab="4"] button').prop('disabled', false);
			});
	}
};

/** 
 * Add the CDPs to pop_page then sort
 */
CenPop.add_CDP = function()
{
			var estorcensus=$('input[name="estorcensus"]:checked').val()

	for(fips in CenPop.gazetteer_page) {
		var curr_page=CenPop.gazetteer_page[fips];
		if(curr_page['LSAD']==='57' && estorcensus!=='Decennial') {
			/* No estimates for CDPs */
			place_name = (curr_page['NAME']+', '+CenPop.state_fips[curr_page['STATE']]).replace(' CDP',' (CDP)');
			page_array=["N/A",place_name,curr_page['STATE'],curr_page['PLACE']];
			CenPop.pop_page.list.push(page_array);
		}
	}
	CenPop.pop_page.list.sort(function(a,b) {
		return 100000*(a[2]-b[2])+(a[3]-b[3]);
	});
};

CenPop.on_load_external = function() {
	/* TODO: fix so for counties it only does the one county and then finishes list */
	
	/* 	Enable run, edit, etc. buttons upon completion */
	$('#stopbutton, #runButton, .editbutton, .CenPoptabc[data-tab="4"] button').prop('disabled', false);
	geo_adding=$('#geoAdding').val();
	var county_fips = 0, county_num=0;
	if(geo_adding==='place') CenPop.add_CDP();
	else if(geo_adding==='cousub') {
		county_fips = $('#county_fips').val();
		county_num = parseInt(county_fips);
	}
	var fips_start=$('#min_fips').val();
	if(fips_start!=='' || (geo_adding === 'cousub' && county_fips!=='')) {
		var fips_num=parseInt(fips_start);
		while(CenPop.pop_page.curr_pos+1<CenPop.pop_page.list.length && 
		((/place/.test(geo_adding) &&
			fips_num > parseInt(CenPop.pop_page.list[CenPop.pop_page.curr_pos+1][3])) ||
			(geo_adding === 'cousub' && county_num > parseInt(CenPop.pop_page.list[CenPop.pop_page.curr_pos+1][3])  )))
		{
			CenPop.pop_page.curr_pos=CenPop.pop_page.curr_pos+1;
		}
	}
};

CenPop.run = function() {
	CenPop.pageCount();
	$('.editbutton').prop('disabled', true);
	$('#overwrite').prop('disabled',true)
	if(CenPop.pop_page.list.length<=CenPop.pop_page.curr_pos) {
		alert('Finished list!');
		return;
	}
	CenPop.next();
}
 
/***** General functions *****/
 
//Clear all existing timers to prevent them from getting errors
CenPop.fn.clearAllTimeouts = function() {
	var i = setTimeout(function() { return void(0); }, 1000);
	for (var n=0;n<=i;n++) {
		clearTimeout(n);
		clearInterval(i);
	}
	console.log('Cleared all running intervals up to index',i);
};
 
//Filter an array to only contain unique values.
CenPop.fn.uniques = function(arr) {
	var a = [];
	for (var i=0, l=arr.length; i<l; i++) if (a.indexOf(arr[i]) === -1 && arr[i] !== '') a.push(arr[i]);
	return a;
};
 
//i18n function
CenPop.msg = function(message) {
	var args = arguments,lang = CenPop.lang,msg;
	if (typeof message === 'object') {
		lang = message[1];
		message = message[0];
	}
	if (!CenPop.messages || !CenPop.messages.en) return false;
	if (CenPop.messages.hasOwnProperty(lang) && 
		CenPop.messages[lang].hasOwnProperty(message)) msg = CenPop.messages[lang][message];
	 else msg = (CenPop.messages.en.hasOwnProperty(message)) ? CenPop.messages.en[message] : '';
	msg = msg.replace(/\$(\d+)/g, function(match, num) { return args[+num] || match; });
	return msg;
};

CenPop.update_estref=function() {
	

		var begin_api_url=CenPop.api.get_api_url($('.estYearText').val(),$('#stateAdding').val(),$('input[name="estorcensus"]:checked').val(),$('#geoAdding').val());
	
	var estRefTextBegin='<ref name="USCensus'+$('input[name="estorcensus"]:checked').val()+$('.estYearText').val()+'CenPopScriptOnly">{{cite web'+
			'|url='+begin_api_url+
			'|title=Census Population API'+
			'|publisher=United States Census Bureau'+
			'|accessdate='+new Date(Date.now()).toDateString().substr(4).replace(/ (\d{4})$/,", $1")+
			'}}</ref>';
			
		$('.estRefText').val(estRefTextBegin);

}
 
/***** Init *****/
CenPop.init = function() {
	console.log(CenPop.messages.en, !!CenPop.messages.en);
	CenPop.fn.clearAllTimeouts();
	if (!CenPop.messages[CenPop.lang]) CenPop.lang = 'en';
	
	/* The Historical Census Input Fields, separating from elsewhere */
	var censusinputfields='<div id="histPopDiv">Historical Census Info</div>'+
	'<div class="replaces" id="censusInputFields">'+
			'<label title="'+CenPop.msg('begin-year-tip')+'" style="display:block;">'+'begin_year='+' <input type="text" class="beginYearText"/></label>'+
		'<label title="'+CenPop.msg('est-year-tip')+'" style="display:block;">'+'estyear='+' <input type="text" class="estYearText"/></label>' +
		'<label style="display:block;">'+'estref='+' <input type="text" class="estRefText"/></label>'+
		'<label style="display:block;">'+'footnote='+' <input type="text" class="footnoteText"/></label>'+
		'</div>';
 
	var NSList = '<select multiple name="namespace" id="namespacelist">';
	for (var i in CenPop.ns) {
		if (parseInt(i) < 0) continue; //No Special: or Media: in the list
		NSList += '<option value="'+CenPop.ns[i].id+'" selected>'+(CenPop.ns[i]['*'] || '('+CenPop.msg('namespace-main')+')')+'</option>';
	}
	NSList += '</select>';
 
 /*	temp_state_list=["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Washington, DC", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"];*/
	temp_state_list=["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"];
	temp_fips_list=["01","02","04","05","06","08","09","10","11","12","13","15","16","17","18","19","20","21","22",
	"23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39",
	"40","41","42","44","45","46","47","48","49","50","51","53","54","55","56"];
	stateAddingStr='';
	for(i=0; i < temp_state_list.length;i++) stateAddingStr = stateAddingStr + '<option value="'+temp_fips_list[i]+'">'+temp_state_list[i]+'</option>';
 
	var file_load_str='<div id="file_load" class="to_add_div">'
	+'<label style="display:block;">'+'File to Load:'+' <input type="text" class="loadFileText"/></label>'
	+'<label style="display:block;">'+'First line is header:'+'<input type="checkbox" id="hasfirstheader" name="firstheader"/></label>'
	+'</div>';
	/***** Interface *****/
 
	document.title = 'CenPop Script'+(document.title.split('-')[1] ? ' -'+document.title.split('-')[1] : '');
	$('body').html(CenPop.layoutHTML.body);
	$('.CenPoptabc[data-tab="1"]').html(CenPop.layoutHTML.datatab1);

	$('.CenPoptabc[data-tab="2"]').html(CenPop.layoutHTML.censusinputfields);
	
	$('.CenPoptabc[data-tab="3"]').html(
		'<div class="my_header">'+'Debug To Show'+'</div>'+
		'<div class="check_div">'+
		'<span class="to_add_span">'+
		'<label><input type="checkbox" id="debug-update" />'+CenPop.msg('debug-update')+'</label>'+
		'</span>'+
		'<span class="to_add_span">'+
		'<label><input type="checkbox" id="debug-gazetteer" />'+CenPop.msg('debug-gazetteer')+'</label>'+
		'</span>'+
		'<span class="to_add_span">'+
		'<label><input type="checkbox" id="debug-upload" />'+CenPop.msg('debug-upload')+'</label>'+
		'</span>'+
		'</div>'
	);
	$('.CenPoptabc[data-tab="4"]').html(
		'<div class="check_div">'+
		'<span class="to_add_span">'+
		'<form id="myform" name="myform">'+
		'<input type="file" id="inputFile" multiple />'+
		'</span>'+
		'</form>'+
		'<span class="to_add_span">'+'Upload Throttle:'+
		'<input type="number" min="0" value="0" id="uploadthrottle">'+
		'</span>'+
		'<span class="to_add_span">'+
		'<button class="editButton" id="uploadButton">'+'Upload'+'</button>'+
		'</span>'+
		'</div>'
		);
	
	$('.CenPoptabc[data-tab="5"]').html('<table id="actionlog"><tbody>'
	+'</tbody></table>');

	$('body').addClass('CenPop'); //allow easier custom styling of CenPop.
 	$('#file_load').hide(); /* Initially hide the file load stuff */
 	$('#county_div').css('visibility','hidden');
	/***** Event handlers *****/
 
	//Alert user when leaving the tab, to prevent accidental closing.
	onbeforeunload = function() { return "Closing this tab will cause you to lose all progress."; };
	ondragover = function(e) { e.preventDefault(); };
 
	$('.CenPoptab').click(function() {
		$('.active').removeClass('active');
		$(this).addClass('active');
		$('.CenPoptabc[data-tab="'+$(this).attr('data-tab')+'"]').addClass('active');
	});
	$('.articleTab').click(function() {
		$('.activeArticle').removeClass('activeArticle');
		$(this).addClass('activeArticle');
		$('.articleTabc[data-tab="'+$(this).attr('data-tab')+'"]').addClass('activeArticle');
	});
 
 
	$('#preparse-reset').click(function() { $('#articleList').val($('#articleList').val().replace(/#PRE-PARSE-STOP/g,'').replace(/\n\n/g, '\n')); });
	if (window.RETF) $('#refreshRETF').click(RETF.load);
	$('#moreReplaces').click(function() { $('#replacesPopup').append(CenPop.layoutHTML.findreplace); });
	$('#replacesPopup').on('keydown', '.replaces:last', function(e) {
		if (e.which === 9) $('#moreReplaces')[0].click();
	});
 
	$('#geoAdding').change(function() {
		curr_geo=$('#geoAdding').val();
		if(curr_geo==='cousub') {
			CenPop.begin_decennial=5;
			$('#min_fips').prop('maxlength',9);
			$('#county_div').css('visibility','visible');
			$('#county_fips').val('001'); }
		else {
			CenPop.begin_decennial=4;
			$('#min_fips').prop('maxlength',5);
			$('#county_div').css('visibility','hidden');
		}
		CenPop.update_estref();
	});
	
	$('#stateAdding').change(function() { CenPop.update_estref(); });
		/* Set the file list to the new files */
	$('#inputFile').change(function() { CenPop.file_list=this.files; });

	$('#autosave').change(function() { $('#throttle').prop('disabled', !this.checked); });
 	
	$('#articleList').prop('readonly',true);
	/* Get the code to make the external call */

	
	$('#startbutton').click(CenPop.start);
	$('#stopbutton').click(CenPop.stop);
	$('#runButton').click(CenPop.run);
	$('#submitButton').click(CenPop.api.submit);
	$('#previewButton').click(CenPop.api.preview);
	$('#diffButton').click(CenPop.api.diff);
	$('#nextButton').click(function() { CenPop.next(); });
	$('#uploadButton').click(CenPop.api.upload);
	

			
	/* Hardcoded for now: read in later? Make easier to change? */
	$('.beginYearText').val(CenPop.default_fields.beginYearText);
	$('.estYearText').val(CenPop.default_fields.estYearText);
	CenPop.update_estref();
	$('.footnoteText').val(CenPop.default_fields.footnoteText);
	/* To read in all the census data for places for 2016 */
	$('.loadFileText').val(CenPop.default_fields.loadFileText);
	


	/* Switch load based on selected radio button */
	$('input[name="load_from"]').change(function() {
		//console.log("Changed load_from");
		if($(this).val()==='census') $('#load_details').html(CenPop.layoutHTML.census_load_str);
		else if($(this).val()==='file') $('#load_details').html(file_load_str);
	});
	$('input[name="estorcensus"]').change(function() {
		//console.log("Changed load_from");
		if($(this).val()==='Decennial') {
			$('#geoAdding').val('place');
			$('.estYearText').val($('.beginYearText').val())
			$('.estYearText').prop('disabled',true);
		}
		else if($(this).val()==='Estimate') {
			$('#geoAdding').val('incplace');
			$('.estYearText').prop('disabled',false);

		}
		CenPop.update_estref();
		
	});
	$('.beginYearText').change(function(e) { console.warn("e=",e) 
		let text=$(this).val();
		let parsed_year=parseInt(text);
//		console.log("parsed_year%10===0",parsed_year%10===0,"estorcensus val=",$('input[name="estorcensus"]:checked').val()==='Decennial',$('input[name="estorcensus"]:checked').val());
		if(parsed_year % 10 === 0 && $('input[name="estorcensus"]:checked').val()==='Decennial') {
			$('.estYearText').val(text);
			
		}
		CenPop.update_estref();
		
		
	});
	
	$('.estYearText').change(function(e) { CenPop.update_estref(); });
	
};


//Disable CenPop altogether when it's loaded on a page other than Project:AutoWikiBrowser/Script. This script shouldn't be loaded on any other page in the first place.
if (CenPop.allowed === false) CenPop = false;