/***************************************************************************
* Program Name       : Javascript program
* Copyright Notice   : COPYRIGHT (C) 2006 BY HSBC
* Creation Date      : 01/06/2006
* Programmer         : Alan Choi
* Abstract           : This file contains functions for SCM and Webtrend
* Amendment History:
* List of Global Variable:	(Please ensure the following variables cannot be used again)
*			 	
*
* Date       By            Description
* ---------  ------------- ------------------------------------------------
* 01/06/2006 Alan Choi	   Initial Version
***************************************************************************/

/***************************************************************************************************************
* Function		: function it_setCookie(name, value)
* Description		: This function set the given value to the cookie of the specified name
* Parameter Usage	: name		:name of the cookie
* 			  value		:value assigned to the cookie
*
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function it_setCookie(name, value)
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	var expires = 90; //90 days
	expires = expires * 1000 * 60 * 60 * 24;
	
	var domain = "hsbc.com.hk";
	
	var expires_date = new Date( today.getTime() + (expires) );

    document.cookie= name + "=" + escape(value) +
        ((expires_date) ? "; expires=" + expires_date.toGMTString() : "") +
        ((domain) ? ";domain=" + domain : "") + ";path=/";
}

/***************************************************************************************************************
* Function		: function it_getCookie(name)
* Description		: This function get the cookie object of the given name
* Parameter Usage	: name		:name of the cookie
*
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function it_getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/***************************************************************************************************************
* Function		: function it_SCMPageTag(pageName)
* Description		: This function insert a SCM page tag. The page name is inserted to the SCM cookie named "SCMVisit"
* Parameter Usage	: pageName	:pageName to be inserted to the SCM cookie
*
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function it_SCMPageTag(pageName)
{
	var cookieValue = it_getCookie("SCMVisit");
	var pageArray;
	var maxPageEntry = 5;

	if(pageName.length == 4)
	{
		if (cookieValue){
	        pageArray =cookieValue.split(","); 
	        if(pageArray){        
	        	pageArray = it_pushFIFO(pageArray, maxPageEntry, pageName);
				cookieValue = pageArray.toString();
	        	it_setCookie("SCMVisit", cookieValue);  	
	        }else{
	        	cookieValue = pageName + it_getTimeString();
	        	it_setCookie("SCMVisit", cookieValue);    	
	        }
	    }else{
	    	cookieValue = pageName + it_getTimeString();
			it_setCookie("SCMVisit", cookieValue);
		}
	}
}

/***************************************************************************************************************
* Function		: function it_pushFIFO(array, maxLength, entry)
* Description		: This function pushes a new string entry into a FIFO array
* Parameter Usage	: array		:array being inserted
*			  maxLength	:maxLength of the array
*			  entry		:the string to be inserted string value
*
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function it_pushFIFO(array, maxLength, entry){

	entry = entry + it_getTimeString();
	
	if(it_checkExistInArray(array, entry)){
		return array;
	}

	if(array.length == maxLength){
		for(var i=0; i<array.length-1; i++){
			array[i] = array[i+1];
		}
		
		array[array.length-1] = entry;
		
		return array;
		
    	}else if (array.length > maxLength){
    		var newArray = new Array(maxLength);

		for(var i=array.length-maxLength+1,j=0; i<array.length&&j<maxLength-1; i++,j++){
			newArray[j] = array[i];	
		}
		
    		newArray[newArray.length-1] = entry;
    	
    		return newArray;
    	}else {	    
    		var newArray = new Array(array.length+1);

		for(var i=0; i<array.length; i++){
			newArray[i] = array[i];	
		}
		
    		newArray[newArray.length-1] = entry;
    	
    		return newArray;
    	}
}

/***************************************************************************************************************
* Function		: function it_checkExistInArray(array, string)
* Description		: This function check if a page already exist in the page array
* Parameter Usage	: array		:array being checked
*			  string	:string being checked
*
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function it_checkExistInArray(array, string)
{
		for(var i=0; i<array.length; i++){
			if(array[i].substr(0,4)==string.substr(0,4)){
				array[i] = array[i].substr(0,4);
				array[i] = array[i] + it_getTimeString();
				return true;
			}
		}
		return false;
}

/***************************************************************************************************************
* Function		: function it_getTimeString()
* Description		: This function get the current time string in the format of yyyyMMddhhmmss
*
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function it_getTimeString()
{
	var time = new Date();
	var year = time.getFullYear();
	var month = time.getMonth()+1;
	if(month<10){
		month = "0" + month;
	}
	var day = time.getDate();
	if(day<10){
		day = "0" + day;
	}
	var hour = time.getHours();
	if(hour<10){
		hour = "0" + hour;
	}
	var min = time.getMinutes();
	if(min<10){
		min = "0" + min;
	}
	var sec = time.getSeconds();
	if(sec<10){
		sec = "0" + sec;
	}
	
	var timeString = year.toString() + month.toString() + day.toString() + hour.toString() + min.toString() + sec.toString();
	return timeString;
}

/***************************************************************************************************************
* Function		: function wt_view(SKU_PARAM)
* Description		: This function is for Webtrend View tag
* Parameter Usage	: SKU_PARAM	:The SKU_PARAM value
*
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function wt_view(SKU_PARAM)
{
	HSBC.PAGE.pn_sku = SKU_PARAM;
	HSBC.PAGE.tx_u = "1";
	HSBC.PAGE.tx_e = "v";
	HSBC.PAGE.si_n = "ProdConvAll";
	HSBC.PAGE.si_x = "1";
}

/***************************************************************************************************************
* Function		: function wt_start(SKU_PARAM)
* Description		: This function is for Webtrend Start tag
* Parameter Usage	: SKU_PARAM	:The SKU_PARAM value
*
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function wt_start(SKU_PARAM)
{	
	HSBC.PAGE.pn_sku = SKU_PARAM;
	HSBC.EXT.HSBC_u = "1";
	HSBC.EXT.HSBC_e = "apst";
	HSBC.PAGE.si_n = "ProdConvAll";
	HSBC.PAGE.si_x = "2";
}

/***************************************************************************************************************
* Function		: function wt_submit(SKU_PARAM)
* Description		: This function is for Webtrend Submit tag
* Parameter Usage	: SKU_PARAM	:The SKU_PARAM value
*
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function wt_submit(SKU_PARAM)
{
	HSBC.PAGE.pn_sku = SKU_PARAM;
	HSBC.EXT.HSBC_u = "1";
	HSBC.EXT.HSBC_e = "apsu";
	HSBC.PAGE.si_n = "ProdConvAll";
	HSBC.PAGE.si_x = "3";
}

/***************************************************************************************************************
* Function		: function wt_approved(SKU_PARAM, ORDER_NUMBER_PARAM, ORDER_TIME_PARAM, ORDER_DATE_PARAM)
* Description		: This function is for Webtrend Approved tag
* Parameter Usage	: SKU_PARAM		:The SKU_PARAM value
*			  ORDER_NUMBER_PARAM	:The ORDER_NUMBER_PARAM value
*			  ORDER_TIME_PARAM	:The ORDER_TIME_PARAM value
*			  ORDER_DATE_PARAM	:The ORDER_DATE_PARAM value
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function wt_approved(SKU_PARAM, ORDER_NUMBER_PARAM, ORDER_TIME_PARAM, ORDER_DATE_PARAM)
{
	HSBC.PAGE.pn_sku = SKU_PARAM;
	HSBC.PAGE.tx_u = "1";
	HSBC.PAGE.tx_e = "p";
	HSBC.PAGE.si_n = "ProdConvAll";
	HSBC.PAGE.si_x = "4";
	HSBC.PAGE.tx_s = SKU_PARAM;
	HSBC.PAGE.tx_i = ORDER_NUMBER_PARAM;	
	HSBC.PAGE.tx_it = ORDER_TIME_PARAM;
	HSBC.PAGE.tx_id = ORDER_DATE_PARAM;
}

/***************************************************************************************************************
* Function		: function wt_submit_approved(SKU_PARAM, ORDER_NUMBER_PARAM, ORDER_TIME_PARAM, ORDER_DATE_PARAM)
* Description		: This function is for Webtrend Submit + Approved tag
* Parameter Usage	: SKU_PARAM		:The SKU_PARAM value
*			  ORDER_NUMBER_PARAM	:The ORDER_NUMBER_PARAM value
*			  ORDER_TIME_PARAM	:The ORDER_TIME_PARAM value
*			  ORDER_DATE_PARAM	:The ORDER_DATE_PARAM value
* Author		: Alan Choi
* Creation Date		: 1 June 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function wt_submit_approved(SKU_PARAM, ORDER_NUMBER_PARAM, ORDER_TIME_PARAM, ORDER_DATE_PARAM)
{
	HSBC.PAGE.pn_sku = SKU_PARAM;
	HSBC.PAGE.tx_u = "1";
	HSBC.PAGE.tx_e = "p";
	HSBC.PAGE.tx_s = SKU_PARAM;
	HSBC.PAGE.tx_i = ORDER_NUMBER_PARAM;	
	HSBC.PAGE.tx_it = ORDER_TIME_PARAM;
	HSBC.PAGE.tx_id = ORDER_DATE_PARAM;
	HSBC.EXT.HSBC_u = "1";
	HSBC.EXT.HSBC_e = "apsu";
	HSBC.PAGE.si_n = "ProdConvAll";
	HSBC.PAGE.si_x = "3";
}


/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function it_getCookieForMI(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function it_HCOI_MI(_web_site_id,_content_id,_is_page,_location) {
  _current_url = escape(location.href);
  _web_site_id = escape(_web_site_id);
  _content_id = escape(_content_id);
  _is_page = escape(_content_id);
  _location = escape(_content_id);

  _protocol=location.protocol.indexOf('https')>-1?'https:':'http:';
  _cookieServerURL = '/images/clear.gif';
  _colorDepth = window.screen.colorDepth;
  _width = window.screen.width;
  _height = window.screen.height;
  _maxWidth = window.screen.availWidth;
  _maxHeight = window.screen.availHeight;
  _javaEnabled = (navigator.javaEnabled()==true)?"y":"n";
  _cookieEnabled = (navigator.cookieEnabled==true)?"y":"n";
  _userAgent=navigator.appName+" "+navigator.appVersion;
  _userAgentB=navigator.userAgent;
  _mac=(_userAgent.indexOf('Mac'));
  _IE=(_userAgent.indexOf('MSIE'));
  _opera=(_userAgentB.indexOf('Opera'));
  _IEV=(parseInt(_userAgent.substr(_IE+5)));
  _netscape=(_userAgent.indexOf('Netscape'));
  _connectionType='0';
  _homePage='0';
  if((_IE>=0)&&(_IEV>=5)&&(_mac==-1)&&(_opera==-1))  {
        if(document.body){
            document.body.addBehavior("#default#clientCaps");
            _connectionType=document.body.connectionType;
            document.body.addBehavior("#default#homePage");
            _homePage=(document.body.isHomePage(location.href))?"y":"n";
        }
  }
  _HSBC_COOKIEMI= it_getCookieForMI('HSBC_COOKIEMI');
  _sessionID = it_getCookieForMI('JSESSIONID'); // the name of the cookie needs to be verified for each site
  _d= new Date();
  _timeZone=_d.getTimezoneOffset()/-60;
  _language='0';
  if((_netscape!=-1)||(_opera!=-1)){
    _language=navigator.language;
  }
  if((_IE!=-1)&&(_opera==-1)){
    _language=navigator.userLanguage;
  }
  _temp='<div style="visibility:hidden"><img src="'+_cookieServerURL+'?'+_current_url+','+_web_site_id+','+_content_id+','+_is_page+','+_location+','+_colorDepth+','+_width+','+_height+','+_maxWidth+','+_maxHeight+','+_javaEnabled+','+_cookieEnabled+','+_connectionType+','+_homePage+','+_timeZone+','+_language+','+_HSBC_COOKIEMI+','+_sessionID+'" border=0 height=1 width=1 alt="*"></div>';
   document.write(_temp);
}

/***************************************************************************************************************
* Function		: it_digitalOrder(formname, cookievalue)
* Description		: To logon PIB with digital order
* Parameter Usage	: formname		:formname of the PIB logon page
*					  	  cookievalue	:cookievalue
* Location			: common.js
* Author			: Chan Tsz To
* Creation Date		: 05 Jul 2006
* Side effect		: N/A
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
* 12 Oct 2008	Alan Choi	Changed width and height to (screen.width, screen.height*0.9)
***************************************************************************************************************/
function it_digitalOrder(formname, cookievalue)
{
	it_popupNewBrowser('', 'yes', 'no', 'yes', 'no', 'no', 'yes', screen.width, screen.height*0.9, 20, 20);

	it_setCookie2('FromWebSite', cookievalue, '/', '.hsbc.com.hk', 'true', 0);

	document.forms[formname].submit();
}

