function elms(name) {
	var ems = document.getElementsByName(name);
	return ems;
}

function elm(name) {
	var ems = elms(name);
	if(ems.length>0) return ems[0];
	var ems = document.getElementById(name);
	if( ems ) return ems;
	return null;
}

function showSelected(ee,iv,enb) {
	for(var i=0;i<ee.length;i++) {
		ee[i].style.display = i==iv ? 'inline' : 'none';
		if( enb ) ee[i].disabled = i==iv ? false : true;
	}
}

function isEmpty(value) {
	if (typeof(value)=="number") return true;
	return (value.toString().replace(/[ \t\n]/g,'')) ? false : true;
}

function setCurrForm(name) { window.tForm = document.forms[name]; }

function thisform(name) {
	try {
		var frm = window.tForm ? window.tForm : (document.forms['cmsForm'] ? document.forms['cmsForm'] : document.forms[0]);
		if( !frm ) throw {code:1,message:"This page has no form." };
		if( !name ) return frm;
		return frm[name];
	} catch(e) {
		alert( ['Error catched:',e.message].join('\n\n') );
		return null;
	}
}

function formElem(name) {
	var elem = thisform(name);
	if( !elem ) thisform().appendChild( elem=document.createElement('<INPUT type=hidden name='+name+'>') );
	return elem;
}

function emptyAlert(name,alt,fcs) {
	if (isEmpty(thisform(name).value))
	{
		alert(alt);
		try{ thisform(fcs?fcs:name).focus(); } catch(e) { }
		return true;
	}
	return false;
}

function compareAlert(name,test,alt,fcs) {
	if( thisform(name).value==thisform(test).value ) return false;
	alert(alt);
	try{ thisform(fcs?fcs:name).focus(); } catch(e) { }
	return true;
}

function doPaging( pg ) {
	setCurrForm('searchForm');
	if( pg ) formElem( "pg" ).value = pg;
	try {
		if (!thisform('sValue').value)
		{
			thisform('sValue').disabled = 
			thisform('sNames').disabled = true;
		}
		thisform('searchNames').disabled = true;
		thisform('searchValue').disabled = true;
	} catch(e) { }
	try { onPaging(); } catch(e) { }
	thisform().submit();
}

function doSearch( pg ) {
	setCurrForm('searchForm');
	if( pg ) formElem( "pg" ).value = pg;
	try {
		formElem('fNames').value = thisform('searchNames').value;
		formElem('fValue').value = thisform('searchValue').value;
		thisform('searchNames').disabled = true;
		thisform('searchValue').disabled = true;
	} catch(e) {}
	if( !thisform('fNames').value ) thisform('fNames').disabled = true;
	if( !thisform('fValue').value ) { thisform('fNames').disabled = thisform('fValue').disabled = true; }
	try { onSearch(); } catch(e) { }
	thisform().submit();
	return true;
}

