/*******************************************************/
/* This is a common Format js                          */
/* naming convention for function: cmXXXXX             */
/*******************************************************/

var digits = "0123456789";
var decimalPointDelimiter = "."

function cmRound(number,X) {
// rounds number to X decimal places, defaults to 2
  X = (!X ? 2 : X);
  return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function cmFormatNumericSeparator(inNum)
{
	var tempnum = inNum.toString();
	if (tempnum.substring(0, 1) == "-")
	{
	   tempnum = tempnum.substring(1)
	}
	if (inNum == "-") {return inNum;}
  if (cmIsEmpty(tempnum) || !cmIsNumber(tempnum))
  {
     return "";	
  }
  	
	inNum = inNum.toString().replace(/\$|\,/g,'');
  if(isNaN(inNum))
  inNum = "0";
  inNum = Math.floor(inNum*100+0.50000000001);
  inNum = Math.floor(inNum/100).toString();
  for (var i = 0; i < Math.floor((inNum.length-(1+i))/3); i++)
  {
     inNum = inNum.substring(0,inNum.length-(4*i+3))+','+
     inNum.substring(inNum.length-(4*i+3));
  }    
  return inNum;  
}

function cmFormatDecimalSeparator(num, dec) 
{
	var tempnum = num.toString();
	if (tempnum.substring(0, 1) == "-" )
	{
	   tempnum = tempnum.substring(1);
	} else if (tempnum.substring(0, 1) == "+" ) {
      tempnum = tempnum.substring(1);
    }
    
	if (num == "-") {return num;}
  if (cmIsEmpty(tempnum) || !cmIsFloat(tempnum))
  {
     return "";	
  }
  
	if (num == 0) {
	 var cents = "0";
   for (var i = 1; i < dec; i++)
   {
     cents = "0" + cents;
   } 
   return "0." + cents;
	}	
	
	dec = (!dec ? 2 : dec);
  var multipleval = 1;
  var tenbase = 10;

  for (var i = 0; i < dec; i++)
  {
     multipleval = multipleval * tenbase;
  }
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
    num = "0";
    
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*multipleval+0.50000000001);
  cents = num%(multipleval);
  num = Math.floor(num/(multipleval)).toString();
 
  var curval = multipleval/10;
  for (var i = 0; i < curval; i++)
  {
  	if (cents < curval)
  	{
     cents = "0" + cents;
    } 
    curval = curval / 10;    
  }   
 
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  {
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
  }  
  return(((sign)?'':'-')  + num + '.' + cents);
}

function cmIsDigit (c) {
   return ((c >= "0") && (c <= "9"))
}

function cmIsFloat (s) {

    var i;
    var seenDecimalPoint = false;
    if (s == decimalPointDelimiter) return false;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!cmIsDigit(c)) return false;
    }
    
    // All characters are numbers.
    return true;
}

function cmIsNumber (s) {

    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (!cmIsDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}

function cmIsEmpty(s) {
   return ((s == null) || (s.length == 0))
}


// TextBox input field length counter
function cmTextCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}

function cmFormatMoney(inNum,dec) 
{
	var tempnum = " ";
//	alert(parseFloat(inNum));
	if (inNum == 0){
		inNum = "0.0";
	}
	if (inNum != ""){
  		tempnum =cmFormatDecimalSeparator(parseFloat(inNum),dec).toString();
		if (tempnum.substring(0, 1) == "-" ){
			tempnum = "("+tempnum.substring(1,tempnum.length)+")"
		}
	}
	return tempnum;
}

function cmFormatDate(inDate)
{
	var resultDate = " ";
	if (inDate != ""){

		var tempYear = inDate.substring(0,4);
		var tempMonth = inDate.substring(5,7);
		var tempDate = inDate.substring(8,10);
		
		resultDate = tempYear+"/"+tempMonth+"/"+tempDate;
	}
	return resultDate;	
}

function cmFormatLeftPadZero(input, inputLenght)
{	
	var output = "";
	output = input;
	if (input.length < inputLenght){
		for (i = 0; i < (inputLenght-input.length); i++){
			output = "0" + output;
		}
	}
		
	return output;
}

function getHighestRate(){
	var highestrate = 0;
	for (var i = 0; i < dep_rate.length; i++)
	{
		  if (dep_rate[i][0] == "T") {
	       if (dep_rate[i][9] * 1.0  > highestrate * 1.0)
	       {
	     		 highestrate = 	dep_rate[i][9];
	       }
		  }	
	}
	return highestrate * 1.00;
}