/***************************************************************************************************************
* Function		: it_popupNewBrowser(url, status, location, scroll, mbar, toolbar, resize, width, height, winname)
* Description		: Open a new browser (popup)
* Parameter Usage	: url		:target URL
*					  status	:the status of the target browser (yes/no)
*					  location	:has address bar or not (yes/no)
*					  scroll	:has scroll bar or not (yes/no)
*					  mbar		:has menu bar or not (yes/no)
*					  toolbar	:has toolbar or not (yes/no)
*					  resize	:resizable or not (yes/no)
*					  height	:height of the target browser
*					  width		:width of the target browser
*					  winname	:window name (default is set to "nb")
* Location			: common.js
* Author			: Chan Tsz To
* Creation Date		: 05 Jul 2006
* Side effect		: N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*
***************************************************************************************************************/
function it_popupNewBrowser(url, status, location, scroll, mbar, toolbar, resize, width, height, left, top, winname)
{
	!winname? winname='nb':winname=winname;
	nb=window.open(url, winname, 'status=' + status + ',location=' + location + ',scrollbars=' + scroll + ',menubar=' + mbar + ',toolbar=' + toolbar + ',resizable=' + resize + ',height=' + height + ',width=' + width + ',left=' + left + ',top' + top);
	nb.focus();
}

