var SESSIONID_APPLET="5D39E477-7E59-45e0-8BBD-3F0F1CCE9A5";
var is_current = 0;
var cntRetry = 0;
var saycontrol_upgraded=0;
var SayControl = undefined;

function fo_finish (IsKbd)
{
  if(IsKbd && typeof(event) != 'undefined' && event)
    event.returnValue = false;

  if(document.fo_login.usrid.type!='hidden' && (0 == document.fo_login.usrid.value.length || 0 == document.fo_login.passwd.value.length))
  {
    if (typeof(loginLayer) == "object" && typeof(loginLayer.handleFocus) == "function")
      loginLayer.handleFocus();
    else
      document.fo_login.usrid.focus();

    login_message_handler("¾ÆÀÌµð¿Í ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.","02",0);
    return;
  }

  if (isIE())
  {
    document.fo_login.passwd.disabled = true;
    document.fo_login.usrid.disabled  = true;
  }

  start_login(document.fo_login.usrid.value);
}

function start_login(usrid)
{
  upgrade_saycontrol();
  enable_saycontrol();
  SayControl = document.getElementById('SayControl');
  try_login(usrid);
}

function try_login(usrid)
{
  if(isIE())
  {
	if(SayControl.sessionversion=="undefined") saycontrol_error();
    if(typeof(SayControl.sessionversion)=="undefined") saycontrol_error();
    if (cntRetry > 10) saycontrol_error();
    if(SayControl.readyState!=4)
    {
      if ("undefinded" != typeof(usrid) && "unknown" != typeof(usrid))
      {
        var cmd = "try_login('"+usrid+"')";
        setTimeout(cmd,100);
        cntRetry++;
      }
      return 0;
    }
  }

  if(usrid.search(/[¤¡-¤¾|¤¿-¤Ó|°¡-Èþ]/) != -1)
  {
    login_message_handler("µî·ÏµÇÁö ¾ÊÀº »ç¿ëÀÚ ÀÔ´Ï´Ù. <br>Á¤È®ÇÑ ¿µ¹®¾ÆÀÌµð¸¦ ÀÔ·ÂÇÏ¿© ÁÖ½Ê½Ã¿ä.","05",1);
    return;
  }

  usrid = usrid.replace(/\s/g, '');
  set_saycontrol(SayControl);
  var err = SayControl.login(usrid,usrid);
  if(err == -44 )
  {
    login_message_handler("µ¿ÀÏÇÑ PC¿¡¼­ ´Ù¼öÀÇ ¾ÆÀÌµð·Î µ¿½Ã¿¡ ·Î±×ÀÎ ÇÏ½Ç ¼ö ¾ø½À´Ï´Ù.(44)","05",1);
    doGo();
  }
  else if(err <= 0)
  {
    login_message_handler("·Î±×ÀÎ ÄÁÆ®·Ñ¿¡¼­ ¿À·ù°¡ ¹ß»ýÇß½À´Ï´Ù.<br>¿¡·¯ÄÚµå: " + (-err),"05",1);
    doGo();
  }
}

function login_error_handler(msg,mascot_file_num)
{
  is_current = 0;
  var args = new Array();
  var winTitle = "·Î±×ÀÎ ¾È³»";
  var from = '';

  if ( document.fo_login.from.value == "" || document.fo_login.from.value == "sayclub" ) from = 'sayclub';
  else from = 'pmang';

  msg = msg.replace(/TAGLEFT;/g,"<");

  args["msg"]       = msg;
  args["icon"]      = "<img src='"+getSSLResourceURL("http://file.pmang.kr/images/img_common_error.gif")+"'>";
  args["winTitle"]  = winTitle;
  args["btnTitle"]  = "È®ÀÎ";
  args["btnWidth"]  = "70";
  args["btnLink"]  = "javascript:yessir();";

  if (from == "sayclub")
  {
    openAlert("common", winTitle, msg);
    if (typeof(loginLayer) == "object" && typeof(loginLayer.handleFail) == "function")
      loginLayer.handleFail();
  }
  else if (from == "pmang")
  {
    if(mascot_file_num == "99")
    {
      openModalAlertLoginBig(winTitle, args["msg"]);
    }
    else
    {
      openModalAlert(winTitle, args["msg"]);
    }
  }
  else
  {
    openModalAlert(winTitle, args["msg"]);
  }
  showloginform();
}

