// Validate serach form
function validateSearch(form) {
	if(!validateEmpty(form.stype, '검색 타입을 선택해주세요.')) return false;
	if(!validateEmpty(form.squery, '검색어를 입력해주세요.')) return false;
	
	return true;
}

// Add event handler on new page.
function addEventHandlerOnNew() {
	var btnAttachment = document.getElementById('btnAttachment');
	var btnCancel = document.getElementById('btnCancel');
	
	// Add event handler.
	if(btnAttachment) {
		btnAttachment.onclick = function() {
			addAttachment();
			return false;
		}
		
		// Add a attachment.
		addAttachment();
	}	
	btnCancel.onclick = function() {
		history.back();
		return false;
	}
}

// Validate post form.
function validatePost(form) {
	// Title
	if(!validateEmpty(form.post_title, '제목을 입력해주세요.')) return false;
	
	// Content
	if(document.getElementById('post_content')) {
		if(!validateEmpty(form.post_content, '내용을 입력해주세요.')) return false;
	} else {
		var oEditor = FCKeditorAPI.GetInstance('post_content_editor');
		content = oEditor.GetHTML();
		content = content.gsub(/<p>&nbsp;<\/p>/, '').strip();	// Remove blank.
		if(content == '') {
			showMessage('내용을 입력해주세요.', 'warn');
			return false;
		}	
	}	
	
	return true;
}

// Add event handler on show page.
function addEventHandlerOnShow() {
	var btnClip = document.getElementById('btnClip');
	var btnDelete = document.getElementById('btnDelete');
	
	if(btnDelete) {
		btnDelete.onclick = function() {
			deletePost();
			return false;
		}
	}	
	if(btnClip) {
		btnClip.onclick = function() {
			var clip_id = this.getAttribute('href').split('#')[1];
			clipPost(clip_id);
			return false;
		}
	}
}

// Clip a post.
function clipPost(post_id) {
	var params = 'clippable_type=Post&clippable_id=' + post_id;
	new Ajax.Request('/my/clippings', {
		asynchronous: true, method: 'post', parameters: params, evalScripts: true
	});
}

// Delete a post.
function deletePost() {
	if(confirm('게시물을 삭제하시겠습니까?')) {
		var form = document.getElementById('delete_form');
		form.submit();
	}
}

// Validate comment form.
function validateComment(form) {
	// Content
	if(!validateEmpty(form.comment_content, '내용을 입력해주세요.')) return false;
	
	var params = Form.serialize(form);
	new Ajax.Request('/comments', {
		asynchronous: true, parameters: params, method: 'post', evalScripts: true,
		onComplete: function(transport) {
			$('comment_form').reset();
		}
	});
	
	return false;
}

// Delete a comment.
function deleteComment(comment_id) {
	if(confirm('댓글을 삭제하시겠습니까?')) {
		new Ajax.Request('/comments/' + comment_id, {
			asynchronous: true, method: 'delete', evalScripts: true
		});
	}
}

// Add event handler on edit page.
function addEventHandlerOnEdit() {
	var btnAttachment = document.getElementById('btnAttachment');
	var btnDeleteAttach = document.getElementById('btnDeleteAttach');
	var btnCancel = document.getElementById('btnCancel');
	
	// Add event handler.
	if(btnAttachment) {
		btnAttachment.onclick = function(){
			addAttachment();
			return false;
		}
		
		// Add a attachment.
		addAttachment();
	}
	$$('a.btnDeleteImage').each(function(link) {
		link.onclick = function() {
			// ex. #<attachment_id>_<attach_type>
			var attach = link.getAttribute('href').split('#')[1].split('_');
			deleteAttachment(attach[0], attach[1]);
			return false;
		}
	});
	if(btnDeleteAttach) {
		btnDeleteAttach.onclick = function() {
			var attachment_id = this.getAttribute('href').split('#')[1];
			deleteAttach(attachment_id);
			return false;
		}
	}
	btnCancel.onclick = function() {
		history.back();
		return false;
	}
}

// Delete a attachment.
function deleteAttachment(attachment_id, attach_type) {
	var confirm_msg = '';
	if(attach_type == 1) confirm_msg = '이미지를 삭제하시겠습니까?'
	else confirm_msg = '첨부파일을 삭제하시겠습니까?'
	
	if(confirm(confirm_msg)) {
		new Ajax.Request('/attachments/' + attachment_id, {
			asynchronous: true, method: 'delete', evalScripts: true,
			onLoading: showAjaxWaiting,
			onComplete: function(xhr) {
				hideAjaxWaiting();
				$('attachment_' + attachment_id + '_id').value = '';
				$('attachment_' + attachment_id + '_description').value = '';
				$('attachment_' + attachment_id).update('');
				$('attachment_' + attachment_id).hide();
			}
		});
	}
}