
var queryParam = new Spry.Data.JSONDataSet(null);

function setSf( vars ) { pSf = vars; }
function setSv( vars ) { pSv = vars; }

function setQueryParam() {
	queryParam.setDataFromDoc("{'id':'"+pTableId+"','sf':'"+pSf+"','sv':'"+pSv+"','page':'"+getPagingNow()+"','auth':'"+AUTH+"'}");
}

//페이징함수
function goPagingSpryPage( page ) {
	setPagingNow(page);
	setQueryParam();
	bbsList('list', '');
}

function getTableId() { return pTableId; }

function getBackURL() {
	var backURL = queryParam.getDocument();
	if( backURL == null ) backURL = "";
	return backURL;
}

//검색
function goSearch() {
	var oSf = document.getElementById("sf");
	var oSv = document.getElementById("sv");

	oSfValue= oSf.options[oSf.selectedIndex].value;
	oSvValue= oSv.value;
	
	if( !oSvValue ) { alert("검색어를 입력하세요."); return; }

	setSf(oSfValue);
	setSv(oSvValue);
	setQueryParam();
	bbsList('list', '');
}

//목록불러오기
function bbsNoticeList( loc, param ) {
	dsBbsNotice.setURL("/2008/system/lib/getXmlBbsList.php?q="+queryParam.getDocument()+"&notice=y");
	dsBbsNotice.loadData();
}


//목록불러오기
function bbsList( loc, param ) {
	if( loc == "list" ) {
		//document.getElementById("textView").value = "/2008/system/lib/getXmlBbsList.php?q="+queryParam.getDocument();
		dsBbs.setURL("/2008/system/lib/getXmlBbsList.php?q="+queryParam.getDocument());
		dsBbs.loadData();
	} else {
		document.location.href = thisURL.replace("/"+loc,"/list")+"?"+param;
	}
}

//내용보기
function view( id ) {
	//fadeIt('contents', thisURL.replace("list","view")+"?bid="+id+"&back="+getBackURL());
	document.location.href = thisURL.replace("/list","/view")+"?aid="+id+"&back="+getBackURL();
}

//새글쓰기
function bbsWrite() {
	//fadeIt('contents', thisURL.replace("list","write")+"?back="+getBackURL());
	document.location.href = thisURL.replace("/list","/write")+"?back="+getBackURL();
}

//답변달기
function bbsReply(action, aid, backURL) {
	//fadeIt('contents', thisURL.replace(action,"write")+"?aid="+aid+"&back="+getBackURL());
	document.location.href = thisURL.replace("/"+action,"/write")+"?aid="+aid+"&back="+backURL;
}

//글수정
function bbsModify(action, aid, backURL) {
	//fadeIt('contents', thisURL.replace(action,"write")+"?aid="+aid+"&back="+getBackURL());
	document.location.href = thisURL.replace("/"+action,"/modify")+"?aid="+aid+"&back="+getBackURL();
}

//입력
function goSubmit( frmName ) {
	var f = document.forms[frmName];
	var html = getInputValue(f.html);
	switch( frmName ) {
		case "frmWrite":
			var obj = {
					"uname": "작성자 이름을 입력하세요.", 
					"title": "제목을 입력하세요.", 
					"html": "글종류를 선택하세요.",
					"fckContents": "내용를 입력하세요.",
					"contents": "내용를 입력하세요.",
					"passwd": "비밀번호를 입력하세요."
				};
		break;
		case "frmModify":
			var obj = {
					"title": "제목을 입력하세요.", 
					"html": "글종류를 선택하세요.",
					"fckContents": "내용를 입력하세요.",
					"contents": "내용를 입력하세요."
				};
		break;
	}

	var result = false;
		
	for(var i in obj ) {
		switch(i) {
			case "fckContents":
				if( html == "H" ) {
					var oEditor = FCKeditorAPI.GetInstance('fckContents') ;
					var strContent = new String(oEditor.GetHTML());
					if(oEditor.GetHTML() == "" || strContent.length <= 0) result = false;
					if( result == false ) alert(obj.fckContents);
				}
			break;
			case "contents":
				if( html == "T" ) result = checkValue(eval("f."+i), obj[i]);
			break;
			default:
				result = checkValue(eval("f."+i), obj[i]);
		}
		if( result == false ) break;
	}

	if( result == true ) f.submit();
}

