/** 
 * Prompts the user as to whether he really wants to log out.
 */
function confirmLogout () {
	if (!confirm('Are you sure you want to log out?')) {
		return false;
	}
}

/** 
 * Hides default text onfocus.
 */
function hideText (field, strValue) {
	if (field.value == strValue) {
		field.value = '';
	}
}

/** 
 * Re-shows default text onblur.
 */
function showText (field, strValue) {
	if (field.value == '') {
		field.value = strValue;
	}
}

/**
 * Displays the date where the user is.
 */ 
function showDate () {
	var datToday = new Date();
	var strMonth = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");
	document.write(strMonth[datToday.getMonth()] + " ");
	document.write(datToday.getDate() + ", ");
	document.write(datToday.getFullYear());
}

/** 
 * Validates any given form.
 */
function validateForm (objForm) {  
	var arrEls = objForm.elements;
	var strErrorMsg = '';
	if (arrEls['username'] != null && (arrEls['username'].value == '' || arrEls['username'].value == 'Username')) {
		strErrorMsg += '* Please enter your username in the "Username" field of the form.\n';
	}
	if (arrEls['password'] != null && (arrEls['password'].value == '' || arrEls['password'].value == 'Password')) {
		strErrorMsg += '* Please enter your password in the "Password" field of the form.\n';
	}
	if (arrEls['newpassword'] != null && arrEls['newpassword'].value == '') {
		strErrorMsg += '* Please enter your new password in the "New Password" field of the form.\n';
	}
	if (arrEls['password-conf'] != null && arrEls['password-conf'].value == '') {
		strErrorMsg += '* Please enter your password in the "Confirm Password" field of the form.\n';
	}
	if (arrEls['newpassword-conf'] != null && arrEls['newpassword-conf'].value == '') {
		strErrorMsg += '* Please enter your new password in the "Confirm New Password" field of the form.\n';
	}
	if (arrEls['newpassword'] != null && arrEls['newpassword-conf'] != null && arrEls['newpassword'].value != arrEls['newpassword-conf'].value) {
		strErrorMsg += '* Please make sure your new and confirm new passwords are identical.\n';
	}
	if (arrEls['password'] != null && arrEls['password-conf'] != null && arrEls['password'].value != arrEls['password-conf'].value) {
		strErrorMsg += '* Please make sure your password and confirm new password are identical.\n';
	}
	if (arrEls['searchterm'] != null && arrEls['searchterm'].value == '') {
		strErrorMsg += '* Please enter a search term in the "Search Term" field of the form.\n';
	}
	if (arrEls['name'] != null && arrEls['name'].value == '') {
		strErrorMsg += '* Please enter your name in the "Your Name" field of the form.\n';
	}
	if (arrEls['first_name'] != null && arrEls['first_name'].value == '') {
		strErrorMsg += '* Please enter your first name in the "Your First Name" field of the form.\n';
	}
	if (arrEls['last_name'] != null && arrEls['last_name'].value == '') {
		strErrorMsg += '* Please enter your last name in the "Your Last Name" field of the form.\n';
	}
	if (arrEls['email'] != null && (arrEls['email'].value == '' || !checkEmail(arrEls['email'].value))) {
		strErrorMsg += '* Please enter a valid email in the "Your Email Address" field of the form.\n';
	}
	if (arrEls['subject'] != null && arrEls['subject'].value == '') {
		strErrorMsg += '* Please enter the subject in the "The Subject" field of the form.\n';
	}
	if (arrEls['comments'] != null && arrEls['comments'].value == '') {
		strErrorMsg += '* Please enter your comments in the "Your Comments" field of the form.\n';
	}
	if (arrEls['author_nick_name'] != null && arrEls['author_nick_name'].value == '') {
		strErrorMsg += '* Please enter your nickname in the "Your Nickname" field of the form.\n';
	}
	if (arrEls['author_email'] != null && (arrEls['author_email'].value == '' || !checkEmail(arrEls['author_email'].value))) {
		strErrorMsg += '* Please enter a valid email in the "Your Email Address" field of the form.\n';
	}
	if (arrEls['author_blurb'] != null && arrEls['author_blurb'].value == '') {
		strErrorMsg += '* Please enter your blurb in the "Your Blurb" field of the form.\n';
	}
	if (arrEls['link_text'] != null && arrEls['link_text'].value == '') {
		strErrorMsg += '* Please enter link text in the "Link Text" field of the form.\n';
	}
	if (arrEls['link_url'] != null && (arrEls['link_url'].value == '' || arrEls['link_url'].value == 'http://')) {
		strErrorMsg += '* Please enter link URL in the "Link URL" field of the form.\n';
	}
	if (arrEls['article_title'] != null && arrEls['article_title'].value == '') {
		strErrorMsg += '* Please enter a title in the "Article Title" field of the form.\n';
	}
	if (arrEls['article_content'] != null && arrEls['article_content'].value == '') {
		strErrorMsg += '* Please enter content in the "Article Content" field of the form.\n';
	}
	if (arrEls['article_content'] != null && (arrEls['article_announced'].checked && !arrEls['article_active'].checked)) {
		strErrorMsg += '* Please make sure that your article has been activated before choosing to announce it.\n';
	}
	if (arrEls['comment_title'] != null && arrEls['comment_title'].value == '') {
		strErrorMsg += '* Please enter a comment title in the "Comment Title" field of the form.\n';
	}
	if (arrEls['comment_content'] != null && arrEls['comment_content'].value == '') {
		strErrorMsg += '* Please enter a comment in the "Comment Content" field of the form.\n';
	}
	
	if (strErrorMsg != '') {
		alert(strErrorMsg);
		return false;
	}
}

/**
 * Validates email address.
 */
function checkEmail (e) {

	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = e.match(emailPat);

	if (matchArray == null) {
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];

	for (i = 0; i < user.length; i++) {
		if (user.charCodeAt(i) > 127) {
			return false;
	   	}
	}
	for (i = 0; i < domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
			return false;
	   	}
	}
	
	if (user.match(userPat) == null) {
		return false;
	}
	
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				return false;
	   		}
		}
		return true;
	}
	
	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	for (i = 0; i < len; i++) {
		if (domArr[i].search(atomPat) == -1) {
			return false;
	  	}
	}
	
	if (checkTLD && domArr[domArr.length - 1].length != 2 && domArr[domArr.length - 1].search(knownDomsPat) == -1) {
		return false;
	}
	
	if (len < 2) {
		return false;
	}
	
	return true;
}