// input 박스 체크
function fldchk(obj, msg)
{
	if (obj.type == 'text') // text
	{
		if (!obj.value)
		{
			alert(msg);
			obj.focus();
			return false;
		}
	}
	else if (obj.type == 'hidden') // hidden
	{
		if (!obj.value)
		{
			alert(msg);
			return false;
		}
	}
	else if (obj.type == 'select-one') // select
	{
		if (!obj.value)
		{
			alert(msg);
			obj.focus();
			return false;
		}
	}
	else if (obj.type == 'radio') // radio
	{
		if (obj.length > 1) 
		{
			checked = false;
			for (i=0; i<obj.length; i++)
			{
				if (obj[i].checked == true)
					checked = true;
			}

			if (checked == false)
			{
				alert(msg);
				return false;
			}
		}
		else
		{
			if (!obj.value)
			{
				alert(msg);
				obj.focus();
				return false;
			}
		}
	}
	else if (obj.type == 'checkbox') // checkbox
	{
		if (obj.checked == false)
		{
			alert(msg);
			return false;
		}
	}

	return true;
}

// 엘리먼트의 위치를 정보를 구함
function get_position(obj) 
{
	var tmp_Obj = obj;
	result = new Object();

	result.width = 0;
	result.height = 0;
	result.left = 0;
	result.top = 0;	

	while (tmp_Obj.offsetParent)
	{
		if (result.width == 0) result.width = tmp_Obj.offsetWidth;
		if (result.height == 0) result.height = tmp_Obj.offsetHeight;

		result.left += tmp_Obj.offsetLeft;
		result.top += tmp_Obj.offsetTop;		

		tmp_Obj = tmp_Obj.offsetParent;
	}

	return result;
}

// 화면 중앙 새창
function window_open( url, win_name, width, height )
{
	align_width = ( screen.width / 2 ) - ( width / 2 );
	align_height = ( screen.height / 3 ) - ( height / 3 );

	var setting = 'width='+width+',';
	setting += 'height='+height+',';
	setting += 'top='+align_height+',';
    setting += 'left='+align_width+',';
    setting += 'scrollbars=yes,';
    setting += 'resizable=no,';
    setting += 'status=no';

	if ( ! win_name )
		win_name = "";

	window.open( url, win_name, setting );
	return;
}

// 화면 중앙 FORM 윈도우
function window_form( url, form_id, width, height, win_name )
{
	form_name = document.getElementById( form_id );

	align_width = ( screen.width / 2 ) - ( width / 2 );
	align_height = ( screen.height / 3 ) - ( height / 3 );

	setting = 'width='+width+',';
	setting += 'height='+height+',';
	setting += 'top='+align_height+',';
    setting += 'left='+align_width+',';
    setting += 'scrollbars=yes,';
    setting += 'resizable=no,';
    setting += 'status=no';

	if ( ! win_name ) 
		win_name = "form_window_submit";

	window.open( '', win_name, setting );

	form_name.target = win_name;
	form_name.action = url;
	form_name.submit();

	return;
}

// 카테고리 윈도우 창 오픈
function category_win_open(form_name) 
{
	window_open(cu_admin_path+'/use_category_select.php', form_name, 800, 360);
}

// 샘플 쿠폰창 오픈
function sample_win_open(form_name) 
{
	window_open(cu_admin_path+'/coupon_sample_select.php', form_name, 480, 320)
}

var _tmp_fld;
var divx, divy;
var _obj_id = null;

// ajax 달력 불러오기
function ajax_calendar(fld, e, m)
{
	if (_obj_id == null)
	{
		_obj_id = document.createElement("DIV");
		with(_obj_id.style) 
		{
			width = '200px';
			border = '1px solid #dddddd';
			backgroundColor = '#ffffff';
			position = 'absolute';
		}
		document.body.appendChild(_obj_id);
	}

	tmp_date = fld.value ? fld.value : _default_date;

	if (_tmp_fld == fld && _obj_id.style.display == '' && !m) 
	{
		_obj_id.style.display = 'none';
		return;
	}

	if (_month == 13) 
	{
		_month = 1;
		_year++;
	}

	if (_month == 0) 
	{
		_month = 12;
		_year--;
	}

	_tmp_fld = fld;

	var url = cu_admin_path+'/ajax_calendar.php';
	var para = '?year='+_year+'&month='+_month+'&fld='+fld;

	if (!m) 
	{
		divx = (parseInt(e.clientX) + document.body.scrollLeft) + 20;
		divy = (parseInt(e.clientY) + document.body.scrollTop) + 20;
	}

	myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			asynchronous: true,
			parameters: para,
			onComplete: function(req) 
			{				
				_obj_id.innerHTML = req.responseText;
				_obj_id.style.left = divx+'px';
				_obj_id.style.top = divy+'px';
				_obj_id.style.display = '';

				selecthidden('hidden', _obj_id);
			}
		}
	);	
}

var select_array;

