

$(document).ready(function() {
	
	// if we're on a page with a join form get the checkboxes right
	if ($("#joinPermissionName").size()) joinPermissionName();
	
	$('#articleShortDesc').keyup(function(){
		limitChars('articleShortDesc', 200, 'articleShortDescLimit');
	});
	if ($('#articleShortDesc').length) limitChars('articleShortDesc', 200, 'articleShortDescLimit');

	$('#joinDescription').keyup(function(){
		limitChars('joinDescription', 200, 'joinDescriptionLimit');
	});
	if ($('#joinDescription').length) limitChars('joinDescription', 200, 'joinDescriptionLimit');


	
	// function for adding and removing fields for the event booking form
	$('#btnNumbers').click(function() {
		// start with the fields for members
		// first remove existing fields, if required
		var currentNum	= $('.clonedInputMember').length;	// how many "duplicatable" input fields we currently have
		var numMembers = $('#numMembers').attr('value');
		for (newNum=currentNum; newNum>=numMembers; newNum=newNum-1) {		
			$('#member' + newNum).remove();		
		}
		// now add in the required number of fields, if any
		if (numMembers > 0) {
			$('#bookingMemberDetails').css('display','block');
			for (newNum=currentNum; newNum<numMembers; newNum=newNum+1) {		
				var html = "<div id='member" + newNum + "' class='clonedInputMember'> <p><label class='labelBookingName' for='memberName" + newNum + "'>Name:</label> <input type='text' class='memberName inputRequired' name='memberName[]' id='memberName" + newNum + "' /> <label class='labelBookingCompany' for='memberCompany" + newNum + "'>Company:</label> <input type='text' class='memberCompany' name='memberCompany[]' id='memberCompany" + newNum + "' /></p> </div>";
				$('#bookingMemberDetails').append(html);
			}
		} else {
			$('#bookingMemberDetails').css('display','none');
		}

		// now the fields for non-members
		var currentNum	= $('.clonedInputNonMember').length;	// how many "duplicatable" input fields we currently have
		var numNonMembers = $('#numNonMembers').attr('value');
		for (newNum=currentNum; newNum>=numNonMembers; newNum=newNum-1) {		
			$('#nonMember' + newNum).remove();		
		}
		// now add in the required number of fields, if any
		if (numNonMembers > 0) {
			$('#bookingNonMemberDetails').css('display','block');
			for (newNum=currentNum; newNum<numNonMembers; newNum=newNum+1) {		
				var html = "<div id='nonMember" + newNum + "' class='clonedInputNonMember'> <p><label class='labelBookingName' for='nonMemberName" + newNum + "'>Name:</label> <input type='text' class='nonMemberName inputRequired' name='nonMemberName[]' id='nonMemberName" + newNum + "' /> <label class='labelBookingCompany' for='nonMemberCompany" + newNum + "'>Company:</label> <input type='text' class='nonMemberCompany' name='nonMemberCompany[]' id='nonMemberCompany" + newNum + "' /></p> </div>";
				$('#bookingNonMemberDetails').append(html);
			}
		} else {
			$('#bookingNonMemberDetails').css('display','none');
		}

		// finally show the value and the part of the form for the booker to enter their details
		if ((numMembers == 0) && (numNonMembers == 0)) {
			$('#bookingDetails').css('display','none');
			$('#bookingPersonDetails').css('display','none');
			$('#bookingValue').css('display','none');
		} else {
			$('#bookingPersonDetails').css('display','block');
			$('#bookingValue').css('display','block');
			var costMembers = $('#costMembers').html();
			var costNonMembers = $('#costNonMembers').html();
			var bookingTotalCost = (numMembers * costMembers);
			if (costNonMembers) bookingTotalCost = bookingTotalCost + (numNonMembers * costNonMembers);
			$('#totalOrderValue').val(bookingTotalCost);
			bookingTotalCost = '&pound;' + bookingTotalCost.toFixed(2);
			$('#bookingTotalCost').html(bookingTotalCost);
			$('#bookingDetails').css('display','block');
		}
	});

	
	// checking the contents of the form before submitting	
	$('#btnDetails').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#bookingErrorMessage').css('display','none');

		var total = $('.memberName').length;
		for (num=0; num<total; num=num+1) {	
			var memberName = $('#memberName' + num).val();
			memberName = memberName.replace(/^\s+|\s+$/g, ''); //strip whitespace
			if (memberName == "") {
				fail = 1;
				$('#memberName' + num).removeClass('inputNormal');
				$('#memberName' + num).addClass('inputHighlight');
			} else {
				$('#memberName' + num).removeClass('inputHighlight');
				$('#memberName' + num).addClass('inputNormal');
			}
		}
		var total = $('.nonMemberName').length;
		for (num=0; num<total; num=num+1) {	
			var nonMemberName = $('#nonMemberName' + num).val();
			nonMemberName = nonMemberName.replace(/^\s+|\s+$/g, ''); //strip whitespace
			if (nonMemberName == "") {
				fail = 1;
				$('#nonMemberName' + num).removeClass('inputNormal');
				$('#nonMemberName' + num).addClass('inputHighlight');
			} else {
				$('#nonMemberName' + num).removeClass('inputHighlight');
				$('#nonMemberName' + num).addClass('inputNormal');
			}
		}
		var bookingPersonName = $('#bookingPersonName').val();
		bookingPersonName = bookingPersonName.replace(/^\s+|\s+$/g, ''); //strip whitespace
		if (bookingPersonName == "") {
			fail = 1;
			$('#bookingPersonName').removeClass('inputNormal');
			$('#bookingPersonName').addClass('inputHighlight');
		} else {
			$('#bookingPersonName').removeClass('inputHighlight');
			$('#bookingPersonName').addClass('inputNormal');
		}
		var bookingPersonPhone = $('#bookingPersonPhone').val();
		bookingPersonPhone = bookingPersonPhone.replace(/^\s+|\s+$/g, ''); //strip whitespace
		if (bookingPersonPhone == "") {
			fail = 1;
			$('#bookingPersonPhone').removeClass('inputNormal');
			$('#bookingPersonPhone').addClass('inputHighlight');
		} else {
			$('#bookingPersonPhone').removeClass('inputHighlight');
			$('#bookingPersonPhone').addClass('inputNormal');
		}
		var bookingPersonEmail = $('#bookingPersonEmail').val();
		bookingPersonEmail = bookingPersonEmail.replace(/^\s+|\s+$/g, ''); //strip whitespace
		if (bookingPersonEmail == "") {
			fail = 1;
			$('#bookingPersonEmail').removeClass('inputNormal');
			$('#bookingPersonEmail').addClass('inputHighlight');
		} else {
			$('#bookingPersonEmail').removeClass('inputHighlight');
			$('#bookingPersonEmail').addClass('inputNormal');
		}
		if (fail == 1) { // display an error message if not all the data is available
			message = '<p id="errorMessage">Please enter all the information required.</p>';
			$('#bookingErrorMessage').append(message);
			$('#bookingErrorMessage').css('display','block');
		} else { // the form is ready to go so submit it
			$('#centralColumnEventBooking').submit();
		}
	});

	// check before deleting a notice
	$('.linkNoticeDelete').click(function() {
		if (confirm('Are you sure?')) {
			return true;
		}
		return false;
	});
	

	// checking the required fields for the Edit Notice form
	$('#btnEditNotice').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#noticeErrorMessage').css('display','none');

		$(".inputRequired").each(function(){ // loop through the required input fields, checking the content
			text = $(this).val();
			text = text.replace(/^\s+|\s+$/g, ''); //strip whitespace
			if (text == "") {
				fail = 1;
				$(this).removeClass('inputNormal');
				$(this).addClass('inputHighlight');
			} else {
				$(this).removeClass('inputHighlight');
				$(this).addClass('inputNormal');
			}
		});
		if (fail == 1) { // display an error message if not all the data is available
			message = '<p id="errorMessage">Please enter all the information required.</p>';
			$('#noticeErrorMessage').append(message);
			$('#noticeErrorMessage').css('display','block');
		} else { // the form is ready to go so submit it
			$('#editNotice').submit();
		}

	});	
	

	// checking the required fields for the Add Notice form
	$('#btnAddNotice').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#noticeErrorMessage').css('display','none');

		$(".inputRequired").each(function(){ // loop through the required input fields, checking the content
			text = $(this).val();
			text = text.replace(/^\s+|\s+$/g, ''); //strip whitespace
			if (text == "") {
				fail = 1;
				$(this).removeClass('inputNormal');
				$(this).addClass('inputHighlight');
			} else {
				$(this).removeClass('inputHighlight');
				$(this).addClass('inputNormal');
			}
		});
		if (fail == 1) { // display an error message if not all the data is available
			message = '<p id="errorMessage">Please enter all the information required.</p>';
			$('#noticeErrorMessage').append(message);
			$('#noticeErrorMessage').css('display','block');
		} else { // the form is ready to go so submit it
			$('#addNotice').submit();
		}

	});	
	
	$('#logoDelete').click(function() {
		ID = $('#ID').val();
		memberType = $('#memberType').val();
		$.get("picDelete.php", { ID: ID, type: "logo", memberType: memberType }, function(data) {
			//alert("Data Loaded: " + data);
			if (data != 0) {
				$('#logoDisplay').remove();
				alert("The logo has been removed.");
			} else {
				alert("Something went wrong and the logo couldn't be removed.");
			}
		});
		return false;
	});
	
	$('#articlePicDelete').click(function() {
		articleID = $('#articleID').val();
		$.get("picDelete.php", { ID: articleID, type: "notice", fileType: "image" }, function(data) {
			// alert("Data Loaded: " + data);
			if (data != 0) {
				$('#articlePicDisplay').remove();
				alert("The image has been removed from the notice.");
			} else {
				alert("Something went wrong and the image couldn't be removed.");
			}
		});
		return false;
	});
	
	$('#articleFileDelete').click(function() {
		articleID = $('#articleID').val();
		$.get("picDelete.php", { ID: articleID, type: "notice", fileType: "file" }, function(data) {
			//alert("Data Loaded: " + data);
			if (data != 0) {
				$('#articleFileDisplay').remove();
				alert("The file has been removed from the notice.");
			} else {
				alert("Something went wrong and the image couldn't be removed.");
			}
		});
		return false;
	});
	



	// checking the required fields for the individual joining form
	$('#btnJoinIndividualPart1').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#joinErrorMessage').css('display','none');

		email = $('#joinEmail').val();		
		if (!isValidEmailAddress(email)) {
			fail = 1;
			$('#joinEmail').removeClass('inputNormal');
			$('#joinEmail').addClass('inputHighlight');
			message = '<p id="errorMessage">Please enter a valid email address.</p>';
			$('#joinErrorMessage').append(message);
			$('#joinErrorMessage').css('display','block');
			$("#joinEmail").focus();
			$('html, body').animate({scrollTop:0}, 'fast');
		} else {
			$('#joinEmail').removeClass('inputHighlight');
			$('#joinEmail').addClass('inputNormal');
			
			$.get("checkEmail.php", { name: email }, function(data) {
   			// alert("Data Loaded: " + data);
				if (data != 0) {
					fail = 1;
					$('#joinEmail').removeClass('inputNormal');
					$('#joinEmail').addClass('inputHighlight');
					message = '<p id="errorMessage">Someone with that email address is already a member of CBN.</p>';
					$('#joinErrorMessage').append(message);
					$('#joinErrorMessage').css('display','block');
					$("#joinEmail").focus();
					$('html, body').animate({scrollTop:0}, 'fast');
				} else {
					$('#joinEmail').removeClass('inputHighlight');
					$('#joinEmail').addClass('inputNormal');

					password = $('#joinPassword').val();
					passwordAgain = $('#joinPasswordAgain').val();
					password = password.replace(/^\s+|\s+$/g, ''); //strip whitespace
					passwordAgain = passwordAgain.replace(/^\s+|\s+$/g, ''); //strip whitespace
					if (((password.length < 4) || (passwordAgain.length < 4)) || (password != passwordAgain)) {
						fail = 1;
						$('#joinPassword').removeClass('inputNormal');
						$('#joinPassword').addClass('inputHighlight');
						$('#joinPasswordAgain').removeClass('inputNormal');
						$('#joinPasswordAgain').addClass('inputHighlight');
						message = '<p id="errorMessage">The password must be more than 4 characters long and the two password fields must match.</p>';
						$('#joinErrorMessage').append(message);
						$('#joinErrorMessage').css('display','block');
						$("#joinPassword").focus();
						$('html, body').animate({scrollTop:0}, 'fast');
					} else {
						$('#joinPassword').removeClass('inputHighlight');
						$('#joinPassword').addClass('inputNormal');
						$('#joinPasswordAgain').removeClass('inputHighlight');
						$('#joinPasswordAgain').addClass('inputNormal');
						
						description = $('#joinDescription').val();
						if (description.length > 200) {
							fail = 1;
							$('#joinDescription').removeClass('inputNormal');
							$('#joinDescription').addClass('inputHighlight');
							message = '<p id="errorMessage">Please enter a description that is shorter than 200 characters.</p>';
							$('#joinErrorMessage').append(message);
							$('#joinErrorMessage').css('display','block');
							$("#joinDescription").focus();
							$('html, body').animate({scrollTop:0}, 'fast');
						} else {
							$('#joinDescription').removeClass('inputHighlight');
							$('#joinDescription').addClass('inputNormal');
						
							$(".inputRequired").each(function(){ // loop through the required input fields, checking the content
								text = $(this).val();
								text = text.replace(/^\s+|\s+$/g, ''); //strip whitespace
								if (text == "") {
									fail = 1;
									$(this).removeClass('inputNormal');
									$(this).addClass('inputHighlight');
									$(this).focus();
								} else {
									$(this).removeClass('inputHighlight');
									$(this).addClass('inputNormal');
								}
							});
							if (fail == 1) { // display an error message if not all the data is available
								message = '<p id="errorMessage">Please enter all the information required.</p>';
								$('#joinErrorMessage').append(message);
								$('#joinErrorMessage').css('display','block');
								$('html, body').animate({scrollTop:0}, 'fast');
							}
						}
					}
					if (fail == 0) { // the form is ready to go so submit it
						$('#joinIndividualPart1').submit();
					}
				}
 			});
		}
	});	


	// checking the required fields for the corporate joining form
	$('#btnJoinCorporatePart1').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#joinErrorMessage').css('display','none');

		email = $('#joinEmail').val();		
		if (!isValidEmailAddress(email)) {
			fail = 1;
			$('#joinEmail').removeClass('inputNormal');
			$('#joinEmail').addClass('inputHighlight');
			message = '<p id="errorMessage">Please enter a valid email address.</p>';
			$('#joinErrorMessage').append(message);
			$('#joinErrorMessage').css('display','block');
			$("#joinEmail").focus();
			$('html, body').animate({scrollTop:0}, 'fast');
		} else {
			$('#joinEmail').removeClass('inputHighlight');
			$('#joinEmail').addClass('inputNormal');
			
			$.get("checkEmail.php", { name: email }, function(data) {
   			// alert("Data Loaded: " + data);
				if (data != 0) {
					fail = 1;
					$('#joinEmail').removeClass('inputNormal');
					$('#joinEmail').addClass('inputHighlight');
					message = '<p id="errorMessage">Someone with that email address is already a member of CBN.</p>';
					$('#joinErrorMessage').append(message);
					$('#joinErrorMessage').css('display','block');
					$("#joinEmail").focus();
					$('html, body').animate({scrollTop:0}, 'fast');
				} else {
					$('#joinEmail').removeClass('inputHighlight');
					$('#joinEmail').addClass('inputNormal');

					password = $('#joinPassword').val();
					passwordAgain = $('#joinPasswordAgain').val();
					password = password.replace(/^\s+|\s+$/g, ''); //strip whitespace
					passwordAgain = passwordAgain.replace(/^\s+|\s+$/g, ''); //strip whitespace
					if (((password.length < 4) || (passwordAgain.length < 4)) || (password != passwordAgain)) {
						fail = 1;
						$('#joinPassword').removeClass('inputNormal');
						$('#joinPassword').addClass('inputHighlight');
						$('#joinPasswordAgain').removeClass('inputNormal');
						$('#joinPasswordAgain').addClass('inputHighlight');
						message = '<p id="errorMessage">The password must be more than 4 characters long and the two password fields must match.</p>';
						$('#joinErrorMessage').append(message);
						$('#joinErrorMessage').css('display','block');
						$("#joinPassword").focus();
						$('html, body').animate({scrollTop:0}, 'fast');
					} else {
						$('#joinPassword').removeClass('inputHighlight');
						$('#joinPassword').addClass('inputNormal');
						$('#joinPasswordAgain').removeClass('inputHighlight');
						$('#joinPasswordAgain').addClass('inputNormal');
						
						description = $('#joinDescription').val();
						if (description.length > 200) {
							fail = 1;
							$('#joinDescription').removeClass('inputNormal');
							$('#joinDescription').addClass('inputHighlight');
							message = '<p id="errorMessage">Please enter a description that is shorter than 200 characters.</p>';
							$('#joinErrorMessage').append(message);
							$('#joinErrorMessage').css('display','block');
							$("#joinDescription").focus();
							$('html, body').animate({scrollTop:0}, 'fast');
						} else {
							$('#joinDescription').removeClass('inputHighlight');
							$('#joinDescription').addClass('inputNormal');
						
							$(".inputRequired").each(function(){ // loop through the required input fields, checking the content
								text = $(this).val();
								text = text.replace(/^\s+|\s+$/g, ''); //strip whitespace
								if (text == "") {
									fail = 1;
									$(this).removeClass('inputNormal');
									$(this).addClass('inputHighlight');
									$(this).focus();
								} else {
									$(this).removeClass('inputHighlight');
									$(this).addClass('inputNormal');
								}
							});
							if (fail == 1) { // display an error message if not all the data is available
								message = '<p id="errorMessage">Please enter all the information required.</p>';
								$('#joinErrorMessage').append(message);
								$('#joinErrorMessage').css('display','block');
								$('html, body').animate({scrollTop:0}, 'fast');
							}
						}
					}
					if (fail == 0) { // the form is ready to go so submit it
						$('#joinCorporatePart1').submit();
					}
				}
 			});
		}
	});	


	// checking the required fields for the Add Notice form
	$('#btnJoinCorpIndividualPart1').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#noticeErrorMessage').css('display','none');

		corporateCode = $('#joinCorporateCode').val();		
		$.get("checkCorporateCode.php", { name: corporateCode }, function(checkCorporateCode) {
			if (checkCorporateCode == 1) {
				message = '<p id="errorMessage">We couldn\'t check that membership code for some reason</p>';
			} else if (checkCorporateCode == 2) {
				message = '<p id="errorMessage">That corporate membership code does not exist.</p>';
			} else if (checkCorporateCode == 3) {
				message = '<p id="errorMessage">Six people are already registered to use that corporate membership code.</p>';
			}
			if (checkCorporateCode != 0) {
				fail = 1;
				$('#joinCorporateCode').removeClass('inputNormal');
				$('#joinCorporateCode').addClass('inputHighlight');
				$('#joinErrorMessage').append(message);
				$('#joinErrorMessage').css('display','block');
				$("#joinCorporateCode").focus();
			} else {
				$('#joinEmail').removeClass('inputHighlight');
				$('#joinEmail').addClass('inputNormal');
				$('#joinCorpIndividualPart1').submit();
			}
		});
	});	



	// checking the required fields for the individual joining form
	$('#btnJoinCorpIndividualPart2').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#joinErrorMessage').css('display','none');

		email = $('#joinEmail').val();		
		if (!isValidEmailAddress(email)) {
			fail = 1;
			$('#joinEmail').removeClass('inputNormal');
			$('#joinEmail').addClass('inputHighlight');
			message = '<p id="errorMessage">Please enter a valid email address.</p>';
			$('#joinErrorMessage').append(message);
			$('#joinErrorMessage').css('display','block');
			$("#joinEmail").focus();
			$('html, body').animate({scrollTop:0}, 'fast');
		} else {
			$('#joinEmail').removeClass('inputHighlight');
			$('#joinEmail').addClass('inputNormal');
			
			$.get("checkEmail.php", { name: email }, function(data) {
   			// alert("Data Loaded: " + data);
				if (data != 0) {
					fail = 1;
					$('#joinEmail').removeClass('inputNormal');
					$('#joinEmail').addClass('inputHighlight');
					message = '<p id="errorMessage">Someone with that email address is already a member of CBN.</p>';
					$('#joinErrorMessage').append(message);
					$('#joinErrorMessage').css('display','block');
					$("#joinEmail").focus();
					$('html, body').animate({scrollTop:0}, 'fast');
				} else {
					$('#joinEmail').removeClass('inputHighlight');
					$('#joinEmail').addClass('inputNormal');

					password = $('#joinPassword').val();
					passwordAgain = $('#joinPasswordAgain').val();
					password = password.replace(/^\s+|\s+$/g, ''); //strip whitespace
					passwordAgain = passwordAgain.replace(/^\s+|\s+$/g, ''); //strip whitespace
					if (((password.length < 4) || (passwordAgain.length < 4)) || (password != passwordAgain)) {
						fail = 1;
						$('#joinPassword').removeClass('inputNormal');
						$('#joinPassword').addClass('inputHighlight');
						$('#joinPasswordAgain').removeClass('inputNormal');
						$('#joinPasswordAgain').addClass('inputHighlight');
						message = '<p id="errorMessage">The password must be more than 4 characters long and the two password fields must match.</p>';
						$('#joinErrorMessage').append(message);
						$('#joinErrorMessage').css('display','block');
						$("#joinPassword").focus();
						$('html, body').animate({scrollTop:0}, 'fast');
					} else {
						$('#joinPassword').removeClass('inputHighlight');
						$('#joinPassword').addClass('inputNormal');
						$('#joinPasswordAgain').removeClass('inputHighlight');
						$('#joinPasswordAgain').addClass('inputNormal');
						
						$(".inputRequired").each(function(){ // loop through the required input fields, checking the content
							text = $(this).val();
							text = text.replace(/^\s+|\s+$/g, ''); //strip whitespace
							if (text == "") {
								fail = 1;
								$(this).removeClass('inputNormal');
								$(this).addClass('inputHighlight');
								$(this).focus();
							} else {
								$(this).removeClass('inputHighlight');
								$(this).addClass('inputNormal');
							}
						});
						if (fail == 1) { // display an error message if not all the data is available
							message = '<p id="errorMessage">Please enter all the information required.</p>';
							$('#joinErrorMessage').append(message);
							$('#joinErrorMessage').css('display','block');
							$('html, body').animate({scrollTop:0}, 'fast');
						}
					}
					if (fail == 0) { // the form is ready to go so submit it
						$('#joinCorpIndividualPart2').submit();
					}
				}
 			});
		}
	});	


	// checking the required fields for the individual joining form
	$('#btnEditIndividualPart1').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#joinErrorMessage').css('display','none');

		email = $('#joinEmail').val();		
		if (!isValidEmailAddress(email)) {
			fail = 1;
			$('#joinEmail').removeClass('inputNormal');
			$('#joinEmail').addClass('inputHighlight');
			message = '<p id="errorMessage">Please enter a valid email address.</p>';
			$('#joinErrorMessage').append(message);
			$('#joinErrorMessage').css('display','block');
			$("#joinEmail").focus();
			$('html, body').animate({scrollTop:0}, 'fast');
		} else {
			$('#joinEmail').removeClass('inputHighlight');
			$('#joinEmail').addClass('inputNormal');
			
			joinMemberID = $('#joinMemberID').val();
			$.get("checkEditEmail.php", { name: email, memberID: joinMemberID }, function(data) {
   			// alert("Data Loaded: " + data);
				if (data != 0) {
					fail = 1;
					$('#joinEmail').removeClass('inputNormal');
					$('#joinEmail').addClass('inputHighlight');
					message = '<p id="errorMessage">Someone with that email address is already a member of CBN.</p>';
					$('#joinErrorMessage').append(message);
					$('#joinErrorMessage').css('display','block');
					$("#joinEmail").focus();
					$('html, body').animate({scrollTop:0}, 'fast');
				} else {
					$('#joinEmail').removeClass('inputHighlight');
					$('#joinEmail').addClass('inputNormal');

					password = $('#joinPassword').val();
					passwordAgain = $('#joinPasswordAgain').val();
					password = password.replace(/^\s+|\s+$/g, ''); //strip whitespace
					passwordAgain = passwordAgain.replace(/^\s+|\s+$/g, ''); //strip whitespace
					if ((((password.length > 0) || (passwordAgain.length > 0)) && ((password.length < 4) || (passwordAgain.length < 4))) || (password != passwordAgain)) {
						fail = 1;
						$('#joinPassword').removeClass('inputNormal');
						$('#joinPassword').addClass('inputHighlight');
						$('#joinPasswordAgain').removeClass('inputNormal');
						$('#joinPasswordAgain').addClass('inputHighlight');
						message = '<p id="errorMessage">The password must be more than 4 characters long and the two password fields must match.</p>';
						$('#joinErrorMessage').append(message);
						$('#joinErrorMessage').css('display','block');
						$("#joinPassword").focus();
						$('html, body').animate({scrollTop:0}, 'fast');
					} else {
						$('#joinPassword').removeClass('inputHighlight');
						$('#joinPassword').addClass('inputNormal');
						$('#joinPasswordAgain').removeClass('inputHighlight');
						$('#joinPasswordAgain').addClass('inputNormal');
						
						description = $('#joinDescription').val();
						if (description.length > 200) {
							fail = 1;
							$('#joinDescription').removeClass('inputNormal');
							$('#joinDescription').addClass('inputHighlight');
							message = '<p id="errorMessage">Please enter a description that is shorter than 200 characters.</p>';
							$('#joinErrorMessage').append(message);
							$('#joinErrorMessage').css('display','block');
							$("#joinDescription").focus();
							$('html, body').animate({scrollTop:0}, 'fast');
						} else {
							$('#joinDescription').removeClass('inputHighlight');
							$('#joinDescription').addClass('inputNormal');
						
							$(".inputRequired").each(function(){ // loop through the required input fields, checking the content
								text = $(this).val();
								text = text.replace(/^\s+|\s+$/g, ''); //strip whitespace
								if (text == "") {
									fail = 1;
									$(this).removeClass('inputNormal');
									$(this).addClass('inputHighlight');
									$(this).focus();
								} else {
									$(this).removeClass('inputHighlight');
									$(this).addClass('inputNormal');
								}
							});
							if (fail == 1) { // display an error message if not all the data is available
								message = '<p id="errorMessage">Please enter all the information required.</p>';
								$('#joinErrorMessage').append(message);
								$('#joinErrorMessage').css('display','block');
								$('html, body').animate({scrollTop:0}, 'fast');
							}
						}
					}
					if (fail == 0) { // the form is ready to go so submit it
						$('#editIndividualPart1').submit();
					}
				}
 			});
		}
	});	



	// checking the required fields for the corporate joining form
	$('#btnEditCorporatePart1').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#joinErrorMessage').css('display','none');

		description = $('#joinDescription').val();
		if (description.length > 200) {
			fail = 1;
			$('#joinDescription').removeClass('inputNormal');
			$('#joinDescription').addClass('inputHighlight');
			message = '<p id="errorMessage">Please enter a description that is shorter than 200 characters.</p>';
			$('#joinErrorMessage').append(message);
			$('#joinErrorMessage').css('display','block');
			$("#joinDescription").focus();
			$('html, body').animate({scrollTop:0}, 'fast');
		} else {
			$('#joinDescription').removeClass('inputHighlight');
			$('#joinDescription').addClass('inputNormal');
		
			$(".inputRequired").each(function(){ // loop through the required input fields, checking the content
				text = $(this).val();
				text = text.replace(/^\s+|\s+$/g, ''); //strip whitespace
				if (text == "") {
					fail = 1;
					$(this).removeClass('inputNormal');
					$(this).addClass('inputHighlight');
					$(this).focus();
				} else {
					$(this).removeClass('inputHighlight');
					$(this).addClass('inputNormal');
				}
			});
			if (fail == 1) { // display an error message if not all the data is available
				message = '<p id="errorMessage">Please enter all the information required.</p>';
				$('#joinErrorMessage').append(message);
				$('#joinErrorMessage').css('display','block');
				$('html, body').animate({scrollTop:0}, 'fast');
			}
			if (fail == 0) { // the form is ready to go so submit it
				$('#editCorporatePart1').submit();
			}
		}
	});	


	// checking the required fields for the individual joining form
	$('#btnEditCorpIndividualPart1').click(function() {
		var fail = 0; // use this to check whether or not the form should be submitted
		$('p#errorMessage').remove();
		$('#joinErrorMessage').css('display','none');

		email = $('#joinEmail').val();		
		if (!isValidEmailAddress(email)) {
			fail = 1;
			$('#joinEmail').removeClass('inputNormal');
			$('#joinEmail').addClass('inputHighlight');
			message = '<p id="errorMessage">Please enter a valid email address.</p>';
			$('#joinErrorMessage').append(message);
			$('#joinErrorMessage').css('display','block');
			$("#joinEmail").focus();
			$('html, body').animate({scrollTop:0}, 'fast');
		} else {
			$('#joinEmail').removeClass('inputHighlight');
			$('#joinEmail').addClass('inputNormal');
			
			joinMemberID = $('#joinMemberID').val();
			$.get("checkEditEmail.php", { name: email, memberID: joinMemberID }, function(data) {
   			// alert("Data Loaded: " + data);
				if (data != 0) {
					fail = 1;
					$('#joinEmail').removeClass('inputNormal');
					$('#joinEmail').addClass('inputHighlight');
					message = '<p id="errorMessage">Someone with that email address is already a member of CBN.</p>';
					$('#joinErrorMessage').append(message);
					$('#joinErrorMessage').css('display','block');
					$("#joinEmail").focus();
					$('html, body').animate({scrollTop:0}, 'fast');
				} else {
					$('#joinEmail').removeClass('inputHighlight');
					$('#joinEmail').addClass('inputNormal');
					
					password = $('#joinPassword').val();
					passwordAgain = $('#joinPasswordAgain').val();
					password = password.replace(/^\s+|\s+$/g, ''); //strip whitespace
					passwordAgain = passwordAgain.replace(/^\s+|\s+$/g, ''); //strip whitespace
					if ((((password.length > 0) || (passwordAgain.length > 0)) && ((password.length < 4) || (passwordAgain.length < 4))) || (password != passwordAgain)) {
						fail = 1;
						$('#joinPassword').removeClass('inputNormal');
						$('#joinPassword').addClass('inputHighlight');
						$('#joinPasswordAgain').removeClass('inputNormal');
						$('#joinPasswordAgain').addClass('inputHighlight');
						message = '<p id="errorMessage">The password must be more than 4 characters long and the two password fields must match.</p>';
						$('#joinErrorMessage').append(message);
						$('#joinErrorMessage').css('display','block');
						$("#joinPassword").focus();
						$('html, body').animate({scrollTop:0}, 'fast');
					} else {
						$('#joinPassword').removeClass('inputHighlight');
						$('#joinPassword').addClass('inputNormal');
						$('#joinPasswordAgain').removeClass('inputHighlight');
						$('#joinPasswordAgain').addClass('inputNormal');
						
						$(".inputRequired").each(function(){ // loop through the required input fields, checking the content
							text = "";
							text = $(this).val();
							text = text.replace(/^\s+|\s+$/g, ''); //strip whitespace
							if (text == "") {
								fail = 1;
								$(this).removeClass('inputNormal');
								$(this).addClass('inputHighlight');
								$(this).focus();
							} else {
								$(this).removeClass('inputHighlight');
								$(this).addClass('inputNormal');
							}
						});
						if (fail == 1) { // display an error message if not all the data is available
							message = '<p id="errorMessage">Please enter all the information required.</p>';
							$('#joinErrorMessage').append(message);
							$('#joinErrorMessage').css('display','block');
							$('html, body').animate({scrollTop:0}, 'fast');
						}
					}
				}
				if (fail == 0) { // the form is ready to go so submit it
					$('#editCorpIndividualPart1').submit();
				}
			});
		}
	});	


	$('#joinPermissionName').click(function() {
		joinPermissionName();
	});
	
	function joinPermissionName() {
		if ($('#joinPermissionName').is(':checked')) {
			$(".joinPermissionDirectory").each(function(){ // loop through the other checkboxes
				$(this).attr("disabled", false); 
			});
		} else {
			$(".joinPermissionDirectory").each(function(){ // loop through the other checkboxes
				$(this).attr("checked", false);
				$(this).attr("disabled", true); 
			});
		}
	}
	
	function limitChars(textid, limit, infodiv) {
 		var text = $('#'+textid).val(); 
		var textlength = text.length;
		if ((textlength > limit) || (textlength == 0)) {
			$('#' + infodiv).html('Maximum '+limit+' characters.');
			$('#'+textid).val(text.substr(0,limit));
			return false;
		} else {
			$('#' + infodiv).html('Maximum '+limit+' characters - '+ (limit - textlength) +' left.');
			return true;
		}
	}

});


/* utility function to check validity of an email address */
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}