function login_check_handler(msg)
{
  is_current = 0;
  var args = new Array();
  var winTitle = "·Î±×ÀÎ ¾È³»";

  msg = msg.replace(/TAGLEFT;/g,"<");

  args["msg"]       = msg;
  args["icon"]      = "<img src='"+getSSLResourceURL("http://file.pmang.kr/images/img_common_error.gif")+"'>";
  args["winTitle"]  = winTitle;
  args["btnTitle"]  = "È®ÀÎ";
  args["btnWidth"]  = "70";
  args["btnLink"]  = "javascript:yessir();";

  openModalAlertLoginBig(winTitle, args["msg"]);
}

function multilogin_handler(usrid,killmeurl)
{
  var ret;
  var is_current = 1;

  if ( document.fo_login.from.value == "" || document.fo_login.from.value == "sayclub" ) from = 'sayclub';
  else from = 'pmang';
  var args = new Array();
  var winTitle = "ÀÌ¹Ì Á¢¼ÓÇØÀÖ½À´Ï´Ù.";

  if( typeof(document.fo_login.sso_type) == "object" && ( document.fo_login.sso_type.value == 4 || document.fo_login.sso_type.value == 5 ) ) 
  {
    ret = openModalAlreadyLoggedIn(usrid, killmeurl, true);
  }
  else
  {
    ret = openModalAlreadyLoggedIn(usrid, killmeurl, false);
  }

  if(1==ret) try_login(usrid);
  else showloginform();
  return;

}

function login_handler2( result )
{
  result = SayControl.codec( 50, result, "" );
  if (typeof(progressid) != 'undefined' && progressid)
  {
    clearTimeout(progressid);
    progressid = '';
  }
  eval(result);
}

function upgrade_saycontrol()
{
  if(saycontrol_upgraded) return;
  if(isIE())
  {
    var SayControlHelper = document.getElementById("SayControlHelper");
	if(SayControlHelper.sessionversion=="undefined") return;
    if(typeof(SayControlHelper.sessionversion)=="undefined") return;
    if(SayControlHelper.sessionversion>=sayctl_version) return;
    if(SayControlHelper.externalmethod("install", SESSIONID_APPLET, 0, 0, "Á¢¼Ó ÇÁ·Î±×·¥ ¾÷±×·¹ÀÌµå"))
    {
      saycontrol_upgraded=1;
    }
  }
/*
  else // Currently FireFox Only  
  {
    var pluginFound = false;
    var pluginName = $("#SayControl").attr("pluginName");
    var pluginVersion = $("#SayControl").attr("pluginVersion");
    var xpiName = $("#SayControl").attr("xpiName");
    var xpiExec = $("#SayControl").attr("xpiExec");
    var xpi = $("#SayControl").attr("xpi");
    var plugins = navigator.plugins;
    for (i = 0; i < plugins.length && pluginFound==false; i++)
    {
      if (plugins[i].name == pluginName && plugins[i].version == pluginVersion)
      {
        pluginFound = true;
      }
    }
    if(!pluginFound) 
    {
      var xpiObj = { xpiName : xpiExec };
      alert(xpiObj);
      InstallTrigger.install(xpiObj);
    }
  }*/
}

function enable_saycontrol()
{
  if(!isIE()) return;
  document.body.insertAdjacentHTML("afterBegin",'<span id="span_sayctl" style="display:none"></span>');
  var text='<object id=SayControl name=SayControl classid="clsid:'+sayctl_classid+'" codebase="'+getSSLResourceURL(sayctl_codebase)+'"></object>';
  span = document.getElementById("span_sayctl");
  span.innerHTML = text;
//  $("#span_sayctl").attr("innerHTML", text);
}

