function  makeRequest(url, handlerFunction, type, method, async, postdata) {
    if (method == undefined || (method != 'GET' && method != 'POST')) method = 'GET';
    if (async == undefined || (async != true && async != false)) async = true;
	var http_request = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/'+type);
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		return false;
	}
	
	http_request.open(method, url, true);
	http_request.onreadystatechange = function() { if (http_request.readyState==4) { handlerFunction(http_request); } };
	http_request.setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT");
	if (postdata != undefined) {
	    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	    http_request.send(postdata);
	} else {
	    http_request.send(null);
	}
	return true;
}

function alertSize() {
	//Based on script from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	var myWidth = 0, 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;
	}
	return Array(myWidth,myHeight);
}

function resizeImage(image,maxWidth,maxHeight) {
	var height=image.height;
	var width=image.width;
	var oWidth = width;
	var oHeight = height;
	if (height > maxHeight || width > maxWidth) {
		//Image is too big
		if ((width/height) > (maxWidth/maxHeight)) {
			//Needs width to be decreased
			width=maxWidth;
			height=Math.round((oHeight/oWidth)*maxWidth);
		} else if (image.width == image.height) {
			//Square
			max = (maxWidth > maxHeight) ? maxHeight : maxWidth;
			width=max;
			height=max;
		} else {
			//Needs height to be decreased
			height=maxHeight;
			width=Math.round((oWidth/oHeight)*maxHeight);
		}
		image.height=height;
		image.width=width;
	}
}

function doResize(img, p) {
	var windowSize = alertSize();
	var xOffset = 300;
	var w = img.width;
	resizeImage(img, windowSize[0]-xOffset, 100000);
	if (w != img.width) {
		//Width has changed
		var h = p.innerHTML;
		p.innerHTML = '<a href="#" onclick="window.open(\''+img.src+'\');return false;">'+h+'</a>';
	}
}


