//gDebug = true;

function doRootLogin()
{
	if (g('loginID').value == "")
	{
		alert("아이디를 입력하세요.");
		setFocus('loginID');
		return ;
	}

	if (g('passwd').value == "")
	{
		alert("패스워드를 입력하세요.");
		setFocus('passwd');
		return ;
	}

	sendXmlHttpRequest(gURL_root + gSitePath + "/auth.php", form2obj('loginForm'), function(oXml)
		{
			try
			{
				var result = oXml.responseXML.selectSingleNode('//message/text()').nodeValue;
				switch (result)
				{
					case 'LOGIN_FAILED':
						g('passwd').value = "";
						setFocus('passwd');
						alert("입력하신 패스워드가 맞는지 다시 확인해 주시기 바랍니다.");
						break;
					case 'LOGINED':
						location.href = g('url').value;
					default:
						if (result.indexOf("LOGIN_DUPLICATE") > -1)
						{
							var ipAddr = result.substr("LOGIN_DUPLICATE:".length);

							if (confirm("다른 PC(" + ipAddr + ")에서 로그인되어 있습니다.\n\n다른 PC에서 로그인된 접속을 끊고 지금 로그인하시겠습니까?") == false)
								break;

							g('txtConfirm').value = "Y";
							doRootLogin();
						}

						break;
					break;
				}
			}
			catch (e)
			{
				alert("로그인에 실패하였습니다. [오류코드: LOGIN ERR-1]\n\n" + oXml.responseText);
			}
		});

	return ;
}

/*
	Prima Page Control
*/
var primaPageControl = function() {};

primaPageControl = {
	_distance:50,
	_curWidth:984,
	_pageMenuWidth:204, // __pageMenuHolder
	_x:null,
	_isDragStarted:false,
	_bMenuVisible:true,
	setWidth:function(nWidth)
	{
		if (this._curWidth == nWidth)
			return ;

		if (nWidth < 984)
		{
			primaPageControl._isDragStarted = false;
			nWidth = 984;
//			alert(loadString('MSG_PAGE_MINIMIZED'));
//			return ;
		}

		this.reSize(nWidth);
	},
	reSize:function(nWidth)
	{
		var nContentWidth = nWidth - this._pageMenuWidth;

		if (!this._bMenuVisible)
		{
			nContentWidth = nContentWidth + 160;

			if (g('__pageMenuHolder'))
			{
				g('__pageMenuHolder').style.float = 'left';
				g('__pageMenuHolder').style.width = '10px';
			}

			hide(g('__treeMenuHide'));
			show(g('__treeMenuShow'));
		}
		else
		{
			if (g('__pageMenuHolder'))
			{
				g('__pageMenuHolder').style.float = 'left';
				g('__pageMenuHolder').style.width = '170px';
			}

			show(g('__treeMenuHide'));
			hide(g('__treeMenuShow'));
		}

		this._curWidth = nWidth;

		g('__masterPage').style.width = nWidth + "px";
		g('__pageHolder').style.width = nWidth + "px";

		if (g('__contentHolder'))
			g('__contentHolder').style.width = nContentWidth + "px";
	},
	plus:function()
	{
		this.setWidth(this._curWidth + this._distance);
	},
	minus:function()
	{
		this.setWidth(this._curWidth - this._distance);
	},
	startDrag:function()
	{
		primaPageControl._isDragStarted = true;
		primaPageControl._x	= event.clientX;

		document.body.style.cursor = "e-resize";

//		document.onmousemove = primaPageControl.onDrag;
		document.onmouseup = primaPageControl.stopDrag;
	},
	onDrag:function()
	{
		if (primaPageControl._isDragStarted == false)
			return;
	},
	stopDrag:function()
	{
		if (primaPageControl._isDragStarted == false)
			return;

		document.body.style.cursor = "auto";

		var nDistance  = parseInt(event.clientX) - primaPageControl._x;
		primaPageControl.setWidth(parseInt(primaPageControl._curWidth) + parseInt(nDistance));

		//window.status = "onDrag: " + nDistance + " , curWidth: " + primaPageControl._curWidth;

		// 너비가 조정되었으므로, 관리자 환경 설정에 반영
		sendXmlHttpRequest(gURL_root + gSitePath + "/manage/preference.php", {
				action:'BackgroundSetting',
				key:'page_width',
				value:primaPageControl._curWidth
			}, function(oXml) {

			});

		primaPageControl._isDragStarted = false;
	},
	toggleMenu:function()
	{
		if (this._bMenuVisible)
		{
			hide(g('__pageMenuTitleHolder'));
			hide(g('__pageMenuTreeHolder'));
		}
		else
		{
			show(g('__pageMenuTitleHolder'));
			show(g('__pageMenuTreeHolder'));
		}

		this._bMenuVisible = !this._bMenuVisible;

		this.reSize(this._curWidth);
	}
};


