
// PoliticsWeb v3.0
// Copyright Michael Dent
// michael@politicsweb.co.uk

// 1. Menu
// 2. Ajax framework
// 3. Comments
// 4. Show/hide full post
// 5. Lookup postcode


// 1. Menu

function openMenu(id) {
	if (document.getElementById('sub-' + id).style.left == '') {
		document.getElementById('sub-' + id).style.left = findPosX(document.getElementById('menu-' + id)) + 'px';
	}
	document.getElementById('sub-' + id).style.display = 'block';
	document.getElementById('menu-' + id).classid += ' cur';
	if (typeof closetimer != 'undefined') {
		if (typeof lastopened != 'undefined') {
			if (lastopened == id) clearTimeout(closetimer);
		}
	}
	lastopened = id;
}

function closeMenu(id) {
	closetimer = setTimeout("document.getElementById('sub-" + id + "').style.display='';oldclass=document.getElementById('menu-" + id + "').classid;newclass=oldclass.replace(/cur/g,'');document.getElementById('menu-" + id + "').classid=newclass;", 100);
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while(1) {
			curleft += obj.offsetLeft;
			if(!obj.offsetParent) break;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}


// 2. Ajax framework

function createAjax(page) {
	xmlHttp = ((window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0"));
	xmlHttp.open("POST", page, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
}


// 3. Comments

function comments(reference) {
	el = document.getElementById(reference);
	el2 = document.getElementById('post-' + reference);
	if (el.style.display == 'block') {
		el.style.display = '';
		el2.style.display = '';
	} else {
		el.style.display = 'block';
		el2.style.display = 'block';
	}
}

function postComment(reference,details) {
	el = document.getElementById('post-' + reference);
	html  = '<form onsubmit="ajaxComment(\'' + reference + '\',\'' + details + '\'); return false;">';
	html += 'Name:<br /><input type="text" id="comment-name" /><br /><br />Comment:<br /><textarea id="comment"></textarea><br /><br />';
	if (userEmailAuthentication) {
		html += 'Email (not public):<br /><input type="text" id="comment-email" /><br /><br />';
	} else {
		html += '<input type="hidden" id="comment-email" value="n/a" />';
	}
	if (userPostcodeLookup) {
		html += 'Postcode (not public):<br /><input type="text" id="comment-postcode" /><br /><br />';
	} else {
		html += '<input type="hidden" id="comment-postcode" value="n/a" />';
	}
	html += '<input type="submit" value="Post comment" id="btn-post"></form>';
	el.innerHTML = html;
	document.getElementById('comment-name').focus();
}

function deleteComment(id) {
	createAjax("/calls/comment.php");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText == 'success') {
				child = document.getElementById('comment-' + id);
				parent = child.parentNode;
				parent.removeChild(child);
			}
		}
	};
	vars = 'delete=' + id;
	xmlHttp.send(vars);
}

function ajaxComment(reference,details) {
	theReference = reference;
	theDetails = details;
	if (userPostcodeLookup) {
		lookupPostcode(document.getElementById('comment-postcode').value,'commentConstituencyReceived');
	} else {
		doSubmitForm();
	}
}

function commentConstituencyReceived(r) {
	document.getElementById('comment-postcode').value = r.name;
	doAjaxComment();
}

function doAjaxComment() {
	reference = theReference;
	details = theDetails;
	name = document.getElementById('comment-name').value;
	comment = document.getElementById('comment').value;
	email = document.getElementById('comment-email').value;
	postcode = document.getElementById('comment-postcode').value;
	document.getElementById('btn-post').disabled = true;
	createAjax("/calls/comment.php");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText == 'success') {
				document.getElementById(reference).innerHTML += '<p>' + comment + '<br />- <strong>' + name + '</strong></p>';
				el = document.getElementById('post-' + reference);
				el.innerHTML = '<a href="javascript:postComment(\'' + reference + '\',\'' + details + '\');">Post a comment</a>';
			} else if (xmlHttp.responseText == 'approval') {
				alert('Your comment has been received and saved, and will appear on the website shortly subject to approval.');
				el = document.getElementById('post-' + reference);
				el.innerHTML = '<a href="javascript:postComment(\'' + reference + '\',\'' + details + '\');">Post a comment</a>'
			} else if (xmlHttp.responseText == 'email') {
				alert('Thank you for your comment. As a final step, please click the link in your email to confirm your email address.');
				el = document.getElementById('post-' + reference);
				el.innerHTML = '<a href="javascript:postComment(\'' + reference + '\',\'' + details + '\');">Post a comment</a>'
			} else if (xmlHttp.responseText == 'politicsweb') {
				alert('Please do not post a comment with a name containing \'PoliticsWeb\'.');
				el = document.getElementById('post-' + reference);
				el.innerHTML = '<a href="javascript:postComment(\'' + reference + '\',\'' + details + '\');">Post a comment</a>'
			} else {
				alert(xmlHttp.responseText);
			}
		}
	};
	comment = comment.replace('&','###');
	vars = 'url=' + window.location + '&reference=' + reference + '&details=' + details + '&name=' + name + '&comment=' + comment + '&email=' + email + '&postcode=' + postcode;
	xmlHttp.send(vars);
}


