var xmlHttp;
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0;
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0;
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0;
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0;

function GetXmlHttpObject(handler)
{
	var objXmlHttp = null;    //Holds the local xmlHTTP object instance

	//Depending on the browser, try to create the xmlHttp object
	if (is_ie)
	{
		//The object to create depends on version of IE
		//If it isn't ie5, then default to the Msxml2.XMLHTTP object
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';

		//Attempt to create the object
		try
		{
			objXmlHttp = new ActiveXObject(strObjName);
			objXmlHttp.onreadystatechange = handler;
		}
		catch(e)
		{
			//Object creation errored
			alert('It would seem that your browser does not support AJAX. As such, some features of this website may be unusable. Contact an administrator for assistance.');
			return false;
		}
	}
	else
	{
		// Mozilla | Netscape | Safari
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload = handler;
		objXmlHttp.onerror = handler;
	}

	//Return the instantiated object
	return objXmlHttp;
}

function xmlHttp_get(xmlHttp, url)
{
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}

function makeCurrentHomepageRevision(postID)
{
	if (confirm('Are you sure you want to make this homepage version current? This will replace the currently visible homepage.'))
	{
		window.location = 'homepage.php?mode=makecurrent&postid=' + postID;
	}
}

function deleteHomepageRevision(postID)
{
	if (confirm('Are you sure you want to delete this homepage version? This cannot be undone.'))
	{
		window.location = 'homepage.php?mode=delete&postid=' + postID;
	}
}

function makeCurrentWhoswhoPageRevision(postID)
{
	if (confirm('Are you sure you want to make this who\'s who page version current? This will replace the currently visible who\'s who page.'))
	{
		window.location = 'whoswho.php?mode=makecurrent&postid=' + postID;
	}
}

function deleteWhoswhoPageRevision(postID)
{
	if (confirm('Are you sure you want to delete this contacts page version? This cannot be undone.'))
	{
		window.location = 'whoswho.php?mode=delete&postid=' + postID;
	}
}

function makeCurrentContactsPageRevision(postID)
{
	if (confirm('Are you sure you want to make this contacts page version current? This will replace the currently visible contacts page.'))
	{
		window.location = 'contacts.php?mode=makecurrent&postid=' + postID;
	}
}

function deleteContactsPageRevision(postID)
{
	if (confirm('Are you sure you want to delete this contacts page version? This cannot be undone.'))
	{
		window.location = 'contacts.php?mode=delete&postid=' + postID;
	}
}

function makeCurrentHistoryPageRevision(postID)
{
	if (confirm('Are you sure you want to make this history page version current? This will replace the currently visible history page.'))
	{
		window.location = 'history.php?mode=makecurrent&postid=' + postID;
	}
}

function deleteHistoryPageRevision(postID)
{
	if (confirm('Are you sure you want to delete this history page version? This cannot be undone.'))
	{
		window.location = 'history.php?mode=delete&postid=' + postID;
	}
}

function deleteEvent(postID)
{
	if (confirm('Are you sure you want to delete this event? This cannot be undone.'))
	{
		window.location = 'events.php?mode=delete&postid=' + postID;
	}
}

function deleteUpdate(updateID)
{
	if (confirm('Are you sure you want to delete this update? This cannot be undone.'))
	{
		window.location = 'events.php?mode=deleteupdate&updateid=' + updateID;
	}
}

function validateEventForm()
{
	if (document.getElementById('title').value == '')
	{
		alert('Please enter a title and try again.');
	}
	else
	{
		document.postform.submit();
	}
}

function makeEventVisible(eventID)
{
	if (confirm('Are you sure you want to make this version visible? This will replace the currently visible event details.'))
	{
		window.location = 'events.php?mode=makevisible&eventid=' + eventID;
	}
}

function makeUpdateVisible(updateID, eventID)
{
	if (confirm('Are you sure you want to make this update visible? This will replace the currently visible event details.'))
	{
		window.location = 'events.php?mode=makeupdatevisible&updateid=' + updateID + '&eventid=' + eventID;
	}
}

function previewContactsRevision()
{
	document.getElementById('postform').action = '../Contact.php?mode=preview';
	document.getElementById('postform').submit();
}

function previewHistoryRevision()
{
	document.getElementById('postform').action = '../History.php?mode=preview';
	document.getElementById('postform').submit();
}

function previewHomepageRevision()
{
	document.getElementById('postform').action = '../index.php?mode=preview';
	document.getElementById('postform').submit();
}

