﻿var comment ={
	Bid:0,
	Bno:0,
	Pid:"",
	Pref:0,
	Pstep:1,
	test:function(){alert("test");},	
	initWithParams:function(pBid, pBno){
		this.Bid = pBid;
		this.Bno = pBno;
		this.init();
	},	
	init:function(){
		//alert("comment init is called\n$(#comment).length ="+$("#comment").length );		
		if ( typeof(board) !== "undefined" ) {
			if ( board.Bid != 0 ) {
				this.Bid = board.Bid;
				this.Bno = board.Bno;	
			};
		};
		this.Pid="";
		this.Pref=0;
		this.Pstep=0;
		
		/*
		//test
		this.Bid=67;
		this.Bno=5959;
		
		//test
		this.Bid=-1;
		this.Bno=-1;		
		this.Pid="club815";
		this.Pref=10;
		*/
		
		if ( $("#comment").length > 0 ) {	// if comment div is loaded
			var row = '<div id="commentBox" >';
			row = row + '<div id="commentForm"></div>';
			row = row + '<div id="commentList"></div>';
			row = row + '<div id="FBComment"></div>';			
			row = row + '</div>';			
			$("#comment").append(row);
			this.list();
			this.form();
			this.FBComment();
		};
		
	},
	list:function(){
		$("#commentList").load("/api/comment/commentList.asp?bid="+this.Bid+"&bno="+this.Bno+"&pid="+this.Pid+"&pref="+this.Pref+"&pstep="+this.Pstep, function(){ });		
	},
	edit:function(num){
		// UI
		if ( $("#commentEditWrapper").length>0 ) $("#commentEditWrapper").remove();
		$("body").append("<div id='commentEditWrapper' ></div>");	
		$("#commentEditWrapper").load("/api/comment/commentEditForm.asp?editnum="+num, function(){ 
			openModalPopup("#commentEditWrapper");
		});
	},
	del:function(num){
		// UI
		if ( $("#commentEditWrapper").length>0 ) $("#commentEditWrapper").remove();
		//else 
		$("body").append("<div id='commentEditWrapper' ></div>");	
		$("#commentEditWrapper").load("/api/comment/commentDelForm.asp?delnum="+num, function(){ 
			openModalPopup("#commentEditWrapper");
		});
	},	
	form:function(){
		$("#commentForm").load("/api/comment/commentForm.asp?bid="+this.Bid+"&bno="+this.Bno+"&pid="+this.Pid+"&pref="+this.Pref+"&pstep="+this.Pstep, function(){ });
	},
	FBComment:function(){
		$("#FBComment").append("<div id='fb-root'></div><fb:comments href='"+ location.href.replace("http://","") +"' num_posts='2' width='500'></fb:comments>");
		FB.XFBML.parse();		
	}
};

// functions
function CommentSubmit(){
	// param
	idx = 0;
	bid = $("#comment_form input[name='bid']").val();
	bno = $("#comment_form input[name='bno']").val();
	pid = $("#comment_form input[name='pid']").val();
	pref = $("#comment_form input[name='pref']").val();
	pstep = $("#comment_form input[name='pstep']").val();
	name = $("#comment_form input[name='name']").val();
	password = $("#comment_form input[name='password']").val();
	txtcomment = $("#comment_form textarea[name='txtcomment']").val().replace(/%/g,"&#37;");
	
	// check parameter
	if ( !user.bLogin ) {
		if ( name=="" ){
			alert("이름을 입력해 주세요.\nPlease fill out your name.");
			$("#comment_form input[name='name']").focus();
			return false;
		};
		if( password=="" ){
			alert("패스워드를 입력해 주세요.\nPlease fill out password.");
			$("#comment_form input[name='password']").focus();
			return false;
		};
	};
	if( txtcomment=="" ){
		alert("내용을 입력해 주세요.\nPlease fill out content.");
		$("#comment_form textarea[name='txtcomment']").focus();
		return false;
	};

	// input
	var input = {"idx":idx,"bid":bid,"bno":bno,"pid":pid,"pref":pref,"pstep":pstep,"name":name,"password":password,"txtcomment":txtcomment};
//alert("bid="+bid+"bno="+bno+"pid="+pid+"pref="+pref+"pstep="+pstep+"name="+name+"password="+password+"txtcomment="+txtcomment);
//return;

	// submit
	$.ajax({
		type: "POST",
		url: "/api/comment/commentWriteOk.asp",
		cache: false,
		data: input,
		dataType: "json",
		//dataType: "text",
		success: function(data, textStatus, jqXHR){
			if ( data.RESULT == 0 ){
				// reload comment list
				alert("Comment writing success!");
				$("#comment_form textarea[name='txtcomment']").val("");
				comment.list();
			} else {
				alert(data.MESSAGE);
				return;
			};
		},
		error: function(jqXHR, textStatus, errorThrown){
			alert("Error occured while submit.");
			//alert("error\ntextStatus="+jqXHR.textStatus +"\n readyState="+jqXHR.readyState
			//	+"\n status="+jqXHR.status +"\n statusText="+jqXHR.statusText
			//	+"\n responseXML="+jqXHR.responseXML+"\n responseHeader="+ jqXHR.getAllResponseHeaders() );
		}
	});	

};
	