// 4. Show/hide full post

function fullPost(id) {
	document.getElementById('short-post-' + id).style.display = 'none';
	document.getElementById('full-post-' + id).style.display = 'block';
}

function shortPost(id) {
	document.getElementById('full-post-' + id).style.display = 'none';
	document.getElementById('short-post-' + id).style.display = 'block';
}


// 5. Lookup postcode

function lookupPostcode(pc,callback) {
	var s = document.createElement('script');
	var url = 'http://www.theyworkforyou.com/api/getConstituency?key=BXJpE7BgtQM6DXMS84C2eGDn&callback=' + callback + '&postcode=' + pc;
	s.setAttribute('src', url);
	s.setAttribute('type', 'text/javascript');
	document.getElementsByTagName('head')[0].appendChild(s);
}


function pollVote(id) {
	thePollId = id;
	if (document.getElementById('check_constituency').value != '') {
		lookupPostcode(document.getElementById('postcode').value,'pollConstituencyReceived');
	} else {
		doPollVote();
	}
}

function pollConstituencyReceived(r) {
	if (r.name == document.getElementById('check_constituency').value) {
		doPollVote();
	} else {
		alert('Sorry, we can only accept votes from postcodes in the ' + document.getElementById('check_constituency').value + ' constituency');
	}
}

function doPollVote() {
	id = thePollId;
	document.getElementById('btn-vote').disabled = true;
	vote = -1;
	button = document.poll.choice;
	for (i=0; i < button.length; i++) {
		if (button[i].checked) vote = i+1;
	}
	email = document.getElementById('email').value;
	address = document.getElementById('address').value;
	if (vote == -1 || email == '' || address == '')  {
		alert('Please choose an option and fill out both fields');
		document.getElementById('btn-vote').disabled = false;
		return false;
	}
	createAjax("/calls/module.php?x=poll");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			document.getElementById("poll").innerHTML = xmlHttp.responseText;
		}
	};
	vars = 'id=' + id + '&vote=' + vote + '&email=' + email + '&address=' + address;
	xmlHttp.send(vars);
}

function seeOlder(content,show,more) {
	createAjax("/calls/module.php?x=blog");
	more = more-10;
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			document.getElementById('see-older').innerHTML = xmlHttp.responseText;
			document.getElementById('see-older-link').href = 'javascript:seeOlder(' + content + ',' + newshow + ',' + more + ');';
			document.getElementById('see-older-link').innerHTML = 'See older posts (' + more + ' more)';
			document.getElementById('see-older-link').style.display = '';
			document.getElementById('see-older-loading').style.display = '';
		}
	};
	xmlHttp.send('content=' + content + '&show=' + show);
	newshow = show+10;
	document.getElementById('see-older-link').style.display = 'none';
	document.getElementById('see-older-loading').style.display = 'block';
}