function checkValue( obj, msg ) {
	var objValue = getInputValue(obj);
	if( !objValue ) {
		//Tip(msg, BALLOON, true, ABOVE, true);
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

function setEditor( type ) {
	var divHtml = document.getElementById("fckHtml");
	var divText = document.getElementById("fckText");
	var oEditor = FCKeditorAPI.GetInstance('fckContents') ;

	switch(type) {
		case "H":
			fck.Value = document.forms[0].contents.value;
			divHtml.innerHTML = fck.CreateHtml();
			divHtml.style.display = "block";
			divText.style.display = "none";
		break;
		case "T":
			document.forms[0].contents.value = oEditor.GetHTML();
			divHtml.style.display = "none";
			divText.style.display = "block";
		break;
	}
}

//글쓰기
function initWriteForm(f, file, html, fck) {
	if( html == "1" || html == "H" ) { 
		setInputValue(f.html, "H");
		var divType = document.getElementById("htmlType");
		divType.style.display = "block";

		//에디터
		var div = document.getElementById("fckHtml");
		div.style.display = "block"; 
		div.innerHTML = fck.CreateHtml();
	}

	if( html == "0" || html == "T" ) { 
		setInputValue(f.html, "T");
		document.getElementById("fckText").style.display = "block"; 
	}

	if( file > 0 ) {
		var divFilePart = document.getElementById("attachBox");
		var divFile  = document.getElementById("attachFile");
		divFilePart.style.display = "block";
		for(i=0;i<file;i++) {
			divFile.innerHTML += "<input type=\"file\" name=\"attach"+i+"\" id=\"attach"+i+"\" class=\"file\"><br/>";
		}
	}
}

//비밀번호입력상자 보이기
var nextAction = "";
var checkViewPassword = false;
function viewPasswordBox(obj, boxName, action) {
	var viewObj = document.getElementById(boxName);
	var nextObj = document.getElementById("nextURL");

	//관리자 삭제의 경우
	if( permAdmin == 1 && action == "delete" ) {
		if( confirm("정말로 삭제하시겠습니까?\n삭제되면 복구하실 수 없습니다.") ) {
			nextObj.value = action;
			nextAction = action;
			document.frmPassword.pwd.value = "checkadmin";
			checkPassword(document.frmPassword, "view");
		}
		return false;
	}

	//관리자 수정의 경우 
	if( permAdmin == 1 && action == "modify" ) {
		nextObj.value = action;
		nextAction = action;
		document.frmPassword.pwd.value = "checkadmin";
		checkPassword(document.frmPassword, "view");
		return false;
	}
	
	if( checkBrowser() == "ie" ) {
		getMouseXY();
		viewX = parseFloat(mousex);
		viewY = parseFloat(mousey);
	}
	if( checkBrowser() != "ie" ) {
		getObjectMouseXY(obj);
		viewX = parseFloat(objMousex);
		viewY = parseFloat(objMousey);
	}

	getObjectSize(viewObj);
	viewY = viewY - 55;

	//viewObj.style.top = viewY+'px';
	//viewObj.style.left = viewX+'px';
	
	if( action != nextAction ) {
		checkViewPassword = true;
		viewObj.style.display = "block";
		if( typeof(document.getElementById("pwd")) !== "undefined" ) document.getElementById("pwd").focus();
	} else {
		if( checkViewPassword == false ) {
			checkViewPassword = true;
			viewObj.style.display = "block";
			if( typeof(document.getElementById("pwd")) !== "undefined" ) document.getElementById("pwd").focus();
		} else {
			checkViewPassword = false;
			viewObj.style.display = "none";
		}
	}
	nextObj.value = action;
	nextAction = action;
}

//비밀번호 확인
function checkPassword(f, thisPoint) {
	if( checkValue(eval("f.pwd"), "비밀번호를 입력하세요.") ) {
		var url = thisURL.replace("/"+thisPoint,"/password")+"?aid="+f.aid.value+"&pwd="+f.pwd.value;
		var nextUrl = url.replace("password",f.nextURL.value)+"&back="+f.back.value;
		var req = Spry.Utils.loadURL("GET", url, false, null, null);
		if( req.xhRequest.responseText == 'false' ) {
			//Tip("비밀번호가 일치하지 않습니다.", BALLOON, true, ABOVE, true);
			alert("비밀번호가 일치하지 않습니다.");
			f.pwd.focus();
		} else {
			if( f.nextURL.value == "delete" ) {
				var req2 = Spry.Utils.loadURL("GET", nextUrl, false, null, null);
				if( req2.xhRequest.responseText == 'false' ) {
					//Tip("삭제되지 못했습니다.", BALLOON, true, ABOVE, true);
					alert("삭제되지 못했습니다.");
					f.pwd.focus();
				} else {
					listUrl = thisURL.replace(thisPoint,"list")+"?"+f.backParam.value;
					document.location.href = listUrl;
				}
			} else {
				document.location.href = nextUrl;
			}
		}
	}
	return false;
}

function closePassword( boxName ) {
	var viewObj = document.getElementById(boxName);
	viewObj.style.display = "none";
}

//첨부파일 삭제
function deleteAttach(bid, aid, pwd) {
	if( confirm("정말로 삭제하시겠습니까?") ) {
		var url = thisURL.replace(thisPoint,"deleteFile")+"?bid="+bid+"&aid="+aid+"&pwd="+pwd;
		var req2 = Spry.Utils.loadURL("GET", url, false, null, null);
		if( req2.xhRequest.responseText == 'true' ) {
			alert("삭제되었습니다.");
			Spry.Utils.addClassName("attach"+bid,'deleteFile');
		} else {
			alert("삭제되지 못했습니다.\n"+req2.xhRequest.responseText);
		}
	}
}

//object 가져오기
//http://www.howtocreate.co.uk/tutorials/javascript/dhtml
function getObjectById(divID, oDoc) {
  if( document.getElementById ) {
    return document.getElementById(divID); }
  if( document.all ) {
    return document.all[divID]; }
  if( !oDoc ) { oDoc = document; }
  if( document.layers ) {
    if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
      for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
        y = getRefToDiv(divID,oDoc.layers[x].document); }
    return y; } }
  return false;
}