function ReplyToComment(PCommentID, NewCommentCode, DefaultTitle) {
	var parentComment = document.getElementById('comment_div_'+PCommentID);
	var newCommentForm = document.getElementById('comment_div_new');
	
	document.getElementById('replyto').value = PCommentID;
	
	document.getElementById('title').value = DefaultTitle;
	document.getElementById('postyourowncomment').style.display = 'none';
	document.getElementById('makenewthread').style.display = 'block';
	
	newCommentForm.className = 'comment nestedcomment';
	
	parentComment.appendChild(newCommentForm);
	
	var newCommentId = parentComment.getElementsByTagName('div').length;
	document.getElementById('newcommenttitle').innerHTML = NewCommentCode+'.'+newCommentId;
	
	newCommentForm.scrollIntoView(true);
	window.scrollBy(0,-250);
}
function CheckNewCommentPreTitle() {
	if (document.getElementById('allcomments')) {
		var num = 0;
		var children = document.getElementById('allcomments').childNodes;
		for (var i=0; i<children.length; i++) {
			if (children[i].tagName) {
				if (children[i].tagName.toLowerCase() == 'div') {
					num++;
				}
			}
		}
		if (document.getElementById('newcommenttitle')) {
			document.getElementById('newcommenttitle').innerHTML = num;
		}
	}
}
var wol = window.onload;
window.onload = function() { wol; CheckNewCommentPreTitle(); }
function tryAjaxCommentSubmission() {
	var title = encodeURIComponent(document.getElementById('title').value);
	var name = encodeURIComponent(document.getElementById('poster').value);
	var comment = encodeURIComponent(document.getElementById('comment').value);
	var replyTo = encodeURIComponent(document.getElementById('replyto').value);
	var Submit = encodeURIComponent(document.getElementById('Submit').value);
	var postData = "id="+NewsID+"&Submit="+Submit+"&title="+title+"&name="+name+"&comment="+comment+"&replyTo="+replyTo;
	makeRequest('/ajax.php?i=commentsubmit', ajaxCommentSubmitResult, 'html', 'POST', true, postData);
}
function ajaxCommentSubmitResult(hr) {
	if (hr.status == 200) {
		eval("var res = "+hr.responseText+";");
		if (res.newID != '0') {
			//Successful post
			//Make new post
			var title = document.getElementById('title').value;
			var comment = document.getElementById('comment').value;
			var name = document.getElementById('poster').value;
			var replyTo = document.getElementById('replyto').value;
			var pretitle = document.getElementById('newcommenttitle').innerHTML;
			var newCommentForm = document.getElementById('comment_div_new');
			
			var div = document.createElement('div');
				div.className = 'comment'+(replyTo>0 ? ' nestedcomment' : '');
				div.id = 'comment_div_'+res.newID;
				
				var h3 = document.createElement('h3');
					
					var a1 = document.createElement('a');
						a1.href = '/News/'+NewsID+'/#c'+res.newID;
						a1.name = 'c'+res.newID;
						a1.id = 'c'+res.newID;
						a1.title = 'Right-click -> Copy Shortcut to link to this comment';
						
						var a1t = document.createTextNode(pretitle);
						a1.appendChild(a1t);
					
					h3.appendChild(a1);
					var h3t = document.createTextNode(' '+title);
					h3.appendChild(h3t);
					
					var a2 = document.createElement('a');
						a2.href = '#';
						eval("a2.onclick = function() { ReplyToComment('"+res.newID+"','"+pretitle+"','"+((title.substr(0, 3)=='Re:')?'':'Re: ')+title+"'); return false; }");
						a2.className = 'commentreplylink';
						
						var a2t = document.createTextNode('(Reply to this comment)');
						a2.appendChild(a2t);
						
					h3.appendChild(a2);
				div.appendChild(h3);
				
				var p1 = document.createElement('p');
					p1.className = 'commentbody';
					
					var p1t = document.createTextNode(comment);
					p1.appendChild(p1t);
				div.appendChild(p1);
				
				var p2 = document.createElement('p');
					p2.className = 'commentfoot';
					
					var p2t1 = document.createTextNode('Posted by ');
					p2.appendChild(p2t1);
					
					var p2b = document.createElement('b');
						var p2bt = document.createTextNode(name);
						p2b.appendChild(p2bt);
					p2.appendChild(p2b);
					
					var p2t2 = document.createTextNode(' at '+res.date);
					p2.appendChild(p2t2);
				
				div.appendChild(p2);
			newCommentForm.parentNode.appendChild(div);
			
			//Reset the new comment form
			comment = document.getElementById('comment').value = '';
			resetCommentFormPosition();
			
			//Now send the validation query
			makeRequest('/ajax.php?i=commentvalidate&cid='+res.newID+'&validatecode='+res.validatecode, ajaxCommentValidateResult, 'html', 'GET');
		} else {
			//Unsuccessful post
			alert("Unable to submit comment:\n"+res.error);
		}
	}
}
function ajaxCommentValidateResult(hr) {
	if (hr.status == 200) {
		eval("var res = "+hr.responseText+";");
		if (res.error == 0) {
			//Successful validation!
		} else if (res.error == 1) {
			//Invalid code
			alert("The comment validation code sent for your comment submission was incorrect so it will not appear until it is manually verified.");
		} else if (res.error == 2) {
			//Invalid comment
			alert("An unknown error occurred while trying to validate your comment submission.");
		} else if (res.error == 3) {
			//Error setting comment to visible
			alert("An unknown error occurred while trying to validate your comment submission.");
		}
	}
}
function resetCommentFormPosition() {
	var newCommentForm = document.getElementById('comment_div_new');
	var allcomments = document.getElementById('allcomments');
	var postown = document.getElementById('postyourowncomment');
	var makenewthread = document.getElementById('makenewthread');
	newCommentForm.className = 'comment';
	title = document.getElementById('title').value = '';
	replyTo = document.getElementById('replyto').value = '0';
	pretitle = document.getElementById('newcommenttitle').innerHTML = '';
	document.getElementById('postyourowncomment').style.display = 'block';
	document.getElementById('makenewthread').style.display = 'none';
	allcomments.appendChild(postown);
	allcomments.appendChild(makenewthread);
	allcomments.appendChild(newCommentForm);
	CheckNewCommentPreTitle();
}