/***************************************************************************************************************
* Function		: it_setCookie2
* Description		: To logon PIB with digital order
* Parameter Usage	: Name:		name
*					  value:	cookievalue
*			  path:		/
*			  domain:	.hsbc.com.hk
*			  secure:	true/false
*			  expires:	expires
* Location			: /hk/personal/common/js/pfscommon.js
* Author			: Chan Tsz To
* Creation Date		: 6 Feb 2004
* Side effect		: N/A
* Amendment History	:
* Date		By		Description
* ---------	-------------------------------------------------------------------------------------------
***************************************************************************************************************/

function it_setCookie2(name, value, path, domain, secure, expires)
{
    document.cookie = name + '=' + value +
    ((path) ? '; path=' + path : '') +
    ((domain) ? '; domain=' + domain : '') +
    ((secure) ? ';secure' : '')+
    ((expires) ? ';expires=' + expires.toGMTString() : '') ;
}

/***************************************************************************************************************
* Function		: it_getSite
* Description		: To get the site metadata values for WebTrends
* Parameter Usage	: str : the value of siteType defined in BDE
* Author		: Carol Chan
* Creation Date		: 5 Sep 2006
* Side effect		: N/A
* Amendment History	:
* Date		By		Description
* ---------	-------------------------------------------------------------------------------------------
***************************************************************************************************************/

function it_getSite(str)
{
	pos = str.indexOf(";")
	if (pos > -1)
		return str.substring(0, pos);
	else
		return str;
}

/***************************************************************************************************************
* Function		: it_getIBType
* Description		: To get the type IB metadata values for WebTrends
* Parameter Usage	: str : the value of siteType defined in BDE
* Author		: Carol Chan
* Creation Date		: 5 Sep 2006
* Side effect		: N/A
* Amendment History	:
* Date		By		Description
* ---------	-------------------------------------------------------------------------------------------
***************************************************************************************************************/

function it_getIBType(str)
{
	pos = str.indexOf(";")
	if (pos > -1)
		return str.substring(pos+1, str.length);
	else
		return "";
}