function login_message_handler(msg,mascot_file_num, loginform, result)
{
  if (!isIE())
  {
    alert(msg);
    return;
  }

  if ( document.fo_login.from.value == "" || document.fo_login.from.value == "sayclub" ) from = 'sayclub';
  else from = 'pmang';
  var args = new Array();
  var winTitle = "·Î±×ÀÎ ¾È³»";

  args["msg"]       = msg;
  args["icon"]      = "<img src='"+getSSLResourceURL("http://file.pmang.kr/images/img_common_error.gif")+"'>";
  args["winTitle"]  = winTitle;
  args["btnTitle"]  = "È®ÀÎ";
  args["btnWidth"]  = "70";
  args["btnLink"]  = "javascript:yessir();";

  if (result)
  {
    args["result"] = result;
    _height = 255;
  }
  else
    _height = 240;

  if (from == "sayclub")
  {
    openAlert("common", winTitle, msg);
    if (typeof(loginLayer) == "object" && typeof(loginLayer.handleFail) == "function")
      loginLayer.handleFail();
  }
  else
  {
    openModalAlert(winTitle, msg);
  }

 if(loginform == 1)showloginform();
}

function isSecureConnect()
{
  var bSSO = false;
  var bSecureConnect = false;

  if(typeof(document.fo_login.conntype)!="undefined")
  {
    if (typeof(document.fo_login.conntype.length)!="undefined")
    {
      if (document.fo_login.conntype.length >= 3 && document.fo_login.conntype[2].checked && document.fo_login.conntype[2].value == 3)
        bSSO = true;

      if( (document.fo_login.conntype[1].checked && document.fo_login.conntype[1].value == 2) || bSSO )
        bSecureConnect = true;
    }
    else
    {
      if (document.fo_login.conntype.value == 3)
        bSSO = true;

      if( (document.fo_login.conntype.checked && document.fo_login.conntype.value == 2) || bSSO )
        bSecureConnect = true;
    }
  }

  return bSecureConnect;
}

function login_handler_sayr(result)
{
  login_handler(result);
}

var progressid = '';

