// Your site address here!

var HomeUrl = "http://library.yonsei.ac.kr/";
var PdsUrl = "http://yulprm2.yonsei.ac.kr/pds";
var mainPage = "http://library.yonsei.ac.kr/main/main.do"

function pageViewLog(menuCode,subCode,libNo){
	
	//메인페이지는 메뉴코드가 없기때문에 URL 비교후 강제부여
	var requestUrl = document.location.href;
	if(requestUrl.indexOf(mainPage) != -1 ){
		menuCode = '00';
	}
	
	url = HomeUrl+"/stat/pageLog.do?menuCode="+menuCode+"&subCode="+subCode+"&libNo="+libNo;
	
	//메뉴코드가 있을때만 로그를 남긴다.
	if(menuCode != ''){
		new Ajax.Request(url,{
								method: 'get',
								parameters : '',
								contentType: 'application/x-www-form-urlencoded',
								encoding:'UTF-8',
								onSuccess: function(xmlHttp){										
									//alert("fContactLog success");
								},
								onComplete: function(xmlHttp){
									//alert(xmlHttp.responseText);
								},
								onFailure:function(xmlHttp){
									//alert("fContactLog failure");
								},
								onException:function(){
									//alert("fContactLog Exception");
								}
							  }
						  );
	}
	
}

/* First Contact Log */
function fContactLog(libNo){
		
	url = HomeUrl+"/stat/fContactLog.do";
	var referUrl = document.referrer;
	
	//referUrl을 비교해서 홈URL이 아닐때만 로그를 남긴다.
	if(referUrl.indexOf(HomeUrl) == -1 && referUrl.indexOf(PdsUrl) == -1){
		new Ajax.Request(url,{
								method: 'get',
								parameters : firstContact(libNo),
								contentType: 'application/x-www-form-urlencoded',
								encoding:'UTF-8',
								onSuccess: function(xmlHttp){										
									//alert("fContactLog success");
								},
								onComplete: function(xmlHttp){
									//alert(xmlHttp.responseText);
								},
								onFailure:function(xmlHttp){
									//alert("fContactLog failure");
								},
								onException:function(){
									//alert("fContactLog Exception");
								}
							  }
						  );
	}
}

/* Banner Log */
function bannerLog(bannerName,bannerUrl,divName,libNo){
		
	url = HomeUrl+"/stat/bannerLog.do?bannerName="+encodeURIComponent(bannerName)+"&bannerUrl="+bannerUrl+"&libNo="+libNo;
	
	if(divName != ''){
		obj_disable(divName);
	}
	
	new Ajax.Request(url,{
							method: 'get',
							parameters : '',
							contentType: 'application/x-www-form-urlencoded',
							encoding:'UTF-8',
							onSuccess: function(xmlHttp){										
								//window.open(bannerUrl);
							},
							onComplete: function(xmlHttp){
								//alert(xmlHttp.responseText);
							},
							onFailure:function(xmlHttp){
								//alert("Banner Log failure");
							},
							onException:function(){
								//alert("Banner Log Exception");
							}
						  }
					  );
}

function obj_disable(divId){
	obj = document.getElementById(divId);
	obj.disabled = true;
	setTimeout("obj_enable('"+divId+"')",2000);
}

function obj_enable(divId){
	obj = document.getElementById(divId);
	obj.disabled = false;
}



// ****************************************************************************************************
//
// Create Date : 2006-09-07
// Modify Date :   
//
//  1. Description : 클라이언트의 정보를 얻는다.
//  2. Parameters : 없음
//  3. Return Value : 없음 
//
// ****************************************************************************************************

function firstContact(libNo)
{
   var ustr = navigator.userAgent;
   var info = [];
   var is_safari = ustr.indexOf("Safari") != -1;
   var is_op = ustr.indexOf('Opera') != -1;
   var is_ie = ustr.indexOf('MSIE') != -1;
   var is_ff = ustr.indexOf('Firefox') != -1;
   var is_cr = ustr.indexOf('Chrome') != -1;
   
   if (is_ie)
   {
       info['ua'] = 'IE';
       info['ver'] = new RegExp('MSIE ([0-9.]+)','gi').exec(ustr)[1];
   }
   else if (is_ff)
   {
       info['ua'] = 'Firefox';
       info['ver'] = new RegExp('Firefox\/([0-9.]+)','gi').exec(ustr)[1];    
   }
   else if (is_op)
   {
       info['ua'] = 'Opera';
       info['ver'] = new RegExp('Opera\/([0-9.]+)','gi').exec(ustr)[1];
   }
   else if (is_cr)
   {
       info['ua'] = 'Chrome';
       info['ver'] = new RegExp('Chrome\/([0-9.]+)','gi').exec(ustr)[1];
   }    
   else if (is_safari)
   {
       info['ua'] = 'Safari';
       info['ver'] = new RegExp('Safari\/([0-9.]+)','gi').exec(ustr)[1];    
   }
   else
   {
       info['ua'] = 'Unknown';
       info['ver'] = '';
   }
   
   var refer            = findAllTheUrlAddresses(document.referrer);
   var url                = document.location.href.replace(new RegExp(HomeUrl,'gi'),'');
   //info['lang']         = navigator.userLanguage ? navigator.userLanguage : navigator.language ? navigator.language : '';
   info['resolution']     = screen.width && screen.height ? screen.width + 'x'+ screen.height : '';
   //info['color_depth'] = screen.colorDepth ? screen.colorDepth : '';
   info['platform']     = GetOsInfo() ? GetOsInfo() : navigator.platform;
   info['url']            = escape(url.replace(new RegExp('#.+?$','gi'),''));
   info['refer']        = escape(refer.indexOf(HomeUrl) == -1 ? refer : '');
   info['libNo']		= libNo;
   
   var statInfo = "browser="+info['ua']+info['ver']+"&resolution="+info['resolution']+"&platform="+info['platform']+"&refer="+info['refer']+"&libNo="+info['libNo'];
   
   return statInfo;
}

