// ±âÁ¸ Site Functions
function window_popup(url,width,height,status,name)
{
    if(!name) name = "";
    if(status) status = ","+status;
    var left = (screen.width - width - 10) / 2;
    var top = (screen.height - height - 80) / 2;
    var win_status = "width="+width+",height="+height+",left="+left+",top="+top+status;
    window.open(url,name,win_status);
}

// Position Elements Functions

// elementÀÇ x, y, width, height¸¦ °¡Á®¿È.
function get_pos(selectObj)
{
    var pos = new Array();
    
    var pos_x = 0, pos_y = 0;
    
    // while¿¡¼­ offsetParent¸¦ Ã£¾Æ°¡´Â ¹æ½ÄÀÌ¶ó
    // ´Ù¸¥ ÁöÁ¤ÀÚ¿¡ ÀÎ¼ö·Î ³Ñ¾î¿Â object¸¦ ³Ñ°ÜÁÜ.
    obj = selectObj;
    
    // ÁÂÇ¥°è»ê
    while(obj.offsetParent)
    {
        pos_y += parseInt(obj.offsetTop);
        pos_x += parseInt(obj.offsetLeft);
        
        obj = obj.offsetParent;
    }
    pos_x += parseInt(obj.offsetLeft);
    pos_y += parseInt(obj.offsetTop);
    
    // ÁÂÇ¥ÀúÀå
    pos.x = pos_x;
    pos.y = pos_y;
    pos.x2 = pos_x + selectObj.offsetWidth;
    pos.y2 = pos_y + selectObj.offsetHeight;
    
    return pos;
}


function show_div(e, id)
{
    var obj = document.getElementById(id);
    if(!e)
        e = event;
    
    if(obj)
    {
        obj.style.visibility = "visible";
    }
}

function hide_div(e, id)
{
    var obj = document.getElementById(id);
    if(obj)
    {
        obj.style.visibility = "hidden";
    }
}

function body_blind(set)
{
    if(typeof move_quick == 'function')
    {
        move_quick();
    }
    
    var bodyCap_div = document.getElementById('bodyCap_div');
    // Body ¿µ¿ªÀÇ ÀüÃ¼ »çÀÌÁî
    var body_h = 0;
    var docElement = document.body || document.documentElement || null;
    
    if(!bodyCap_div)
    {
        var bodyCap_div = document.createElement("DIV");
        docElement.appendChild(bodyCap_div);
    }
    
    if(typeof(set) != 'boolean')
        set = true;
    
    bodyCap_div.id = "bodyCap_div";
    bodyCap_div.style.position = "absolute";
    bodyCap_div.style.top = "0px";
    bodyCap_div.style.left = "0px";
    
    try
    {
        var width = parseInt(window.scrollMaxX) + parseInt(docElement.clientWidth);
        var height = parseInt(window.scrollMaxY) + parseInt(docElement.clientHeight);
        
        bodyCap_div.style.width = width + 'px';
        bodyCap_div.style.height = height + 'px';
    }
    catch(e)
    {
        bodyCap_div.style.width = (docElement.scrollWidth > docElement.clientWidth)?  docElement.scrollWidth : docElement.clientWidth;
        bodyCap_div.style.height = (docElement.scrollHeight > docElement.clientHeight)?  docElement.scrollHeight : docElement.clientHeight;
    }
    
    //bodyCap_div.style.backgroundColor = "#00004f";
    bodyCap_div.style.backgroundColor = "#000000";
    bodyCap_div.style.zIndex = 10;
    
    if(set == true)
    {
        if(typeof bodyCap_div.style.MozOpacity == "string")
        {
            bodyCap_div.style.MozOpacity = 0.35;
        }
        else if(typeof bodyCap_div.style.filter == "string")
        {
             bodyCap_div.style.filter = "Alpha(opacity="+parseInt(100*0.35)+")";
        }
        else
        {
            bodyCap_div.style.opacity = 0.35;
        }
        bodyCap_div.style.visibility = "visible";
        
        window.onresize = body_blind;
    }
    else
    {
        bodyCap_div.style.width = "0px";
        bodyCap_div.style.height = "0px";
        bodyCap_div.style.visibility = "hidden";
        
        window.onresize = function(e) { return false; }
    }
}

// Image Element Functions