function sendUserMap() {
	createAjax("/calls/module.php?x=map");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText == 'success') {
				window.location.reload();
			} else if (xmlHttp.responseText == 'approval') {
				alert('Your map post has been received and saved, and will appear on the website shortly subject to approval.');
				window.location.reload();
			} else {
				alert(xmlHttp.responseText);
			}
		}
	};
	els = document.getElementById('add-issue').elements;
	vars = '';
	for (var i = 0; i < els.length; i++) {
		if (els[i].name != '') vars += els[i].name + "=" + els[i].value + '&';
	}
	vars = vars.slice(0,(vars.length-1)); 
	xmlHttp.send(vars);
}

function deleteMapPost(id) {
	createAjax("/calls/module.php?x=map");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText == 'success') {
				window.location.reload();
			}
		}
	};
	vars = 'delete=' + id;
	xmlHttp.send(vars);
}

function setIcons() {
	icons = new Array();
	icons['blue-marker'] = new GIcon();
	icons['blue-marker'].image = "/images/marker.png";
	icons['blue-marker'].iconSize = new GSize(20, 34);
	icons['blue-marker'].iconAnchor = new GPoint(9, 34);
	icons['red-exclamation'] = new GIcon();
	icons['red-exclamation'].image = "/images/exclamation.png";
	icons['red-exclamation'].iconSize = new GSize(32, 32);
	icons['red-exclamation'].iconAnchor = new GPoint(9, 34);
}

function addMarker() {
	window.onmousemove = mouseFollower;
	document.getElementById("add-marker-img").style.opacity = '0.3';
	document.getElementById("add-marker-img").style.filter = 'alpha(opacity=30)';
	img = new Image();
	img.src = '/images/marker-ticked.png';
	GEvent.addListener(gmap, "click", function(overlay, latlng) {
		if (document.getElementById("location").value == '') {
			markericon = icons['blue-marker'];
			mymarker = new GMarker(latlng,markericon);
			gmap.addOverlay(mymarker);
			document.getElementById('mouse-follower').style.display = '';
			window.onmousemove = '';
			document.getElementById("location").value = latlng;
			document.getElementById("add-marker-img").onclick = '';
			document.getElementById("add-marker-img").src = '/images/marker-ticked.png';
			document.getElementById("add-marker-img").style.opacity = '';
			document.getElementById("add-marker-img").style.filter = '';
		}
	});
}

function addMarkerAdmin() {
	div = document.getElementById('map');
	div.style.position = 'absolute';
	div.style.top = '0';
	div.style.left = '0';
	div.style.width = '100%';
	div.style.height = '100%';
	div.style.zIndex = '8';
	document.getElementById('icon').style.visibility = 'hidden';
	load();
	window.onmousemove = mouseFollower;
	document.getElementById("add-marker-img").style.opacity = '0.3';
	document.getElementById("add-marker-img").style.filter = 'alpha(opacity=30)';
	img = new Image();
	img.src = '/images/marker-ticked.png';
	GEvent.addListener(gmap, "click", function(overlay, latlng) {
		if (document.getElementById("location").value == '') {
			markericon = icons['blue-marker'];
			mymarker = new GMarker(latlng,markericon);
			gmap.addOverlay(mymarker);
			document.getElementById('mouse-follower').style.display = '';
			window.onmousemove = '';
			document.getElementById("location").value = latlng;
			document.getElementById("add-marker-img").onclick = '';
			document.getElementById("add-marker-img").src = '/images/marker-ticked.png';
			document.getElementById("add-marker-img").style.opacity = '';
			document.getElementById("add-marker-img").style.filter = '';
			setTimeout("document.getElementById('map').style.display = 'none';",500);
			setTimeout("document.getElementById('icon').style.visibility = '';",500);
		}
	});
}


function createMarker(lat,lng,id,icon) {
	latlng = new GLatLng(lat,lng);
	markericon = icons[icon];
	mymarker = new GMarker(latlng,markericon);
	GEvent.addListener(mymarker, "click", function() {
		window.location.href = '#' + id;
		document.getElementById('map-post-' + id).style.backgroundColor = '#fffde5';
		setTimeout("document.getElementById('map-post-" + id + "').style.backgroundColor = ''",800);
	});
	gmap.addOverlay(mymarker);
}

function mouseFollower(e) {
	document.getElementById('mouse-follower').style.display = 'block';
	img = document.getElementById('mouse-follower');
	var mouseX = 0;
	var mouseY = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		mouseX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		mouseY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	mouseX -= 10;
	mouseY -= 34;
	img.style.left = mouseX + 'px';
	img.style.top = mouseY + 'px';
}



