/*
게시판 이미지 리사이즈
호출함수 setContentIMG() 메인ID,넓이값
*/
max_imagewidth = 600;
function setContentIMG(names,maxWeight){
	max_imagewidth = maxWeight;
	var imgobjs = getContentImg(names);

	for(var i = 0 ; i < imgobjs.length; i++)
		loadIMG(imgobjs[i]);
}
function searchFrm(f){
	f.searchType.value = f.searchType_.value;
	f.searchVal.value = f.searchVal_.value;
	return true;
}
function boardPasswordChk(f){
	try{
		if(!checkForm(f.nbspw,"비밀번호를 입력해주세요.",1,true)) return false;
		Ajax = newXMLHttpRequest(); //request 객체 생성
		var bf = document.BoardForm;
		Ajax.onreadystatechange=function(){
			if(Ajax.readyState==4) {
				
				if(Ajax.status == 200) {
					
					if(parseInt(Ajax.responseText) == 1){
						
						switch(parseInt(f.kind.value)){
							case 1: movePage(bf,"OkBBS_sub.php",f.idx.value,-1); break;
							case 2: movePage(bf,"OkBBS_write.php",f.idx.value,-1,"NormalBBSModify"); break;
							case 3: NormalBBSDelete(bf,document.getElementById("deleteButton"));cancelPasswordLayout(f); break;
						}
					}else{
						cancelPasswordLayout(f);
						alert("패스워드가 일치하지 않습니다.");
					}
				}else{
					cancelPasswordLayout(f);
					alert('수정도중 장애발생 :'+Ajax.statusText); 
				}
			}
			
		}
		Ajax.open("POST", "./OkBBS_list.php", true);
		Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		Ajax.send("reqestAction=checkBoardPassword&table="+f.table.value+"&idx="+f.idx.value+"&pw="+f.nbspw.value+"&iOkBBSAId="+bf.iOkBBSAId.value);
	}catch(e){
		alert(e)
		return false;
		
	}
	
	return false;
}
function cancelPasswordLayout(f){
	f.table.value = "";
	f.idx.value = "";
	f.nbspw.value = "";
	f.kind.value = "";
	viewLayout("nbsSecretBK","none");
}
function viewPasswordLayout(kind,f,table,idx){
	try{
		f.kind.value = kind;
		f.table.value = table;
		f.idx.value = idx;
		viewLayout("nbsSecretBK","block");
		f.nbspw.focus();
	}catch(e){
		alert(e);
	}
}
function BoardForverUrl(){
	window.clipboardData.setData('Text', document.location.href); 
}
function loadIMG(imgobj){
	
	if (imgobj.width != undefined && imgobj.width > 0) {

		if (imgobj.width > max_imagewidth) {
			imgobj.height = Number(imgobj.height * max_imagewidth / imgobj.width);
			imgobj.width = 	max_imagewidth;
			imgobj.style.height=Number(imgobj.height * max_imagewidth / imgobj.width);
			imgobj.style.width= max_imagewidth;
			
		}
		imgobj.onerror="this.style.disply='none'";
		imgobj.style.cursor= 'pointer';
		imgobj.onclick = function (){
			viewImg(imgobj)
		}
	}
	if(imgobj.fileSize == -1){
		imgobj.src="http://"+document.domain+"/samesubject/common/noImage.php?width="+max_imagewidth;
	}
}

function viewImg(img){
		/*src_ = img.src.replaceAll("-thumb\\(([0-9]{3,4})\\)","");
		img_tem = new Image();
		img_tem.src = src_;
		if(img_tem.width > 0){
			img = img_tem;
		}*/
		
		var width = (img.width > screen.width) ? screen.width : img.width;
		width += (height > screen.height && width+26 < screen.width) ? 26 : 0;
		var height =(img.height > screen.height) ? screen.height : img.height;
		window.open("http://"+document.domain+"/samesubject/common/bbs_image_view.php?img="+img.src,"popUp","fullscreen=no,left=0,top=0	,menubar=no,status=no,toolbar=no,titlebar=no,location=no,scrollbars=no,width="+width+",height="+height)
}

