// set up ajax
function ajaxOn() {
	xmlHTTP = false;
	if (window.XMLHttpRequest) { xmlHTTP = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP"); }
	else { return false; }
}

// open certain links in a new window without using "target"
function newWinLinks() {
	var links = document.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) {
		if (!links[i].href.match(window.location.hostname) && !links[i].href.match("javascript:")) {
			links[i].onclick = function() {
				window.open(this.href);
				return false;
            		};
		}
	}
}

// toggle alerts
function toggleAlerts()
{
	$.getJSON("ajax.php?method=togglealerts", function(json) { $("#alertSuccess").html(json.info); $("#alertSuccess").fadeOut(3000); });
}

// delete a comment
function deleteComment(commentId) {
	if (confirm("Are you sure you want to delete this comment?")) {
		$.get("ajax.php?method=deletecomment&comment_id="+commentId);
		$("#comment"+commentId).fadeOut(1500);
	}
}

// delete a post
function deletePost(postId) {
	if (confirm("Are you sure you want to delete this post?")) {
		$.get("ajax.php?method=deletepost&post_id="+postId);
		$("#post"+postId).fadeOut(1500);
	}
}

// change a password
function changePassword(form)
{
	ajaxOn();
	changePasswordForm = form;
	var oldPassword = changePasswordForm.oldpassword.value;
	var password1 = changePasswordForm.newpassword1.value;
	var password2 = changePasswordForm.newpassword2.value;
	var userid = changePasswordForm.userid.value;
	if (password1 == password2 && password1.length > 5 && oldPassword.length > 0)
	{
		params = "&user_id=" + userid + "&oldpassword=" + oldPassword + "&newpassword=" + password1;
		xmlHTTP.onreadystatechange = function()
		{
			if (xmlHTTP.readyState == 4)
			{
				var ajaxResponse = eval('('+xmlHTTP.responseText+')');
				document.getElementById('error').innerHTML = ajaxResponse.message;
				if (ajaxResponse.success == 1)
				{
					document.getElementById('error').className = "success";
					setTimeout("document.getElementById('error').className = 'error'",1000);
					setTimeout("toggleDiv('changepassword')",1000);
					setTimeout("changePasswordForm.reset()",1000);
					setTimeout("document.getElementById('error').innerHTML = ''",1000);
				}
			}
		};
		xmlHTTP.open("POST","ajax.php?method=changepassword",true);
		xmlHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHTTP.setRequestHeader("Content-length", params.length);
		xmlHTTP.setRequestHeader("Connection", "close");
		xmlHTTP.send(params);
	}
	else if (password1.length == 0 && password2.length == 0)
	{
		document.getElementById('error').innerHTML = "You did not enter a new password twice.";
	}
	else if (password1 != password2)
	{
		document.getElementById('error').innerHTML = "The new passwords you entered don't match.";
	}
	else if (password1.length < 6)
	{
		document.getElementById('error').innerHTML = "The new password is not long enough.";
	}
}

// ajax upload functions
function startAjaxUpload(form)
{
	ajaxUploadForm = form;
	document.getElementById('ajaxUploadIframe').innerHTML = '<iframe name="ajaxUploadTarget" src="" style="display: none"></iframe>';
	form.target = 'ajaxUploadTarget';
}

function finishAjaxUpload()
{
	ajaxUploadForm.reset();
	document.getElementById('ajaxUploadIframe').innerHTML = '';
}

function keepSessionAlive() {
	ajaxOn();
	xmlHTTP.open("GET","ajax.php",true);
	xmlHTTP.send(null);
}	
setInterval("keepSessionAlive()",300000);

$(document).ready(function() {
	newWinLinks();
});