function selecthidden(status, obj)
{
	if (typeof(status) == 'undefined')
	{
		for (var i=0; i<select_array.length; i++)
			select_array[i].style.visibility = 'visible';

		select_array = null;
	}
	else
	{
		select_array = new Array();

		var sel = document.getElementsByTagName('SELECT');
		var obj_pos = get_position(obj);

		for (var i=0, k=0; i<sel.length; i++)
		{
			if (sel[i].getAttribute('nothidden') != null) 
				continue;

			sel_pos = get_position(sel[i]);

			if (((obj_pos.top <= sel_pos.top && obj_pos.top + obj_pos.height >= sel_pos.top) || (obj_pos.top <= sel_pos.top + sel_pos.height && obj_pos.top + obj_pos.height >= sel_pos.top + sel_pos.height)) && ((obj_pos.left <= sel_pos.left && obj_pos.left + obj_pos.width >= sel_pos.left) || (obj_pos.left <= sel_pos.left + sel_pos.width && obj_pos.left + obj_pos.width >= sel_pos.left + sel_pos.width)))
			{
				select_array[k] = sel[i];
				select_array[k].style.visibility = status;
				k++;
			} 
		}
	}

  return;
}

// ajax 달력 display
function calendar_click(fld, n) 
{
	if (n)
	{
		if (String(_month).length == 1) _month = '0'+_month;
		if (String(n).length == 1) n = '0'+n;

		string = _year+'-'+_month+'-'+n;
	}
	else
	{
		string = "";
	}

	selecthidden();

	_obj_id.style.display = 'none';
	document.getElementById(fld).value = string;	
}

// input 박스에 컴마를 붙임
function number_format_check(obj)
{
	result = obj.value.replace(/[^0-9]/g, '');

	if (result == 0) 
	{
		obj.value = 0;
		return;
	}

	result = number_format(String(parseInt(result)));
	
	obj.value = result;
	return;
}

// 정수만 가져옴
function number_int_check(obj)
{
	result = obj.value.replace(/[^0-9]/g, '');

	if (result == 0) 
	{
		obj.value = 0;
		return;
	}

	return result;
}

// 소수점 이하를 구함
function number_float_check(obj)
{
	loc = obj.value.indexOf('.');

	if (loc > 0)
	{
		string_len = String(obj.value).length;

		obj.value = obj.value.replace(/[^0-9]/g, '');
		obj.value = obj.value.substring(0, loc)+'.'+obj.value.substring(loc, string_len);
	}

	return;
}

// 체크 박스를 라디오 박스처럼 활용
function checkbox_radio(obj) 
{
	is_checked = obj.checked;

	byname = document.getElementsByName(obj.name);

	for (var i = 0; i < byname.length; i++)
		byname[i].checked = false;

	obj.checked = is_checked;
}

// 쿠폰 사용 페이지 이동
function coupon_using(cc_kind, cu_no) 
{
	f = document.getElementById('coupon_use');

	if (g4_is_member) 
	{
		f.cc_kind.value = cc_kind;
		f.cu_no.value = cu_no;

		if ( confirm('쿠폰을 사용하시겠습니까?')) 
		{
			f.target = '';
			f.action = g4_path+'/coupon/coupon_using.php';
			f.submit();
		}
	} 
	else
		alert('쿠폰 사용은 회원만 가능함니다');

	return;
}

// 쿠폰 선물하기 페이지 이동
function coupon_give(cc_kind, cu_no) 
{
	f = document.getElementById('coupon_use');

	if (g4_is_member) 
	{
		f.cc_kind.value = cc_kind;
		f.cu_no.value = cu_no;

		window_form(g4_path+'/coupon/coupon_give.php', 'coupon_use', 640, 300, 'coupon_give')
	} 
	else
		alert('쿠폰을 선물 하시려면 로그인 하셔야 함니다');

	return;
}

// 쿠폰 삭제 페이지 이동
function coupon_delete(cc_kind, cu_no) 
{
	f = document.getElementById('coupon_use');

	if (g4_is_member) 
	{
		f.cc_kind.value = cc_kind;
		f.cu_no.value = cu_no;

		if ( confirm( '한번 삭제하신 쿠폰은 영구히 삭제처리됨니다\n\n쿠폰을 삭제하시겠습니까?' ) ) 
		{
			f.target = '';
			f.action = g4_path+'/coupon/mypage_coupon.delete.php';
			f.submit();
		}
	} 
	else
		alert( '쿠폰을 삭제하시려면 로그인하세요' );

	return;
}

function move_div(target, e) 
{
	obj = document.getElementById(target);

	add_position = 10;
	
	event_x = e ? e.clientX : event.clientX;
	event_y = e ? e.clientY : event.clientY;

	body_left = document.body.scrollLeft;
	body_width = document.body.scrollWidth;
	body_top = document.body.scrollTop;
	body_height = document.body.scrollHeight;

	style_width = obj.style.width;
	style_height = obj.style.height;
	
	result_x = (event_x + body_left) + parseInt(style_width) + add_position;

	if (result_x > body_width) x = ((event_x + body_left) - parseInt(style_width)) - add_position;
	else x = (event_x + body_left) + add_position;

	result_y = (event_y + body_top) + parseInt(style_height) + add_position;

	if (result_y > body_height) y = ((event_y + body_top) - parseInt(style_height)) - add_position;
	else y = (event_y + body_top) + add_position;

	obj.style.left = x;
	obj.style.top = y;
	obj.style.display = 'block';

	return;
}

function close_div(obj) 
{
	return document.getElementById(obj).style.display = 'none';
}