function swap_image(img, txt, ext)
{
    if(img.src.indexOf(txt) != -1)
        img.src = img.src.replace(txt, "");
    else
        img.src = img.src.replace(ext, txt+ext);
}
function change_image(img, src, des)
{
    if(img.src.indexOf(src) != -1)
        img.src = img.src.replace(src, des);
}
function setOpacity(obj, opacity)
{
    var f_opacity = (opacity <= 0)? 0 : opacity/100;
    
    if(typeof obj.style.MozOpacity == "string") // firefox
    {
        obj.style.MozOpacity = f_opacity;
    }
    else if(typeof obj.style.KhtmlOpacity == "string") // safari
    {
        obj.style.KhtmlOpacity = f_opacity;
    }
    else if(typeof obj.style.filter == "string") // ie 
    {
        obj.style.filter = "Alpha(opacity="+parseInt(100*f_opacity)+")";
    }
    else // other
    {
        obj.style.opacity = f_opacity;
    }
}

// Media Object Functions
function show_flash(swf, width, height)
{
    document.write("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab#version=8,0,24,0\" width=\""+width+"\" height=\""+height+"\" align=\"middle\">\n");
    document.write("<param name=\"movie\" value=\""+swf+"\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#FFFFFF\" /><param name=wmode value=\"transparent\">\n");
    document.write("<embed src=\""+swf+"\" width=\""+width+"\" height=\""+height+"\" align=\"middle\" quality=\"high\" bgcolor=\"#FFFFFF\" wmode=\"transparent\" name=\"skin_navigation\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />\n");
    document.write("</object>\n");
}


// Cookie Functions
function setCookie(name, value, expiredays)
{
    var todayDate = new Date();
    todayDate.setDate(todayDate.getDate() + expiredays);
    document.cookie = name + "=" + escape(value) + "; path=/; expires=" + todayDate.toGMTString() + "; domain=" + ".hanin.co.kr;";
}

function getCookie(name)
{
    var nameOfCookie = name + "=";
    var x = 0;
    while(x <= document.cookie.length)
    {
        var y = (x+nameOfCookie.length);
        if (document.cookie.substring( x, y ) == nameOfCookie)
        {
            if ((endOfCookie=document.cookie.indexOf( ";", y )) == -1)
                endOfCookie = document.cookie.length;
            return unescape(document.cookie.substring( y, endOfCookie ));
        }
        x = document.cookie.indexOf(" ", x) + 1;
        if (x == 0)
            break;
    }
    return "";
}

// Valid Check Functions
function check_companyno(rgstNo1, rgstNo2, rgstNo3){
    var rgstNo = rgstNo1 + rgstNo2 + rgstNo3;
    var sumMod;

    if(rgstNo.length != 10)
    {
        return false;
    }

    sumMod = 0;
    sumMod += parseInt(rgstNo.substring(0,1));
    sumMod += parseInt(rgstNo.substring(1,2)) * 3 % 10;
    sumMod += parseInt(rgstNo.substring(2,3)) * 7 % 10;
    sumMod += parseInt(rgstNo.substring(3,4)) * 1 % 10;
    sumMod += parseInt(rgstNo.substring(4,5)) * 3 % 10;
    sumMod += parseInt(rgstNo.substring(5,6)) * 7 % 10;
    sumMod += parseInt(rgstNo.substring(6,7)) * 1 % 10;
    sumMod += parseInt(rgstNo.substring(7,8)) * 3 % 10;
    sumMod += Math.floor(parseInt(rgstNo.substring(8,9)) * 5 / 10);
    sumMod += parseInt(rgstNo.substring(8,9)) * 5 % 10;
    sumMod += parseInt(rgstNo.substring(9,10));
    
    if (sumMod % 10!= 0)
    {
        return false;
    }
    
    return true;
}

function check_publicno(sno1, sno2) 
{ 
    var serialValue = sno1 + "-" + sno2;
    
    if(serialValue.length < 14)
        return false;
    var sex = serialValue.substring(7,8);
    if(sex != 1 && sex != 2 && sex != 3 && sex != 4)
        return false;
        
    //³¯Â¥ À¯È¿¼º °Ë»ç 
    birthYear = (serialValue.charAt(7) <= "2") ? "19" : "20"; 
    birthYear += serialValue.substr(0, 2); 
    birthMonth = serialValue.substr(2, 2) - 1; 
    birthDate = serialValue.substr(4, 2); 
    var birth = new Date(birthYear, birthMonth, birthDate); 
    if ((birth.getYear() % 100 != serialValue.substr(0, 2)) || 
        (birth.getMonth() != birthMonth) || 
        (birth.getDate() != birthDate))
    { 
        return false; 
    } 
    
    //ÄÚµå À¯È¿¼º °Ë»ç 
    var buf = new Array(13); 
    for (var i = 0; i < 6; i++)
        buf[i] = parseInt(serialValue.charAt(i)); 
    
    for (var i = 6; i < 13; i++)
        buf[i] = parseInt(serialValue.charAt(i + 1)); 

    var multipliers = [2,3,4,5,6,7,8,9,2,3,4,5]; 
    for (var i = 0, sum = 0; i < 12; i++)
        sum += (buf[i] *= multipliers[i]); 

    if ((11 - (sum % 11)) % 10 != buf[12])
        return false; 
    
    return true;
}

