function logIn() {
	if (isBlank($F('f_username')))	{
		alert("Please enter your username or email.");
		$('f_username').focus();
		$('f_username').select();
		return false;
	}
	if (isBlank($F('f_password')))  {
		alert("Please enter your password.");
		$('f_password').focus();
		$('f_password').select();
		return false;
	}
	return true;
}

function signUp() {
	if (isBlank($F('f_username')))	{
		alert("Please enter a username.");
		$('f_username').focus();
		$('f_username').select();
		return false;
	}
	if (!validateUsername($F('f_username'))) {
		$('f_username').focus();
		$('f_username').select();
		return false;
	}
	if ($F('f_username').length < 3) {
		alert("Username is too short.\nIt must be at least 3 characters long.");
		$('f_username').focus();
		$('f_username').select();
		return false;
	}
	if (!checkEmail($F('f_email'))) {
		$('f_email').focus();
		$('f_email').select();
		return false;
	}
	if (isBlank($F('f_password'))) {
		alert("Please enter a password.");
		$('f_password').focus();
		return false;
	}
	if (isBlank($F('f_password2'))) {
		alert("Please confirm your password.");
		$('f_password2').focus();
		return false;
	}
	if ($F('f_password') != $F('f_password2')) {
		alert("The passwords do not match!\nPlease confirm your password.");
		$('f_password2').focus();
		$('f_password2').select();
		return false;
	}
	return true;
}

function signUpFacebook() {
	if (isBlank($F('f_username')))	{
		alert("Please enter a username.");
		$('f_username').focus();
		$('f_username').select();
		return false;
	}
	if (!validateUsername($F('f_username'))) {
		$('f_username').focus();
		$('f_username').select();
		return false;
	}
	if ($F('f_username').length < 3) {
		alert("Username is too short.\nIt must be at least 3 characters long.");
		$('f_username').focus();
		$('f_username').select();
		return false;
	}
	return true;
}

function checkResetPass() {
	if (isBlank($F('f_password'))) {
		alert("Please enter a password.");
		$('f_password').focus();
		return false;
	}
	if (isBlank($F('f_password2'))) {
		alert("Please confirm your password.");
		$('f_password2').focus();
		return false;
	}
	if ($F('f_password') != $F('f_password2')) {
		alert("The passwords do not match!\nPlease confirm your password.");
		$('f_password2').focus();
		$('f_password2').select();
		return false;
	}
	return true;
}

function validateUsername(thisUsername)
{
   if (thisUsername == "")
   {
    return false;
   }
   thisUsername = thisUsername.toLowerCase();
   var Chars = "0123456789abcdefghijklmnopqrstuvwxyz";
   for (var i = 0; i < thisUsername.length; i++)
   {
       if (Chars.indexOf(thisUsername.charAt(i)) == -1)
       {
           alert("Invalid Character(s)\n\nOnly numbers (0-9) and letters (a-z) are allowed.");
           return false;
       }
   }
   return true;
}

function isBlank(str) {
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ") {
			return false;
		}
	}
	return true;
}

function checkTagContent(thisForm) {
	if (thisForm.value == "") {
		thisForm.value = "Labels to define your entry...";
		thisForm.style.color = "#bbbbbb";
	} else if (thisForm.value == "Labels to define your entry...") {
		thisForm.value = "";
		thisForm.style.color = "#000000";
	}	
}

function checkContent(thisForm) {
	var defaultText = "";
	switch (thisForm.id) {
		case "f-group-name": {
			defaultText = "Prayer Group Name...";
			break;
		}
		case "f-group-description": {
			defaultText = "Prayer Group Description...";
			break;
		}
		case "f-journal-entry-tags": {
			defaultText = "Labels to define your entry...";
			break;
		}
	}
	
	if (thisForm.value == "") {
		thisForm.value = defaultText;
		thisForm.style.color = "#bbbbbb";
	} else if (thisForm.value == defaultText) {
		thisForm.value = "";
		thisForm.style.color = "#000000";
	}

}