function login_handler(result,key,ip,port,cip,macaddress,uuid,gwmacaddress,mac2)
{
  if(result=="currentalready")
  {
    try {
      if (document.fo_login.from.value == "sayclub")
        login_message_handler("´Ù¸¥ ÀÎÅÍ³Ý ÀÍ½ºÇÃ·Î·¯ Ã¢¿¡<br> ÇØ´ç ¾ÆÀÌµð·Î ÀÌ¹Ì ·Î±×ÀÎ ÁßÀÔ´Ï´Ù.","02",1, result);
      else if( typeof(document.fo_login.sso_type) == "object" && ( document.fo_login.sso_type.value == 4 || document.fo_login.sso_type.value == 5 ) )
      {
        login_message_handler("<center>´Ù¸¥ ÀÎÅÍ³Ý ÀÍ½ºÇÃ·Î·¯ Ã¢¿¡¼­<br> ÇØ´ç ¾ÆÀÌµð·Î Á¦ÈÞ ¼­ºñ½º¸¦ ÀÌ¿ë ÁßÀÔ´Ï´Ù.</center>","02",1, result);
        document.location = "http://www." + document.domain;
      }
      else
        login_message_handler("<center>´Ù¸¥ ÀÎÅÍ³Ý ÀÍ½ºÇÃ·Î·¯ Ã¢¿¡<br> ÇØ´ç ¾ÆÀÌµð·Î ÀÌ¹Ì ·Î±×ÀÎ ÁßÀÔ´Ï´Ù.</center>","02",1, result);

      if (document.fo_login.conntype[2].checked) // if paran sso (partner»ç Ãß°¡½Ã ¼öÁ¤ÇÊ¿ä)
        document.location = 'http://www.pmang.com';
    }catch(e){}

    return;
  }

  if(result=="anotheralready")
  {
    login_message_handler("Á¢¼Ó ¿À·ùÀÔ´Ï´Ù.","05",1);
    return;
  }

  if(result=="dualconnected")
  {
    if(typeof(post_login_script) != 'undefined')
    {
      eval(post_login_script);
    }

    if(pageurl.indexOf('istachy=1') < 0 )
    {
      if (pageurl.indexOf("?") < 0)
        pageurl += "?istachy=1";
      else
        pageurl += "&istachy=1";
    }

    window.open(pageurl,"_top");
    return;
  }

  if(result=="abused")
  {
    login_message_handler("ÀÌ PC´Â Æú¸®½º¿¡ ÀÇÇØ ÀÏ½Ã Á¢¼ÓÀÌ Á¦ÇÑµË´Ï´Ù.<br>Àá½Ã ÈÄ(ÃÖÀå 30ºÐ) ´Ù½Ã Á¢¼ÓÀ» ½ÃµµÇØ ÁÖ½Ã±â ¹Ù¶ø´Ï´Ù.","05",1);
    return;
  }

  if(result=="excessconnect")
  {
    if( typeof(document.fo_login.sso_type) == "object" && ( document.fo_login.sso_type.value == 4 || document.fo_login.sso_type.value == 5 ) )
    {
      login_message_handler("Á¦ÈÞ¼­ºñ½º´Â µ¿ÀÏ PC¿¡¼­ ´Ù¼öÀÇ ¾ÆÀÌµð·Î ÀÌ¿ëÇÏ½Ç ¼ö ¾ø½À´Ï´Ù.","05",1, result);
      document.location = 'http://www.'+document.domain;
    }
    else
    {
      login_message_handler("µ¿ÀÏÇÑ PC¿¡¼­ ´Ù¼öÀÇ ¾ÆÀÌµð·Î µ¿½Ã¿¡ ·Î±×ÀÎ ÇÏ½Ç ¼ö ¾ø½À´Ï´Ù.","05",1, result);
    }
    return;
  }

  if(result!="connected")
  {
    if (result != "notreg")
    {
      login_message_handler("¾Ë¼ö ¾ø´Â ¿À·ù°¡ ¹ß»ýÇß½À´Ï´Ù.","05",1);
      if ( document.fo_login.from.value == "DAUM" )
        doGo();
      return;
    }
  }

  var login_host = "member.pmang.com";
  switch ( document.fo_login.from.value )
  {
    case "pmang" : login_host = "www.pmang.com"; break;
    case "NOW" : login_host = "slugger.afreeca.com"; break;
    case "NC" : login_host = "slugger.plaync.co.kr"; break;
    case "CY" : login_host = "debut.game.cyworld.com"; break;
  }
  var login_url = "http://"+login_host+"/global/login.nwz";

  var bSSO = false;
  var bSecureConnect = false;

  if(typeof(document.fo_login.conntype)!="undefined")
  {
    if (typeof(document.fo_login.conntype.length)!="undefined")
    {
      if (document.fo_login.conntype.length >= 3 && document.fo_login.conntype[2].checked && document.fo_login.conntype[2].value == 3)
        bSSO = true;

      if( (document.fo_login.conntype[1].checked && document.fo_login.conntype[1].value == 2) || bSSO )
        bSecureConnect = true;
    }
    else
    {
      if (document.fo_login.conntype.value == 3)
        bSSO = true;

      if( (document.fo_login.conntype.checked && document.fo_login.conntype.value == 2) || bSSO )
        bSecureConnect = true;
    }
  }

  if (bSecureConnect)
    login_url="https://secure.pmang.com/global/login.nwz";

  if(typeof(loginfromjukeon)=="undefined" || !loginfromjukeon)
    progressid = setTimeout("showloginprogress()", 3500);

  var temp = typeof(document.fo_login.admin_index);
  if (temp == "undefined" || temp == "unknown")
    admin_index = 0;
  else
    admin_index = document.fo_login.admin_index.value;

  postdata
      ="usrid="+SayControl.codec(0,document.fo_login.usrid.value,"")
      +"&passwd="+SayControl.codec(0,document.fo_login.passwd.value,"")
      +"&pageurl="+SayControl.codec(0,document.fo_login.pageurl.value,"")
      +"&from="+SayControl.codec(0,document.fo_login.from.value,"")
      +"&key="+key
      +"&myip="+ip
      +"&myport="+port
      +"&admin_index="+admin_index
      +"&clientip="+cip
      +"&macaddress="+macaddress
      +"&gwmacaddress="+gwmacaddress
      +"&uuid="+uuid
      +"&mac2="+mac2
      +"&page_domain="+document.domain
      +"&is_current="+is_current
      +"&page_gameid="+document.location.hostname.split( "." )[0]
      ;

  if(typeof(document.fo_login.inpath) == "undefined") postdata += "&inpath=";
  else postdata += "&inpath=" + document.fo_login.inpath.value;

  if (typeof(document.fo_login.iscapslock) == "object")
      postdata += "&iscapslock="+SayControl.codec(0,document.fo_login.iscapslock.value,"")
      ;

  if (bSSO)
  {
    postdata += "&sso="+(bSSO ? '1' : '0');
    if( typeof(document.fo_login.from_user_id) == "object")
    postdata += "&from_user_id="+SayControl.codec(0,document.fo_login.from_user_id.value,"");
    if( typeof(document.fo_login.from_site) == "object")
    postdata += "&from_site="+SayControl.codec(0,document.fo_login.from_site.value,"");
    if( typeof(document.fo_login.from_msrl) == "object")
    postdata += "&from_msrl="+SayControl.codec(0,document.fo_login.from_msrl.value,"");
    if( typeof(document.fo_login.channel_id) == "object")
    postdata += "&from_site="+SayControl.codec(0,document.fo_login.channel_id.value,"");
    if( typeof(document.fo_login.channel_srl) == "object")
    postdata += "&from_msrl="+SayControl.codec(0,document.fo_login.channel_srl.value,"");
    if( typeof(document.fo_login.chkey) == "object")
    postdata += "&chkey="+SayControl.codec(0,document.fo_login.chkey.value,"");
    if( typeof(document.fo_login.sso_type) == "object")
    postdata += "&sso_type="+SayControl.codec(0,document.fo_login.sso_type.value,"");
    if( typeof(document.fo_login.fingerprint) == "object")
    postdata += "&fingerprint="+SayControl.codec(0,document.fo_login.fingerprint.value,"");
    if( typeof(document.fo_login.timestamp) == "object")
    postdata += "&timestamp="+SayControl.codec(0,document.fo_login.timestamp.value,"");
  }

  if( eval("document.fo_login.popupurl") != null ) postdata += "&popupurl="+document.fo_login.popupurl.value;
  if( eval("document.fo_login.popupoption") != null ) postdata += "&popupoption="+document.fo_login.popupoption.value;
  if(typeof(loginfromjukeon)!="undefined"&&loginfromjukeon)
  {
    if(typeof(JukeonAX)!="object" || JukeonAX.readyState!=4)
    {
      login_message_handler("ÁêÅ©¿Â ÄÁÆ®·Ñ¿¡¼­ ¿À·ù°¡ ¹ß»ýÇß½À´Ï´Ù.", 05, 1);
      return;
    }

    var jukecookieinfo=JukeonAX.getaxvalue("JukCInfo_"+document.fo_login.usrid.value);
    postdata += "&JukeonConnectionInfo="+jukecookieinfo;
  }
  SayControl.executeHTTP( login_url, postdata, "login_handler2", "" );
}