function getContentImg(contentId){ //return array()
  var els = new Array();
  try {
   tags = document.getElementById(contentId).getElementsByTagName("IMG");
   for(i=0;i<tags.length;i++){
    els[els.length]=tags[i];
   }
  } catch(ex) {}
  return els;
}
/*
게시판 이미지 리사이즈 종료
*/

function checkForm(obj,msg,len,flag){
	if(obj.value.length < len){
		alert(msg);
		if(flag) obj.focus();
		return false;
	}
	return true;
}

function commentLoad(f,commentPage){//커멘트 페이지 이동
	getCommentLayOut(f,f.iBBSIdx.value,parseInt(commentPage));
}
function clearSr(f){
	f.searchVal.value="";
	f.submit();
}
function checkCommentWrite(f){
	try{
		if(!checkForm(f.commentWriteTextArea,"코맨트 내용을 입력해주세요.",1,true)) return false;
		f.reqestAction.value = "CommentWrite";
		f.method="POST";
	}catch(e){
		alert(e);
		return false;	
	}
	return true;
}
function getNowDate(){
  var d = new Date();
  var month_list = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
  setDateFormat = function(str){
  	str = String(str);
  	return str.length == 1 ? "0"+str : str;
  }
  return d.getFullYear() + "."
      + month_list[d.getMonth()] + "."
      + this.setDateFormat(d.getDate()) + "."
      + this.setDateFormat(d.getDay()) + " "
      + this.setDateFormat(d.getHours()) + ":"
      + this.setDateFormat(d.getMinutes()) + ":"
      + this.setDateFormat(d.getSeconds());
}
function CommentWrite(f){
	try{
		var modifyIdx = document.getElementsByName("modifyIdx[]");
		var BoardComment_Layout = document.getElementById("BoardComment_Layout");
		var MAX_COUNT = parseInt(f.maxCount.value);
		if(!checkForm(f.commentWriteTextArea,"코맨트 내용을 입력해주세요.",1,true)) return false;
		Ajax = newXMLHttpRequest(); //request 객체 생성
		Ajax.onreadystatechange=function(){
			if(Ajax.readyState==4) {
				if(Ajax.status == 200) {
					getCommentLayOut(f,f.iBBSIdx.value,1);
					f.commentWriteTextArea.value = ""; //작성 후 작성내용 클리어
				}else{
					alert('수정도중 장애발생 :'+Ajax.statusText); return false;
				}
			}
		}
		Ajax.setParentNone = function(){
			if(modifyIdx && modifyIdx.length > 1){
				modifyIdx[modifyIdx.length-2].parentNode.parentNode.parentNode.className="BoardCommentKind";
			}else if( modifyIdx.length == 1) {
				document.getElementById("commentNoResult").removeNode(true);
			}
			
		}
		Ajax.setContent = function(){
			
			var param = Ajax.responseText.split("|@|");
			
			
			var BoardCommentKind =document.createElement("div");//메인 레이어
			BoardCommentKind.className = "BoardCommentKindEnd";
			
			
			var BoardCommentName =document.createElement("div");
			BoardCommentName.className = "BoardCommentName";
			BoardCommentName.innerHTML = param[1];
			BoardCommentKind.appendChild(BoardCommentName);
			
			
			var BoardCommentView =document.createElement("div");
			BoardCommentView.className = "BoardCommentView";
			BoardCommentView.style.display = "block";
			BoardCommentKind.appendChild(BoardCommentView);
			
			
			var BoardCommentTxt =document.createElement("div");
			BoardCommentTxt.className = "BoardCommentTxt";
			BoardCommentTxt.innerHTML = "<input type='hidden' name='modifyIdx[]' value='"+param[0]+"'>";
			BoardCommentTxt.innerHTML+= "<pre><font class='commentSubText'>"+(f.commentWriteTextArea.value)+"</font>&nbsp;<font class='BoardCommentDate'>"+getNowDate()+"</font></pre>";
			BoardCommentView.appendChild(BoardCommentTxt);
			
			
			var BoardCommentOpt =document.createElement("div");
			BoardCommentOpt.className = "BoardCommentOpt";
			BoardCommentOpt.innerHTML = "<a href=\"javascript:Commentdelete('"+param[0]+"')\">삭제</a>";
			BoardCommentOpt.innerHTML+= " | ";
			BoardCommentOpt.innerHTML+= "<a href=\"javascript:CommentModify('"+param[0]+"')\">수정</a>";
			BoardCommentView.appendChild(BoardCommentOpt);
			
			
			var commentModify =document.createElement("div");
			commentModify.className = "commentModify";
			commentModify.style.display = "none";
			BoardCommentKind.appendChild(commentModify);
			
			
			var BoardCommentTxt =document.createElement("div");
			BoardCommentTxt.className = "BoardCommentTxt";
			BoardCommentTxt.innerHTML = "<textarea class='commentWriteTextArea' style='width:98%;' name='commentModifyTextArea[]'>"+f.commentWriteTextArea.value+"</textarea>";
			commentModify.appendChild(BoardCommentTxt);
			
			
			var BoardCommentOpt =document.createElement("div");
			BoardCommentOpt.className = "BoardCommentOpt";
			BoardCommentOpt.innerHTML = "<input type='button' value='변경' class='styleInput6' onclick=\"CommentModifyProc('"+param[0]+"');\">\n";
			BoardCommentOpt.innerHTML+= "<input type='button' value='취소' class='styleInput6' onclick=\"CommentModify('"+param[0]+"');\" >";
			commentModify.appendChild(BoardCommentOpt);
			BoardComment_Layout.appendChild(BoardCommentKind);
		}
		Ajax.open("POST", "./OkBBS_sub.php", true);
		Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		Ajax.send("reqestAction=CommentWrite&commentWriteTextArea="+f.commentWriteTextArea.value+"&iBBSIdx="+f.iBBSIdx.value+"&iOkBBSAId="+f.iOkBBSAId.value);
		
	}catch(e){
		alert(e);
		return;	
	}
	return true;
}
function CommentModify(indexVal){
	try{
		var modifyDiv = document.getElementsByName("commentModifyTextArea[]");
		var viewDiv = document.getElementsByName("modifyIdx[]");
		idx = getIndexVals(viewDiv,indexVal);
		
		viewDiv[idx].parentNode.parentNode.style.display = (viewDiv[idx].parentNode.parentNode.style.display=="block" ? "none": "block");
		modifyDiv[idx].parentNode.parentNode.style.display = (modifyDiv[idx].parentNode.parentNode.style.display=="block" ? "none" : "block");
		if(modifyDiv[idx].parentNode.parentNode.style.display=="block"){
			modifyDiv[idx].focus();
		}
	}catch(e){
		alert(e)
	}
}
function CommentModifyProc(indexVal){
	try{
		var f = document.BoardForm;
		var sSubText = document.getElementsByName("commentModifyTextArea[]");
		var iSubIdx = document.getElementsByName("modifyIdx[]");
		idx = getIndexVals(iSubIdx,indexVal);
		if(confirm("변경 하시겠습니까?")){
			Ajax = newXMLHttpRequest(); //request 객체 생성
			Ajax.onreadystatechange=function(){
				if(Ajax.readyState==4) {
					if(Ajax.status == 200) {
						CommentModify(iSubIdx[idx].value);
						var contents_ = iSubIdx[idx].parentNode;
						contents_.getElementsByTagName("font")[0].innerHTML= sSubText[idx].value.replaceAll("\\n","<br>").replaceAll(" ","&nbsp;");
					}else{
						alert('수정도중 장애발생 :'+Ajax.statusText); return false;
					}
				}
			}
			Ajax.open("POST", "./OkBBS_sub.php", true);
			Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			Ajax.send("reqestAction=CommentModify&sSubText="+sSubText[idx].value+"&iSubIdx="+iSubIdx[idx].value+"&iOkBBSAId="+f.iOkBBSAId.value);
		}
	}catch(e){
		alert(e)
	}
}
function getIndexVals(element_,val){
	for(i=0;i<element_.length;i++){
		if(element_[i].value == val) return i;
	}
	return null
}
function Commentdelete(indexVal){
	try{
		var f = document.BoardCommentWriteFrm;
		var iSubIdx = document.getElementsByName("modifyIdx[]");
		idx = getIndexVals(iSubIdx,indexVal);
		if(confirm("정말 삭제 하시겠습니까?")){
			Ajax = newXMLHttpRequest(); //request 객체 생성
			Ajax.onreadystatechange=function(){
				if(Ajax.readyState==4) {
					if(Ajax.status == 200) {
						getCommentLayOut(f,f.iBBSIdx.value,parseInt(f.commentPage.value));	
					}else{
						alert('수정도중 장애발생 :'+Ajax.statusText); return false;
					}
				}
			}
			Ajax.open("POST", "./OkBBS_sub.php", true);
			Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			Ajax.send("reqestAction=CommentDelete&iSubIdx="+iSubIdx[idx].value+"&iBBSIdx="+f.iBBSIdx.value+"&iOkBBSAId="+f.iOkBBSAId.value);
		}
	}catch(e){
		alert(e)
	}
}
function getCommentLayOut(f,iBBSIdx,commentPage){
	try{
		Ajax = newXMLHttpRequest(); //request 객체 생성
		Ajax.onreadystatechange=function(){
			if(Ajax.readyState==4) {
				if(Ajax.status == 200) {
					document.getElementById("BoardComment").innerHTML = Ajax.responseText;
				}else{
					alert('수정도중 장애발생 :'+Ajax.statusText); return false;
				}
			}
		}
		Ajax.open("POST", "./OkBBS_sub.php", true);
		Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		Ajax.send("reqestAction=CommentList&commentPage="+(commentPage)+"&iBBSIdx="+iBBSIdx+"&iOkBBSAId="+f.iOkBBSAId.value);
	}catch(e){
		alert("getCommentLayOut Error ");
	}
}
function commentDetail(indexVal){
	try{
		var sSubText = document.getElementsByName("commentModifyTextArea[]");
		var iSubIdx = document.getElementsByName("modifyIdx[]");
		idx = getIndexVals(iSubIdx,indexVal);
		var contents_ = iSubIdx[idx].parentNode;
		contents_.getElementsByTagName("font")[0].innerHTML= sSubText[idx].value.replaceAll("\\n","<br>").replaceAll(" ","&nbsp;");
		contents_.parentNode.parentNode.parentNode.style.display = "inline-block";
		contents_.parentNode.parentNode.style.display = "inline-block";
	}catch(e){
		alert(e)
	}
}
function setWriteInit(){
	//var debug_mode=1;
	var reditor = new rainEditor();
	reditor.renderElementsById('sBBSText');
	document.getElementById("sBBSText").parentNode.style.display="inline-block";
}
function NormalBBSWrite(f){
	try{
		if(!checkVal(f.detail_name,"작성자명을 입력해주세요.",1,true,false,/[0-9]|[a-z]|[A-Z]|[ㄱ-힣]/)) return false;
		if(!checkVal(f.detail_email1,"이메일(아이디)를 입력해주세요.",1,true,false,/[0-9]|[a-z]|[A-Z]|[ㄱ-힣]/)) return false;
		if(!checkVal(f.detail_email2,"이메일(주소)을 입력해주세요.",1,true,false,/[0-9]|[a-z]|[A-Z]|[.]/)) return false;
		if(!checkVal(f.detail_password,"비밀번호을 입력해주세요.",1,true,false,false)) return false;
		if(!checkVal(f.detail_subject,"제목을 입력해주세요.",1,true,false,false)) return false;
		//if(!checkVal(f.sCategory,"카테고리를 선택해주세요.",1,true,false,false)) return false;
		f.sBBSText.parse();
		
		if(!checkVal(f.zsfCode,"보안코드를 입력해주세요.",1,true,false,/[0-9]/)) return false;
		f.reqestAction.value="NormalBBSWrite";
		f.method="post";
		document.getElementById("NormalBBSWriteButton").disabled = true;
	}catch(e){
		alert(e);
		return false;
	}
	return true;
}
function NormalBBSDelete(f,button){
	try{
		if(f.iBBSIdx.value && confirm("정말 삭제하시겠습니까?")){
			f.reqestAction.value="NormalBBSDelete";
			f.method="post";
			f.submit();
			button.disabled = true;
		}
	}catch(e){
		alert(e);
		return false;
	}
	return false;
}