function checkGroupURL(thisForm) {
	var URLValue = thisForm.value;
	res(thisForm,alphanum);
	$('group-url').innerHTML = thisForm.value;
	if (URLValue.length == 0) {
		$('group-url-status').className = "group-url-blank";
		$('group-url-status').innerHTML = "";
	} else {
		if (URLValue.length > 2) {
			if (URLValue == db_group_url) {
				$('group-url-status').className = "group-url-available";
				$('group-url-status').innerHTML = "This is your current URL";
			} else {
				var url = "http://prayerity.com/groupurlavailable";
				var pars = 'url=' + URLValue;
				var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: processCheckGroupURL});
			}
		} else {
			$('group-url-status').className = "group-url-taken";
			$('group-url-status').innerHTML = "This URL is too short";
		}
	}

}

function processCheckGroupURL(theResponse) {
	if (theResponse.responseText == "true") {
		$('group-url-status').className = "group-url-available";
		$('group-url-status').innerHTML = "This URL is available";
	} else {
		$('group-url-status').className = "group-url-taken";
		$('group-url-status').innerHTML = "This URL is not available";
	}
}

function checkUsername(thisForm) {
	var usernameValue = thisForm.value;
	res(thisForm,alphanum);
	if (usernameValue.length == 0) {
		$('username-status').className = "username-status-blank";
		$('username-status').innerHTML = "";
	} else {
		if (usernameValue.length > 2) {
			var url = "http://prayerity.com/usernameavailable";
			var pars = 'id=' + usernameValue;
			var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: processCheckUsername});
		} else {
			$('username-status').className = "username-status-taken";
			$('username-status').innerHTML = "Username is too short";
		}
	}

}

function processCheckUsername(theResponse) {
	if (theResponse.responseText == "true") {
		$('username-status').className = "username-status-available";
		$('username-status').innerHTML = "Username is available";
	} else {
		$('username-status').className = "username-status-taken";
		$('username-status').innerHTML = "Username is not available";
	}
}

var phone = "()- 0123456789";
var numb = "0123456789";
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var alphanum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

function res(t,v){
  var w = "";
  for (i=0; i < t.value.length; i++) {
    x = t.value.charAt(i);
    if (v.indexOf(x,0) != -1)
    w += x;
  }
  t.value = w;
}

function removeJournalEntry() {
	var questionResponse = confirm('Are you sure you want to remove this journal entry?');
	if (questionResponse) {
		return true;
	} else {
		return false;
	}
}

function deleteJournalEntry() {
	var questionResponse = confirm('Are you sure you want to delete this journal entry?');
	if (questionResponse) {
		return true;
	} else {
		return false;
	}
}

function deleteComment() {
	var questionResponse = confirm('Are you sure you want to delete this comment?');
	if (questionResponse) {
		return true;
	} else {
		return false;
	}
}

function setShareWith(thisRadio) {
	switch (thisRadio) {
		case "share": {
			$('f-journal-entry-privacy-share').checked = true;
		}
	}
}

function checkAccountSettings() {
	if (isBlank($F('f-name')))	{
		alert("Please enter your name.");
		$('f-name').focus();
		$('f-name').select();
		return false;
	}
	if (!checkEmail($F('f-email'))) {
		$('f-email').focus();
		$('f-email').select();
		return false;
	}
	if (!isBlank($F('f-password1')) && !isBlank($F('f-password2'))) {
		if (isBlank($F('f-password1'))) {
			alert("Please enter a password.");
			$('f-password1').focus();
			return false;
		}
		if (isBlank($F('f-password2'))) {
			alert("Please confirm your password.");
			$('f-password2').focus();
			return false;
		}
		if ($F('f-password1') != $F('f-password2')) {
			alert("The passwords do not match!\nPlease confirm your password.");
			$('f-password2').focus();
			$('f-password2').select();
			return false;
		}
	}
	return true;
}

