function viewComment(url, codeNo) {

   if($('#commentFrame' + codeNo).css('height')=='0px') $('#commentFrame' + codeNo)[0].src = url;
   else $('#commentFrame' + codeNo).css('height', '0px');

}

function viewTrackback(url, codeNo) {

   if($('#trackbackFrame' + codeNo).css('height')=='0px') $('#trackbackFrame' + codeNo)[0].src = url;
   else $('#trackbackFrame' + codeNo).css('height', '0px');	 

}

// 게시물 신고 확인
function checkFormReport() {

   if (!$('#subject').val()) {

    alert(_i('신고사유를 작성하세요.'));
    $('#subject').focus();
    return false;

   }

   return true;

}

// 게시판 검색시 검색 내용 확인
function checkFormSearch() {

   if(!$('#keyword').val()) { 

	alert(_i('검색어를 입력하세요.'));
	$('#keyword').focus();
	return false; 
	
   }

   return true;

}

// 게시물 작성 확인
function checkFormBoardWrite() {

   if((typeof($('#name').val())!='undefined')&&!$('#name').val()) {

    alert(_i('닉네임을 입력하세요.'));
    $('#submit').css('visibility', ''); 
    $('#name').focus();
    return false;

   }

   if((typeof($('#passwd').val())!='undefined')&&!$('#passwd').val()) {

    alert(_i('비밀번호를 입력하세요.'));
    $('#submit').css('visibility', ''); 
    $('#passwd').focus();
    return false;

   }

   if((typeof($('#name').val())!='undefined')&&($('#passwd').val().length<4)) {

    alert(_i('비밀번호는 최소 4자 이상 적어주세요.'));
    $('#submit').css('visibility', ''); 
    $('#passwd').focus();
    return false;

   }   

   if((typeof($('#categoryNo').val())!='undefined')&&!$('#categoryNo').val()) {

    alert(_i('카테고리를 선택하세요.'));
    $('#submit').css('visibility', ''); 
    $('#categoryNo').focus();
    return false;

   }

   if(!$('#subject').val()) {

    alert(_i('제목을 입력하세요.'));
    $('#submit').css('visibility', ''); 
    $('#subject').focus();
    return false;

   }

   if(!$('#content').val()) {

    alert(_i('내용을 입력하세요.'));
    $('#submit').css('visibility', ''); 
    return false;

   }

   if((typeof($('#writeKey').val())!='undefined')&&!$('#writeKey').val()) {

    alert(_i('자동글 방지 번호를 정확히 입력하세요.'));
    $('#submit').css('visibility', ''); 
    return false;

   }

   wysiwygs($('#content')).fixHTML(); 
   wysiwygs($('#content')).copyToTextarea(); 

   return true;
   
}

// 댓글 작성 확인
function checkFormCommentWrite() {

   if((typeof($('#commentName').val())!='undefined')&&!$('#commentName').val()) {

    alert(_i('닉네임을 입력하세요.'));
    $('#commentName').focus();
    return false;

   }

   if((typeof($('#commentPasswd').val())!='undefined')&&!$('#commentPasswd').val()) {

    alert(_i('비밀번호를 입력하세요.'));
    $('#commentPasswd').focus();
    return false;

   }

   if(!$('#commentContent').val()) {

    alert(_i('댓글 내용을 입력하세요.'));
    return false;

   }

   if((typeof($('#commentWriteKey').val())!='undefined')&&!$('#commentWriteKey').val()) {

    alert(_i('자동글 방지 번호를 정확히 입력하세요.'));
    return false;

   }

   return true;
   
}

// 댓글 레이어 저장
function setComment(value) {

   commentWrite = value;   

}

