
// -------------------------------------------------------------
//
// FILENAME		: onload.js
// COPYRIGHT	: © 2003 - 09 Espen Andersson
// WWW			: http://ebascripts.com/
//
// -------------------------------------------------------------

window.onload = function() {
	setupZoom();

	var leftMenu = document.getElementById('leftmenu');

	if (leftMenu) {
		/** css hack to make sure the document validates (problems with noscript & style tags) */
		var homeLink = document.getElementById('home');
	
		if (homeLink) {
			homeLink.style.width = "92%";
		}

		/** left column is hidden on page load */
		if (readCookie("columnStatus") == "none") {
			/** collapse menu */
			document.getElementById('leftmenu').style.width = '3%';

			/** make content layout wider */
			document.getElementById('content').style.width = '93%';

			/** make home and collaps button 100% wide */
			document.getElementById('displayColumn').style.width = '100%';
			document.getElementById('home').style.width = '100%';
			document.getElementById('home').style.marginBottom = 0;

			centerAlignHeadlines();

			/** hide text inside headlines and only show their images */
			hideTheseElements = new Array('commonTasksText', 'commentsText', 'recentArticlesText', 'blogrollText', 
				'showWebsiteText', 'blog', 'comments', 'recentArticles', 'commonTasks');

			for (var i = 0; i < hideTheseElements.length; i++) {
				hideElement(hideTheseElements[i]);
			}

			/** finally hide the expand/collapse buttons */
			var expandButtons = document.getElementsByClassName('columnDisplayButton');
		
			for (var i = 0; i < expandButtons.length; i++) {
				expandButtons[i].style.display = 'none';
			}
		}

		if (leftMenu.style.width == '22%') {
			document.getElementById('changeColumnStatusImg').src = 'templates/images/icons/arrow_left.png';
		} else {
			document.getElementById('changeColumnStatusImg').src = 'templates/images/icons/arrow_right.png';
		}
	}

	var submitButtons = document.getElementsByName("submit");

	for (i = 0; i < submitButtons.length; i++) {
		submitButtons[i].disabled = true; 
	}

	/** font size adjuster */
	var fontSizeAdjusterSmallLink = document.getElementById('fontSizeAdjusterSmallLink');
	var fontSizeAdjusterMediumLink = document.getElementById('fontSizeAdjusterMediumLink');
	var fontSizeAdjusterLargeLink = document.getElementById('fontSizeAdjusterLargeLink');

	if (fontSizeAdjusterSmallLink) {
		var currentFontSize = readCookie("fontSize");

		if (currentFontSize == 70) {
			fontSizeAdjusterSmallLink.className = "small current";
		} else if (currentFontSize == 80) {
			fontSizeAdjusterMediumLink.className = "medium current";
		} else if (currentFontSize == 120) {
			fontSizeAdjusterLargeLink.className = "large current";
		} else {
			fontSizeAdjusterMediumLink.className = "medium current";
		}
	}

	/** manage comments */
	var updateCommentName = document.getElementById('commentName');
	var updateCommentText = document.getElementById('comment_text');
	var updateCommentSubmit = document.getElementById('updateCommentSubmit');

	if (updateCommentName) {
		updateCommentName.onkeyup = updateCommentText.onkeyup = function() {
			updateCommentSubmit.disabled = (updateCommentName.value.length == 0 || updateCommentText.value.length == 0);
		}
	}

	/** public search */
	var publicSearchInput = document.getElementById('publicSearchInput');
	var publicSearchSubmit = document.getElementById('publicSearchSubmit');

	if (publicSearchInput) {
		if (publicSearchInput.value.length > 0) {
			publicSearchInput.focus();
		}

		if (publicSearchInput.value.length == 0) {
			publicSearchInput.onkeyup = function() {
				publicSearchSubmit.disabled = (publicSearchInput.value.length == 0);
			}
		} else {
			publicSearchSubmit.disabled = false;
		}
	}
	
	/** add comment (public) */
	var publicCommentFormName = document.getElementById('publicCommentFormName');
	var publicCommentFormMessage = document.getElementById('publicCommentFormMessage');
	var publicCommentFormSubmit = document.getElementById('publicCommentFormSubmit');
	
	if (publicCommentFormName) {
		publicCommentFormName.onkeyup = publicCommentFormMessage.onkeyup = function() {
			publicCommentFormSubmit.disabled = (publicCommentFormName.value.length == 0 || publicCommentFormMessage.value.length == 0);
		}
	}	
	/** image upload */
	var uploadFileInput = document.getElementById('uploadFileInput');
	var uploadFileSubmit = document.getElementById('uploadFileSubmit');

	if (uploadFileInput) {
		uploadFileInput.onchange = function() { // note onchange because of type="file"
			uploadFileSubmit.disabled = (uploadFileInput.value.length == 0);
		}
	}

	/** dashboard */
	var dashboardSubmitButton = document.getElementById('dashboardSaveNote');
	var dashboardMessageField = document.getElementById('dashboardNoteMsg');

	if (dashboardSubmitButton) {
		dashboardMessageField.onkeyup = function() {
			dashboardSubmitButton.disabled = (dashboardMessageField.value.length == 0);
		}
	}

	/** blogroll */
	var blogrollTitle = document.getElementById('blogTitle');
	var blogrollUrl = document.getElementById('blogUrl');
	var blogrollSubmit = document.getElementById('blogrollSubmit');

	if (blogrollTitle) {
		blogrollTitle.onkeyup = blogrollUrl.onkeyup = function() {
			blogrollSubmit.disabled = (blogrollTitle.value.length == 0 || blogrollUrl.value.length == 0);
		}
	}

	/** create category */
	var createCategorySubmitButton = document.getElementById('create_category');
	var createCategoryCatTitle = document.getElementById('category_title');
	var createCategoryPwd = document.getElementById('category_password');
	var createCategoryRepPwd = document.getElementById('category_rep_password');

	if (createCategorySubmitButton) {
		if (createCategoryCatTitle.value.length == 0) {
			createCategoryCatTitle.onkeyup = function() {
				if (createCategoryPwd.value.length > 0 || createCategoryRepPwd.value.length > 0) {
					if (createCategoryPwd.value != createCategoryRepPwd.value) {
						createCategorySubmitButton.disabled = true;
					}
				} else {
					createCategorySubmitButton.disabled = (createCategoryCatTitle.value.length == 0);
				}
			}
			
			createCategoryPwd.onkeyup = createCategoryRepPwd.onkeyup = function() {
				createCategorySubmitButton.disabled = false;

				if (createCategoryPwd.value != createCategoryRepPwd.value) {
					createCategorySubmitButton.disabled = true;
				}
			}
		} else {
			createCategorySubmitButton.disabled = false;
		}
	}

	/** create webpage */
	var webpageTitle = document.getElementById('webpageTitle');
	var webpageSubmit = document.getElementById('createWebpageSubmit');
	var webpagePwd = document.getElementById('webpagePwd');
	var webpageRepeatPwd = document.getElementById('webpageRepeatPwd');

	if (webpageTitle) {
		if (webpageTitle.value.length == 0) {
			webpageTitle.onkeyup = function() {
				webpageSubmit.disabled = (webpageTitle.value.length == 0);
			}
		} else {
			webpageSubmit.disabled = false;
		}
	}

	/** login form */
	var loginSubmitButton = document.getElementById('loginSubmit');
	var loginUsername = document.getElementById('loginUsername');
	var loginPassword = document.getElementById('loginPwd');

	if (loginSubmitButton) {
		loginUsername.focus();
		
		loginUsername.onkeyup = loginPassword.onkeyup = function() {
			loginSubmitButton.disabled = (loginUsername.value.length == 0 || loginPassword.value.length == 0);
		}
	}

	/** article search */
	var searchArticleInput = document.getElementById('searchArticleInput');
	var searchArticleSubmit = document.getElementById('searchArticleSubmit');
	
	if (searchArticleInput) {
		/** let's focus the search field automatically if a search has been made */
		if (searchArticleInput.value.length > 0) {
			searchArticleInput.focus();
		}

		if (searchArticleInput.value.length == 0) {
			searchArticleInput.onkeyup = function() {
				searchArticleSubmit.disabled = (searchArticleInput.value.length == 0);
			}
		} else {
			searchArticleSubmit.disabled = false;
		}
	}
	
	/** comments search */
	var searchCommentInput = document.getElementById('searchCommentInput');
	var searchCommentSubmit = document.getElementById('searchCommentSubmit');
	
	if (searchCommentInput) {
		/** let's focus the search field automatically if a search has been made */
		if (searchCommentInput.value.length > 0) {
			searchCommentInput.focus();
		}

		if (searchCommentInput.value.length == 0) {
			searchCommentInput.onkeyup = function() {
				searchCommentSubmit.disabled = (searchCommentInput.value.length == 0);
			}
		} else {
			searchCommentSubmit.disabled = false;
		}
	}

	/** manage user profile */
	var currentPwdInputField = document.getElementById('currentPwd');
	var newPwdInputField = document.getElementById('newPwd');
	var newPwdConfirmInputField = document.getElementById('newPwdConfirm');
	var updatePwdButton = document.getElementById('updateUserPwd');
	var updateProfile = document.getElementById('updateProfile');
	var usernameField = document.getElementById('username');
	
	if (usernameField) {
		if (usernameField.value.length == 0) {
			updateProfile.onkeyup = function() {
				updateProfile.disabled = (updateProfile.value.length == 0);
			}
		} else {
			updateProfile.disabled = false;
		}
	}

	/** disable change pwd submit button if "newPwd" and "newPwdConfirm" don't match */
	if (currentPwdInputField) {
		newPwdInputField.onkeyup = newPwdConfirmInputField.onkeyup = function() {
			if (newPwdInputField.value != newPwdConfirmInputField.value) {
				if (newPwdInputField.value.length > 0 || newPwdConfirmInputField.value.length > 0) {
					updatePwdButton.disabled = true;
				} else {
					updatePwdButton.disabled = false;
				}
			} else {
				updatePwdButton.disabled = false;
			}
		}
	}

	/** publish article */
	var articleTitle = document.getElementById('articleTitle');
	var articleSubmitButton = document.getElementById('articleSubmitButton');

	if (articleTitle) {
		if (articleTitle.value.length == 0) {
			articleTitle.onkeyup = function() {
				articleSubmitButton.disabled = (articleTitle.value.length == 0);
			}
		} else {
			articleSubmitButton.disabled = false;
		}
	}

	/** create user */
	var createUserUsername = document.getElementById('createUserUsername');
	var createUserPwd = document.getElementById('createUserPwd');
	var createUserRepeatPwd = document.getElementById('createUserRepeatPwd');
	var createUserSubmit = document.getElementById('createUserSubmit');

	if (createUserUsername) {
		createUserUsername.onkeyup = createUserPwd.onkeyup = createUserRepeatPwd.onkeyup = function() {
			if (createUserUsername.value.length == 0 || createUserPwd.value.length == 0 || createUserRepeatPwd.value.length == 0) {
				createUserSubmit.disabled = true;
			} else if (createUserPwd.value != createUserRepeatPwd.value) {
				createUserSubmit.disabled = true;
			} else {
				createUserSubmit.disabled = false;
			}
		}
	}
	
	/** blogroll */
	var blogTitle = document.getElementById('blog_title');
	var blogUrl = document.getElementById('blog_url');
	var blogSubmit = document.getElementById('blogrollSubmit');

	if (blogTitle) {
		blogTitle.onkeyup = blogUrl.onkeyup = function() {
			blogSubmit.disabled = (blogTitle.value.length == 0 || blogUrl.value.length == 0);
		}
	}
	
	/** preferences */
	var enableComments = document.getElementById('enable_comments');
	var commentNotificationDisabled = document.getElementById('comment_notification_disabled');
	var commentNotificationEnabled = document.getElementById('comment_notification_enabled');
	var reverseCommentsDisabled = document.getElementById('reverse_comments_disabled');
	var reverseCommentsEnabled = document.getElementById('reverse_comments_enabled');
	var smiliesEnabled = document.getElementById('smilies_enabled');
	var smiliesDisabled = document.getElementById('smilies_disabled');
	var gravatarsEnabled = document.getElementById('gravatars_enabled');
	var gravatarsDisabled = document.getElementById('gravatars_disabled');
	var commentNotificationDt = document.getElementById('comment_notification_dt');
	var commentNotificationDd = document.getElementById('comment_notification_dd');
	var reverseCommentsDt = document.getElementById('reverse_comments_dt');
	var reverseCommentsDd = document.getElementById('reverse_comments_dd');
	var gravatarsDt = document.getElementById('gravatars_dt');
	var gravatarsDd = document.getElementById('gravatars_dd');
	var smiliesDt = document.getElementById('smilies_dt');
	var smiliesDd = document.getElementById('smilies_dd');
	var gravatarSizeDt = document.getElementById('gravatar_size_label');
	var gravatarSizeInput = document.getElementById('gravatar_size');
	
	if (enableComments) {
		if (!enableComments.checked) {
		
		
		
		
		
			commentNotificationDisabled.disabled = true;
			commentNotificationEnabled.disabled = true;
			reverseCommentsDisabled.disabled = true;
			reverseCommentsEnabled.disabled = true;
			smiliesEnabled.disabled = true;
			smiliesDisabled.disabled = true;
			gravatarsEnabled.disabled = true;
			gravatarsDisabled.disabled = true;
			gravatarSizeInput.disabled = true;
			commentNotificationDt.className = 'gray line_through';
			commentNotificationDd.className = 'gray line_through';
			reverseCommentsDt.className = 'gray line_through';
			reverseCommentsDd.className = 'gray line_through';
			gravatarsDt.className = 'gray line_through';
			gravatarsDd.className = 'gray line_through';
			smiliesDt.className = 'gray line_through';
			smiliesDd.className = 'gray line_through';
			gravatarSizeDt.className = 'gray line_through';
		}
	}
	
	/** public view, comment form, additional information */
	var showInfoBtn = document.getElementById('add_info');
	var commentFormName = document.getElementById('publicCommentFormName');
	var commentFormHomepage = document.getElementById('publicCommentFormHomepage');
	var commentFormGravatar = document.getElementById('publicCommentFormGravatar');

	if (showInfoBtn) {
		hideElement('add_info');

		commentFormName.value = inputFieldNameValue;
		commentFormHomepage.value = inputFieldUrlValue;
		commentFormGravatar.value = inputFieldGravatarValue;

		commentFormName.onfocus = commentFormHomepage.onfocus = commentFormGravatar.onfocus = function() {
			this.value = '';
		};

		commentFormName.onblur = function() {
			if (commentFormName.value.length == 0) {
				this.value = inputFieldNameValue;
			}
		};

		commentFormHomepage.onblur = function() {
			if (commentFormHomepage.value.length == 0) {
				this.value = inputFieldUrlValue;
			}
		};

		commentFormGravatar.onblur = function() {
			if (commentFormGravatar.value.length == 0) {
				this.value = inputFieldGravatarValue;
			}
		};
	}
}