function saycontrol_error()
{
  is_current = 0;
  var from;
  if(document.fo_login)
    from = document.fo_login.from.value;
  else
    from = "";

  if(document.fo_login)
    target_url = document.fo_login.pageurl.value;
  else
    target_url = "";

  if( document.location.hostname.indexOf( "member.pmang.com" ) >= 0 && from != "pmang" )
  {
    document.location.href = "http://www.pmang.com/install_control.nwz?from="+from+"&target_url="+encodeURI(target_url);
  }
  else if( document.location.hostname.indexOf( "www.pmang.com" ) >= 0 || from == "pmang" )
  {
    var origin_url = target_url;
    document.location.href = "http://www.pmang.com/install_control.nwz?origin="+origin_url+"&from="+from;
  }
  else
  {
    if( 
      typeof(document.fo_login.sso_type) == "object" && 
      ( document.fo_login.sso_type.value == 4 || document.fo_login.sso_type.value == 5 ) 
    ) {
      var origin_url = escape('http://'+document.location.hostname);
      document.location.href = "http://"+document.location.hostname+"/gamepub/pm/install_control.nwz?origin="+origin_url+"&from="+from;
    } else {
      var origin_url = escape('http://'+document.location.hostname);
      document.location.href = "http://www.pmang.com/install_control.nwz?origin="+origin_url+"&from="+from;
    }
  }
  return;
}

function doGo()
{
  switch( document.fo_login.from.value )
  {
    case "SAYCLUB" :
    case "SAYCLUBCH" : document.location.href = "http://game.sayclub.com"; break;
    case "DAUM" : document.location.href = "http://game.daum.net"; break;
  }
  if( typeof(document.fo_login.sso_type) == "object" && ( document.fo_login.sso_type.value == 4 || document.fo_login.sso_type.value == 5 ) )
    document.location = "http://www." + document.domain;
}

