/*
 * Single Sign On Sript file
 *
 * ÀÛ¼ºÀÚ : ÇÑºû¼ÒÇÁÆ® ±è¼ºÀç (zeru@hanbitsoft.co.kr)
 * 
 * »ç¿ë¹ý : ·Î±×¾Æ¿ô ÆäÀÌÁö¿Í, ÅäÅ« Ã¼Æ® ÆäÀÌÁö¸¦ ÀÛ¼ºÇÏ¼Å¼­
 *          logoutUrl°ú checkTokenUrl °æ·Î¸¸ ¹Ù²ãÁÖ½Ã¸é µË´Ï´Ù.
 */


var logoutUrl = "/HanbitOnModule/LogOut.asp"; // ·Î±×¾Æ¿ô ÆäÀÌÁö ÁöÁ¤
var checkTokenUrl = "/HanbitOnModule/CheckToken.asp"; // CheckToken °æ·Î ÁöÁ¤
var mainUrl = "/main.asp"; // ¸ÞÀÎ URL '/' °¡ ¾Æ´Ò °æ¿ì ¸ÞÀÎ °æ·Î ÁöÁ¤

var ls_codebase = "http://login.hanbiton.com/cab/NLSnSSO.cab#version=1,2,0,0";
var ls_clsid = "377FF862-62E0-4f33-B6E5-F58E0BC0F209";

var ls_vistar_codebase = "http://login.hanbiton.com/cab/NLSnSSO_V.cab#version=1,2,0,0";
var ls_vistar_clsid = "377FF862-62E0-4f33-B6E5-F58E0BC0F209";

//trust site µî·Ï activex
var ts_vistar_codebase = "http://login.hanbiton.com/cab/TrustSite.cab#version=1,0,0,1";
var ts_vistar_clsid = "07BBFE34-8B6B-4C96-B965-C7C2878118D0";


var isError = false;
var axErrorUrl = "http://www.hanbiton.com/home/ssoerror.html"; //Á¤±âÁ¡°Ë ÆäÀÌÁö
var redirectUrl = "http://www.hanbiton.com/home/error.html"; //Á¤±âÁ¡°Ë ÆäÀÌÁö

var isVistar = false;
var ls = null;
var ts = null;


function CheckOS() {

	if(navigator.userAgent.indexOf('IRIX') != -1)
	{ 
		var OpSys = "Irix"; 
		isVistar = false; 
	}
	else if(navigator.userAgent.indexOf('Win') != -1)
	{ 
		var Version;
		var temp = navigator.userAgent.split(";");
		
		for(i=0;i<temp.length;i++)
		{
			if(temp[i].indexOf('Win') != -1)
			{
				Version = temp[i];
			}
		}
		
		if(Version.indexOf('6.0') != -1)
		{
			var OpSys = "Vistar"; 			
			isVistar = true; 
		} else {
			var OpSys = "Windows Me or NT or 2000"; 
			isVistar = false;
		}
	}
	else if(navigator.userAgent.indexOf('Mac') != -1)
	{ 
		var OpSys = "Macintosh"; 
		isVistar = false; 
	}
	else 
	{ 
		var OpSys = "other"; 
		isVistar = false;
	}	
	
	return OpSys;
}


// Parent Ts Check And Ts Object Load
function LoadCheckParentTS()
{
	if (document.all["hanbitonts"] ) {
		ts = document.all["hanbitonts"];
	}

	if(ts == null)
	{
		if(parent != null && parent != this)
		{
			if(top.ts != null)
				ts = top.ts;
		}
	}	
}

// Ts Object Load
function LoadHanbitOnTS() {

	CheckOS();
	LoadCheckParentTS();
	
	if (ts != null) {			
		return;
	} else {
		if(isVistar) 
		{			
			var str='<OBJECT id="hanbitonts" codebase="' + ts_vistar_codebase + '" classid="CLSID:' + ts_vistar_clsid + '" style="display:none"></OBJECT>';
			document.write(str);	
			ts = document.all["hanbitonts"];				
		}	
	}		
}


function ActiveXError() {
	// alert("ÇÑºû¿Â¿¡¼­ Á¦°øÇÏ´Â ¼­ºñ½º¸¦ ¸ðµÎ ÀÌ¿ëÇÏ½Ã·Á¸é ActiveX¸¦ ¹Þ¾Æ¾ßÇÕ´Ï´Ù");
}