function isDate(checkDate)
{
    if(checkDate.length != 8)
        return false;
    
    var y = checkDate.substring(0, 4);
    var m = checkDate.substring(4, 6);
    var d = checkDate.substring(6, 8);
    var t = new Date(y, m-1, d);
    
    var t_year = (document.layers || document.all)? t.getYear() : 1900 + t.getYear();
    
    if(t_year != Number(y) || t.getMonth()+1 != Number(m) || t.getDate() != Number(d))
        return false;
    else
        return true;
}

// Formated Functions
function numberFormat(str)
{
    str = ""+str+"";
    var retValue = "";
    for(iii=0; iii<str.length; iii++)
    {
        if(iii > 0 && (iii%3)==0)
        {
            retValue = str.charAt(str.length - iii -1) + "," + retValue;
        }
        else
        {
            retValue = str.charAt(str.length - iii -1) + retValue;
        }
    }
    if(retValue=="0")
    {
        return "";
    }
    
    return retValue;
}


// Elements Controll Functions

function check_input_len(input_obj, msg_obj_name, max_len)
{
    // Usage
    // onKeyUp="check_input_len(this, 'message_display_object_name(div or span)', 200)"
    
    var msg_obj = document.getElementById(msg_obj_name);
    var cnt = 0;
    
    if(typeof(input_obj) == "undefined") return false;
    
    for(i=0; i<input_obj.value.length; i++)
    {
        if(input_obj.value.charCodeAt(i) == 13)
            continue;
        
        cnt ++;
        if(input_obj.value.charCodeAt(i) > 0x7f)
            cnt ++;
        
        if(cnt > max_len)
        {
            alert(max_len+"ÀÚ ÀÌ³»·Î ÀÛ¼ºÇÏ¼Å¾ß ÇÕ´Ï´Ù.");
            input_obj.value = input_obj.value.substring(0,i);
            
            if(input_obj.value.charCodeAt(i) > 0x7f)
                cnt -= 2;
            else
                cnt -= 1;
            
            break;
        }
    }
    if(cnt > max_len)
        cnt = max_len;
    if(msg_obj)
        msg_obj.innerHTML = cnt;
}

function checkbox_select_all(s, chk_name)
{
    var obj = document.getElementsByName(chk_name);
    
    for(var i=0; i<obj.length; i++)
    {
        if(obj[i].disabled == false)
            obj[i].checked = s.checked;
        else
            obj[i].checked = false;
    }
}

function checkbox_selected(f, chk_name)
{
    var obj = document.getElementsByName(chk_name);
    
    for(var i=0; i<obj.length; i++)
    {
        if(obj[i].disabled == false && obj[i].checked == true)
            return true;
    }
    return false;
}

// Usage : onKeyUp="onlyNumber(this)"
// http://www.openphp.co.kr/samples/onlynumber.html
function onlyNumber(obj)
{
    if(typeof obj != 'object' || obj.value == 'undefined')
        return;
    
    if(obj.value.replace(/[^0-9]/g, '') != obj.value)
    {
        alert("¼ýÀÚ¸¸ ÀÔ·ÂÇÒ ¼ö ÀÖ½À´Ï´Ù.");
        obj.value = obj.value.replace(/[^0-9]/g, '');
    }
}

// Usage : onKeyUp="nextIndex(this, 6, document.inform.public_no2)"
// http://www.openphp.co.kr/samples/onlynumber.html
function nextIndex(obj, length, next_obj)
{
    if(typeof obj != 'object' || obj.value == 'undefined')
        return;
    
    if(obj.value.length == length)
        next_obj.focus();
}