function newGroup() {
	loadPopup('<form onsubmit="doNewGroup();return false;"><p><strong>Add new group:</strong></p><p>Name:<br /><input type="text" id="group-name" /></p><p>Emails (put each one on a new line):<br /><textarea id="group-emails" style="width:300px;height:300px;"></textarea></p><p><input type="submit" value="Create" /></p></form>');
	stylePopup(340,480);
	document.getElementById('group-name').focus();
}

function doNewGroup() {
	newGroup = document.getElementById('group-name').value;
	emails = document.getElementById('group-emails').value;
	loadPopup('<p id="loading"><img src="http://files.politicsweb.co.uk/loading.gif" /></p>');
	stylePopup(50,50);
	createAjax("/calls/module.php?x=scheduler");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			closePopup();
			window.location.reload();
		}
	};
	xmlHttp.send('new-group=' + newGroup + '&emails=' + emails);
}

function editGroup(id,name,emails) {
	loadPopup('<form onsubmit="doEditGroup(\'' + id + '\');return false;"><p><strong>Add new group:</strong></p><p>Name:<br /><input type="text" id="group-name" value="' + name + '" /></p><p>Emails (put each one on a new line):<br /><textarea id="group-emails" style="width:300px;height:300px;">' + emails.replace(/,/g,'\n') + '</textarea></p><p><input type="submit" value="Update" /></p></form>');
	stylePopup(340,480);
	document.getElementById('group-name').focus();
}

function doEditGroup(id) {
	name = document.getElementById('group-name').value;
	emails = document.getElementById('group-emails').value;
	loadPopup('<p id="loading"><img src="http://files.politicsweb.co.uk/loading.gif" /></p>');
	stylePopup(50,50);
	createAjax("/calls/module.php?x=scheduler");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			closePopup();
			window.location.reload();
		}
	};
	xmlHttp.send('edit-group=' + id + '&name=' + name + '&emails=' + emails);
}

priceAdult = new Array();
priceConcession = new Array();
priceChild = new Array();

function buyTickets(id) {
	document.getElementById('choose-tickets-'+id).style.display = 'block';
	document.getElementById('total-price-'+id).style.display = 'block';
	document.getElementById('buy-button-'+id).style.display = 'block';
}

function addTicket(id,type) {
	document.getElementById('tickets_'+type+'_'+id).innerHTML++;
	recalculateTotal(id);
}

function subtractTicket(id,type) {
	if (document.getElementById('tickets_'+type+'_'+id).innerHTML > 0) {
		document.getElementById('tickets_'+type+'_'+id).innerHTML--;
		recalculateTotal(id);
	}
}

function recalculateTotal(id) {
	totalPrice = document.getElementById('tickets_adult_'+id).innerHTML * priceAdult[id];
	if (priceChild[id] != 0) totalPrice += document.getElementById('tickets_child_'+id).innerHTML * priceChild[id];
	if (priceConcession[id] != 0) totalPrice += document.getElementById('tickets_concession_'+id).innerHTML * priceConcession[id];
	document.getElementById('total-price-'+id).innerHTML = 'Total: <span>&pound;' + totalPrice + '</span>';	
	
}

function showPayPal(id) {
	document.getElementById('choose-tickets-'+id).style.display = '';
	document.getElementById('buy-button-'+id).style.display = '';
	document.getElementById('events-paypal-form-'+id).style.display = 'block';
	document.getElementById('paypal-amount-'+id).value = totalPrice;
	description = document.getElementById('tickets_adult_'+id).innerHTML + ' adult ticket(s)';
	if (priceChild[id] != 0) description += ', ' + document.getElementById('tickets_child_'+id).innerHTML + ' child ticket(s)';
	if (priceConcession[id] != 0) description += ', ' + document.getElementById('tickets_concession_'+id).innerHTML + ' concession ticket(s)';
	document.getElementById('paypal-description-'+id).value = description;
}