function InitHanbitOnLS() {

	try {
		if (ls != null) {
			var ret = ls.fnInitialize(0, 0);
			if (ret != 0) {
				window.location = axErrorUrl;
				return;
			}

		}
	} catch (e) {
		// ¼³Ä¡µÇÁö ¾ÊÀ½
	}

}


//½Å±Ô ·Î±×¾Æ¿ô ½ÃÀÛ(2008.09.24)
// logout
function LogOut() {
	if (confirm("¸ðµç ÇÑºû »çÀÌÆ®¿¡¼­ ·Î±×¾Æ¿ô µË´Ï´Ù.\n·Î±×¾Æ¿ô ÇÏ½Ã°Ú½À´Ï±î? ")) {
//		LogoutProcess(0);
		LogoutUrlGo();
	}
}

// logout process
function LogoutProcess(evt) {
	    RequestLogout(LogoutUrlGo);
}

function LogoutUrlGo()
{
    window.location = logoutUrl + "?ReturnUrl=" + window.location;
}
//½Å±Ô ·Î±×¾Æ¿ô ³¡(2008.09.24)


function timeOutMessage()
{
	var w = window.open("/HanbitOnModule/MsgPopup/timeout.html", "HanbitON_TimeOut", "width=425,height=290,status=no,scrollbars=no");
	
	if(w != null)
		w.focus();
}

// 
// HanbitON - Notice
//
var notice = new Array();
var notice_index = 0;
var notice_count = 0;

// notice
function Notice(msg) {
	notice[notice_count++] = msg;
	notice_index = notice_count - 1;

	ShowNotice(notice_index);
}

function ShowNotice(idx) {
  var layer = '<div style="position:absolute; left:125px; top:27px; width:290px; height:98px; background:white;">';
  layer += notice[idx] + '</div><div style="position:absolute; left:187px;top:145px;width:61px;height:22px;cursor:hand;" onclick="HideNotice()"></div>';

	var divs = document.getElementById("noticedivs");
	divs.innerHTML = layer;
	divs.style.top = (document.body.clientHeight - 178) / 2;
	divs.style.left = (document.body.clientWidth - 440) / 2;
	divs.style.display = '';
}

function HideNotice() {
	var divs = document.getElementById("noticeDivs");

	if (notice_index > 0) {
		notice_index--;
		ShowNotice(notice_index);
	} else {
		divs.style.display = 'none';
	}
}

// notice layer
document.write('<div id="noticedivs" style="position:absolute; left:0px; top:0px; width:440px; height:178px; z-index:1; background-image:url(../images/pop_layer.gif); display:none;"></div>');


// verify token when authorization
function VerifyToken(UserID) {
	//alert("³×¿À½ºÆÀ VerifyToken()" + "\nToken:" + getCookie("t") + "\nUserID:" + getCookie("UserID") + "\nHANBITON_SEARCH:" + getCookie("HANBITON_SEARCH"));

	try {
		if (ls != null) {
			if (ls.fnCheckToken()) {
			}
		} else  {
			// object ¾øÀ½
		}
	} catch (e) {
		// ¼³Ä¡µÇÁö ¾ÊÀ½
	}

}

// set token
//º¯°æ
function SetToken(UserID, token) {

	try {
		if (ls != null) {
			ls.fnSetToken(UserID, token);
		} else  {
			// object ¾øÀ½
		}
	} catch (e) {
		// ¼³Ä¡µÇÁö ¾ÊÀ½
	}

}


// set cookie
function setCookie( name, value )
{
	document.cookie = name + "=" + escape( value ) + "; path=/; domain=hanbiton.com"
}

// get cookie
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 "";	
}

// input cookie value

function setCookieValue()
{
	setCookie("sso","no");

	if(getMainUrl() == "")
		window.location.href = "/";
	else
		window.location.href = getMainUrl();
}

function setMainUrl(url)
{
	mainUrl = url;
}

function getMainUrl()
{
	return mainUrl;
}

function topNaviReLoad()
{	/*
	if(self.parent.frames.length != 0)
	{
		self.parent.topReLoad();
	}
	*/
}

