var	szPort	= "" ;
var doing = 0 ;
var ndoing = 0 ;
var NameCheckWindow = null ;
var checkPassword = "";

function setCookie(name, value) {
    if(value == null)
        return;
    exp = new Date();
    exp.setMonth(exp.getMonth() + 1);
	value = escape(value);
    document.cookie = name + "=" + value + ";" + "expires=" + exp.toGMTString() + ";";
}
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if(endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if(document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf("", i) + 1;
            if(i == 0) break;
    }
    return "";
}
function CheckCookie(value) {
    value1 = getCookie("name");
    if(value1 != value)
        setCookie("name",value)
}

function callNameCheck ( )
{
	if ( NameCheckWindow != null && !NameCheckWindow.closed )
		NameCheckWindow.close() ;
	szPort	= location.port ? location.port : '80' ;
	time =  (new Date()).getTime();
	checkPassword	= document.LoginMenu.Password.value;
    document.LoginFrame.location='/jsp/org/login/LoginParse.jsp?x=' + time;	
//	NameCheckWindow = window.open('/jsp/org/login/LoginParse.jsp?x=' + time,'LoginParse','left=8000,top=0,width=1,height=1,resizable=yes,menubar=no,scrollbars=no');
//    NameCheckWindow.opener.name='LOGIN'+time;
}
function setLoginInfo ( )
{
	document.LoginMenu.Name.value = document.FORM1.Name.value;
	document.LoginMenu.Password.value = document.FORM2.Password.value;
    document.LoginMenu.AliasName.value = document.FORM4.AliasName.checked ;
	document.LoginMenu.INTRA.value = szBIntra ;

}
function reloginFree()
{
	ndoing = 0 ;
}

function NameCheck()
{
    document.FORM1.Name.value =document.FORM1.Name.value.replace(/^\s*|\s*$/g,"");
	if(document.FORM1.Name.value == "") {
		alert(szNoName);
		return;
	}
	if(document.FORM2.Password.value == "") {
		alert('¾ÏÈ£¸¦ ÀÔ·ÂÇÏ½Ê½Ã¿À.');
		return;
	}

    var checkPreviousPcLogin=true;
    if(checkPreviousPcLogin)
    {

        try{
            //HDNotifyBiz ÄÜÆ®·ÑÀÌ ÀÖ´ÂÁö °Ë»çÇÏ°í ÀÖÀ¸¸é °æ°íÃ¢À» Ç¥½ÃÇÏ°í ·Î±×ÀÎÀ» ÁßÁöÇÑ´Ù.
            var hwebui = document.all["hwebui"];
            var lWnd	= hwebui.FindWindow ( "GROUPWARE", "" ) ;
            if(lWnd>0)
            {
                alert("ÀÌ¹Ì ÄÄÇ»ÅÍ¿¡ ±×·ì¿þ¾î¸¦ »ç¿ëÇÏ°í ÀÖ´Â »ç¿ëÀÚ°¡ Á¸ÀçÇÕ´Ï´Ù.\nÀÌÀü »ç¿ëÀÚ¸¦ ·Î±×¾Æ¿ôÇÏ°í ´Ù½Ã ·Î±×ÀÎ ÇÏ½Ê½Ã¿À.");
                return;
            }
        }
        catch(e){}
    }

	if ( ndoing != 1 )
	{
		ndoing = 1 ;
		setLoginInfo () ;
		callNameCheck ();
		setTimeout("ndoing=0;", 2*1000);
	}else
        alert(szLogInProcessMessage);
}
function Init() {
	setDefaultSetting () ;
}
function setDefaultSetting ()
{
	loadCookie ( );
    document.FORM2.Password.value = "";
    if(document.FORM1.Name.value == "")
        document.FORM1.Name.focus();
    else
        document.FORM2.Password.focus();
}

function setLoginName(name)
{
    document.LoginMenu.Name.value=name;
    saveCookie ();
}

