// JavaScript Document						   
function commentSub(){
		
		var msg = FCKeditorAPI.GetInstance('editor_txt').GetXHTML();
		
		if(msg == ""){
			alert("กรุณากรอกรายละเอียดด้วยคะ");
		}else{
            $("#subComm").attr("disabled","true");
			var UserID = $('#UserID').val();
			var RefID = $('#RefID').val();
			var CmmOption = $('#CmmOption').val();
			var owner = $('#memberId').val();
			$.ajax({
				url:'modules/comment/formCommentSub.php',
				cache: true,
				data:{'UserID':UserID,'RefID':RefID,'CmmOption':CmmOption,'messageXHTML':msg,'ownerId':owner},
				type:'POST',
				success:function(data){
					if(data == 1){						
						getFrmComment();						
					}else if(data == -50){
                        sessionTimeout();
                    }else{
						alert(data);	
					}	
				}
				
			});
			
		}
		
		return false;
}


function getFrmComment(){
	var RefID = $('#RefID').val();
	var CmmOption = $('#CmmOption').val();
	$.ajax({
		url:'modules/comment/formComment.php', 
		cache: true, 
		data:{'reqTypeCmt':'ajax','refId':RefID,'option':CmmOption}, 
		type:'POST', 
		success:function(data){
				if(CmmOption != 'sms'){
					$('#showAllComment').empty().append(data);
				}else{
					$('#showComment'+RefID).append(data);
				}
				getComment();
		}
	});
}//E-func

function getComment(){
	var RefID = $('#RefID').val();
	var CmmOption = $('#CmmOption').val();
	$.ajax({
		url:'modules/comment/comment.php', 
		cache: true, 
		data:{'reqTypeCmt':'ajax','refId':RefID,'option':CmmOption}, 
		type:'POST', 
		success:function(data){
			    if(CmmOption != 'sms'){
					$('#showAllComment').append(data);	
				}else{
					$('#showComment'+RefID).append(data);
				}
		}
	});
}//E-func

function showEditorCommentProfile(refId){
   var mId;
   if(refId==''){
     mId = $('#memberId').val();
   }else{
     mId = refId;     
   }

   $.ajax({
		url:'modules/comment/formProfileComment.php',
		cache: false,
		data:{'reqType':'ajax','mid':mId},
		type:'POST',
		success:function(data){
			if(refId==''){
              $('#editorCommentProfile').empty().append(data);
              $('#treditorCommentProfile').fadeIn('slow');
              $('#trAddComment').hide();
            }else{
              blockUI(data);
              $('#flgCommentProfile').val('reply');
            }
		}

	});
}

function hideEditorComment(){
   var mid = $('#refId').val();
   var flg = $('#flgCommentProfile').val();
   if($("#flgCommentProfile").val()!='reply' && $('#flgSaveProfile').val()=='save'){
      $.ajax({
          url:'modules/comment/commentProfile.php',
          cache: true,
          data:{'reqType':'ajax','mid':mid},
          type:'POST',
          success:function(data){
              $('#treditorCommentProfile').fadeOut('slow');
              $('#editorCommentProfile').empty();
              $('#trAddComment').fadeIn('slow');
              $('#showCommentProfile').empty().append(data);
          }

      });
   }else if(flg=='reply' && $('#flgSaveProfile').val()!='save'){
     $.unblockUI();
   }else if(flg=='reply' && $('#flgSaveProfile').val()=='save'){
     msgSuccess('บันทึกข้อมูลเรียบร้อยแล้วค่ะ');
   }else{
     msgSuccess('บันทึกข้อมูลเรียบร้อยแล้วค่ะ');     
   }
}

function addCommentJNMsg(refId){
	var nameEditor = $('#divNews').val();
    var msg = FCKeditorAPI.GetInstance('editorCommentJN').GetXHTML();
    $('#messageXHTML_comment').val(msg);
    $('#flgSaveProfile'+refId+nameEditor).val('save');
}


function commentJNSub(){
   var flag = false;
   var msg = $('#messageXHTML_comment').val();

   if(msg == '' || msg == '<br />'){
        //errHTML('tdErrComment','กรุณากรอกรายละเอียดด้วยคะ');
        //$('#trErrComment').show();
		msgConfirm('กรุณากรอกรายละเอียดด้วยคะ','$.unblockUI()');
   }else{
       $('#trErrComment').hide();
       $('#btnSubmit').attr("disabled","true");
       flag = true;
   }

   if(flag == false){
       return false;
   }else{
       
       return true;
       
   }
}

function commentJNSubSuccess(data){
    if(data == 1){
		var refId = $('#refId').val();
		var mId = $('#ownID').val();
		var status = $('#memState').val();
		var divide = $('#divNews').val();
        msgSuccess('บันทึกข้อมูลเรียบร้อยแล้วค่ะ');
		$.ajax({
          url:'modules/comment/formJeedNewsComment.php',
          cache: true,
          data:{'reqTypeCmt':'ajax','mid':mId,'refId':refId, 'status':status, 'option':'sms', 'divide':divide},
          type:'POST',
          success:function(data){
              $('#showComment'+refId+divide).empty().append(data);
          }

      });
    }else if(data == -50){
        sessionTimeout();
    }else{
        alert(data);
    }
}

function delComment(id){
     var currPage = $("#currPage").val();
     var currOffset = $("#currOffset").val();
     var totComment = $("#totComment").val();
     var limit = $("#limit").val();
     var option = $("#optComment").val();
     
    if(totComment==1 && currOffset>0){
       currPage = currPage-1;
       currOffset = currOffset-limit;
    }

    $.ajax({
		url:'modules/comment/deleteComment.php',
		cache: false,
		data:{'commentId':id},
		type:'POST',
		success:function(data){
            
			if(data == 1){
				var divide = $('#order').val();
                var refId = $('#refId').val();
				var mId = $('#ownID').val();
				var status = $('#memState').val();
				var divide = $('#divNews').val();
				msgSuccess('บันทึกข้อมูลเรียบร้อยแล้วค่ะ');
				$.ajax({
					  url:'modules/comment/formJeedNewsComment.php',
					  cache: true,
					  data:{'reqTypeCmt':'ajax','mid':mId,'refId':refId, 'status':status, 'option':'sms', 'divide':divide},
					  type:'POST',
					  success:function(data){
						  //$('#showComment'+refId+divide).empty().append(data);
                          divPage(currPage,currOffset,'getCommentJeedNews');
                          $.unblockUI();
					  }
		
			  	});                
            }else{
                alert(data);
            }
		}

	});
}
// JavaScript Document