/* onSubmit */
function isValidFormData(oForm)
{
	return true;
}

function findForm(oResetFormFields, bFillFormValue)
{
	if (bFillFormValue == undefined)
		var bFillFormValue = false;

	var oForm = document.forms['pageForm'];

	if (!oForm)
	{
		alert('pageForm 폼을 찾을 수 없습니다.');
		return ;
	}

	if (oForm.onsubmit &&
		oForm.onsubmit() == false)
		return ;

	if (oForm.prefix)
		oForm.prefix.value = '';

	if (oResetFormFields != undefined)
	{
		for (var k in oResetFormFields)
			oForm[k].value = bFillFormValue ? oResetFormFields[k] : "";
	}

	if (oForm.page)
		oForm.page.value = '1';

	oForm.submit();
}

/*
	선택상자 컨트롤
*/
var SelectBoxCtrl = function() {};
SelectBoxCtrl = {
	findOptionIndex:function(src, value)
	{
		var objSrc = g(src);

		for(var i = 0 ; i < objSrc.options.length ; i++ )
		{
			if (objSrc.options[i].value == value)
				return i;
		}

		return -1;
	},
	MoveOption:function(src, tar, defaultValue)
	{
		var objSrc = g(src);

		if (defaultValue != undefined)
			var selIdx = this.findOptionIndex(src, defaultValue);
		else
			var selIdx = objSrc.selectedIndex;
		
		if (selIdx < 0)
		{
			alert('항목을 선택 후, 버튼을 누르세요.');
			return ;
		}

		var text = objSrc.options[selIdx].text;
		var value = objSrc.options[selIdx].value;

		this.RemoveOption(objSrc, value);
		this.AddOption(g(tar), text, value);	
	},
	RemoveOption:function(obj, value)
	{
		var dat = [];

		for (var i = 0; i < obj.options.length ; i++ )
		{
			if (obj.options[i].value == value)
				continue;

			dat.push({text:obj.options[i].text,value:obj.options[i].value});
		}

		obj.length = 0;
		
		if (dat.length < 1)
			return ;

		for (var i = 0; i < dat.length ; i++ )
		{
			obj.options[i] = new Option(dat[i].text, dat[i].value);
		}
	},
	AddOption:function(obj, text, value)
	{
		obj.options[obj.length] = new Option(text, value);
	},
	GetOptions:function(objName)
	{
		var dat = [];
		var obj = g(objName);
		for (var i = 0; i < obj.length ; i++ )
		{
			dat.push(obj.options[i].value);
		}

		return dat;
	}
};

/* 우편번호 찾기(manage 용) */
function findZipcode(formName, zipcode1, zipcode2, address, focusFormName)
{
	showPopup(gURL_root + gSitePath + "/manage/zipcode.php?formName=" + formName + "&zipcode1=" + zipcode1 + "&zipcode2=" + zipcode2 + "&address=" + address + "&focusFormName=" + focusFormName, "zipcode", 460, 370, false, false);
}

/* 엑셀자료 */
function saveAsExcel()
{
	var oForm = document.forms['pageForm'];

	oForm['is_excel'].value = 1;
	oForm.submit();
	oForm['is_excel'].value = 0;
}

// 폼 페이지에서 현재 폼 값으로 리로드 시 사용
function needReloadForm(oForm)
{
	oForm['action'].value = "Form";
	oForm.submit();
}

// 관리도구 메인의 업무 프로세스별 메뉴 묶음의 탭 처리
function showProcessFlow(tabID, bAuto)
{
	if (bAuto == undefined)
	{
		clearTimeout(startProcessFlowAnimation.intervalID);
	}

	if (this.tabID)
	{
		g('__processFlowTab_' + this.tabID).className = "";
		hide(g('__processFlow_' + this.tabID));
	}

	g('__processFlowTab_' + tabID).className = "selected";
	show(g('__processFlow_' + tabID));

	this.tabID = tabID;
}

function startProcessFlowAnimation(idx)
{
	var arrProcessFlow = ['score', 'register']; // 'contents', 

	startProcessFlowAnimation.intervalID = setTimeout(function()
	{
		startProcessFlowAnimation(arrProcessFlow.length - 1 > idx ? idx + 1 : 0);
	}, 1500);

	showProcessFlow(arrProcessFlow[idx], true);
}

// 관리도구 메인의 메뉴 아이콘 처리 함수
function showMenuPocket(menuID)
{
	if (this.currentMenuID)
	{
		g('__menu_' + this.currentMenuID).className = "menu-icon-" + this.currentMenuID;
		hide(g('__menuPocket_' + this.currentMenuID));
	}

	g('__menu_' + menuID).className = "menu-icon-" + menuID + "-clicked";
	show(g('__menuPocket_' + menuID));

	this.currentMenuID = menuID;
}


