
function showAll(restWords, linkWord) {
    $(restWords).show();
	$(linkWord).hide();
	return false;
}
function clearSentence(target) {
	if($(target).value == $(target).defaultValue) {
		$(target).value = "";
	}
}

function upStep(currentId, stepId) {
	var currentElement = document.getElementById(currentId);
	var previousId = currentElement.previous('.step-area');

	var url = '/member/step/up/';
	var parameters = 'id=' + stepId;

	if (previousId != null) {
    
		new Ajax.Request(url, {
		method: 'post',
		parameters: parameters,
		onSuccess:function(httpObj){
			new Insertion.Before(previousId, currentElement);
			renumberStep();
		},
		onFailure:function(httpObj){
		},
		onException: function(httpObj) {
		}
		});
	}

}
function downStep(currentId, stepId) {
	var currentElement = document.getElementById(currentId);
	var nextId = currentElement.next('.step-area');

	var url = '/member/step/down/';
	var parameters = 'id=' + stepId;

	if (nextId != null) {

		new Ajax.Request(url, {
		method: 'post',
		parameters: parameters,
		onSuccess:function(httpObj){
    		new Insertion.After(nextId, currentElement);
			renumberStep();
		},
		onFailure:function(httpObj){
		},
		onException: function(httpObj) {
		}
		});
	}
}


function addStep(currentId, articleId, stepId) {
	var url = '/member/step/add/';
	var parameters = "article_id=" + articleId + "&step_id=" + stepId;

	new Ajax.Request(url, {
	method: 'post',
	parameters: parameters,
	onSuccess:function(httpObj){
        new Insertion.After(currentId, httpObj.responseText);
		renumberStep();
	},
	onFailure:function(httpObj){
	},
	onException: function(httpObj) {
	}
	});
	
}

function deleteStep(currentId, stepId) {
	var i = 0;
	$A( document.getElementsByTagName( 'h3' )).each(
		function (){
			i++;
		}
	);
	if(i==4){
		alert("STEPは最低１つ必要です。");	
	}else{
		if (confirm("削除しますか？")) {
			var url = '/member/step/delete/';
			var parameters = "id=" + stepId;
			var currentElement = document.getElementById(currentId);
	
			new Ajax.Request(url, {
				method: 'post',
				parameters: parameters,
				onSuccess:function(httpObj){
					currentElement.remove();
					renumberStep();
				},
				onFailure:function(httpObj){
				},
				onException: function(httpObj) {
				}
			});
		}
	}
}

function renumberStep() {
	var i = 1;
    $$(".step-image").each(function(obj){
     	obj.src = "/img/article/step" + i + ".gif";
		i++;
    });
}

function upArticle(articleId) {
	var url = '/member/article/up/';
	var parameters = "id=" + articleId;
//	var currentElement = document.getElementById(currentId);

	new Ajax.Request(url, {
	method: 'post',
	parameters: parameters,
	onSuccess:function(httpObj){
			
	},
	onFailure:function(httpObj){
	},
	onException: function(httpObj) {
	}
	});

}

function downArticle(articleId) {
	var url = '/member/article/down/';
	var parameters = "id=" + articleId;
//	var currentElement = document.getElementById(currentId);

	new Ajax.Request(url, {
	method: 'post',
	parameters: parameters,
	onSuccess:function(httpObj){
			
	},
	onFailure:function(httpObj){
	},
	onException: function(httpObj) {
	}
	});

}

function inputForm(id, input, max) {

	var str = document.getElementById(input).value.replace(/<[^<>]+>/g, '').replace(/&nbsp;/g, '');
	var restLength = parseInt(max) - str.length;
	if(restLength < 0){
		document.getElementById(input).value = str.substr(0,max);
		restLength = 0;
		document.getElementById(input).focus;
	}
    document.getElementById(id).innerHTML = restLength;
}

function displayDivision(showId, hideId) {
	hideDivision(hideId);
	showDivision(showId);
}

