var loginstr="", mainLogin = false, msDuelgoLogin=false, commonSecureLogin = false, publishingLogin = false ;

function loginForm(nxtURL)
{
	// 
	var svctype     = "ijji";
		//  
	this.imgProperty = new bgImgProperty(sImageURL + "/v3/login/www.png");
	this.idProperty  = new idTextProperty("0", "0", "140", "22", sImageURL + "/v3/login/mainpage_txt01.png", "9", "7d7b73", "ffffff", "ffffff", "c3c3c3", "c3c3c3");
	this.pwdProperty = new pwdTextProperty("0", "27", "140", "22", sImageURL + "/v3/login/mainpage_txt02.png", "9", "7d7b73", "ffffff", "ffffff", "c3c3c3", "c3c3c3"); 

	loginstr += "	<div id='loginerr1' style='display:none;font-size:11px;font-color:#000000'><br>Can't use secure login.<br><br><a href='javascript:setMsgViewNetwork();'>[Normal Login]</a></div>";
	loginstr += "	<div id='loginerr2' style='display:none;font-size:11px;font-color:#000000'><br>Can't use secure login.<br><br><a href='javascript:setMsgViewFlash();'>[Normal Login]</a></div>";
	loginstr += "	<div id='loginerr3' style='display:none;font-size:11px;font-color:#000000'><br>Can't use secure login.<br><br><a href='javascript:setMsgViewBrowser();'>[Normal Login]</a></div>";
	loginstr += "   <div id='CapslockInfo' style='display:none; position:absolute; overflow:hidden; left:-62px; top:55px; width:176px; height:30px; z-index:500; '></div>";	
	
	loginstr += "<form name='loginform' id='loginform' method='post' action='' style='margin: 0pt; padding: 0pt; height: 50pt;' AUTOCOMPLETE='off' onsubmit='entersubmit_security()'>";
	loginstr += "	<input type='hidden' name='turtle'     id='turtle'     value=''>";
	loginstr += "	<input type='hidden' name='keyname'    id='keyname'    value=''>";
	loginstr += "	<input type='hidden' name='earthworm'  id='pwdObj'     value='earthworm'> ";
	loginstr += "	<input type='hidden' name='seculogin'  id='seculogin'  value='true'>";
	loginstr += "	<input type='hidden' name='nextURL'     value='"+nxtURL+"'>";
	loginstr += "	<input type='hidden' name='nxtURL'     value=''>";
	loginstr += "	<input type='hidden' name='hanilogin'  value=''>";
	loginstr += "	<input type='hidden' name='force'      value=''>";
	loginstr += "	<input type='hidden' name='secukey'    value=''>";
	loginstr += "	<input type='hidden' name='secutype'   value='2'>";			
	loginstr += "	<input type='hidden' name='ssl'   id='ssl' value=''>	";
	loginstr += "	<input type='hidden' name='m'   value='login'>	";
	loginstr += "	<!-- text box and submit button -->";
	loginstr += "		<div class='input_area' id='flashTr' style='position: relative; top:  0px; left: 0px; display:none; margin:0; padding:0;'>";
	if (AC_FL_RunContent == 0) {
		flashenable = -1;
	} else if (DetectFlashVer(9,0,48)) {
		loginstr += "		<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"142\" height=\"52\" id=\"login\" align=\"middle\">\n";
		loginstr += "		<param name=\"allowScriptAccess\" value=\"always\" />\n";
		loginstr += "		<param name=\"quality\" VALUE=\"high\" />\n";
		loginstr += "		<param name=\"wmode\" value=\"transparent\"/>\n";
		loginstr += "		<param name=\"bgcolor\" VALUE=\"#E9E8E5\" />\n";
		loginstr += "		<param name=\"movie\" value=\"/common/scripts/securelogin/commonLoginF.swf\" />\n";
		loginstr += "		<embed swLiveConnect=\"true\" quality=\"high\" bgcolor=\"#E9E8E5\" src=\"/common/scripts/securelogin/commonLoginF.swf\" width=\"142\" height=\"52\" name=\"login\" align=\"middle\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" wmode=\"transparent\"/>\n";
		loginstr += "		</object>\n";
	} else {
		flashenable = -1;
	}		
	loginstr += "		</div>";
	loginstr += "		<div class='input_area' id='generalTr' style='position: relative; top: 0px; left: 0px; display: block;'>";
	loginstr += "			<!-- userrid -->";
	loginstr += "				<input class='id' type='text' name='turtle2' value='' size='20' id='idObj' title='UserID' tabindex='1' onkeydown='entersubmit_key_Han(0,event)' maxlength='20' onFocus=\"this.style.backgroundImage=''\"' style='width:134px; height:18px; border:1px solid #bcbcbc; margin-bottom:3px; color:#7d7b73; url(" + sImageURL + "/v3/login/luniapage_txt01.png) no-repeat 5px 1px' />";
	loginstr += "			<!-- //userrid -->";
	loginstr += "			<!-- password -->";
	loginstr += "				<input class='id' type='password' name='earthworm2' value='' tabindex='2' id='pwdObj2' title='Password' onKeyPress='checkCapsLockPre(loginform);setKeyInfo();' onkeydown='entersubmit_key_security(1,event)' size='20' maxlength='20' title='Password' onFocus=\"this.style.backgroundImage='';\" style='width:134px; height:18px; border:1px solid #bcbcbc; color:#7d7b73; url(" + sImageURL + "/v3/login/luniapage_txt02.png) no-repeat 5px 1px' />";
	loginstr += "			<!-- //password -->";
	loginstr += "		</div>";
	loginstr += "		<a href='#' onClick='clickcr(this,\"sin_nlg.signin\",\"\",\"\",event);" + ( (flashenable==1) ? "loginClick()" : "entersubmit_security()" ) + "'><img id='submitTD' style='position: relative; top:-52px; left: 144px; display:inline' src='" + sImageURL + "/v3/login/btn_signin.gif'  width='68' height='51' alt='SIGN IN' /></a>";
	
	loginstr += "<!-- //text box and submit button -->";
	loginstr += "";
	loginstr += "<!-- checkbox area -->";
	loginstr += "<div style='clear:both; width:173px; margin-bottom:6px; padding:8px 0px 0px 0px; overflow:hidden; position: relative; top: -54px; left:0px;'><input type='checkbox' id='secure' style='float:left; width:14px; height:14px; margin:0 3px 0 0px;' name='secure' value='true' onClick='clickcr(this,\"sin_nlg.secure\",\"\",\"\",event);' onchange='javascript:changeSecure(document.form);'/><label for='secure' style='float:left; color:#666; vertical-align:top;'>Secure Sign In <a href='javascript:void(openSimpleFrameWindow(\""+sIdURL+"/info.nhn\", \"info\", \"500\", \"450\"));'>(?)</a></label></div>";
	loginstr += "</form>";
	document.write(loginstr);
	
	setLoginForm(svctype);
	
	setSessionKey();
	if (!keySplit()) {
		flashenable = -1;
		initEnc = SECUTYPE_GEN;
	}
	
 	logininit();
 	
}