function CommentEditSubmit(){
	// param
	idx = $("#comment_edit_form input[name='idx']").val();
	bid = $("#comment_edit_form input[name='bid']").val();
	bno = $("#comment_edit_form input[name='bno']").val();
	pid = $("#comment_edit_form input[name='pid']").val();
	pref = $("#comment_edit_form input[name='pref']").val();
	pstep = $("#comment_edit_form input[name='pstep']").val();
	name = $("#comment_edit_form input[name='name']").val();
	password = $("#comment_edit_form input[name='password']").val();
	txtcomment = $("#comment_edit_form textarea[name='txtcomment']").val().replace(/%/g,"&#37;");
	
	// check parameter
	if ( !user.bLogin ) {
		if ( name.val()=="" ){
			alert("이름을 입력해 주세요.\nPlease fill out your name.");
			$("#comment_edit_form input[name='name']").focus();
			return false;
		} else if( password=="" ){
			alert("패스워드를 입력해 주세요.\nPlease fill out password.");
			$("#comment_edit_form input[name='password']").focus();
			return false;
		};
	};
	if( txtcomment=="" ){
		alert("내용을 입력해 주세요.\nPlease fill out content.");
		$("#comment_edit_form textarea[name='txtcomment']").focus();
		return false;
	};

	// input
	var input = {"idx":idx,"bid":bid,"bno":bno,"pid":pid,"pref":pref,"pstep":pstep,"name":name,"password":password,"txtcomment":txtcomment};
//alert("editnum="+editnum+"bid="+bid+"bno="+bno+"pid="+pid+"pref="+pref+"pstep="+pstep+"name="+name+"password="+password+"txtcomment="+txtcomment);
	//return;

	// submit
	$.ajax({
		type: "POST",
		url: "/api/comment/commentWriteOk.asp",
		cache: false,
		data: input,
		dataType: "json",
		//dataType: "text",
		success: function(data, textStatus, jqXHR){
			if ( data.RESULT == 0 ){
				// reload comment list
				alert("Comment editing success!");
				$("#comment_edit_form textarea[name='txtcomment']").val("");
				closeModalPopup("#commentEditWrapper");
				comment.list();
			} else{
				alert(data.MESSAGE);
				return;
			};
		},
		error: function(jqXHR, textStatus, errorThrown){
			alert("Error occured while submit.");
			//alert("error\ntextStatus="+jqXHR.textStatus +"\n readyState="+jqXHR.readyState
			//	+"\n status="+jqXHR.status +"\n statusText="+jqXHR.statusText
			//	+"\n responseXML="+jqXHR.responseXML+"\n responseHeader="+ jqXHR.getAllResponseHeaders() );
		}
	});	

/*
// test fields
$("#comment_edit_form input[name='idx']").remove();$("#comment_edit_form").append("<input type='hidden' name='idx' value='"+idx+"'>");
$("#comment_edit_form input[name='bid']").remove();$("#comment_edit_form").append("<input type='hidden' name='bid' value='"+bid+"'>");
$("#comment_edit_form input[name='bno']").remove();$("#comment_edit_form").append("<input type='hidden' name='bno' value='"+bno+"'>");
$("#comment_edit_form input[name='pid']").remove();$("#comment_edit_form").append("<input type='hidden' name='pid' value='"+pid+"'>");
$("#comment_edit_form input[name='pref']").remove();$("#comment_edit_form").append("<input type='hidden' name='pref' value='"+pref+"'>");
$("#comment_edit_form input[name='pstep']").remove();$("#comment_edit_form").append("<input type='hidden' name='pstep' value='"+pstep+"'>");
$("#comment_edit_form input[name='name']").remove();$("#comment_edit_form").append("<input type='hidden' name='name' value='"+name+"'>");
$("#comment_edit_form input[name='password']").remove();$("#comment_edit_form").append("<input type='hidden' name='password' value='"+password+"'>");
$("#comment_edit_form input[name='txtcomment']").remove();$("#comment_edit_form").append("<input type='hidden' name='txtcomment' value='"+txtcomment+"'>");

// test submit
var frm=$('#comment_edit_form'); 
frm.attr("action","/api/comment/commentWriteOk.asp");
frm.attr("enctype", "");
frm.attr("method", "post"); //post for testing
//frm.ajaxForm(SubmitCallback); 
frm.submit(); 	
*/
};

function CommentDelSubmit(){

	// check parameter
	delnum = $("#comment_del_form input[name='delnum']").val();
	password = $("#comment_delpassword").val();
	
	if( delnum==undefined || delnum=="" ){
		alert("번호를 입력해 주세요.\nPlease fill out number.");
		$("#comment_del_form input[name='delnum']").focus();
		return false;
	};
	if ( !user.bLogin ) {
		if ( delnum==undefined || delnum=="" ){
			alert("패스워드를 입력해 주세요.\nPlease fill out password.");
			$("#comment_delpassword").focus();
			return false;
		};
	};
		
	// submit
	$.post("/api/comment/commentDelOk.asp", { delnum:delnum, password:password }, function(data){
			data = $.parseJSON(data);
			if ( data.RESULT==0 ) {
				comment.list();
				$("#commentDelInnerWrapper").html("Comment deleted successfully. ");
			} else {
				$("#commentDelInnerWrapper").html( data.MESSAGE );
			};
	});
		
};

