/*
 * ??R??????????g??????W
 *
 * Copyright (c) 2003 DRECOM CO.,LTD. All rights reserved.
 * 
 * info@drecom.co.jp
 * http://www.drecom.co.jp/
 */



var COMMENT_FORM_NAME               = 'WriteCommentForm';
var COMMENT_TEXTAREA_NAME           = 'comment';
var COMMENT_COOKIE_CHECKBOX_NAME    = 'EatCookie';
var COMMENT_PREVIEW_ID              = 'comment_preview';
var COMMENT_TOOLBAR_POSMARK_ID      = 'comment_toolbar_position_marker';

var COMMENT_AUTHOR_NAME             = 'author';
var COMMENT_EMAIL_NAME              = 'email';
var COMMENT_URL_NAME                = 'url';
var gCommentSaveCookieNames = [COMMENT_AUTHOR_NAME, COMMENT_EMAIL_NAME, COMMENT_URL_NAME];

var gCommentForm               = null;
var gCommentTextArea           = null;
var gCommentSaveCookieCheckbox = null;

// *** Cookie ***//
var COMMENT_COOKIE_NAME         = 'blog.comment.preferences';
var COMMENT_COOKIE_EXPIRE_DAYS  = 30; 

var gCommentCookie = new Cookie(document, COMMENT_COOKIE_NAME, COMMENT_COOKIE_EXPIRE_DAYS * 24);

loadCommentCookie.loaded = false;
function loadCommentCookie()
{
	var loaded = false;
	
	if (null == gCommentForm || null == gCommentSaveCookieCheckbox) {
		return;
	}
	
	loaded = gCommentCookie.load();
	
	// load ??????????????????????f
	loadCommentCookie.loaded = loaded;
	if (gCommentSaveCookieCheckbox != null) {
		gCommentSaveCookieCheckbox.checked = loaded;
		if (false == loadCommentCookie.loaded) {
			return;
		}
	}
	
	for (var i = 0; i < gCommentSaveCookieNames.length; i++) {
		var nm = gCommentSaveCookieNames[i];
		var t  = null;
		var v  = null;
		
		if (null == nm) continue;
		t = gCommentForm[nm];
		v = gCommentCookie[nm];
		if (null == v || null == t) continue;
		
		t.value = v;
	}
}
function storeCommentCookie()
{
	for (var i = 0; i < gCommentSaveCookieNames.length; i++) {
		var nm = gCommentSaveCookieNames[i];
		var t  = null;
		
		if (null == nm) continue;
		t = gCommentForm[nm];
		if (null == t || null == t.value) continue;
		
		gCommentCookie[nm] = t.value;
	}
	gCommentCookie.store();
	loadCommentCookie.loaded = true;
}
function removeCommentCookie()
{
	if (loadCommentCookie.loaded) {
		gCommentCookie.remove();
	}
}

/**
 * checkbox - onclick
 */
function handleEatCookieCheckBoxOnClick(aCheckBox)
{
	var checked = (true == aCheckBox.checked);
	if (checked) {
		storeCommentCookie();
	} else {
		removeCommentCookie();
	}	
}
/** form.onsubmit */
function comment_form_onsubmit()
{
	var lyer = XBSLayer.makeLayer(gColorPaletteID);
	
	// 2004-05-19  Takanori Ishikawa 
	// -----------------------------------------------------------
	// ??J??????[??p??????b??g??????@RGB ????????????????????????
	// HTML ??????\??????????AForm ????????????????????????????????????????????????????????
	// ????????????????
	if (lyer != null && lyer.isVisible()) {
		colorPaletteFieldValueDidAction();
		return false;
	}
	if (comment_form_onsubmit.tooLate) {
		return false;
	}
	comment_form_onsubmit.tooLate = true;
	
	var checked;
	checked = (gCommentSaveCookieCheckbox != null && true == gCommentSaveCookieCheckbox.checked);	
	if (checked) {
		storeCommentCookie();
	} else {
		removeCommentCookie();
	}
	return true;
}
function comment_setUp_textArea(textArea)
{
	// 2004-05-19  Takanori Ishikawa 
	// -----------------------------------------------------------
	// ??e??????v??????[??g??????W??????????[??U???? JavaScript ??????????????????????????????????????A????????????????????

	textArea.onfocus     = function(e){ };
	textArea.onmouseup   = function(e){ initializeSelectedText(this); cutOutTagFragmentOnSelectionArea(); renderPreview(this, COMMENT_PREVIEW_ID);}; 
	textArea.onchange    = function(e){ renderPreview(this, COMMENT_PREVIEW_ID); };
	textArea.onkeydown   = function(e){ initializeSelectedText(this); controlKeystroke(event); };
	textArea.onkeyup     = function(e){ initializeSelectedText(this); finalizeSelectedText(); renderPreviewOnKeyEvent(event, this, COMMENT_PREVIEW_ID); };
}

