var codesLookup = new Object();
var disCodesLookup = new Object();

function initCodes() {
	doGetUrl({url: './postcodes.txt', handleAs: 'text/plain', load: populateCodes});
}

function populateCodes(xmlhttp) {
	var txt = xmlhttp.responseText;
	var lines = txt.split('\n');
	var pc;
	for (var i = 0; i < lines.length; i++) {
		var tokens = lines[i].split(',');
		pc = tokens[0];
		if (pc.indexOf('>') > 0) {
			pc = pc.substring(pc.indexOf('>') + 1);
		}
		codesLookup[pc] = {id: tokens[1], name: tokens[2]};
		var dis = tokens[0].substring(0, 4)
		if (disCodesLookup[dis] != null) {
			disCodesLookup[dis].push(tokens[0]);
		}
		else {
			disCodesLookup[dis] = new Array(tokens[0]);
		}
		dis = tokens[0].substring(0, 3)
		if (disCodesLookup[dis] != null) {
			disCodesLookup[dis].push(tokens[0]);
		}
		else {
			disCodesLookup[dis] = new Array(tokens[0]);
		}
	}
}

function doPostcodeSearch() {
	var d = document.getElementById('postcodeRequestDialog');
	var wsize = getSize();
	var dws = d.style.width;
	var dw = (d.offsetWidth > 0 ? d.offsetWidth : (dws.indexOf('px') > 0 ? parseInt(dws.substring(0, dws.indexOf('px'))) : 300));
	var dhs = d.style.height;
	var dh = (d.offsetHeight > 0 ? d.offsetHeight : (dhs.indexOf('px') > 0 ? parseInt(dhs.substring(0, dhs.indexOf('px'))) : 150));
	d.style.left = ((wsize.width / 2) - (dw / 2)) + 'px';
	d.style.top = ((wsize.height / 2) - (dh / 2)) + 'px';
	d.style.display = 'block';
}


function findPostcode(b) {
	var d = document.getElementById('postcodeRequestDialog');
	var f = null;
	if (b != null) {
		f = b.form;
	}
	if (f == null) {
		f = document.forms['pcRequestForm'];
	}
	if (f == null) {
		f = document.getElementById('pcRequestForm');
	}
	var pc = '';
	if (f.elements['postcode']) {
		pc = f.elements['postcode'].value;
	}
	d.style.display = 'none';
	if (pc == '') {
		pc = window.prompt('Type your postcode in the box below, then click OK', '');
	}
	if ((pc == null) || (pc == ''))
		return;
	var pcode = pc + '';
	while (pcode.indexOf(' ') >= 0) {
		pcode = pcode.replace(' ', '');
	}
	pcode = pcode.toUpperCase();
	var ward = codesLookup[pcode];
	if (ward == null) {
		var matching = '';
		if (pcode.length >= 4) {
			var sub1 = pcode.substring(0, 4);
			var codes = disCodesLookup[sub1];
			if (codes != null) {
				matching = '\n\nThe following postcodes match some or all of your text \'' + sub1 + '\': ' + codes.slice(0, Math.min(codes.length, 15)).join(', ') + (codes.length > 15 ? '..., ' + codes[codes.length - 1] : '');
			}
		}
		if ((pcode.length >= 3) && (matching == '')){
			var sub1 = pcode.substring(0, 3);
			var codes = disCodesLookup[sub1];
			if (codes != null) {
				matching = '\n\nThe following postcodes match some or all of your text \'' + sub1 + '\': ' + codes.slice(0, Math.min(codes.length, 15)).join(', ') + (codes.length > 15 ? '..., ' + codes[codes.length - 1] : '');
			}
		}
		alert('The system could not find a ward for postcode \'' + pc + '\'. Please try again.\n\nPostcodes should be in the format AA1 1AA and should contain at least 6 letters or numbers.' + matching);
	}
	else {
		var s = document.getElementById('postcodeResultsPrompt');
		var p = pcFoundPrompt + '';
		while (p.indexOf('{0}') >= 0) {
			p = p.replace('{0}', pc.toUpperCase());
		}
		while (p.indexOf('{1}') >= 0) {
			p = p.replace('{1}', areaTypeName);
		}
		while (p.indexOf('{2}') >= 0) {
			p = p.replace('{2}', ward.name);
		}
		s.innerHTML = p;
		d = document.getElementById('postcodeResultsDialog');
		var f = document.forms['pcResultsForm'];
		if (f == null) {
			f = document.getElementById('pcResultsForm');
		}
		if (f.elements['pcArea']) {
			f.elements['pcArea'].value = ward.id;
		}
		lastMatch = ward.id;
		var wsize = getSize();
		var dws = d.style.width;
		var dw = (d.offsetWidth > 0 ? d.offsetWidth : (dws.indexOf('px') > 0 ? parseInt(dws.substring(0, dws.indexOf('px'))) : 300));
		var dhs = d.style.height;
		var dh = (d.offsetHeight > 0 ? d.offsetHeight : (dhs.indexOf('px') > 0 ? parseInt(dhs.substring(0, dhs.indexOf('px'))) : 150));
		d.style.left = ((wsize.width / 2) - (dw / 2)) + 'px';
		d.style.top = ((wsize.height / 2) - (dh / 2)) + 'px';
		d.style.display = 'block';
	}
}

function doNothing() {
}

function getSize() {
	var myWidth = 0;
	var myHeight = 0;
	if (typeof(window.innerWidth) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} 
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	var mySize = new Object();
	mySize.width = myWidth;
	mySize.height = myHeight;
	return mySize;
}

function zoomToPostcodeResults(btn) {
	var f = btn.form;
	hidePostcodeResults();
	var fid = (f && f.elements['pcArea'] ? f.elements['pcArea'].value : lastMatch);
	var movie = thisMovie('instantAtlasReport');
	movie.zoomToMapFeature(fid);
}

function goToPostcodeResults(btn) {
	var f = btn.form;
	hidePostcodeResults();
	var fid = (f && f.elements['pcArea'] ? f.elements['pcArea'].value : lastMatch);
	window.location.href = './report_' + (fid.substring(0, 1) == '_' ? fid.substring(1) : fid) + '.html';
}

function hidePostcodeResults() {
	var d = document.getElementById('postcodeResultsDialog');
	d.style.display = 'none'
}