function ssl_window()
{
  var wndNew;
  wndNew = window.open ('http://member.pmang.com/ssl_message.nwz', 'ssl_window', 'width=560,height=450,toolbar=no,location=no');
  if (wndNew)
    wndNew.focus();
}

function showloginform()
{
  if(typeof(document.fo_login) != 'undefined' && (typeof(loginfromjukeon) == "undefined" || !loginfromjukeon))
  {
    if( typeof(document.fo_login.usrid) != 'undefined' && document.fo_login.usrid.type != 'hidden')
    {
      if (typeof(GlobalPage) != "undefined") GlobalPage.style.display="";
      if (typeof(LoginPage) != "undefined") LoginPage.style.display="none";
      if (typeof(idTbl_MainBanner) != "undefined") idTbl_MainBanner.style.display = "";

      document.fo_login.passwd.disabled=false;
      document.fo_login.usrid.disabled=false;
      document.fo_login.passwd.value="";
      document.fo_login.usrid.select();
      if (typeof(loginLayer) == "object" && typeof(loginLayer.handleFocus) == "function") loginLayer.handleFocus();
      else document.fo_login.usrid.focus();
    }
    else if(window.open_login_page) // °³º°È¨ÇÇ¿¡¼­ ·Î±×ÀÎ ÇÏ´Â ºÎºÐÀ» À§ÇØ
    {
      open_login_page();
    }
  }
  else
  {
    if(loginfromjukeon)
      document.location.href = document.fo_login.pageurl.value;
    else
      document.location.href = pageurl;
  }
}

function showloginprepare()
{
  /*
  document.fo_login.passwd.disabled=true;
  document.fo_login.usrid.disabled=true;
  return;
  */
  if (typeof(LoginPage) != "undefined")
  {
    LoginPage.innerHTML=
    '<table border=0 cellpadding=0 cellspacing=0 width=100% height=400 class="bd">'+
    '<tr height=40%><td></td></tr>'+

      '<tr height=17>'+
        '<td align=center colspan=2>'+
          '<font class="ttl">Ready...</font><br>'+
        '</td>'+
      '</tr>'+
      '<tr height=15>'+
        '<td>'+
        '</td>'+
      '</tr>'+
      '<tr height=40><td></td></tr>'+
      '<tr height=80>'+
        '<td></td>'+
      '</tr>'+

    '<tr height=60%><td></td></tr>'+
    '</table>';
  }

  if (typeof(GlobalPage) != "undefined")
    GlobalPage.style.display="none";
  if (typeof(LoginPage) != "undefined")
    LoginPage.style.display="";
  if (typeof(idTbl_MainBanner) != "undefined")
    idTbl_MainBanner.style.display = "none";
}

function showloginprogress()
{
  //return;
  if (typeof(LoginPage) != "undefined")
  {
    LoginPage.innerHTML=
    '<table border=0 cellpadding=0 cellspacing=0 width=100% height=400 class="bd">'+
    '<tr height=40%><td></td></tr>'+

      '<tr height=17>'+
        '<td align=center colspan=2>'+
          '<font class="ttl">Connecting...</font><br>'+
        '</td>'+
      '</tr>'+
      '<tr height=15>'+
        '<td align=center colspan=2>'+
          '<img src="' + getSSLResourceURL('http://file.pmang.kr/images/img_progress.gif') + '" width=220 height=12>'+
        '</td>'+
      '</tr>'+
      '<tr height=40><td></td></tr>'+
      '<tr height=80>'+
        '<td align=right width=44%><img src="' + getSSLResourceURL('http://file.pmang.kr/images/lock.gif') +'"></td>'+
        '<td align=left valign=center width=66%><font color=gray><b>¿À´Ãµµ ºñ¹Ð¹øÈ£ Á¶½É~, ¶Ç Á¶½É!</b></font></td>'+
      '</tr>'+

    '<tr height=60%><td></td></tr>'+
    '</table>';
  }

  if (typeof(GlobalPage) != "undefined")
    GlobalPage.style.display="none";
  if (typeof(LoginPage) != "undefined")
    LoginPage.style.display="";
  if (typeof(idTbl_MainBanner) != "undefined")
    idTbl_MainBanner.style.display = "none";
}