function saveCookie ()
{
    if(document.LoginMenu)
        setCookie("login_name", document.LoginMenu.Name.value);

    if(document.FORM4)
        setCookie("login_intra", (document.FORM4.AliasName.checked ? "1" :"0"));
}

function loadCookie ( )
{
    document.FORM1.Name.value = getCookie("login_name");
    document.FORM4.AliasName.checked = getCookie("login_intra")=="1";
}

// custom Script
function loginWithPKI( empCode , loginPasswd , errMessage )
{
	if( errMessage )
	{
		if( errMessage == "NODNCODE" )
		{
			resetVisable( $('pkiArea') ) ;
			setVisable( $('nameArea') ) ;
			setVisable( $('passwdArea') ) ;
			setVisable( $('submitArea') ) ;
			return ;
		}
		else if ( errMessage == "WRONGPKI")
		{
			alert("»ç¿ëÀÚÀÇ ÀÎÁõ¼­°¡ ¾Æ´Õ´Ï´Ù.");
			return ;
		}
		else 
		{
			alert( errMessage ) ;
			return ;
		}
	}
	alert("Err in PKI Login ");
	
}
function processNext( nextStep )
{
	if( nextStep =="PKI" )
	{
		sign_check($('PKILOGINFORM'));
	} 
	else if( nextStep =="PASSWD")
	{
		$('NeisId').disabled = true ;
		setVisable( $('LoginPasswd') ) ;
		resetVisable( $('firstLoginArea') );
		setVisable( $('secondLoginArea' ) );
		$('passwd').focus();
	}
}


function checkUserType( userId )
{
	if( !userId || userId.length < 1 ) { alert("ID¸¦ È®ÀÎÇÏ¿© ÁÖ½Ê½Ã¿ä"); return ; }
	if( !checkPreviousPcLogined() ) return false ;
	$('LoginFrame').src = '/welcome/pki/checkUser.jsp?userId=' + userId ;
	setCookie( 'GroupwareNeisId' , userId ) ;
}

function loginWithPasswd( userId , passwd ) 
{
	if( !checkPreviousPcLogined() ) return false ;
	if( !userId || userId.length < 1 ) { alert("ID¸¦ È®ÀÎÇÏ¿© ÁÖ½Ê½Ã¿ä"); return ; }
	if( !passwd || passwd.length < 1 ) { alert("Passwd¸¦ È®ÀÎÇÏ¿© ÁÖ½Ê½Ã¿ä"); return ; }
	$('LoginFrame').src = '/welcome/pki/neisLoginStub.jsp?userId=' + userId + "&passwd=" + encodeURIComponent(passwd);
	
}

function checkPreviousPcLogined()
{
	try
	{
	    //HDNotifyBiz ÄÜÆ®·ÑÀÌ ÀÖ´ÂÁö °Ë»çÇÏ°í ÀÖÀ¸¸é °æ°íÃ¢À» Ç¥½ÃÇÏ°í ·Î±×ÀÎÀ» ÁßÁöÇÑ´Ù.
	    var hwebui = document.all["hwebui"];
	    var lWnd	= hwebui.FindWindow ( "HDNotifyBiz", "" ) ;
	    if(lWnd>0)
	    {
	        alert("ÀÌ¹Ì ÄÄÇ»ÅÍ¿¡ ±×·ì¿þ¾î¸¦ »ç¿ëÇÏ°í ÀÖ´Â »ç¿ëÀÚ°¡ Á¸ÀçÇÕ´Ï´Ù.\nÀÌÀü »ç¿ëÀÚ¸¦ ·Î±×¾Æ¿ôÇÏ°í ´Ù½Ã ·Î±×ÀÎ ÇÏ½Ê½Ã¿À.");
	        return false;
	    }
	}
	catch(ex){alert( ex ) ; return true ; }
	return true ;
	
}

function isVisable( checkObject ){return checkObject.style.display != 'none'}
function setVisable( targetObject ){ targetObject.style.display='' ;}
function resetVisable( targetObject ) { targetObject.style.display='none';}
