
// 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 + '\',document.getElementById(\'comment-name\').value,document.getElementById(\'comment\').value,document.getElementById(\'comment-email\').value,\'' + 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:<br /><input type="text" id="comment-email" /><br /><br />';
	} else {
		html += '<input type="hidden" id="comment-email" 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,name,comment,email,details) {
	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;
	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);
}


//Settings

userEmailAuthentication = false;
userPostcodeLookup = true;