// 댓글 레이어 
function layerComment(mode, codeSubNo, name) {

   if(!mode) mode = 'write';
   if(!name) name = '';

   switch(mode) {

	 case 'reply' : commentLayer = '#commentReply' + codeSubNo; break;
	 case 'modify' : commentLayer = '#commentModify' + codeSubNo; break;
	 default : commentLayer = '#wrapCommentWrite'; break;

   }

   if(saveCommentLayer!=commentLayer) {

    if(saveCommentLayer) {
     
	 $(saveCommentLayer).css('display', 'none');
	 $(saveCommentLayer).html('');

    }

	$(commentLayer).css('display', 'block');
	$(commentLayer).html(commentWrite);
	var action = $(commentLayer + '>#formCommentWrite')[0].action;
    $(commentLayer + '>#formCommentWrite')[0].action = action.replace(/\/comment\-.+\-ok/gi, '/comment-' + mode + '-ok');

    if(mode!='write') $(commentLayer + '>#formCommentWrite>input.codeSubNo').val(codeSubNo);
	if(mode=='modify') {

     var html = $('#commentContent' + codeSubNo).html();
     if(agent.IE) html = html.replace(/(<br>)|(<br \/>)/gi, "\r\n"); else html = html.replace(/(<br>)|(<br \/>)/gi, "");
	 html = html.replace(/^\s*|\s*$/g, '');

     $('#commentContent').val(html);
     $('#commentName').val(name);
     if(parseInt($('#commentUseSecret' + codeSubNo).html())) $('#commentUseSecret').attr('checked', 'checked');
	 else $('#commentUseSecret').removeAttr('checked');

	}

    saveCommentLayer = commentLayer;

   }

}

function addSurvey(limitSurvey, imgPath) {

   var surveyCount = $('#survey>p.surveyTxt').length;

   if(limitSurvey<=surveyCount) {

	alert(_i('더이상 설문조사 항목을 추가 할 수 없습니다.'));
	return false;

   }

   var html = '<p class="line surveyTxt"><label for="surveyTxt' + surveyCount + '">' + _i('항목') + '</label><input type="text" id="surveyTxt' + surveyCount + '" name="surveyTxt[]" value="" maxlength="50" /><span class="deleteSurveyBt"><a href="javascript:;" onclick="$(this).parent().parent().remove();"><img src="' + imgPath + '/imgs/write_bt_survey_delete.gif" alt="' + _i('항목삭제') + '" /></a></span></p>';

   $('#survey').append(html);

}

function addTag(tag, code, limitTag, imgPath) {

   if(!tag) return false;

   if($('#tagList').val().split(',').length>=limitTag) {

    alert(_i('태그 등록에 제한은 %d개까지 입니다.', limitTag));
	return false;

   }

   $('#tagBt').css('visibility', 'hidden'); 

   $.ajax({
     url: '/board/tag',
     data: 'code=' + code + '&tag=' + encodeURIComponent(tag),
     type: 'GET',
     dataType: 'text',
     async: true,
     timeout: 5000,
     error: function() {
      alert(_i('데이터를 불러올 수 없습니다. 다시 시도하세요.'));
      $('#tagBt').css('visibility', ''); 
     },
	 success: function(text) {

      if(text) {

       alert('\'' + text + '\' ' + _i('태그를 등록하였습니다.'));

       var tagList = $('#tagList').val();
	   var tagHtml = '<span class="tag">' + text + ' <a href="javascript:;" onclick="deleteTag(this, \'' + text + '\', \'' + code + '\', \'write\');"><img src="' + imgPath + '/imgs/write_bt_tag_delete.gif" alt="' + _i('태그삭제') + '" /></a>&nbsp;</span>';

	   $('#tagList').val((tagList ? tagList + ',' : '') + text);
	   $('p.tagList').html($('p.tagList').html() + tagHtml).attr('class', 'tagList');

      } else alert('\'' + $('#tag').val() + '\' ' + _i('태그를 등록 할 수 없습니다.'));

      $('#tag').val(''); 
      $('#tagBt').css('visibility', ''); 

	 }
   });

}

function deleteTag(obj, tag, code, type) {

   if(!tag) return false;

   $.ajax({
     url: '/board/tag-delete',
     data: 'code=' + code + '&tag=' + encodeURIComponent(tag),
     type: 'GET',
     dataType: 'text',
     async: true,
     timeout: 5000,
     error: function() {
      alert(_i('데이터를 불러올 수 없습니다. 다시 시도하세요.'));
     },
	 success: function(text) {

      alert('\'' + text + '\' ' + _i('태그를 삭제하였습니다.'));
	  if(obj) $(obj).parent().remove();

	  if(type=='write') {
       var arrTagList = $('#tagList').val().split(',');
	   $('#tagList').val('');
	   for(var i=0, count=arrTagList.length; i<count; i++) {
		var _tagList = $('#tagList').val();
        if(tag!=arrTagList[i]) $('#tagList').val((_tagList ? _tagList + ',' : '') + arrTagList[i]);
	   }
	  }

	 }
   });

}