var ipSecurity ; //

function setSSLWWWcheck(form) {
    if(mainLogin){
		var ipSecurityFlag = false;
        if(checkSSLCookie()) {
        	form.ssl.value = 'Y';
            ipSecurityFlag = true;
        }
        
	}else{
		if(checkSSLCookie()) {
			form.ssl.value = 'Y';
			try{
				document.getElementById('secure').checked = true; //
			}catch(e){}
		}
	}
}

function checkSSLCookie() {
	return getCookie('NHN_P_CAT') == "true" ? true : false;	
}

function entersubmit_key_Han(n, e) {
	//secureKeyConfirm();
	if (e.keyCode==13) {
		if (n==0) {	document.loginform.earthworm2.focus();
		} else { entersubmitHan();	}
	}
}

function entersubmitHan() {
	var f = document.getElementById("loginform");
	var sslgbn = getSSLLoginCheck();	
	f.force.value = sslgbn ? "Y" : "N";
	f.hanilogin.value = "N";
	f.action = getLoginServer(sslgbn, getLoginRefererString(f));
	f.nxtURL.value = (f.nxtURL.value=="")? top.document.URL : f.nxtURL.value;
	if (f.turtle.value.length==0) {
		alert('Please enter your User ID.');
	} else if(f.earthworm.value.length==0) {
		alert('Please enter your password.' );
	} else {
		f.submit();
	}
}

function getSSLLoginCheck() {
	if(mainLogin){
		return getIPLoginCheck();
	}else if(commonSecureLogin){
		if (document.getElementById('secure').checked) return true;
		else return false;
	}else{
		if (document.getElementById('ssl').checked) {
			return true;
		} else {
			return false;
		}
	}
}

function loginSubmit() {
	if (document.getElementById('submitTD') != null) {
			document.getElementById('submitTD').style.display  = "none";
			document.getElementById('normalTD').style.display = "inline";
	}
}