// ****************************************************************************************************
//
// Create Date : 2006-09-07
// Modify Date :   
//
//  1. Description : 클라이언트의 OS 종류를 얻는다.
//  2. Parameters : 없음
//  3. Return Value : 없음 
//
// ****************************************************************************************************

function GetOsInfo()
{
        var osinfo;

        var sUserAgent = navigator.userAgent;
        var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
        var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh");
        var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
        
        if ( isWin )
        {
                if ( sUserAgent.indexOf("Win95") > -1 || sUserAgent.indexOf("Windows 95") > -1 )
                        osinfo = "Windows 95";
                else if ( sUserAgent.indexOf("Win98") > -1 || sUserAgent.indexOf("Windows 98") > -1 )
                        osinfo = "Windows 98";
                else if ( sUserAgent.indexOf("Win 9x 4.90") > -1 || sUserAgent.indexOf("Windows ME") > -1 )
                        osinfo = "Windows ME";
                else if ( sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1 )
                        osinfo = "Windows 2000";
                else if ( sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1 )
                        osinfo = "Windows XP";
                else if ( sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1 )
                        osinfo = "Windows 2003";
                else if ( sUserAgent.indexOf("WinNT") > -1 || sUserAgent.indexOf("Windows NT") > -1 || sUserAgent.indexOf("WinNT4.0") > -1 || sUserAgent.indexOf("Windows NT 4.0") > -1 )
                        osinfo = "Windows NT4";

        }
  
        if ( isMac ) 
        {
                if ( sUserAgent.indexOf("Mac_68000") > -1 || sUserAgent.indexOf("68K") > -1 )
                        osinfo = "Mac_68000";
                else if ( sUserAgent.indexOf("Mac_PowerPC") > -1 || sUserAgent.indexOf("PPC") > -1 )
                        osinfo = "Mac_PowerPC";
        }
 
        if ( isUnix ) 
        {
                var isSunOS = sUserAgent.indexOf("SunOS") > -1;

                if ( isSunOS ) 
                {
                        var reSunOS = new RegExp("SunOS (\\d+\\.\\d+(?:\\.\\d+)?)");
                        reSunOS.test(sUserAgent);
         
                        if ( compareVersions(RegExp["$1"], "4.0") >= 0 )
                                osinfo = "SunOS 4.0";
                        if ( compareVersions(RegExp["$1"], "5.0") >= 0 )
                                osinfo = "SunOS 5.0";
                        if ( compareVersions(RegExp["$1"], "5.5") >= 0 )
                                osinfo = "SunOS 5.5";
                }
        }
        
        return osinfo;
}

// ****************************************************************************************************
//
// Create Date : 2009-09-20
// Modify Date :   
//
//  1. Description : 정규식을 이용해 URL 에서 도메인 추출
//  2. Parameters : StrObj
//  3. Return Value : StrObj 
//
// ****************************************************************************************************
/**/
function findAllTheUrlAddresses(StrObj) {
	var separateurlBy = ", ";
	var url = "<none url present>"; // if no match, use this
	var urlArray = StrObj.match(/([a-zA-Z]+tp\:\/\/[a-zA-Z0-9._-]+)/gi);
		if (urlArray) {
			url = "";
			for (var i = 0; i < urlArray.length; i++) {
				if (i != 0) url += separateurlBy;
					url += urlArray[i];
	     	 }
	   }
	return url;
}


function findAllTheWwwAddresses(StrObj) {
	var separatewwwsBy = ", ";
	var www = "<none www present>"; // if no match, use this
	var wwwsArray = StrObj.match(/(www[a-zA-Z0-9._-]+)/gi);
		if (wwwsArray) {
			www = "";
			for (var i = 0; i < wwwsArray.length; i++) {
				if (i != 0) www += separatewwwsBy;
					www += wwwsArray[i];
	      	}
	   }
	return www;
}
