// SYSTEM

var select_obj = null;
var select_over = false;

document.onclick = on_mouse_click;

function on_mouse_click() {
  if (select_obj && !select_over) {
    select_obj.style.visibility = "hidden";
    select_obj = null;
  }
  
  select_over = false;
}

function show_menu(name,status) {
  var obj = document.all[name];
  var x, y, width, height;
  
  x = event.clientX + parseInt(obj.offsetWidth);
  y = event.clientY + parseInt(obj.offsetHeight);
  
  marginx = document.body.clientWidth - x;
  marginy = document.body.clientHeight - y;
  
  if (marginx < 0)
    x = event.clientX + document.body.scrollLeft + marginx;
  else
    x = event.clientX + document.body.scrollLeft;
  if (marginy < 0)
    y = event.clientY + document.body.scrollTop + marginy;
  else
    y = event.clientY + document.body.scrollTop;
  
  obj.style.posLeft = x;
  obj.style.posTop = y;
  
  if (status == "visible") {
    if (select_obj) {
      select_obj.style.visibility = "hidden";
      select_obj = null;
    }
    select_obj = obj;
    select_over = true;
  } else {
    select_obj = null;
  }
  
  obj.style.visibility = status;
}

function show_info(name,status) {
  var obj = document.all[name];
  var x, y;
  
  if (document.body.clientWidth / 2 > event.clientX) {
    x = event.clientX + document.body.scrollLeft + 10;
    if (x + parseInt(obj.offsetWidth) - document.body.scrollLeft > document.body.clientWidth)
      x = x - (x + parseInt(obj.offsetWidth) - document.body.scrollLeft - document.body.clientWidth);
  } else {
    x = event.clientX + document.body.scrollLeft - parseInt(obj.offsetWidth) - 10;
    if (x < document.body.scrollLeft)
      x = document.body.scrollLeft;
  }
  
  if (document.body.clientHeight / 2 > event.clientY) {
    y = event.clientY + document.body.scrollTop + 10;
    if (y + parseInt(obj.offsetHeight) - document.body.scrollTop > document.body.clientHeight) {
      y = y - (y + parseInt(obj.offsetHeight) - document.body.scrollTop - document.body.clientHeight);
    }
  } else {
    y = event.clientY + document.body.scrollTop - parseInt(obj.offsetHeight) - 10;
    if (y < document.body.scrollTop) {
      y = document.body.scrollTop;
    }
  }
  
  obj.style.posLeft = x;
  obj.style.posTop = y;
  
  if (status == "visible") {
    if (select_obj) {
      select_obj.style.visibility = "hidden";
      select_obj = null;
    }
    select_obj = obj;
    select_over = true;
  } else {
    select_obj = null;
  }
  
  obj.style.visibility = status;
}

function do_blink() {
  var blink = document.all.tags("BLINK");
  for (var i = 0; i < blink.length; i++)
    blink[i].style.visibility = blink[i].style.visibility == "" ? "hidden" : "";
}

function start_onload() {
  if (document.all) {
    setInterval("do_blink()", 1000);
  }
  
  // ÀÚµ¿ Æ÷Ä¿½Ì
  if (document.mov_fes_award != null && document.mov_fes_award.name != null)
    document.mov_fes_award.name.focus();
  
  if (document.mov_fes_section != null && document.mov_fes_section.name != null)
    document.mov_fes_section.name.focus();
}

//window.onload=start_onload;


/*
function document_load() {
  if (typeof(allslidedown) == "function")
    allslidedown();
}
*/


// PAGE

function popup_noacq_emailadd() {
  window.open("../member/popup_noacq_emailadd.php","popup_noacq_emailadd","width=300,height=220");
}

function popup_forget_id() {
  window.open("../member/popup_forget_id.php","popup_forget_id","width=300,height=225");
}

function popup_forget_password() {
  window.open("../member/popup_forget_password.php","popup_forget_password","width=300,height=435");
}

function popup_img(img_file, img_width, img_height) {
  window.open("../mid/popup_img.php?img_file="+img_file,"popup_img","width="+(img_width + 10)+",height="+(img_height + 48)+",resizable=yes");
}

// MEMBER

function popup_profile(mbr_id) {
  window.open("../member/popup_profile.php?mbr_id="+mbr_id,"popup_profile","width=634,height=480,scrollbars=yes,resizable=yes");
}

function popup_homepage(homepage) {
  window.open(homepage);
}

function popup_memo(receiver_id) {
  window.open("../member/popup_memo.php?receiver_id="+receiver_id,"popup_memo","width=300,height=280");
}

// GET

function get_bytes_length(str) {
  var str_len = 0;
  
  for (var i = 0; i < str.length; i++) {
    if (escape(str.charAt(i)).length >= 4)
      str_len += 2;
    else
      str_len++;
  }
  
  return str_len;
}

function get_checkbox_count(checkbox) {
  if (checkbox == null) {
    return 0;
  } else if (checkbox.length == null) { // ¼±ÅÃ Ç×¸ñÀÌ ÇÏ³ª»Ó
    return (checkbox.checked) ? 1 : 0;
  } else {
    var checked_cnt = 0;
    for (var i = 0; i < checkbox.length; i++) {
      if (checkbox[i].checked) checked_cnt++;
    }
    
    return checked_cnt;
  }
}

function get_radio_value(radio) {
  for (var i = 0; i < radio.length; i++) {
    if (radio[i].checked) return radio[i].value;
  }
  
  return "";
}

// FUNCTION

function ltrim(a) {
  var i = 0;
  while (a.substring(i, i+1) == " " || a.substring(i, i+1) == "¡¡") i = i + 1;
  return a.substring(i);
}