function previewWhoswhoRevision()
{
	document.getElementById('postform').action = '../Who%27s%20who.php?mode=preview';
	document.getElementById('postform').submit();
}

function previewEvent()
{
	document.getElementById('postform').action = '../Events.php?mode=preview';
	document.getElementById('postform').submit();
}

function deleteUser(userID)
{
	if (confirm('Are you sure you want to delete this administrator? This cannot be undone.'))
	{
		window.location = 'administrators.php?mode=delete&userid=' + userID;
	}
}

var imageBoxVisible = false;
function loadAddImageBox(albumID)
{
	var url = '?mode=add&albumid=' + albumID;
	xmlHttp = GetXmlHttpObject(loadAddImageBoxHandler);
	xmlHttp_get(xmlHttp, url);
}

function loadAddImageBoxHandler()
{
	if (xmlHttp.readyState == 4)
	{
		document.getElementById('addimagebox').innerHTML = xmlHttp.responseText;
		document.getElementById('contentsurround').disabled = true;
		
		if (!is_ie && !is_ie5)
		{
			Effect.Fade('contentsurround');
		}
		
		Effect.Appear('addimagebox');
		imageBoxVisible = true;
	}
}

function closeAddImageBox()
{
	Effect.Fade('addimagebox');
	document.getElementById('contentsurround').disabled = false;
	
	if (!is_ie && !is_ie5)
	{
		Effect.Appear('contentsurround');
	}
	imageBoxVisible = false;
}

function loadEditImageBox(imageID)
{
	var url = '?mode=edit&imageid=' + imageID;
	xmlHttp = GetXmlHttpObject(loadEditImageBoxHandler);
	xmlHttp_get(xmlHttp, url);
}

function loadEditImageBoxHandler()
{
	if (xmlHttp.readyState == 4)
	{
		document.getElementById('addimagebox').innerHTML = xmlHttp.responseText;
		document.getElementById('contentsurround').disabled = true;
		
		if (!is_ie && !is_ie5)
		{
			Effect.Fade('contentsurround');
		}
		
		Effect.Appear('addimagebox');
		imageBoxVisible = true;
	}
}

function deleteAlbum(albumID)
{
	if (confirm('Are you sure you want to delete this album? This cannot be undone.'))
	{
		window.location = 'gallery.php?mode=deletealbum&albumid=' + albumID;
	}
}

function validateAddImageForm()
{
	if (document.getElementById('file').value == '')
	{
		alert('Please make sure you have selected a file for upload, and try again.');
	}
	else
	{
		document.addimageform.submit();
	}
}

function makeAlbumImage(albumID, imageID)
{
	if (confirm('Are you sure you want to make this the current album image? This will replace the previous image (if any).'))
	{
		window.location = 'gallery.php?mode=makealbumimage&albumid=' + albumID + '&imageid=' + imageID;
	}
}

function makeAlbumVisible(albumID)
{
	if (confirm('Are you sure you want to make this album visible? It will be displayed on the live website.'))
	{
		window.location = 'gallery.php?mode=makevisible&albumid=' + albumID;
	}
}

function makeAlbumInvisible(albumID)
{
	if (confirm('Are you sure you want to make this album invisible? It will be removed from the live website.'))
	{
		window.location = 'gallery.php?mode=makeinvisible&albumid=' + albumID;
	}
}

function viewImage(imageID)
{
	window.open('Pictures.php?mode=viewimage&imageid=' + imageID, 'imagewindow_' + imageID, 'scrollbars=yes,resizable=yes');
}

function previewAlbum(albumID)
{
	window.location = '../Pictures.php?mode=previewalbum&albumid=' + albumID;
}

function validateChangePasswordForm()
{
	if ((document.getElementById('password').value != document.getElementById('confirmpassword').value) || document.getElementById('password').value == '' || document.getElementById('confirmpassword').value == '')
	{
		alert('The passwords you have entered do not match. Please re-enter them, ensuring they match, and try again.');
	}
	else
	{
		document.changepasswordform.submit();
	}
}

function validateAddUserForm()
{
	if ((document.getElementById('password').value != document.getElementById('confirmpassword').value) || document.getElementById('password').value == '' || document.getElementById('confirmpassword').value == '')
	{
		alert('The passwords you have entered do not match. Please re-enter them, ensuring they match, and try again.');
	}
	else
	{
		if (document.getElementById('loginname').value == '')
		{
			alert('You have not entered a login name. Without one, the user will not be able to log in. Please enter one and try again.');
		}
		else
		{
			document.adduserform.submit();
		}
	}
}