function checkEmail(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);

	if (str.indexOf(at)==-1) {
		alert("Please enter a valid email address.");
		return false;
	}

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr){
		alert("Please enter a valid email address.");
		return false;
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr){
		alert("Please enter a valid email address.");
		return false;
	}

	if (str.indexOf(at,(lat + 1)) != -1){
		alert("Please enter a valid email address.");
		return false;
	}

	if (str.substring(lat - 1,lat) == dot || str.substring(lat + 1, lat + 2) == dot){
		alert("Please enter a valid email address.");
		return false;
	}

	if (str.indexOf(dot, (lat + 2)) == -1){
		alert("Please enter a valid email address.");
		return false;
	}

	if (str.indexOf(" ") != -1){
		alert("Please enter a valid email address.");
		return false;
	}

 	return true;
}

function watch_entry(entry_id) {
	var url = "http://prayerity.com/watchentry";
    var pars = "id=" + entry_id;
    var updateHTML = "<button type=\"button\" class=\"right negative\" onclick=\"javascript:stop_watching('" + entry_id + "');\"><img src=\"http://prayerity.com/images/delete.png\" width=\"16\" height=\"16\" border=\"0\" alt=\"\" /> Stop Watching</button>";
    var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('watch_entry').innerHTML = updateHTML; Cufon.refresh('button'); }});
}

function stop_watching(entry_id) {
	var url = "http://prayerity.com/stopwatching";
    var pars = "id=" + entry_id;
    var updateHTML = "<button type=\"button\" class=\"right\" onclick=\"javascript:watch_entry('" + entry_id + "');\"><img src=\"http://prayerity.com/images/add.png\" width=\"16\" height=\"16\" border=\"0\" alt=\"\" /> Watch This</button>";
    var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('watch_entry').innerHTML = updateHTML; Cufon.refresh('button'); }});
}

function add_friend(friend_id) {
	var url = "http://prayerity.com/addfriend";
    var pars = "id=" + friend_id;
    var updateHTML = "<img src='http://prayerity.com/images/tick.png' width='16' height='16' border='0' class='middle' title='Request Sent' alt='' /> Request Sent";
    var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('add_friend').innerHTML = updateHTML; alert('This user will have to confirm you as their friend.'); }});
}

function add_friend_search(friend_id) {
	var url = "http://prayerity.com/addfriend";
    var pars = "id=" + friend_id;
    var updateHTML = "<img src='http://prayerity.com/images/tick.png' width='16' height='16' border='0' class='middle' title='Request Sent' alt='' /> Request Sent";
    var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('commands_' + friend_id).innerHTML = updateHTML; alert('This user will have to confirm you as their friend.'); }});
}

function confirm_friend(friend_id) {
	var url = "http://prayerity.com/addfriend";
    var pars = "id=" + friend_id;
    var updateHTML = "<img src='http://prayerity.com/images/tick.png' width='16' height='16' border='0' class='middle' title='Friend Confirmed' alt='' /> Confirmed";
    var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('commands_' + friend_id).innerHTML = updateHTML; }});
}

function remove_friend(friend_id) {
	var questionResponse = confirm('Are you sure you want to remove this friend?');
	if (questionResponse) {
		var url = "http://prayerity.com/removefriend";
		var pars = "id=" + friend_id;
		var updateHTML = "<img src='http://prayerity.com/images/cross.png' width='16' height='16' border='0' class='middle' title='Friend Denied' alt='' /> Done";
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('commands_' + friend_id).innerHTML = updateHTML; }});
	}
}

function join_group(group_id, group_name, group_type) {
	var url = "http://prayerity.com/joingroup";
    var pars = "id=" + group_id + "&type=" + group_type;
    var updateHTML = "<img src='http://prayerity.com/images/tick.png' width='16' height='16' border='0' class='middle' title='Request Sent' alt='' /> Request Sent";
    switch (group_type) {
		case "share": {
			var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('join_group').innerHTML = updateHTML; alert('A group admin will have to approve your membership.'); }});
			break;
		}
		case "public": {
			var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { window.document.location = 'http://prayerity.com/group?id=' + group_id; }});
			break;
		}
	}
}

