/*
 *****************************************************************
 * COPYRIGHT. HSBC HOLDINGs PLC 2004. ALL RIGHTS RESERVED.
 *
 * THIS SOFTWARE IS ONLY TO BE USED FOR THE PURPOSE FOR WHICH IT 
 * HAS BEEN PROVIDED. NO PART OF IT IS TO BE REPRODUCED, 
 * DISASSEMBLED, TRANSMITTED, STORED IN A RETRIEVAL SYSTEM  NOR 
 * TRANSLATED IN ANY HUMAN OR COMPUTER LANGUAGE IN ANY WAY OR 
 * FOR ANY OTHER PURPOSES WHATSOEVER WITHOUT THE PRIOR WRITTEN 
 * CONSENT OF HSBC HOLDINGS PLC.
 *****************************************************************
 *
 * commfcn.js
 * 
 * Created on 12 October 2004, 10:57
 *
 * GRBS IPO Allotment datastructure
 *
 * Amendment History:
 * Amendment Date      	Programmer  PPCR NO.    Description 
 * --------------       ---------   ----------  ------------------------
 * 9/06/01				Velda Kam	EBD090602	Block invalid character input via URL (cross-site reference) raised by ITP
 * 												Modify the function quickQuote() to remove any language/codeset dependent for common JS file
 *												Problems found int UTF-8/Big5 page to display message box TC wordings
 * @author Lydia
 * @version 1.0 12 October2004
 */

/**
* Pop-up NetTrader demo page
*
* @param lang, language indicator
* 
* @return void
*/   
function popUpNetTraderDemo(lang)
{
    var url = "";
    if (lang == 2)
    {
    	url = "/nettrader/tch/common/demo/demo.htm";
    }
    else
    {
        url = "/nettrader/en/common/demo/demo.htm";
    }
    
    var demoWin = window.open(url,'popUpDemo','width=660,height=540,scrollbars=no,resizable=no,status=no,location=no,menubar=no,toolbar=no,directories=no,pageXOffset=0,pageYOffset=0');
    demoWin.moveTo(0,0);
    if (!demoWin.opener){demoWin.opener=self;}
    if (demoWin.focus){demoWin.focus();}
}

/**
* Pop-up a new window for market commentary
*
* @param url, a string contain the location for the pop-up window
* 
* @return void
*/   
function popUpFull4NetNews(url)
{
    var mktcomWin = window.open(url,'popUpFull4MktCom','width=560,height=400,scrollbars=yes,resizable=no,status=yes,location=no,menubar=no,toolbar=no,directories=no,pageXOffset=0,pageYOffset=0');
    if (!mktcomWin.opener){mktcomWin.opener=self;}
    if (mktcomWin.focus){mktcomWin.focus();}
}

/**
* Pop-up a new window for stock code lookup
*
* @param url, a string contain the location for the pop-up window
* 
* @return void
*/   
function popLookUp(url)
{
    var lookupWin = window.open(url,'CodeLookUp','width=500,height=300,resizable=yes,scrollbars=yes,status=no,menu=no,location=no,directories=no,pageXOffset=0,pageYOffset=0');
    if (!lookupWin.opener){lookupWin.opener=self;}
    if (lookupWin.focus){lookupWin.focus();}
}

/**
* Redirect to Trading Site logon page
*
* @param lang, language indicator
* 
* @return void
*/   
function tradeNow(lang)
{
    var tradeWin = window.open("","tradeWin","width="+screen.availWidth+",height="+screen.availHeight*0.93+",scrollbars=yes,resizable=yes,status=yes,location=no,menubar=no,toolbar=no,directories=no,left=0,top=0");
    if (!tradeWin.opener){tradeWin.opener=self;} 
    if (tradeWin.focus){tradeWin.focus();}
                    
    //HSBC Broking mainly supports IE and Netscape
    if((navigator.appName.indexOf("Microsoft")>-1)){
	//tradeWin.resizeTo(screen.availWidth, screen.availHeight);
    }
    if((navigator.appName.indexOf("Netscape")>-1)){	
	tradeWin.outerWidth = screen.availWidth;
	tradeWin.outerHeight = screen.availHeight;
    }
         
    if (lang == 2)
    {
//    	tradeWin.location.href="https://www.qualityassurance.ebroking.hsbc.com.hk/PreLogon_ct.htm";	//UAT
    	tradeWin.location.href="https://www.ebroking.hsbc.com.hk/PreLogon_ct.htm";	//Prod
//    	tradeWin.location.href="https://172.26.49.142:30050/PreLogon_ct.htm";	//UAT IP
    	//tradeWin.location.href="http://130.1.182.17:30210/Logon_ct.htm";	//DEV
    	//parent.location.href="https://www.ebroking.hsbc.com.hk/Logon_ct.htm";
    	//parent.location.href="http://130.1.147.86:32060/Logon_ct.htm";
    	//parent.location.href="http://130.1.182.17:30210/Logon_ct.htm";
    }
    else
    {
//        tradeWin.location.href="https://www.qualityassurance.ebroking.hsbc.com.hk/PreLogon_en.htm";	//UAT
        tradeWin.location.href="https://www.ebroking.hsbc.com.hk/PreLogon_en.htm";	//Prod
//        tradeWin.location.href="https://172.26.49.142:30050/PreLogon_en.htm";	//UAT IP
    	//tradeWin.location.href="http://130.1.182.17:30210/Logon_en.htm";	//DEV
    	//parent.location.href="https://www.ebroking.hsbc.com.hk/Logon_en.htm";
    	//parent.location.href="http://130.1.147.86:32060/Logon_en.htm";
    	//parent.location.href="http://130.1.182.17:30210/Logon_en.htm";
    }
}