function showDivision(id) {
	var div = document.getElementById(id);
	div.style.display = 'block';
}
function showDivisionInline(id) {
	var div = document.getElementById(id);
	div.style.display = 'inline';
}
function hideDivision(id) {
	var div = document.getElementById(id);
	div.style.display = 'none';
}

function showInputForm(page, target, hide, max) {
    var formId = page + '-' + target + '-form';
    var viewId = page + '-' + target + '-' + hide;

	showDivision(formId);
	hideDivision(viewId);

	var lengthId = page + '-' + target + '-length';
	var input = page + '-' + target;
	//inputForm(lengthId, input, max);
	//$(input).focus();
}

function writeArticle(articleId, targetColumn) {
	var url = '/member/article/write/';
	var inputValue = document.getElementById('article-' + targetColumn).value;
	var parameters = 'id=' + articleId + '&targetColumn=' + targetColumn + '&inputValue='
		+ encodeURIComponent(inputValue);

	new Ajax.Request(url, {
		method: 'post',
		parameters: parameters,
		onSuccess:function(httpObj){
			var responseJson = httpObj.responseText.evalJSON();
			if (responseJson.result == 'false') {
				alert(responseJson.error);
				return false;
			}
			var formId = 'article-' + targetColumn + '-form';
			if(inputValue.length > 0) {
				var viewId = 'article-' + targetColumn + '-view-filled';
			}else{
				var viewId = 'article-' + targetColumn + '-view-empty';
			}
			hideDivision(formId);
			showDivision(viewId);
			if (targetColumn == 'title') {
				document.getElementById('article-title-modify').innerHTML = inputValue;
			}
			if (targetColumn == 'body') {
				inputValue = inputValue.escapeHTML();
				inputValue = inputValue.replace(/\r\n/g, "<br />");
				inputValue = inputValue.replace(/(\n|\r)/g, "<br />");
				document.getElementById('article-body-modify').innerHTML = inputValue;
			}
	},
	onFailure:function(httpObj){
	},
	onException: function(httpObj) {
	}
	});

}

function writeStep(articleId, stepId, targetColumn) {
	var url = '/member/step/write/';
	var inputValue = document.getElementById('step-' + stepId + '-' + targetColumn).value;
	var parameters = 'id=' + stepId + '&article_id=' + articleId + '&targetColumn=' + targetColumn + '&inputValue='
		+ encodeURIComponent(inputValue);

	new Ajax.Request(url, {
	method: 'post',
	parameters: parameters,
	onSuccess:function(httpObj){
                var responseJson = httpObj.responseText.evalJSON();
                if (responseJson.result == 'false') {
                        alert(responseJson.error);
                        return false;
                }

		var formId = 'step-' + stepId + '-' + targetColumn + '-form';
		if(inputValue.length > 0) {
			var viewId = 'step-' + stepId + '-' + targetColumn + '-view-filled';
		}else{
			var viewId = 'step-' + stepId + '-' + targetColumn + '-view-empty';
		}
		hideDivision(formId);
		showDivision(viewId);
		if (targetColumn == 'title') {
			if (inputValue == '') {
				inputValue = "新しいマニュアル";
			}
			document.getElementById('step-' + stepId + '-title-modify').innerHTML = inputValue;
		}
		if (targetColumn == 'body') {
			inputValue = inputValue.escapeHTML();
			inputValue = inputValue.replace(/\r\n/g, "<br />");
			inputValue = inputValue.replace(/(\n|\r)/g, "<br />");
			document.getElementById('step-' + stepId + '-body-modify').innerHTML = inputValue;
		}
	},
	onFailure:function(httpObj){
	},
	onException: function(httpObj) {
	}
	});

}

function cancelForm(page, id, target) {
	var url = '/member/' + page + '/get/';
	var parameters = 'id=' + id + '&column=' + target;

	new Ajax.Request(url, {
	method: 'post',
	parameters: parameters,
	onSuccess:function(httpObj){
		var input  = page + '-' + target;
		var formId = page + '-' + target + '-form';
		if(httpObj.responseText.length > 0) {
			var viewId = page + '-' + target + '-view-filled';
		}else{
			var viewId = page + '-' + target + '-view-empty';
		}
		document.getElementById(input).value = httpObj.responseText;
		hideDivision(formId);
		showDivision(viewId);
	},
	onFailure:function(httpObj){
	},
	onException: function(httpObj) {
	}
	});

}

function cancelFormStep(id, target) {
	var url = '/member/step/get/';
	var parameters = 'id=' + id + '&column=' + target;

	new Ajax.Request(url, {
		method: 'post',
		parameters: parameters,
		onSuccess:function(httpObj){
			var input  = 'step-' + id + '-' + target;
			var formId = 'step-' + id + '-' + target + '-form';
			if(httpObj.responseText.length > 0) {
				var viewId = 'step-' + id + '-' + target + '-view-filled';
			}else{
				var viewId = 'step-' + id + '-' + target + '-view-empty';
			}
			document.getElementById(input).value = httpObj.responseText;
			hideDivision(formId);
			showDivision(viewId);
		},
		onFailure:function(httpObj){
		},
		onException: function(httpObj) {
		}
	});
}

function createArticle(parentId, manualId, articleId) {
	if(!manualId) {
		alert('error');
	}

	var url = '/member/article/create/';
	var parameters;
	if(articleId) {
		parameters = 'manual_id=' + manualId + '&article_id=' + articleId;
	}else{
		parameters = 'manual_id=' + manualId;
	}
	new Ajax.Request(url, {
		method: 'post',
		parameters: parameters,
		onSuccess:function(httpObj){
	        new Insertion.Bottom(parentId, httpObj.responseText);
		},
		onFailure:function(httpObj){
		},
		onException: function(httpObj) {
		}
	});
}

function getPart(url, parameters) {
	new Ajax.Request(url, {
		method: 'post',
		parameters: parameters,
		onSuccess:function(httpObj){
			return httpObj.responseText;
		},
		onFailure:function(httpObj){
		},
		onException: function(httpObj) {
		}
	});
}

function switchArticle(articleId) {
	var url = '/member/article/get/';
	var parameters = 'id=' + articleId + '&column=disclosure_code';

	new Ajax.Request(url, {
		method: 'post',
		parameters: parameters,
		onSuccess:function(httpObj){
			
			if(confirm('非表示にしますか？')) {
				var url = '/member/article/undisp/';
				var parameters = 'id=' + articleId;

				new Ajax.Request(url, {
					method: 'post',
					parameters: parameters,
					onSuccess:function(httpObj){
				},
				onFailure:function(httpObj){
				},
				onException: function(httpObj) {
				}
			});
	}

		},
		onFailure:function(httpObj){
		},
		onException: function(httpObj) {
		}
	});

}
function deleteArticle(articleId) {
	if(confirm('削除しますか？')) {
		var url = '/member/article/delete/';
		var parameters = 'id=' + articleId;

		new Ajax.Request(url, {
			method: 'post',
			parameters: parameters,
			onSuccess:function(httpObj){
			},
			onFailure:function(httpObj){
			},
			onException: function(httpObj) {
			}
		});
	}
}

function uploadImage(page,src) {
	hideItemWindow();
//	var viewEmptyId = page + '-image-view-empty';
	var viewFilledId = page + '-image-view-filled';
  var viewYoutubeId = page + '-youtube-space';
	if(src){
		var imageId = page + '-image';
		showDivision(viewFilledId);
		hideDivision(viewYoutubeId);
//		hideDivision(viewEmptyId);
		document.getElementById(imageId).src = '/files/' + src;
	}else{
//		showDivision(viewEmptyId);
		hideDivision(viewFilledId);
		hideDivision(viewYoutubeId);
	}
}

function uploadYoutube(page,src) {


	hideItemWindow();
//	var viewEmptyId = page + '-image-view-empty';
	var viewFilledId = page + '-image-view-filled';
	    var viewYoutubeId = page + '-youtube-space';
	if(src){
	        $(viewYoutubeId).innerHTML = createYoutubeTag(src);
	        showDivision(viewYoutubeId);
		hideDivision(viewFilledId);
	}else{
//		showDivision(viewEmptyId);
		hideDivision(viewYoutubeId);
		hideDivision(viewFilledId);
	}
}

function createYoutubeTag(id , target) {
  if (target == "article")
    return '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/'+ id +'" id="article-youtube-movie"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' +id + '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355" id="article-youtube-embed"></embed></object>';
  else
    return '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/'+ id +'" id="article-youtube-movie"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' +id + '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355" id="article-youtube-embed"></embed></object>';

//  return '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/'+ id +'" id="article-youtube-movie"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' +id + '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355" id="article-youtube-embed"></embed></object>';
}

function setArticleCategory(articleId){
	var url = '/member/article/write/';
	var inputValue = $('category').value;
	var parameters = 'id=' + articleId + '&targetColumn=category_id&inputValue='
		+ encodeURIComponent(inputValue);

	new Ajax.Request(url, {
		method: 'post',
		parameters: parameters,
		onSuccess:function(httpObj){
			$('category-image').src = "/img/categ/" + $('category').value + "_off.gif";
		},
		onFailure:function(httpObj){
		},
		onException: function(httpObj) {
		}
	});

}
function setKeyword(articleId, keywordId) {
	var url = '/member/article/register-keyword/';
	var keyword = document.getElementById(keywordId).value;
	var parameters = 'id=' + articleId + '&keyword=' + encodeURIComponent(keyword); 

	new Ajax.Request(url, {
		method: 'post',
		parameters: parameters,
		onSuccess:function(httpObj){
			new Ajax.Request('/member/article/get/', {
				method: 'post',
				parameters: 'id=' + articleId + '&column=keyword',
				onSuccess:function(httpObj){
					document.getElementById('keyword-dis').innerHTML = httpObj.responseText;
					$('keyword_input').value = '';
				},
				onFailure:function(httpObj){
				},
				onException: function(httpObj) {
				}
			});
		},
		onFailure:function(httpObj){
		},
		onException: function(httpObj) {
		}
	});
}

function closeKeyword(articleId, keyword, target) {
	var url = '/member/article/close-keyword/';
	var parameters = 'id=' + articleId + '&keyword=' + encodeURIComponent(keyword);

	var targetElement = document.getElementById(target);
	new Ajax.Request(url, {
	method: 'post',
	parameters: parameters,
	onSuccess:function(httpObj){
		new Ajax.Request('/member/article/get/', {
		method: 'post',
		parameters: 'id=' + articleId + '&column=keyword',
		onSuccess:function(httpObj){
			$(target).update(httpObj.responseText);
		},
		onFailure:function(httpObj){
		},
		onException: function(httpObj) {
		}
		});
	},
	onFailure:function(httpObj){
	},
	onException: function(httpObj) {
	}
	});
}
function cancelKeywordForm(id) {
	$(id).value = "";
	$(id).focus();
}
function evaluateArticle(evaluation, articleId, displayField, historyId) {
	var url = '/evaluate/' + evaluation + '/';
	var parameters = 'article_id=' + articleId;
	new Ajax.Request(url, {
	method: 'post',
	parameters: parameters,
	onSuccess:function(httpObj){
		var responseJson = httpObj.responseText.evalJSON();
		if (responseJson.result == 'false'){
			alert('評価に失敗しました。\n時間をおいてやり直してください。');
			return;
		}
		new Ajax.Request('/evaluate/end-evaluate/', {
		method: 'post',
		onSuccess:function(httpObj){
			$(displayField).update(httpObj.responseText);
            updateParameter('evaluate-parent-area', 'common/evaluate_detail_finish.tpl', articleId, historyId);
		},
		onFailure:function(httpObj){
			alert('通信エラーが起きました\n画面をリロードしてください。');
			return;
		},
		onException: function(httpObj) {
			alert('通信エラーが起きました\n画面をリロードしてください。');
			return;
		}
		});
	},
	onFailure:function(httpObj){
	},
	onException: function(transport, ex) {
	}
	});}