/** body.onload */
function comment_body_onload()
{
	/*
	 * HTML ??????L????????????????
	 * @see render.js
	 */
	Renderer.enableHTML = false;
	Renderer.isComment = true;
	
	gCommentForm = document[COMMENT_FORM_NAME];
	if (gCommentForm != null) {
		gCommentTextArea = gCommentForm[COMMENT_TEXTAREA_NAME];

		// null ??????????\????????????????
		gCommentSaveCookieCheckbox = gCommentForm[COMMENT_COOKIE_CHECKBOX_NAME];
		if (typeof gCommentTextArea != 'undefined') {
			comment_setUp_textArea(gCommentTextArea);
			renderPreviewAll(gCommentForm);
		}
	
		// UtilKit.addhock ???????? false ??????????????????L??????????Z????????????????????
		gCommentForm.onsubmit = comment_form_onsubmit;
		loadCommentCookie();
	}
	
	// window.onload ??????????s??????????????????????????A'creating' (??????????????t??????O)????
	// false ??????????????????????????B
	// - now_creating_entry.html
	// - entry_write_edit.jsp
	window.creating=false;
}

/* shortcut for render.js*/
function comment_render()
{
	if (gCommentTextArea != null) {
		renderPreview(gCommentTextArea, COMMENT_PREVIEW_ID);
	}
}

/**
 *	Post methods for Comment/Contact forms 
*/
function DRECOMCMSForm_E (id) { return document.getElementById (id); }

// Callback function on Comment form submitted
function DRECOMCMS_onSubmitCommentForm (form) {
	// submitted page is not loaded yet.
	document.commentpost = {
			isLoadedWriteCommentFormHiddenTarget : 0
	};

	// Start polling to check the submitted page loaded
	function _DRECOMCMS_callbackCommentFormSubmitDone () {
		if (! document.commentpost.isLoadedWriteCommentFormHiddenTarget) {
			setTimeout (_DRECOMCMS_callbackCommentFormSubmitDone, 100);
			return;
		}
		if (! document.commentpost.errorCode) {

			// if redirectURL is specified, redirect it after comment posted
			if (document.commentpost.redirectURL) {
				document.location.href = document.commentpost.redirectURL;
				return;
			}

			form.comment.value = '';
			var href = document.location.href;
			href = href.replace (/\?.*/, '') + '?post_success';
			document.location.href = href;
			return;
		}
		
		DRECOMCMSForm_E('divCommentErrMsg').innerHTML = document.commentpost.msgCommentErrMsg;
		DRECOMCMSForm_E('spanCommentErrMsgAuthor').innerHTML = document.commentpost.msgCommentErrMsgAuthor;
		DRECOMCMSForm_E('spanCommentErrMsgEmail').innerHTML = document.commentpost.msgCommentErrMsgEmail;
		DRECOMCMSForm_E('spanCommentErrMsgUrl').innerHTML = document.commentpost.msgCommentErrMsgUrl;
		DRECOMCMSForm_E('spanCommentErrMsgComment').innerHTML = document.commentpost.msgCommentErrMsgComment;
	}
	_DRECOMCMS_callbackCommentFormSubmitDone ();

	form.submit ();
}

// Callback function on Contact form submitted
function DRECOMCMS_onSubmitContactForm (form) {
	// submitted page is not loaded yet.
	document.commentpost = {
			isLoadedWriteCommentFormHiddenTarget : 0
	};

	// Start polling to check the submitted page loaded
	function _DRECOMCMS_callbackSubmitDone () {
		if (! document.commentpost.isLoadedWriteCommentFormHiddenTarget) {
			setTimeout (_DRECOMCMS_callbackSubmitDone, 100);
			return;
		}
		if (! document.commentpost.errorCode) {

			// if redirectURL is specified, redirect it after contact form posted
			if (document.commentpost.redirectURL) {
				document.location.href = document.commentpost.redirectURL;
				return;
			}

			form.body.value = '';
			var href = document.location.href;
			href = href.replace (/\?.*/, '') + '?post_success';
			document.location.href = href;
			return;
		}
		
		DRECOMCMSForm_E('ContactErrMsgAuthor').innerHTML = document.commentpost.msgCommentErrMsgAuthor;
		DRECOMCMSForm_E('ContactErrMsgEmail').innerHTML = document.commentpost.msgCommentErrMsgEmail;
		DRECOMCMSForm_E('ContactErrMsgBody').innerHTML = document.commentpost.msgCommentErrMsgComment;

/*
1009: ??????????????t??H??[??????????J??X??^??}??C??Y??????????C??????O??E??????[??????????????s??????????l??????????M??????????????u??????b??Z??[??W??????M??????v????????????????????????????????????????????
http://kagemai.drecom.co.jp/cgi-bin/admin.rbx?id=1009&project=drecomcms&action=view_report
*/
		// Re-present the hidden form again
		var eSendingMessage = DRECOMCMSForm_E('sendingMessage');
		if (eSendingMessage)
			eSendingMessage.style.display = 'none';
		document.WriteContactForm.style.display = '';
	}
	_DRECOMCMS_callbackSubmitDone ();
	
    if(form && form.scriptCheckFlag){
    	// Flag for protection against spam
    	form.scriptCheckFlag.value = '1';
	}else{
	  
	}

	// if contactform.js is loader
	if (typeof (ContactForm) != "undefined")
		ContactForm.eventSubmit ();
	form.submit ();
}


/* onmousedown??????????????????AonClick??????????????????????????????????Aup */
UtilKit.addhook(document, 'onmouseup', hideAllPalette);

// window.onload ??????????s??????????????????????????A'creating' (??????????????t??????O)????
// false ??????????????????????????B
// - now_creating_entry.html
// - entry_write_edit.jsp
window.creating=true;
UtilKit.addhook(window, 'onload', comment_body_onload);