/**
* Redirect to NetTraderCards Public Site logon page
*
* @param lang, language indicator ('eng' - English; 'tch' - Tranditional Chinese; 'sch' - Simplified Chinese)
* 
* @return void
*/   
function ntcLogon(lang)
{
    var ntcWin = window.open("","ntcWin","width="+screen.availWidth+",height="+screen.availHeight*0.93+",scrollbars=yes,resizable=yes,status=yes,location=no,menubar=no,toolbar=no,directories=no,left=0,top=0");
    if (!ntcWin.opener){ntcWin.opener=self;} 
    if (ntcWin.focus){ntcWin.focus();}
                    
    //HSBC Broking mainly supports IE and Netscape
    if((navigator.appName.indexOf("Microsoft")>-1)){
	//ntcWin.resizeTo(screen.availWidth, screen.availHeight);
    }
    if((navigator.appName.indexOf("Netscape")>-1)){	
	ntcWin.outerWidth = screen.availWidth;
	ntcWin.outerHeight = screen.availHeight;
    }
         
    if (lang == "sch")	//Simplified Chinese
    {
    	ntcWin.location.href="https://www.nettradercards.broking.hsbc.com.hk/jsp/ifs/sch/logon.jsp";	//Prod
    }
    else if (lang == "tch")	//Tranditional Chinese
    {
        ntcWin.location.href="https://www.nettradercards.broking.hsbc.com.hk/jsp/ifs/tch/logon.jsp";	//Prod    	
    }
    else	//English
    {
    	ntcWin.location.href="https://www.nettradercards.broking.hsbc.com.hk/";	//Prod
    }
}

/**
* Check whether cookie is enabled. If not, prompt a warning message
* 
* @return void
*/   
function cookieCheck()
{
    if (document.cookie.length <= 0)
        alert("You will need to enable cookies to allow this programme to function properly. Please double check and try again.")
}

/**
* Get the cookie value for the specific cookie name
*
* @param name, the required cookie name
* 
* @return the cookie value for the cookie name
*/   
function getCookie(name) 
{
    var cookieFound = false;
    var start = 0;
    var end = 0;
    var cookieString = document.cookie;
    var i = 0;

    // SCAN THE COOKIE FOR name
    while (i <= cookieString.length) 
    {
        start = i;
        end = start + name.length;
        if (cookieString.substring(start,end) == name) 
        {
            cookieFound = true;
            break;
        }
        i++;
    }

    // IS name FOUND?
    if (cookieFound) 
    {
        start = end + 1;
        end = document.cookie.indexOf(";",start);
        if (end < start)
            end = document.cookie.length;
        return document.cookie.substring(start,end);
    }
    return "";
}

/**
* Delete the specific cookie name
*
* @param name, the required cookie name
* 
* @return void
*/   
function DeleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