function leave_group(group_id, group_name) {
	var questionResponse = confirm('Are you sure you want to leave the ' + group_name + ' group?');
	if (questionResponse) {
		var url = "http://prayerity.com/leavegroup";
		var pars = "id=" + group_id;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { window.document.location = 'http://prayerity.com/friends'; }});
	}
}

function accept_group(group_id, friend_id) {
	var url = "http://prayerity.com/acceptgroup";
	var pars = "id=" + group_id + "&f_id=" + friend_id;
	var updateHTML = "<img src='http://prayerity.com/images/tick.png' width='16' height='16' border='0' class='middle' title='Membership Accepted' alt='' /> Accepted";
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('commands_' + friend_id).innerHTML = updateHTML; }});
}

function accept_group2(group_id, friend_id, notification_id) {
	var url = "http://prayerity.com/acceptgroup";
	var pars = "id=" + group_id + "&f_id=" + friend_id + "&n_id=" + notification_id;
	var updateHTML = "<img src='http://prayerity.com/images/tick.png' width='16' height='16' border='0' class='middle' title='Membership Accepted' alt='' /> Accepted";
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $(notification_id).innerHTML = updateHTML; }});
}

function deny_group(group_id, friend_id, friend_name) {
	var questionResponse = confirm('Are you sure you want to deny membership for ' + friend_name + '?');
	if (questionResponse) {
		var url = "http://prayerity.com/denygroup";
		var pars = "id=" + group_id + "&f_id=" + friend_id;
		var updateHTML = "<img src='http://prayerity.com/images/cross.png' width='16' height='16' border='0' class='middle' title='Membership Denied' alt='' /> Denied";
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('commands_' + friend_id).innerHTML = updateHTML; }});
	}
}

function dismis_group(group_id, friend_id, notification_id) {
	var questionResponse = confirm('Are you sure you want to dismis this group invitation?');
	if (questionResponse) {
		var url = "http://prayerity.com/denygroup";
		var pars = "id=" + group_id + "&f_id=" + friend_id + "&n_id=" + notification_id;
		var updateHTML = "<img src='http://prayerity.com/images/cross.png' width='16' height='16' border='0' class='middle' title='Membership Dismissed' alt='' /> Dismissed";
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $(notification_id).innerHTML = updateHTML; }});
	}
}

function dismis_notification(notification_id) {
	var url = "http://prayerity.com/dismisnotification";
	var pars = "id=" + notification_id;
	var updateHTML = "";
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('notification_' + notification_id).innerHTML = updateHTML; }});
}

function promote_member(group_id, friend_id, friend_name) {
	var questionResponse = confirm('Are you sure you want to promote ' + friend_name + ' to a group moderator?');
	if (questionResponse) {
		var url = "http://prayerity.com/promotemember";
		var pars = "id=" + group_id + "&f_id=" + friend_id;
		var updateHTML = "<img src='http://prayerity.com/images/tick.png' width='16' height='16' border='0' class='middle' title='Promoted' alt='' /> Promoted";
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('commands_' + friend_id).innerHTML = updateHTML; }});
	}
}

function remove_member(group_id, friend_id, friend_name) {
	var questionResponse = confirm('Are you sure you want to remove ' + friend_name + ' from this group?');
	if (questionResponse) {
		var url = "http://prayerity.com/denygroup";
		var pars = "id=" + group_id + "&f_id=" + friend_id;
		var updateHTML = "<img src='http://prayerity.com/images/cross.png' width='16' height='16' border='0' class='middle' title='Membership Removed' alt='' /> Removed";
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:function() { $('commands_' + friend_id).innerHTML = updateHTML; }});
	}
}