function IsNaviXp2()
{
	var strMatch = window.navigator.appVersion;
	
	if( strMatch.match("SV1") == "SV1" && strMatch.match("Windows NT 5.1") == "Windows NT 5.1" )
		return true;
	else
		return false;
}

// ÂÊÁö º¸³»±â
function SendMemoMsg(msg) {
	//alert('BEFORE CALL fnSendMsg:' + msg);
	try {
		if (ls) {
			ls.fnSendMsg(msg);
			//alert('AFTER CALL fnSendMsg:' + msg);
		} else  {
			// object ¾øÀ½
			alert('SSO ActiveX°¡ »ý¼ºµÇÁö ¾Ê¾Ò½À´Ï´Ù');
		}
	} catch (e) {
		// ¼³Ä¡µÇÁö ¾ÊÀ½
	}
}

var ReceiveMessage;

// ÂÊÁö ¹Þ¾Æ¼­ º¸¿©ÁÖ±â
function RecieveMemoMsg(msg) 
{
	var LeftPosition = 0;//(screen.width) ? (screen.width-300)/2 : 0;
	var TopPosition = 0;//(screen.height) ? (screen.height-100)/2 : 0;
	
	var settings='width=335,height=320,top='+TopPosition+',left='+LeftPosition ;
	//+',location=no,toolbar=no,menubar=no';

	if (msg != null && msg != ''){
		var kind_of_msg = msg.split("|");

		if (1 <= parseInt(kind_of_msg[0]) <= 3 || 7 <= parseInt(kind_of_msg[0]) <= 14) {			
			var win = window.open("http://www.hanbiton.com/h_note/ms_bypass_note.aspx?page=r&op=on&userid=" + kind_of_msg[5] + "&paper=" + kind_of_msg[1] + "&type=" + kind_of_msg[0] + "&up=0" ,"",settings);
			win.focus();
		} 
		//¼±¹° ÂÊÁö °ü·Ã ºí·Ï -------------------------- ÄÚ³Ê½ºÅæ 2005.7.9
		else if (parseInt(kind_of_msg[0]) == 6) {
			var Recv_cd_user = kind_of_msg[5]; //¼ö½ÅÀÚ cd_user
			var cd_paper = kind_of_msg[1]; //cd_paper
			var cd_goods = kind_of_msg[11];     
			     
		   var settings_gift ='width=385,height=400,top='+TopPosition+',left='+LeftPosition ;
		   
		   var win = window.open("http://www.hanbiton.com/shop/bric/Paper_RecvGift.aspx?r_cd_user=" + Recv_cd_user  + "&cd_paper=" + cd_paper + "&cd_goods=" + cd_goods,"",settings_gift);
		   win.focus();
		}
		//¼±¹° ÂÊÁö °ü·Ã ºí·Ï --------------------------		
	}
}

function SendMsgRsp(msg)
{
	
	var kind_of_msg = msg.split("|");
	if (parseInt(kind_of_msg[0]) == 1 || parseInt(kind_of_msg[0]) == 2) {
		//var win = window.open("http://www.hanbiton.com/h_note/recvnote.aspx","HanbitOnRecvNote",'width=335,height=320');
	} 	
	
	//GetMsg(msg);	
	//alert('SendMsgRsp=' + msg);
}

// ÀÌ¿ôÃß°¡
function ho_AddNeighbor(id) {
	openWindow("http://www.hanbiton.com/popup/hanbiton/AddNeighbor.aspx?id=" + id, "HanbitOnAddNeighbor", "width=350,height=300");
}

// ÂÊÁö º¸³»±â
function ho_SendNote(id) {
	var LeftPosition = (screen.width) ? (screen.width-335)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-320)/2 : 0;
  openWindow("https://members.hanbiton.com:8080/paper/popup/Reply.aspx?bid=" + id, "HanbitOnNote", "width=335,height=320,status=no,scrollbars=no,top=" + TopPosition + ",left=" + LeftPosition);
}

function openWindow(loc, name, opt) {
	var win = window.open(loc, name, opt);
	if (win == null) {
		alert("ÇÑºû¿ÂÀ» ¿øÈ°ÇÏ°Ô ÀÌ¿ëÇÏ½Ã·Á¸é ¸ÕÀú ÆË¾÷Â÷´Ü¼³Á¤À» ²¨ÁÖ¼¼¿ä");
	}
	return win;
}