/**
* Set the cookie value for the specific cookie name
*
* @param name, the required cookie name
* @param value, the cookie value
* 
* @return void
*/   
function SetCookie (name, value) {
	var expDays = 365;
	var exp = new Date();
	exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var path = "/";
	var domain = (argc > 2) ? argv[2] : null;
	var secure = (argc > 3) ? argv[3] : false;
	document.cookie = name + "=" + escape (value) +
	"; expires=" + exp.toGMTString() +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

/**
* Check whether the stock code entered is valid
* Include different msg_en.js / msg_utf8.js / msg_big5.js for corresponding caller page
*
* @param form, the quickQuote form
* 
* @return void
*/   
function quickQuote(form) 
{
    // check valid stock
    if(form.StockCode.value == null || form.StockCode.value.length == 0 || isEmptyString(form.StockCode.value)) 
    {
        // show error for empty code
        var errMsg = Error_01;
    	alert(errMsg);
    
        form.StockCode.focus();
        return;
    }  
    if(checkNum(form.StockCode.value)) 
    {
        // show error for invalid stock code
		var errMsg = Error_02;
		alert(errMsg);

        return;
    }    
        // check searching for Stockquote or NetTrader card...
    
//    if(form.SearchType[0].checked == 1) 
//    {
    	if (form.LANG.value == 2)
    	{
        	// stockquote - check whether the code exists in the market
        	parent.location.href="/script/nettrader/tch/mktinfo/miquote.asp?xchgcode=" + form.StockCode.value;
        }
        else
        {
        	// stockquote - check whether the code exists in the market
        	parent.location.href="/script/nettrader/en/mktinfo/miquote.asp?xchgcode=" + form.StockCode.value;        	
        }
//    }
//    else {
        // pop NetTrader Cards
        //popNetTraderCards(form.StockCode.value);
//        popNetTraderCards(form.LANG.value);
//    }
}       

/**
* Check whether a string is an empty string
*
* @param str, the string to be checked
* 
* @return true if the string is an empty string.
* @return false if the string is non-empty string.
*/   
function isEmptyString(str) 
{
    // answer 1 if it is an empty string, otherwise answer 0.
    var ans = false; 
    var ansStr = "";
    if(str=="") return true;
    for (var i = 0; i < str.length; i++)
    {
        var ch = str.substring(i, i + 1)
        if (ch != " ")
            ansStr += ch;
    }
    if (ansStr == "")
    ans = true;
    return ans;
}

/**
* Check whether a stock code is a valid number
*
* @param stockCode, the stock code to be checked
* 
* @return true if the stock code is valid. 
* @return false if the stock code is invalid. 
*/   
function checkNum(stockCode) 
{
    // check the valid numeric field    

    var check_element = String(stockCode);
    var flg=0;
    for (var j = 0; j < check_element.length; j++) 
    {
        var cmp= new String("0123456789");
        var tst = check_element.substring(j, j+1);
        if (cmp.indexOf(tst) < 0) 
        {
            flg++;
        }
    }
    if (flg!=0) return true;
    return false;
}

/**
* Load the given page in the current browser
*
* @param url, page to be displyed
* 
* @return void.
*/   
function LoadFile(url)
{
    parent.location.href=url;	
}


/**
* Goes to the corresponding English, Traditional Chinese or Simplified Chinese page, specific to HK
*
* @param orilang, original language
* @param tolang, language to switch to
* 
* @return convert URL.
*/   
function switchLang(orilang, tolang) {
	var url;
	url=escape(window.location.pathname);
	if (tolang=="tch") {	
		if (orilang=="en") {
			url=url.replace("/en/","/tch/");	
		}
		else if (orilang=="sch") {
			url=url.replace("/sch/","/tch/");	
		}		
	} else 	if (tolang=="sch") {	
		if (orilang=="en") {
			url=url.replace("/en/","/sch/");	
		}
		else if (orilang=="tch") {
			url=url.replace("/tch/","/sch/");	
		}		
	} else if (tolang=="en") {
		if (orilang=="tch") {
			url=url.replace("/tch/","/en/");	
		}
		else if (orilang=="sch") {
			url=url.replace("/sch/","/en/");	
		}		
	} 

	window.location=url;
}


/**
* Goes to the corresponding English, Traditional Chinese or Simplified Chinese page, specific to HK
*
* @param orilang, original language
* @param tolang, language to switch to
* @param appendString, Supporting parameters
* 
* @return convert URL.
*/   
function switchLangWithParam(orilang, tolang, appendString) {
	var url;
	url=escape(window.location.pathname);
	if (tolang=="tch") {	
		if (orilang=="en") {
			url=url.replace("/en/","/tch/");	
		}
		else if (orilang=="sch") {
			url=url.replace("/sch/","/tch/");	
		}		
	} else 	if (tolang=="sch") {	
		if (orilang=="en") {
			url=url.replace("/en/","/sch/");	
		}
		else if (orilang=="tch") {
			url=url.replace("/tch/","/sch/");	
		}		
	} else if (tolang=="en") {
		if (orilang=="tch") {
			url=url.replace("/tch/","/en/");	
		}
		else if (orilang=="sch") {
			url=url.replace("/sch/","/en/");	
		}		
	} 

	url=url+appendString;	
	window.location=url;
}

/**
* This function check for any invalid character. 
* Include different msg_en.js / msg_utf8.js / msg_big5.js for corresponding caller page as codeset problem 
* found in TC Big5 and UTF8 pages with original file compiled in Big5 suggested by the function quickQuote(form) above
*
* Invalid Characters include <(60) >(62) "(34) %(37) ;(59) +(43) +(32)
* Note: space in JavaScript (32) 
* This works the same logic as ASP script\nettrader\common\validchk.asp
* As ()' maybe input by user in Contact Us page, hence ()' are accepted until further requirement for restriction
*
* @param input string for checking
* 
* @return whether invalid character is found
*/ 
 
function chkInvalidChr(chkString, fieldName)
{
	var invalidChr = false;
	for(y=0 ; y < chkString.length ; y++) {	
		nCharCode = chkString.charCodeAt(y);		// <(60) >(62) "(34) %(37) ;(59) +(43)
		if (nCharCode==60 || nCharCode==62 || nCharCode==34 || nCharCode==37 || nCharCode==59 || nCharCode==43) {
			invalidChr = true;

			var errMsg = Error_03.replace(/~~1/, fieldName);
			errMsg = errMsg.replace(/~~2/, chkString);
			alert(errMsg);

			break;
		}
	}		
	return invalidChr;
}