function sendEventsPayPalForm() {
	createAjax("/calls/module.php?x=events");
	els = document.getElementById('events-paypal-form').elements;
	vars = '';
	for (var i = 0; i < els.length; i++) {
		if (els[i].value == '') {
			alert('Please ensure that all fields are filled in');
			return false;
		}
		if (els[i].name != '') vars += els[i].name + "=" + els[i].value + '&';
	}
	vars = vars.slice(0,(vars.length-1)); 
	vars += '&tickets=' + description;
	xmlHttp.send(vars);
	return true;
}

function populateTags() {
	document.getElementById('tags').innerHTML = '';
	tags = document.getElementById('param1').value.split('/');
	for (i=0; i<tags.length; i++) {
		parts = tags[i].split('###');
		if (parts[1] != null) {
			display = parts[0] + ' (' + parts[1] + ')';
		} else {
			display = parts[0];
		}
		moveUp = '';
		if (i != 0) moveUp = '<a href="javascript:moveTagUp(' + i + ');"><img src="http://files.politicsweb.co.uk/move-up.gif" class="move-up" /></a>';
		if (display != '') document.getElementById('tags').innerHTML += '<div>' + display + '<a href="javascript:deleteTag(' + i + ');"><img src="http://files.politicsweb.co.uk/icn-delete.png" class="delete" /></a>' + moveUp + '</div><br />';
	}
}

function deleteTag(num) {
	tags = document.getElementById('param1').value.split('/');
	tags.splice(num,1);
	if (tags[0] == '') tags.splice(0,1);
	tags = tags.join('/');
	document.getElementById('param1').value = tags;
	populateTags();
}

function moveTagUp(num) {
	tags = document.getElementById('param1').value.split('/');
	tag1 = tags[num];
	tag2 = tags[num-1];
	tags[num] = tag2;
	tags[num-1] = tag1;
	tags = tags.join('/');
	document.getElementById('param1').value = tags;
	populateTags();
}

function addTag() {
	loadPopup('<form onsubmit="doAddTag();return false;"><p>Tag:<br /><input type="text" id="add-tag" /></p><p>Label (optional):<br /><input type="text" id="add-tag-label" /></p><p>Position:<br /><select id="add-tag-position"><option value="top">Top of list</option><option value="bottom">Bottom of list</option></select></p><p><input type="submit" value="Add tag" /></p></form>');
	stylePopup(218,220);
	document.getElementById('add-tag').focus();
}

function doAddTag() {
	toAdd = document.getElementById('add-tag').value;
	if (document.getElementById('add-tag-label').value != '') toAdd += '###' + document.getElementById('add-tag-label').value;
	tags = document.getElementById('param1').value.split('/');
	if (document.getElementById('add-tag-position').value == 'top') {
		tags.unshift(toAdd);
	} else if (document.getElementById('add-tag-position').value == 'bottom') {
		tags.push(toAdd);
	}
	if (tags[0] == '') tags.splice(0,1);
	tags = tags.join('/');
	document.getElementById('param1').value = tags;
	populateTags();
	closePopup();
}

function sendPayPalForm() {
	createAjax("/calls/module.php?x=paypal");
	els = document.getElementById('paypal-form').elements;
	vars = '';
	for (i=0;i<els.length;i++) {
		if (els[i].value == '' && els[i].name != 'company' && els[i].name != 'amount') {
			alert('Please ensure that all fields are filled in');
			return false;
		}
		if (els[i].name != '') vars += els[i].name + "=" + els[i].value + '&';
	}
	vars = vars.slice(0,(vars.length-1)); 
	xmlHttp.send(vars);
	document.getElementById('paypal-started').innerHTML = '<p><em>Your information has been successfully saved in our database. Please complete the PayPal transaction that has opened in a new window. Once you have completed this, your transaction will be complete.</em></p>';
	document.getElementById('paypal-started').style.display = 'block';
	document.getElementById('paypal-form').style.visibility = 'hidden';
	return true;
}

function updatePrice(price,discount) {
	document.getElementById('paypal-subscription-price').value = price;
	if (discount) {
		document.getElementById('paypal-item-name').value += ' (' + document.getElementById('discount-description').value + ')';
	} else {
		document.getElementById('paypal-item-name').value = document.getElementById('paypal-item-name').value.replace(' (' + document.getElementById('discount-description').value + ')','');
	}
}