function getHighestRate(rateType, ccy, frequency, period){
	var highestrate = -1;
	for (var i = 0; i < dep_rate.length; i++)
	{
		  if ((dep_rate[i][0] == rateType || rateType == "" || rateType == null) &&
              (dep_rate[i][2] == ccy || ccy == "" || ccy == null) && 
              (dep_rate[i][5] == frequency || frequency == "" || frequency == null) && 
              (dep_rate[i][6] == period || period == "" || period == null)) {
	          if (dep_rate[i][9] * 1.0  > highestrate * 1.0)
	          {
	     		    highestrate = 	dep_rate[i][9];
	          }
           }	
	}
    if (highestrate < 0) {
        return "N/A";
    }
    return highestrate * 1.00 + "%";
}

function getHighestRateDate(){
	var highestrate = 0;
	var highestratedate = "";
	for (var i = 0; i < dep_rate.length; i++)
	{
		  if (dep_rate[i][0] == "T") {
	       if (dep_rate[i][9] * 1.0 > highestrate * 1.0)
	       {
	     		 highestrate = 	dep_rate[i][9];
	     		 highestratedate = dep_rate[i][10];
	       }
		  }	
	}
	
	return convertDateFormat(highestratedate, "YYYYMMDDHHMMSS", "LongDesc");
}

function getRates(ccy, ratetype, freq, period, tiergroup) {
	var tmprate = -1;

  for (var i = 0; i < dep_rate.length; i ++)
  {  
	   if ((dep_rate[i][0] == ratetype || ratetype == "" || ratetype == null) &&
	       (dep_rate[i][2] == ccy || ccy == "" || ccy == null) && 
	       (dep_rate[i][5] == freq || freq == "" || freq == null) &&
	       (dep_rate[i][6] == period || period == "" || period == null) &&
	       (dep_rate[i][4] == tiergroup || tiergroup == "" || tiergroup == null)){
	       	
         tmprate = dep_rate[i][9];	   	   
	   	}
  }	
  
  if (tmprate >= 0) { tmprate = tmprate * 1.0 + "%";}    
  else {tmprate = "N/A"}
  return tmprate;
  
}

function getRateIdx(ccy, ratetype, freq, period, tiergroup) {
	var tmprate = 0;
	
  for (var i = 0; i < dep_rate.length; i ++)
  {  
	   if ((dep_rate[i][0] == ratetype || ratetype == "")&&
	       (dep_rate[i][2] == ccy)&& 
	       (dep_rate[i][5] == freq || freq == "")&&
	       (dep_rate[i][6] == period || period == "") &&
	       (dep_rate[i][4] == tiergroup || tiergroup == "")){
	       	
         return i;
	   	}
  }	
  
  return -1; 
}

function getField(idx, fieldno) {
	   var fieldvalue = dep_rate[idx][fieldno];
	   
	   if (fieldvalue == null) {
	      fieldvalue = "-";
	   }
	   
	   return fieldvalue;
}

function formatRate(inRate) {
  if (inRate > 0) { inRate = inRate * 1.0 + "%"}
  else {inRate = "N/A"}
  return inRate;
}

function getLastUpdate(ccy) {
	var tmpLastUpdate = "";
  for (var i = 0; i < dep_rate.length; i ++)
  {  
	   if (dep_rate[i][2] == ccy) {
	   	     tmpLastUpdate = dep_rate[i][10];
	   	}
  }	
       return convertDateFormat(tmpLastUpdate, "YYYYMMDDHHMMSS", "ShortDateHHMM");
}

function getLastUpdateFormat(ccy,inFormat) {
	var tmpLastUpdate = "";
	
  for (var i = 0; i < dep_rate.length; i ++)
  {  
	   if (dep_rate[i][2] == ccy) {
	   	     tmpLastUpdate = dep_rate[i][10];
	   	}
  }	
       return convertDateFormat(tmpLastUpdate, "YYYYMMDDHHMMSS", inFormat);
}


function getMaxTDRate(){
	var maxRate = 0;
	for (var i = 0; i < dep_rate.length; i++)
	{
		  if (dep_rate[i][0] == "X") {
     		 maxRate = 	dep_rate[i][9];
		  }	
	}
	return maxRate * 1.00;
    
}

