//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #
//
//
//  w66q.js
//  JavaScript functions for websmart66.net Discussion Forum Functions
//
//
//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function DeleteQMember( Id ) {
	var IdList = "";
	var DelCt = 0;
	if( Id ) {
		IdList = Id;
		DelCt++;
	} else {
		var Pattern = /^membercb-(\d+)$/i;
		for( var i = 0 ; i < document.forms[0].elements.length ; i++ ) {
			if( document.forms[0].elements[i].name.match( Pattern ) ) {
				if( document.forms[0].elements[i].checked ) {
					IdList += document.forms[0].elements[i].name.match( Pattern )[ 1 ] + ",";
					DelCt++;
				}
			}
		}
	}
	if( ! IdList ) {
		alert( "\n  There are no members selected for deletion.      \n\n" );
		return false;
	} else {
		var ConfMesg;
		if( DelCt == 1 ) {
			ConfMesg = "\n  You're sure you want to delete this member profile?    \n\n";
		} else {
			ConfMesg = "\n  You're sure you want to delete thse member profiles?    \n\n";
		}
		if( confirm( ConfMesg ) ) {
			window.location = "/cgi-bin/d/w66d-q-members-delete.cgi?id=" + IdList
		} else {
			return false;
		}
		return false;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function VFQMember() {
	var Mesg = "";
	if( ! trim( document.member.alias.value ) ) {
		Mesg += "    + Alias is empty      \n";
	}
	if( ! trim( document.member.name.value ) ) {
		Mesg += "    + Name is empty      \n";
	}
	if( ! ValidEmail( document.member.email.value ) ) {
		Mesg += "    + Email is empty or invalid      \n";
	}
	if( Mesg ) {
		alert( "\n  The form cannot be submitted:      \n\n" + Mesg + "\n" );
		return false;
	} else {
		return true;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function QThreadsListAction( Id ) {
	var Action = document.action.action.value;
	var IdList = "";
	var IdCt = 0;
	if( Id ) {
		IdList = Id;
		IdCt++;
	} else {
		var Pattern = /^threadcb-(\d+)$/i;
		for( var i = 0 ; i < document.forms[0].elements.length ; i++ ) {
			if( document.forms[0].elements[i].name.match( Pattern ) ) {
				if( document.forms[0].elements[i].checked ) {
					IdList += document.forms[0].elements[i].name.match( Pattern )[ 1 ] + ",";
					IdCt++;
				}
			}
		}
	}
	if( ! IdList ) {
		alert( "\n  There are no threads selected.      \n\n" );
		return false;
	} else {
		if( Action == "lock" ) {
			window.location = "/cgi-bin/d/w66d-q-threads-lock.cgi?id=" + IdList + "&action=lock";
		} else if( Action == "unlock" ) {
			window.location = "/cgi-bin/d/w66d-q-threads-lock.cgi?id=" + IdList + "&action=unlock";
		} else if( Action == "hide" ) {
			window.location = "/cgi-bin/d/w66d-q-threads-hide.cgi?id=" + IdList + "&action=hide";
		} else if( Action == "unhide" ) {
			window.location = "/cgi-bin/d/w66d-q-threads-hide.cgi?id=" + IdList + "&action=unhide";
		} else if( Action == "public" ) {
			window.location = "/cgi-bin/d/w66d-q-threads-public.cgi?id=" + IdList + "&action=public";
		} else if( Action == "unpublic" ) {
			window.location = "/cgi-bin/d/w66d-q-threads-public.cgi?id=" + IdList + "&action=unpublic";
		} else if( Action == "delete" ) {
			if( confirm( "\n  You're sure you want to permanently delete the selected thread(s)?      \n\n" ) ) {
				window.location = "/cgi-bin/d/w66d-q-threads-delete.cgi?id=" + IdList;
			} else {
				return false;
			}
		}
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function QThreadsEditAction( Id ) {
	var Action = document.action.action.value;
	var ThreadId = document.posts.threadid.value;
	if( ! Action ) {
		alert( "\n  No action selected.    \n\n" );
		return false;
	}
	var IdList = "";
	var IdCt = 0;
	if( Id ) {
		IdList = Id;
		IdCt++;
	} else {
		var Pattern = /^postcb-(\d+)$/i;
		for( var i = 0 ; i < document.forms[0].elements.length ; i++ ) {
			if( document.forms[0].elements[i].name.match( Pattern ) ) {
				if( document.forms[0].elements[i].checked ) {
					IdList += document.forms[0].elements[i].name.match( Pattern )[ 1 ] + ",";
					IdCt++;
				}
			}
		}
	}

	// If this is just for posts, we need to have some of them selected.
	// If none are selected, bail out. Tuck and roll.
	if( Action.match( /^posts/ ) ) {
		if( ! IdList ) {
			alert( "\n  No posts are selected.      \n\n" );
			return false;
		}
	}

	if( Action == "postslock" ) {
		window.location = "/cgi-bin/d/w66d-q-posts-lock.cgi?id=" + IdList + "&action=lock";
	} else if( Action == "postsunlock" ) {
		window.location = "/cgi-bin/d/w66d-q-posts-lock.cgi?id=" + IdList + "&action=unlock";
	} else if( Action == "postshide" ) {
		window.location = "/cgi-bin/d/w66d-q-posts-hide.cgi?id=" + IdList + "&action=hide";
	} else if( Action == "postsunhide" ) {
		window.location = "/cgi-bin/d/w66d-q-posts-hide.cgi?id=" + IdList + "&action=unhide";
	} else if( Action == "postsdelete" ) {
		if( confirm( "\n  You're sure you want to permanently delete the selected post(s)?      \n\n" ) ) {
			window.location = "/cgi-bin/d/w66d-q-posts-delete.cgi?id=" + IdList;
		} else {
			return false;
		}
	} else if( Action == "threadlock" ) {
		window.location = "/cgi-bin/d/w66d-q-threads-lock.cgi?id=" + ThreadId + "&action=lock";
	} else if( Action == "threadunlock" ) {
		window.location = "/cgi-bin/d/w66d-q-threads-lock.cgi?id=" + ThreadId + "&action=unlock";
	} else if( Action == "threadhide" ) {
		window.location = "/cgi-bin/d/w66d-q-threads-hide.cgi?id=" + ThreadId + "&action=hide";
	} else if( Action == "threadunhide" ) {
		window.location = "/cgi-bin/d/w66d-q-threads-hide.cgi?id=" + ThreadId + "&action=unhide";
	} else if( Action == "public" ) {
		window.location = "/cgi-bin/d/w66d-q-threads-public.cgi?id=" + ThreadId + "&action=public";
	} else if( Action == "unpublic" ) {
		window.location = "/cgi-bin/d/w66d-q-threads-public.cgi?id=" + ThreadId + "&action=unpublic";
	} else if( Action == "threaddelete" ) {
		if( confirm( "\n  You're sure you want to permanently delete this thread (including all posts)?      \n\n" ) ) {
			window.location = "/cgi-bin/d/w66d-q-threads-delete.cgi?id=" + ThreadId;
		} else {
			return false;
		}
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function QVFPost() {
	var Mesg = "";
	if( ! trim( document.post.text.value ) ) {
		Mesg += "    + Text is empty      \n";
	}
	if( Mesg ) {
		alert( "\n  The form cannot be submitted:      \n\n" + Mesg + "\n" );
		return false;
	} else {
		return true;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function QDeletePost( ID ) {
	if( confirm( "\n  You're sure you want to permanently delete this post?      \n\n" ) ) {
		window.location = "/cgi-bin/d/w66d-q-posts-delete.cgi?id=" + ID;
	} else {
		return false;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function QResetPassword( MemberId ) {
	if( confirm( "\n  Clicking okay will generate a new password for this user      \n" + "  and send it to them in an email.      \n\n" ) ) {
		window.location = "/cgi-bin/d/w66d-q-members-password-reset.cgi?id=" + MemberId;
	} else {
		return false;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function QMembersPostsAction( MemberId , Id ) {
	var Action = document.action.action.value;
	var IdList = "";
	var IdCt = 0;
	if( Id ) {
		IdList = Id;
		IdCt++;
	} else {
		var Pattern = /^postcb-(\d+)$/i;
		for( var i = 0 ; i < document.forms[0].elements.length ; i++ ) {
			if( document.forms[0].elements[i].name.match( Pattern ) ) {
				if( document.forms[0].elements[i].checked ) {
					IdList += document.forms[0].elements[i].name.match( Pattern )[ 1 ] + ",";
					IdCt++;
				}
			}
		}
	}
	if( ! IdList ) {
		alert( "\n  There are no threads selected.      \n\n" );
		return false;
	} else {
		if( Action == "postshide" ) {
			window.location = "/cgi-bin/d/w66d-q-posts-hide.cgi?id=" + IdList + "&action=hide&mid=" + MemberId;
		} else if( Action == "postsunhide" ) {
			window.location = "/cgi-bin/d/w66d-q-posts-hide.cgi?id=" + IdList + "&action=unhide&mid=" + MemberId;
		} else if( Action == "postsdelete" ) {
			if( confirm( "\n  You're sure you want to permanently delete the selected post(s)?      \n\n" ) ) {
				window.location = "/cgi-bin/d/w66d-q-posts-delete.cgi?id=" + IdList + "&mid=" + MemberId;
			} else {
				return false;
			}
		}
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function QMembersInvite() {
	var Mesg = "";
	if( ! trim( document.members.subject.value ) ) {
		Mesg += "    + Subject line is empty        \n";
	}
	if( ! trim( document.members.body.value ) ) {
		Mesg += "    + Email Message is empty        \n";
	}
	if( trim( document.members.list.value ) == "" ) {
		Mesg += "    + No email addresses are listed.        \n";
	} else {
		var BadEmailAddrs = new Array;
		var EmailAddrs = document.members.list.value.replace( /;/g , "," );
		EmailAddrs = EmailAddrs.replace( /[,;]\s+/g , "," );
		EmailAddrs = EmailAddrs.replace( /\s+[,;]/g , "," );
		EmailAddrs = EmailAddrs.replace( /^,/ , "" );
		EmailAddrs = EmailAddrs.replace( /,$/ , "" );
		var EmailArray = EmailAddrs.split( "," );
		for( var i = 0 ; i < EmailArray.length ; i++ ) {
			if( ! ValidEmail( EmailArray[ i ] ) ) {
				BadEmailAddrs[ BadEmailAddrs.length ] = EmailArray[ i ];
			}
		}
		if( BadEmailAddrs.length > 0 ) {
			if( Mesg ) {
				Mesg += "\n";
			}
			Mesg += "    + The following email addresses in the list are not valid:      \n";
			for( var i = 0 ; i < BadEmailAddrs.length ; i++ ) {
				Mesg += "\n      < " + BadEmailAddrs[ i ] + " >      ";
			}
		}
	}
	if( Mesg ) {
		alert( "\n  The form cannot be submitted yet:        \n\n" + Mesg + "\n" );
		return false;
	} else {
		if( confirm( "\n  The addresses in the list are of a valid format. By clicking OK an email will be sent to each      \n  inviting them to join your discussion forum.      \n\n" ) ) {
			return true;
		}
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function trim( myStr ) {
	myStr = myStr.replace( /^\s+/ , "" );
	myStr = myStr.replace( /\s+$/ , "" );
	return myStr;
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function VFQMemberP() {
	var Mesg = "";
	var GoodEmail = false;
	var e1 = trim( document.signup.email.value );
	var e2 = trim( document.signup.emailv.value );
	if( ! trim( document.signup.name.value ) ) {
		Mesg += "    + Name field is empty        \n";
	}
	if( ValidEmail( e1 ) & ( e1 == e2 ) ) {
		GoodEmail = true;
	}
	if( ! GoodEmail ) {
		Mesg += "    + Email fields must be valid and match exactly      \n";
	}
	if( ! trim( document.signup.alias.value ) ) {
		Mesg += "    + Alias field is empty      \n";
	}
	if( ! document.signup.agree.checked ) {
		Mesg += "    + You must accept the membership terms      \n";
	}
	if( Mesg ) {
		alert( "\n  The form cannot be submitted yet:      \n\n" + Mesg + "\n" );
		return false;
	} else {
		return true;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function VFQSignin() {
	var Mesg = "";
	if( ! ValidEmail( trim( document.signin.email.value ) ) ) {
		Mesg += "  + Email is empty or invalid      \n";
	}
	if( ! trim( document.signin.password.value ) ) {
		Mesg += "  + Password is empty      \n";
	}
	if( Mesg ) {
		alert( "\n  The sign in form cannot be submitted:      \n\n" + Mesg + "\n" );
		return false;
	} else {
		return true;
	}
}



//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function ValidEmail( Email ) {
	if( ( ! Email ) | ( ! Email.match( /^[\w\.-_]+@[^\.][\w\.]+$/ ) ) | ( ! Email.match( /\.[a-zA-Z]{2,}$/ ) ) ) {
		return false;
	} else {
		return true;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function VFQReply() {
	if( ! trim( document.reply.text.value ) ) {
		alert( "\n  The post cannot be submitted without anything in the \"reply\" field.      \n\n" );
		return false;
	} else {
		return true;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function VFQPost() {
	var Mesg = "";
	if( ! trim( document.post.title.value ) ) {
		Mesg += "  + The headline is empty      \n";
	}
	if( ! trim( document.post.text.value ) ) {
		Mesg += "  + The post is empty      \n";
	}
	if( Mesg ) {
		alert( "\n  The post cannot be submitted:      \n\n" + Mesg + "\n" );
		return false;
	} else {
		return true;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function VFQSettings() {
	var Mesg = "";
	var Email = trim( document.settings.email.value.toLowerCase() );
	var Emailv = trim( document.settings.emailv.value.toLowerCase() );
	var Password = document.settings.password.value.toLowerCase();
	var Passwordv = document.settings.passwordv.value.toLowerCase();
	if( ! document.settings.name.value ) {
		Mesg += "    + Name field is empty      \n";
	}
	if( ! document.settings.alias.value ) {
		Mesg += "    + Alias field is empty      \n";
	}
	if( Email != Emailv ) {
		Mesg += "    + Both Email fields must match exactly      \n";
	} else {
		if( ! ValidEmail( Email ) ) {
			Mesg += "    + Email must be a valid email pattern      \n";
		}
	}
	if( Password && Passwordv && ( Password == Passwordv ) ) {
		// Good!
	} else {
		Mesg += "    + Both Password forms must be valid and match exactly      \n";
	}
	if( Mesg ) {
		alert( "\n  The form cannot be submitted:      \n\n" + Mesg + "\n" );
		return false;
	} else {
		return true;
	}
}



//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #
//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #

