﻿function recommend(channel, articleID) {

	if(channel.length != 0 ) {			
		goUrl(web_login + "&return_url=" + escape( 'http://'+hostname+'/common/inc/loginCheck_yahoo?id=3&url='+escape(window.location)+'&articleID='+articleID+'&command='+uri_part+'RecommendArticle' ));
	}else {
		if( isLogged() == 'true' ) {		
			var parameters="score=1&articleID=" + articleID;
			new Ajax.Request("RecommendArticle",
				{
					method:'post',
					encoding: 'UTF-8',
					parameters: parameters ,
					onComplete: recommend_response,
					onFailure: function()
					{
						alert("추천에 실패했습니다. ");
					}
				}
			);
		} else {		
			goUrl(web_login + "?site_id=1&return_url=" + escape(window.location));
		}
	}
}
function recommendArticle(articleID, command){
	var parameters="score=1&articleID=" + articleID;
	new Ajax.Request(command,
		{
			method:'post',
			encoding: 'UTF-8',
			parameters: parameters ,
			onComplete: recommend_response,
			onFailure: function()
			{
				alert("추천에 실패했습니다. ");
			}
		}
	);
}

function recommend_response(request){

	var result = eval(request.responseText);
	if ( result == 0 ) {
		alert("이미 추천하셨습니다.");
	}else if ( result == 1 ) {
		alert("추천하셨습니다.");
		location.reload();
	}
}

function goWriteArticleForm(channel) {	
	try {
		if(channel.length == 0) {
			goUrl("write");
		} else {
			goUrl(web_login + "&return_url="  + escape('http://'+hostname+uri_part+'write') );
		}
	}catch(e) {
		alert(e.description);
	}
}

function goModifyArticleForm(channel, articleId) {
	try {	
		var hash = {
			articleID : articleId
		};
		var parameters  = $H(hash).toQueryString(); 		
		
		if(channel.length == 0)  {
			goUrl("modify?"+parameters);
		} else {
			goUrl(web_login + "&return_url="  + escape('http://'+hostname+uri_part+'modify?'+parameters) );
		}
	}catch(e) {
		alert(e.description);
	}
}

function goDeleteArticleForm(channel, articleId) {
	try {
		var hash = {
			articleID : articleId
		};
		var parameters  = $H(hash).toQueryString(); 	
		if(confirm('글을 삭제하시겠습니까?')) {
			if(channel.length == 0)  {
				goUrl("DeleteArticle?"+parameters);
			} else {
				goUrl(web_login + "&return_url="  + escape('http://'+hostname+uri_part+'DeleteArticle?'+parameters) );
			}
		}
	}catch(e) {
		alert(e.description);
	}
}

function goReWriteArticleForm(channel, articleId) {
	try {
		var hash = {
			articleID : articleId
		};
		var parameters  = $H(hash).toQueryString(); 	
		if(channel.length == 0)  {
			goUrl("reWrite?"+parameters);
		} else {
			goUrl(web_login + "&return_url="  + escape('http://'+hostname+uri_part+'reWrite?'+parameters) );
		}
	}catch(e) {
		alert(e.description);
	}
}


function writeComment(channel){
	var writeForm = document.WriteComment;
	var params = Form.serialize('WriteComment');
	
	if( isLogged() == 'true' ) {
	
		if(commentCheck()){
			writeForm.action = "WriteComment";
			writeForm.submit();			
		}
		
	} else {
		goUrl(web_login + "&return_url=" + escape(window.location));
	}
} 

function deleteCommentForEvent(articleID, idx, subIdx) {
	if(confirm('댓글을 삭제하시겠습니까?')) {
		location.href = '/event/comment/DeleteComment?articleID='+articleID+'&idx='+idx+'&subidx='+subIdx+'&queryString=eventID%3D'+articleID;
	}
}

function writeCommentForEvent(channel){
	var writeForm = document.WriteComment;
	var params = Form.serialize('WriteComment');
	if( isLogged() == 'true' ) {
		if(commentCheck()){  			
			writeForm.action = "/event/comment/WriteComment";
			writeForm.submit();
		}
	} else {
		goUrl(web_login + "&return_url=" + escape(window.location));
	}
} 

function commentCheck(){
	var content = document.getElementById('content');
	
	if(content == null || content.value.trim() == '') {
		alert('댓글을 입력해 주세요.');
		content.focus();
		return false;
	}
	if(content.value.length > 300) {
		alert('댓글은 300자까지만 가능합니다.');
		content.value = content.value.substring(0,300);
		content.focus();
		return false;
	}
	if(content.value.length < 2) {
		alert('댓글은 2자 이상 입력해야합니다.');
		content.focus();
		return false;
	}
	if($('input_schedule_ymd') != null ) {
		$('schedule_hm').value = $('hour').value + $('min').value;
		$('schedule_ymd').value = $F('input_schedule_ymd');

		if ($F('schedule_ymd') < $F('today')){
			alert("유효한 약속 날짜를 선택 해주세요.");
			return false;
		}
	}	
	return true;	
}