function getMaxTDRateDate(){
	var maxDate = "";
	for (var i = 0; i < dep_rate.length; i++)
	{
		  if (dep_rate[i][0] == "X") {
     		 maxDate = 	dep_rate[i][10];
		  }	
	}
	return convertDateFormat(maxDate, "YYYYMMDDHHMMSS", "LongDateHHMM");
    
}

  /* Convert Date Format */
  function convertDateFormat(indate, inDateFormat, outDateFormat) {
     //alert("ConverDateFormat:" + indate + "," + inDateFormat + "," + outDateFormat);
     var day = "";
     var year= "";
     var month ="";
     var hour ="";
     var minutes="";
     var second="";
     
     var m="";
     var formatDate= "";

     switch (inDateFormat) {
        case "YYYYMMDDHHMMSS":
            month = indate.substring(4,6);
            day = indate.substring(6,8);
            year = indate.substring(0,4);
            hour = indate.substring(8,10);
            minutes = indate.substring(10,12);
            second = indate.substring(12,14);
            break;
        case "YYYYMMDD":
            month = indate.substring(4,6);
            day = indate.substring(6,8);
            year = indate.substring(0,4);
            break;
				case "YYYY/MM/DD":
            month = indate.substring(5,7);
            day = indate.substring(8,10);
            year = indate.substring(0,4);
            break;    
        case "HHMMSS":
            hour = indate.substring(8,10);
            minutes = indate.substring(10,12);
            second = indate.substring(12,14);
            break;
        case "MMDD":
            month = indate.substring(0,2);
            day = indate.substring(2,4);    
            break;          
        case "DDMM":
            day = indate.substring(0,2);
            month = indate.substring(2,4);    
            break;              
        case "MMDDYYYY":
            month = indate.substring(0,2);
            day = indate.substring(2,4);    
            year = indate.substring(4,8);    
            break;
			  case "YYYY-MM-DD HH:MM:SS":
            month = indate.substring(5,7);
            day = indate.substring(8,10);
            year = indate.substring(0,4); 
            hour = indate.substring(11,13);
            minutes = indate.substring(14,16);
            second = indate.substring(17,19);
            break;     
            
     }  
     /*
     if (hour == null || hour=="") hour="00";
     if (minutes== null || minutes=="") minutes="00";
     if (second== null || second=="") second="00";
       */   
     switch (outDateFormat) {

      case "LongDesc":
         if (hour == "" || minutes == "" ) {
            formatDate = year + ' 年 ' + month + ' 月 ' + day + ' 日  ';
         }   
         else {            
						 if (hour == null || hour=="") hour="00";
				     if (minutes== null || minutes=="") minutes="00";
				     if (second== null || second=="")         	
            		formatDate = year + ' 年 ' + month + ' 月 ' + day + ' 日 ， ' + hour + ' 時 ' + minutes + ' 分 ';
             else
             		formatDate = year + ' 年 ' + month + ' 月 ' + day + ' 日 ， ' + hour + ' 時 ' + minutes + ' 分 ' + second + ' 秒 ';
        }            
         break;

			case "LongDate":
				formatDate = year + '年' + month + '月' + day + '日  ';
				break;
			
			case "LongDateHHMM":
				if (hour == null || hour=="") hour="00";
				if (minutes== null || minutes=="") minutes="00";
        formatDate = year + '年' + month + '月' + day + '日 ' + hour + ":" + minutes;
				break;
			
			case "LongDateHHMMSS":
				if (hour == null || hour=="") hour="00";
				if (minutes== null || minutes=="") minutes="00";
				if (second== null || second=="") second="00";
        formatDate = year + '年' + month + '月' + day + '日 ， ' + hour + ":" + minutes + ":" + second;
				break;


      case "ShortDesc":         
         if (year == "") {
            formatDate = month + '月' + day + '日 ';
         } else {
            formatDate = year + '年' + month + '月' + day + '日  ';
         }         

         break;
         
			case "ShortDate":
				formatDate = year + '年 ' + month + '月' + day + '日 ';
				break;
			
			case "ShortDateHHMM":
				if (hour == null || hour=="") hour="00";
				if (minutes== null || minutes=="") minutes="00";
        formatDate = year + '年' + month + '月' + day + '日， ' + hour + ":" + minutes;
				break;
			
			case "ShortDateHHMMSS":
				if (hour == null || hour=="") hour="00";
				if (minutes== null || minutes=="") minutes="00";
				if (second== null || second=="") second="00";
        formatDate = year + '年' + month + '月' + day + '日 ' + hour + ":" + minutes + ":" + second;
				break;
				

      case "Standard":
         formatDate = year + "/" + month + "/" + day;
         break;
      
      case "Standard2":
         formatDate = year + "-" + month + "-" + day;
         break;
      
      case "StandardDayTime":
         formatDate = year + "-" + month + "-" + day +" " + hour + ":" + minutes;
         break;
      
         
      case "SQLFmt107":
        formatDate =    year + '年' + month + '月' + day + '日  ';
        break;

     case "TimeHHMM":
         formatDate = hour + ":" + minutes;
         break;   

    }    
    
    return formatDate;
    
     
  
  }
    
  /* Convert the Last date/time in the welcome page */
  function convertLastLogin(indate,inInd){
     
     
     if (inInd=="S"){
     	  formatStatus="成 功 登 入 。"}
     else {
        formatStatus="登 入 失 敗 。"};
      
     formatDate = convertDateFormat(indate, "YYYYMMDDHHMMSS", "LongDesc")
    
     document.write('您 上 次 於 ' + formatDate + '(香 港 時 間) ， '+ formatStatus);         
   
   }

   function ShowCustName()
   {
    document.write('&nbsp;');
   }
   
   function ShowWelcomeCustName(inCustomerName)
   {
   	if (inCustomerName=="")	
   	    document.write('歡迎');
   	else
    	   { 
   	   	str=inCustomerName
   	        //newstr=str.replace(/\*/gi, "");
   	        //newstr=newstr.replace(/#/gi,"");
   	        document.write('歡迎, ', str);
   	    }
   	
   }