function serialize_form(f)
{
    if(!f)
        var f = document.forms[0].elements;
    var buf = "";
    for(i=0; i<f.length; i++)
    {
        switch(f[i].type)
        {
            case 'hidden' :
            case 'text'  :
            case 'password' :
            case 'textarea' :
            case 'select-one' :
                if(f[i].disabled == false)
                    buf += "&" + f[i].name + "=" + f[i].value.replace(/&/g, '%26');
                break;
            case 'checkbox' :
                if(f[i].disabled == false && f[i].checked == true && f[i].name)
                    buf += "&" + f[i].name + "=" + f[i].value.replace(/&/g, '%26');
                break;
            case 'radio'   :
                if(f[i].disabled == false && f[i].checked == true)
                    buf += "&" + f[i].name + "=" + f[i].value.replace(/&/g, '%26');
                break;
            case 'select-multiple' :
                for(j=0; j<f[i].length; j++)
                {
                    if(f[i].disabled == false && f[i][j].selected == true)
                    {
                        if(f[i].name.indexOf("[]") != -1)
                            buf += "&" + f[i].name + "=" + f[i][j].value.replace(/&/g, '%26');
                        else
                            buf += "&" + f[i].name + "[]=" + f[i][j].value.replace(/&/g, '%26');
                    }
                }
                break;
        }
    }
    return buf;
}

// document.event controll functions

/**
*
*  Unselectable text
*  http://www.webtoolkit.info/
*
**/
var Unselectable =
{
    enable : function(e)
    {
        var e = e ? e : window.event;
        if(e.button != 1) {
            if(e.target)
                var targer = e.target;
            else if(e.srcElement)
                var targer = e.srcElement;
            
            var targetTag = targer.tagName.toLowerCase();
            if((targetTag != "input") && (targetTag != "textarea"))
                return false;
        }
    },
    disable : function ()
    {
        return true;
    }
}

// http://www.openphp.co.kr/samples/contextmenu_disable.html
function enableUnselectable()
{
    if(typeof(document.onselectstart) != "undefined")
    {
        document.onselectstart = Unselectable.enable;
    }
    else
    {
        document.onmousedown = Unselectable.enable;
        document.onmouseup = Unselectable.disable;
    }
}

// http://www.openphp.co.kr/samples/contextmenu_disable.html
function disableContextmenu()
{
    document.oncontextmenu = function(e)
    {
        if(!e) var e = window.event;
        var target = e.target || e.srcElement;
        
        if(target.getAttribute('name') == 'contextMenuOn')
        {
            alert('contextmenu');
        }
        return false;
    }
}

function setBorder(e)
{
    if(!e) var e = window.event;
    var obj = e.target || e.srcElement;
    
    if(e.type == "focus")
    {
        obj.style.border = "1px solid #f17e0b";
    }
    else
    {
        obj.style.cssText = "";
    }
}

function setFocus()
{
    var element1 = document.getElementsByTagName('input');
 //   var element2 = document.getElementsByTagName('textarea');
    for(var i=0; i<element1.length; i++)
    {
        if(element1[i].getAttribute('type') == 'text' || element1[i].getAttribute('type') == 'password')
        {
            element1[i].onfocus = setBorder;
            element1[i].onblur = setBorder;
        }
    }
    
//    for(var i=0; i<element2.length; i++)
  //  {
    //    element2[i].onfocus = setBorder;
     //   element2[i].onblur = setBorder;
   // }
}

//window.onload = setFocus;

// Ajax logout function
// <script type="text/javascript" src="/js/ajax.js"></script>
// onClick="logout()" or href="javascript:logout()"
function logout()
{
    var url = "/login/ajax_logout.php";
    var data = "";
    
    if(typeof AjaxTxt == 'function')
    {
        AjaxTxt(url, data, 'ajax_callback');
    }
    else
    {
        alert("ajax.js°¡ includeµÇÁö ¾Ê¾Ò½À´Ï´Ù.");
    }
}

function check_login()
{
    var f = document.inform;
    
    if(f.user_id.value == "")
    {
        alert("¾ÆÀÌµð¸¦ ÀÔ·ÂÇØÁÖ½Ê½Ã¿À.");
        f.user_id.focus();
        return false;
    }
    
    if(f.passwd.value == "")
    {
        alert("ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇØÁÖ½Ê½Ã¿À.");
        f.passwd.focus();
        return false;
    }
    
    var url = "/login/ajax_login.php";
    var data = "user_id=" + f.user_id.value + "&passwd=" + f.passwd.value.replace(/&/g, '%26') + "&url=" + encodeURIComponent(f.url.value);
    
    if(typeof f.id_save == 'object')
    {
        if(f.id_save.checked == true)
            data += "&id_save=1";
        else
            data += "&id_save=0";
    }
    AjaxTxt(url, data, 'ajax_callback');
    
    return false;
}