// 파일 업로드
function addFile(part, fileType, fileName, filePath, fileSize, codeNo, fileNo) {

   $('#uploadList')[0].options.add(new Option(fileName, fileType + '|' + filePath + '|' + fileSize + ((typeof(codeNo)!='undefined')?'|' + codeNo:'') + ((typeof(fileNo)!='undefined')?'|' + fileNo:''))); 

}

function addThumnail(up) {

   if(up) $('#thumnail').val(1);

}

// 이미지 파일 내용 삽입
function addFileContents(str) {

   str = base64_decode(str);
   str = '<img src="/data/' + str + '" alt="" class="imgView" /><br />';

   wysiwygs($('#content')).pasteHTML(str);

}

// 파일 삭제
function deleteFile(code) {

   var uploadList = $('#uploadList')[0];

   if(!uploadList.value) {

    alert(_i('파일을 선택하세요.'));
	return false;

   }

   var fileInfo = '', fileIndex = new Array();

   for(var i=0; i<uploadList.length; i++) {
    if(uploadList.options[i].selected) {
	 fileInfo += '&fileInfo[]=' + uploadList.options[i].value;	
	 fileIndex.push(i);
	}
   }

   fileIndex.reverse();

   $.ajax({
     url: '/board/file-delete',
     data: 'code=' + code + fileInfo,
     type: 'POST',
     dataType: 'text',
     async: true,
     timeout: 5000,
     success: function(text) {
        for(var i=0; i<fileIndex.length; i++) uploadList.options[fileIndex[i]] = null;
     }
   });

}

// 파일 업로드 확인
function checkFormFileUpload() {

   if(!$('#file').val()) { 

    alert(_i('파일을 선택하세요.')); 
	return false; 
	
   } else {

    $('#upload').hide(); 
	$('#loading').show();

   }   

   return true;

}

var totalUploadFileSize = 0;
var arrTotalUploadFile = new Array();

function fIsUpload() {

   alert(_i('업로드 중입니다. 잠시만 기다려주세요.'));

}

function fFileClear() { 

   totalUploadFileSize = 0;
   arrTotalUploadFile = new Array();

   $('#fileUploadBar').css('width', parseInt($('#fileProgress').css('width')) + 'px');
   $('#filePecent').html('100%');
   $('#fileUploadBar').css('width', '0px');
   $('#filePecent').html('');

   $('#fileProgress').addClass('none');

}

function fFileLimitCount() {

   alert(_i('더이상 파일을 업로드 할 수 없습니다.'));

}

function fFileCompleteData(data) { 

   var obj = eval('(' + data + ')');   

   if(obj.errorMsg) { 

    alert(obj.errorMsg);  
	return false;

   }

   if(obj.thumnail) addThumnail(true);
   else if(obj.upload) addFile(((obj.upload.part==null) ? obj.upload.part : ''), obj.upload.fileType, obj.upload.fileName, obj.upload.filePath, obj.upload.fileSize);  

}

function fFileStatus(totalFile, totalFileSize) { 

   if(totalFile) {

   $('#fileProgress').removeClass('none');
    totalUploadFileSize = totalFileSize;

   } else {

    totalUploadFileSize = 0;
    arrTotalUploadFile = new Array();

   }

}

function fFileProgress(fileName, uploadFileSize, fileSize) {

   var _totalUploadFileSize = 0;
   arrTotalUploadFile[fileName] = uploadFileSize;

   for(var i in arrTotalUploadFile) _totalUploadFileSize += parseInt(arrTotalUploadFile[i]);	   
   var percent = Math.round(_totalUploadFileSize/totalUploadFileSize*100);

   $('#fileUploadBar').css('width', (((parseInt($('#fileProgress').css('width'))-40)/100)*percent) + 'px');
   $('#filePecent').html(percent + '%');

}