function gotoCcyRatePage(form){
//	alert( "111111111" );
//	alert("form.optSelectCcy.value"+form.optSelectCcy.value);
	if (form.optSelectCcy.value == '0') {
		window.location = "/hongkong/chinese/rateenquiry/AUD_rates_Jay.html";
	} else if (form.optSelectCcy.value == '1') {
		window.location = "/hongkong/chinese/rateenquiry/CAD_rates_Jay.html";
	} else if (form.optSelectCcy.value == '4') {
		window.location = "/hongkong/chinese/rateenquiry/GBP_rates_Jay.html";
	} else if (form.optSelectCcy.value == '6') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_cny.html";
	} else if (form.optSelectCcy.value == '5') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates.html";
	} else if (form.optSelectCcy.value == '2') {
		window.location = "/hongkong/chinese/rateenquiry/EUR_rates_Jay.html";
	} else if (form.optSelectCcy.value == '3') {
		window.location = "/hongkong/chinese/rateenquiry/NZD_rates_Jay.html";
	}
}

function gotoCcyRatePage(form){
//	alert( "111111111" );
//	alert("form.optSelectCcy.value"+form.optSelectCcy.value);
	if (form.optSelectCcy.value == '0') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_aud.html";
	} else if (form.optSelectCcy.value == '1') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_cad.html";
	} else if (form.optSelectCcy.value == '2') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_chf.html";
	} else if (form.optSelectCcy.value == '3') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_eur.html";
	} else if (form.optSelectCcy.value == '4') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_gbp.html";
	} else if (form.optSelectCcy.value == '5') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_jpy.html";
	} else if (form.optSelectCcy.value == '6') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_nzd.html";
	} else if (form.optSelectCcy.value == '7') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_sgd.html";
	} else if (form.optSelectCcy.value == '8') {
		window.location = "/hongkong/chinese/rateenquiry/dep_rates_zar.html";
	}
}

function buildCcyList(dep){

	document.write("<table cellpadding=0 cellspacing=0><form name='formRate'>");
	document.write("<tr><td class=rateTable2>貨幣利率&nbsp;&nbsp;:&nbsp;&nbsp;</td>");
	document.write("<td class=rateTable2>");	
	document.write("<select name='optSelectCcy' onChange='gotoCcyRatePage(formRate)'>");
	
		document.write("<option value='10'selected>請選擇</option>");
		document.write("<option value='0'>澳 元</option>");
		document.write("<option value='1'>加 元</option>");
		document.write("<option value='2'>瑞 士 法 郎</option>");
		document.write("<option value='3'>歐 羅</option>");
		document.write("<option value='4'>英 鎊</option>");
		document.write("<option value='5'>日 元</option>");
		document.write("<option value='6'>紐 西 蘭 元</option>");
		document.write("<option value='7'>新加坡元</option>");
		document.write("<option value='8'>南 非 蘭 特</option>");

	document.write("</select>");
	document.write("</td></tr>");
	document.write("<tr><td>&nbsp;</td></tr>");
	document.write("</form></table>");
}

function addRateFootNote1(){
	document.write('<table width=90% border="0" cellspacing="0">');
  document.write('<tr><td width=40 valign="top" class=rateTable2 nowrap>備註&nbsp;:&nbsp;</font></td>');
  document.write('<td class=rateTable2>以上的存款利率只供參考之用。</td></tr></table>');
}

function addRateFootNote2(){
	document.write('<table width=90% border="0" cellspacing="0">');
  document.write('<tr><td width=40 nowrap valign="top" class=rateTable2>備註&nbsp;:&nbsp;</font></td>');
  document.write('<td class=rateTable2>以上的存款利率只供參考之用。<br></td></tr></table>');
}