function getLoginRefererString(form) {
	if (isNeoLoginService(form)) return 'neo';
	if (isReactor(form)) return 'reactor';
	if (mainLogin) return 'main';
	if (publishingLogin) return 'publishing';
	return 'other';
}

function getIPLoginCheck() {
       if(existIPCheck()){
       	var ipSecurityChecked ;
       	try{ 
       		ipSecurityChecked = ipSecurity.getChecked();
       	}catch(e){
       		ipSecurityChecked = parent.ipSecurity.getChecked();
       	}
       	return ipSecurityChecked;
       }
       return document.getElementById('ssl').checked;
}

function getLoginSecureLevel(referer){
	var loginSecureLevel = -1;
	try{
		if(referer=='main'){
			loginSecureLevel = getSecureLevel();
		}else if(referer=='other'){
			if(commonSecureLogin)
				loginSecureLevel = initEnc;
		}
	}catch(e){}
	
	return loginSecureLevel;
}

function getLoginServer(ssl, referer) {
	var brwVersion = getBrowser5UnderVersion();
	var serverDomain = slformURL;
	var loginSecureLevel = getLoginSecureLevel(referer);
	
	if (referer == 'neo') {
			serverDomain = sIdURL;
	} else if (referer == 'publishing') {                // publishing
		if(ssl && !brwVersion && loginSecureLevel<=0)
			serverDomain = sPubIdsSSLURL;
		else
			serverDomain = sPubIdURL;
	}else {	                                      // main, other
		if(ssl && !brwVersion && loginSecureLevel<=0)
			serverDomain = sIdsSSLURL;
		else
			serverDomain = sIdURL;
	} 
	
	if (referer == 'reactor')
		return serverDomain + '/reactorLogin.nhn';
	else 
		return serverDomain + '/login.nhn';
}

function getBrowser5UnderVersion() {
	var win = navigator.appVersion.charAt(0);
	if(win >= 2 && win <= 3) 	return true;
	else if(win == 4 && navigator.appVersion.indexOf("MSIE 5.0") != -1) return true;
	else	return false;
}

function checkCapsLock(pwdlength) {
 	if(!mainLogin) return ;
 	
	var seculogin = (typeof(pwdlength)=="number") ? true : false;
	var temp_pwd ;
	try{
		temp_pwd = document.getElementById("loginform").earthworm.value.length
	}catch(e){
		temp_pwd = parent.document.getElementById("loginform").earthworm2.value.length
	}
	var pwd = (seculogin) ? pwdlength : temp_pwd;
	if (pwd > 1) return;

	// 
	var e = event;
	var myKeyCode=0;
	var myShiftKey=false;

	if ( document.all ) {					// Internet Explorer 4+
		myKeyCode=e.keyCode; myShiftKey=e.shiftKey;	
	} else if ( document.layers ) {			// Netscape 4
		myKeyCode=e.which;	myShiftKey=( myKeyCode == 16 ) ? true : false;
	} else if ( document.getElementById ) {	// Netscape 6
		myKeyCode=e.which; myShiftKey=( myKeyCode == 16 ) ? true : false;
	}
	capslockNotice();
	// Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
	if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) { 
		setCapslockInfoOn(seculogin);
	// Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
	} else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
		setCapslockInfoOn(seculogin);
	} else if (typeof(document.CKKeyPro)!=null && typeof(document.CKKeyPro)!='undefined' && document.CKKeyPro.GetKeyState('caps') == 'ON')  {
		setCapslockInfoOn(seculogin);
	}

}

function setCapslockInfoOn(seculogin) {
//	position(seculogin);
	try{
		document.getElementById('CapslockInfo').style.display  = "inline";
	}catch(e){
		top.document.getElementById('CapslockInfo').style.display  = "inline";
	}
	setTimeout("setCapslockInfoOff()", 2000);
}
function setCapslockInfoOff() {	
	try{
		document.getElementById('CapslockInfo').style.display  = "none";
	}catch(e){
		top.document.getElementById('CapslockInfo').style.display  = "none";
	}
}

function capslockNotice(){
	var noticestr = ""; 	
  try{
		document.all.CapslockInfo.innerHTML = noticestr;
	}catch(e){
		top.document.getElementById("CapslockInfo").innerHTML =noticestr;
	}

}

function isNeoLoginService(form){
	if(typeof(form.serviceid)=='object'){
		if(form.serviceid.value == 'CH_TR') 
			return true;
	}
	return false;
}

function isReactor(form){
	if(typeof(form.serviceid)=='object'){
		if(form.serviceid.value == 'reactor') 
			return true;
	}
	return false;
}