function ajaxRequest() {
	if(window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	return new XMLHttpRequest();
}

function ajaxGet(url,async) {
	var ax = ajaxRequest();
	ax.open("GET", url, async );
	return ax;
}

function ajaxPost(url,async) {
	var ax = ajaxRequest();
	ax.open("POST", url, async );
	ax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8;');
	return ax;
}

function toParams(obj) {
	var objs = [];
	for(var s in obj) {
		if( !obj[s] ) continue;
		objs[objs.length] = [s,encodeURIComponent(obj[s])].join('=');
	}
	return objs.join('&');
}

function adminLogout() {
	document.location = "/manage/manager_login.php?at=logout";
}

function resizeImg(w,h) {
	if( this.width>this.height ) {
		if( this.width>w ) this.width = w;
	} else {
		if( this.height>h ) this.height = h;
	}
}

function imgLoaded(img) {
	var lDiv = document.getElementById('photoLoad');
	var pDiv = document.getElementById('photoView');
	pDiv.tr = img.height/img.width;
	pDiv.ts = img.width/img.height;
	pDiv.tw = Math.min( img.width,800 );
	pDiv.th = pDiv.tw * pDiv.tr;
	if( pDiv.th>400 ) {
		pDiv.th = Math.min( img.height,400 );
		pDiv.tw = pDiv.th * pDiv.ts;
	}
	pDiv.timg = img;
	pDiv.innerHTML = '';
	pDiv.style.backgroundColor = 'white';
	pDiv.style.display = 'block';
	pDiv.cx = pDiv.bw * 0.5;
	pDiv.cy = pDiv.th * 0.5 + 20;
	pDiv.style.left = pDiv.cx-pDiv.sw*0.5;
	pDiv.style.top  = pDiv.cy-pDiv.sh*0.5;
	pDiv.timg.width  = pDiv.sw - 6;
	pDiv.timg.height = pDiv.sh - 6;
	pDiv.appendChild( pDiv.timg );
}

function viewOpen(aTag) {
	var lDiv = document.getElementById('photoLoad');
	var pDiv = document.getElementById('photoView');
	if( !pDiv.sw ) pDiv.sw = 20;
	if( !pDiv.sh ) pDiv.sh = 20;
	if( !pDiv.bw ) pDiv.bw = lDiv.offsetWidth;
	if( !pDiv.bh ) pDiv.bh = lDiv.offsetHeight;
	lDiv.innerHTML = '<img src="'+aTag.href+'" onload="imgLoaded(this)">';
	viewProc.prototype.timeout = -1;
	if( !viewProc.prototype.timer ) {
		viewProc.prototype.timer = setInterval('viewProc()',10);
	}
}

function viewReset() {
	viewProc.prototype.timeout = new Date();
	var pDiv = document.getElementById('photoView');
}

function viewProc() {
	var pDiv = document.getElementById('photoView');
	if( !pDiv.tw ) return;

	if( viewProc.prototype.timeout!=-1
	 && viewProc.prototype.timeout ) {
		var tdx = new Date()-viewProc.prototype.timeout;
		if( tdx<200 ) return;
		viewProc.prototype.timeout = 0;
		pDiv.innerHTML = '';
		pDiv.tw = 20;
		pDiv.th = 20;
	}

	var tx = pDiv.tw;
	var ty = pDiv.th;
	var sx = pDiv.sw;
	var sy = pDiv.sh;
	var dx = tx-sx,ddx = Math.sqrt( dx*dx );
	var dy = ty-sy,ddy = Math.sqrt( dy*dy );
	var rx = dx?(dx/ddx):0;
	var ry = dy?(dy/ddy):0;
	var ox = (ddx*0.15)*rx;
	var oy = (ddy*0.15)*ry;
	var ow = ( rx )?(sx + ox):tx;
	var oh = ( ry )?(sy + oy):ty;

	pDiv.sw = pDiv.style.width  = ow;
	pDiv.sh = pDiv.style.height = oh;

	pDiv.style.left = pDiv.cx-pDiv.sw*0.5;
	pDiv.style.top  = pDiv.cy-pDiv.sh*0.5;
	if( pDiv.timg ) {
		pDiv.timg.width  = pDiv.sw-6;
		pDiv.timg.height = pDiv.sh-6;
	}

	if( Math.floor(ddx)==0 && Math.floor(ddy)==0 ) {
		if( !viewProc.prototype.timeout ) {
			pDiv.style.display = 'none';
		} else if( pDiv.timg ) {
			pDiv.timg.width  = pDiv.tw-6;
			pDiv.timg.height = pDiv.th-6;
			pDiv.timg = null;
		}
	}
}

function confirmDelete() {
	return confirm( '삭제하시겠습니까?' );
}

function checkItems(name) {
	var ems = elms(name);
	for(var i=1;i<ems.length;i++)
		ems[i].checked = ems[0].checked;
}

function checkedValue(name) {
	var ems = document.getElementsByName(name);
	var ret = [];
	for(var i=0;i<ems.length;i++) {
		if( !ems[i].checked ) continue;
		if( !ems[i].value ) continue;
		ret[ret.length] = ems[i].value;
	}
	return ret.join();
}

function openerValues(sName) {
	var eee = checkedValue(sName);
	try { opener.selectedValues(eee,window.name); } catch(e) { }
	self.close();
}

function openAddress(name) {
	window.open("../cms/zipcode.php",name,"width=460,height=300,scrollbars=1");
	return false;
}

function openCheckid(name) {
	window.open("../cms/checkid.php",name,"width=320,height=160,scrollbars=1");
	return false;
}

function attachEvents(ev,fn) {
	if( window.attachEvent ) window.attachEvent( 'on'+ev,fn );
	else window.addEventListener( ev,fn,false );
}

function attachLoad(fn) { try { attachEvents('load',fn); } catch(e) { } }

function ifEmpty(val,empty)
{
	return val ? val : empty;
}