function rtrim(a) {
  var b;
  var i = a.length - 1;
  while (i >= 0 && (a.substring(i, i+1) == " " || a.substring(i, i+1) == "¡¡")) i = i - 1;
  return a.substring(0, i+1);
}

function trim(a) {
  return(ltrim(rtrim(a)));
}

function rpad(str, len, ch) {
  if (typeof(str) != "string")
    str = str.toString();
  
  var strlen = trim(str).length;
  var ret = "";
  var alen = len - strlen;
  var astr = "";
  
  //ºÎÁ·ÇÑ ¼ýÀÚ¸¸Å­ len Å©±â·Î ch ¹®ÀÚ·Î Ã¤¿ì±â
  for (var i = 0; i < alen; ++i) {
    astr = astr + ch;
  }
  
  ret = trim(str) + astr; //µÚ¿¡¼­ Ã¤¿ì±â
  
  return ret;
}

function lpad(str, len, ch) {
  if (typeof(str) != "string")
    str = str.toString();
  
  var strlen = trim(str).length;
  var ret = "";
  var alen = len - strlen;
  var astr = "";
  
  //ºÎÁ·ÇÑ ¼ýÀÚ¸¸Å­ len Å©±â·Î ch ¹®ÀÚ·Î Ã¤¿ì±â
  for (var i = 0; i < alen; ++i) {
    astr = astr + ch;
  }
  
  ret = astr + trim(str); //¾Õ¿¡¼­ Ã¤¿ì±â
  
  return ret;
}

// CHECK

function is_hangul(str) {
  for (var i = 0; i < str.length; i++) {
    var code = parseInt(str.charCodeAt(i));
    if (code <= 255) return false;
  }
  return true;
}

function is_number(str) {
  for (var i = 0; i < str.length; i++) {
    var char = str.substr(i,1).toUpperCase();
    if (char < "0" || char > "9") return false;
  }
  return true;
}

function is_alphabet_or_number(str) {
  for (var i = 0; i < str.length; i++) {
    var char = str.substr(i, 1).toUpperCase();
    if ((char < "0" || char > "9") && (char < "A" || char > "Z")) return false;
  }
  return true;
}

function is_hangul_or_number(str) {
  for (var i = 0; i < str.length; i++) {
    var code = parseInt(str.charCodeAt(i));
    var char = str.substr(i, 1).toUpperCase();
    if (code <= 255 && (char < "0" || char > "9")) return false;
  }
  return true;
}

function is_hangul_or_alphabet_or_number(str) {
  for (var i = 0; i < str.length; i++) {
    var code = parseInt(str.charCodeAt(i));
    var char = str.substr(i, 1).toUpperCase();
    if (code <= 255 && (char < "0" || char > "9") && (char < "A" || char > "Z")) return false;
  }
  return true;
}

function is_all_space(str) {
  for (var i = 0; i < str.length; i++) {
    var char = str.substr(i,1);
    if (char != " ") return false;
  }
  return true;
}

function is_valid_email(str) {
  for (var i = 0; i < str.length; i++) {
    var char = str.substr(i,1).toUpperCase();
    if ((char < "0" || char > "9") && (char < "A" || char > "Z") && char != "." && char != "-" && char != "_") return false;
  }
  return true;
}


// IMG PICKER

function popup_img_picker(input_name) {
  window.open("../poptooni/popup_img_picker.php?input_name="+input_name,"popup_img_picker","width=600,height=530,scrollbars=yes,resizable=yes,status=yes");
}

function popup_img_picker_new(input_name) {
  window.open("../poptooni/popup_img_picker.php?act=new&input_name="+input_name,"popup_img_picker","width=600,height=530,scrollbars=yes,resizable=yes,status=yes");
}

function set_img_picker(input_name, input_value) {
  var input_obj = document.getElementById(input_name);
  input_obj.value = input_value;
}



/**
·£´ý Ãâ·Â Àû¿ëÇÔ¼ö
ÆäÀÌÁö³»ÀÇ ¸ðµç TBODY.RandomContainer ¸¦ Ã£¾Æ¼­ ·£´ý ¼³Á¤À» Àû¿ëÇÑ´Ù.
*/
function GetNumberAttribute(obj, name, defaultValue)
{
	var s = obj.getAttribute(name);
	if (s == null || s == '' || isNaN(Number(s)))
		return defaultValue;
	else
		return Number(s);
}

function ShowRandomItem()
{
	var containers = document.getElementsByTagName('TBODY');
	for (var i = 0; i < containers.length; i++)
	{
		var container = containers.item(i);
		if (container == null || container.className != 'RandomContainer')
			continue;

		var count = GetNumberAttribute(container, 'r:count', 1);
		var item_list = new Array(), item_count = 0;
		for (var j = 0; j < container.childNodes.length; j++)
		{
			var child = container.childNodes.item(j);
			if (child.nodeType == 1 && child.tagName == 'TR' && child.className == 'RandomItem')
			{
				var weight = GetNumberAttribute(child, 'r:weight', 1);
				for (var k = 0; k < weight; k++)
					item_list[item_list.length] = child;
				if (weight > 0)
					item_count++;
			}
		}

		var show_count = 0;
		while (show_count < count && show_count < item_count)
		{
			var rand = Math.random();
			var index = Math.floor(rand * 1000) % item_list.length;
			var tr = item_list[index];
			if (tr == null)
				continue;
			for (var j = 0; j < item_list.length; j++) {
				if (item_list[j] == tr) {
					item_list[j] = null;
				}
			}
			tr.className = '';
			show_count++;
		}
	}
}