function updateAmount(amount) {
	document.getElementById('paypal-amount').value = amount;
}



function submitForm(formid) {
	theFormId = formid;
	if (typeof(document.getElementById('postcode_lookup')) != 'undefined') {
		els = document.getElementById('form'+formid).elements;
		for (i=0;i<els.length;i++) {
			if (els[i].name == 'field' + document.getElementById('postcode_lookup').value) {
				pc = els[i].value;
			}
		}
		lookupPostcode(pc,'formConstituencyReceived');
	} else {
		doSubmitForm();
	}
}

function formConstituencyReceived(r) {
	document.getElementById('postcode_lookup').value = r.name;
	doSubmitForm();
}

function doSubmitForm() {
	formid = theFormId;
	createAjax("/calls/module.php?x=form");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText == 'security') {
				alert('Sorry, you filled the security form out incorrectly. Please check your answer and try again.');
			} else {
				thisForm.innerHTML = xmlHttp.responseText;
			}
		}
	};
	thisForm = document.getElementById('form'+formid);
	els = thisForm.elements;
	fields = [];
	for (i=0;i<els.length;i++) {
		if (els[i].name != 'postcode_lookup') {
			if (els[i].type == 'checkbox' || els[i].type == 'radio') {
				if (els[i].checked) {
					existed = false;
					for (j=0;j<fields.length;j++) {
						if (fields[j][0] == els[i].name) {
							value = fields[j][1];
							value = value.slice(0,(value.length-2)); 
							value += ',' + els[i].value + '}}';
							fields[j][1] = value;
							existed = true;
						}
					}
					if (!existed) {
						fields.push([els[i].name,'{{'+els[i].value+'}}']);
					}
				}
			} else {
				fields.push([els[i].name,els[i].value]);
			}
		}
	}
	vars = '';
	for (i=0;i<fields.length;i++) {
		vars += fields[i][0] + '=' + fields[i][1] + '&';
	}
	if (typeof(document.getElementById('postcode_lookup')) != 'undefined') {
		vars += 'postcode_lookup=' + document.getElementById('postcode_lookup').value + '&';
	}
	vars = vars.slice(0,(vars.length-1)); 
	xmlHttp.send(vars);
}

function renameField(fieldName,fieldId) {
	loadPopup('<form onsubmit="doRenameField(\'' + fieldId + '\');return false;"><p>Rename field <strong>' + fieldName + '</strong>:</p><p><input type="text" value="' + fieldName + '" id="rename-field" /></p><p><input type="submit" value="Rename field" /></p></form>');
	stylePopup(220,123);
	document.getElementById('rename-field').select();
}

function doRenameField(fieldId) {
	newName = document.getElementById('rename-field').value;
	if (newName != null) {
		loadPopup('<p id="loading"><img src="http://files.politicsweb.co.uk/loading.gif" /></p>');
		stylePopup(50,50);
		createAjax("/calls/module.php?x=form");
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				closePopup();
				window.location.reload();
			}
		};
		xmlHttp.send('rename-field=' + newName + '&field=' + fieldId);
	}
}

function editOptions(fieldId,fieldName,options) {
	loadPopup('<form onsubmit="doEditOptions(\'' + fieldId + '\');return false;"><p>Edit options for <strong>' + fieldName + '</strong> (put each option on a new line):</p><p><textarea id="edit-options" style="width:350px;height:200px;">' + options.replace(/,/g,'\n') + '</textarea></p><p><input type="submit" value="Update" /></p></form>');
	stylePopup(390,300);
	document.getElementById('edit-options').focus();
}

function doEditOptions(fieldId) {
	newOptions = document.getElementById('edit-options').value;
	loadPopup('<p id="loading"><img src="http://files.politicsweb.co.uk/loading.gif" /></p>');
	stylePopup(50,50);
	createAjax("/calls/module.php?x=form");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			closePopup();
			window.location.reload();
		}
	};
	xmlHttp.send('edit-options=' + newOptions + '&field=' + fieldId);
}

//Settings

userEmailAuthentication = true;
userPostcodeLookup = true;