// 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)
}

// ajax ´Þ·Â ºÒ·¯¿À±â
function ajax_calendar( fld, e, m )
{
	tmp_date = fld.value ? fld.value : _default_date;

	if ( _tmp_fld == fld && document.getElementById( _obj_id ).style.display == 'block' && !m ) 
	{
		document.getElementById(_obj_id).style.display = 'none';
		return;
	}

	if ( _month == 13  ) 
	{
		_month = 1;
		_year++;
	}

	if ( _month == 0  ) 
	{
		_month = 12;
		_year--;
	}

	_tmp_fld = fld;

	url = cu_admin_path+'/ajax_calendar.php';
	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 = document.getElementById( _obj_id );

				obj.innerHTML = req.responseText;
				obj.style.left = divx+'px';
				obj.style.top = divy+'px';
				obj.style.display = '';

				selecthidden( 'hidden', obj );
			}
		}
	);	
}

var select_array;

function selecthidden( status, obj )
{
  if ( ! status )
  {
    for ( 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 ( 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();

	document.getElementById( '_hom_calendar' ).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 ( 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';
}

