

Number.prototype.toDecimals=function(n){

	n=(isNaN(n))?2:n;

	// rounds number to X decimal places, defaults to 2
	var s = new String(Math.round(this*Math.pow(10,n))/Math.pow(10,n));
    return format(s, n);

/*
    var nT=Math.pow(10,n);

    function pad(s){
            s=s||'.';
            return (s.length>n)?
                s:
                pad(s+'0');
    }
    return (isNaN(this))?this:(new String(Math.round(this*nT)/nT)).replace(/(\.\d*)?$/,pad);*/
}

function format(total,decimals) {

	var num = parseFloat(total);
	// First section sets non-number value to zero
	if (!(num = parseFloat(num)))
	num = "0.00";
	// Second section sets two decimal place format
	var Pad = "";
	num = "" + Math.floor(num * Math.pow(10,decimals + 1) + 5);
	// Pad if less than 0.10
	if(num.length < decimals+1) 
	{
	   for(Count = num.length; Count <= decimals; Count++)
			Pad += "0";
	}
	num = Pad + num;
	// Parse into final string
	num = num.substring(0,num.length - decimals - 1) + 
	"." + num.substring(num.length - decimals -1, num.length -1);
	// If less than 1 then add 0 to the left of the decimal
	  if((num == "") || (parseFloat(num) < 1))
		   num = "0" + num;
	// Final section returns formatted number
	  return num;
}

function setWindowName(winName) {
  window.name = winName;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_openBrWindow(theURL,winName,features) { 
  // Set default features
  if (features == '')
    features = 'menubar=no, status=no, scrollbars=yes,resizable=yes,width=800,height=600';
  var win = window.open(theURL,winName,features);
  win.focus();
}

function P7_Snap() { //v2.61 by PVII
  var x,y,ox,bx,oy,p,tx,a,b,k,d,da,e,el,args=P7_Snap.arguments;a=parseInt(a);
  for (k=0; k<(args.length-3); k+=4)
   if ((g=MM_findObj(args[k]))!=null) {
    el=eval(MM_findObj(args[k+1]));
    a=parseInt(args[k+2]);b=parseInt(args[k+3]);
    x=0;y=0;ox=0;oy=0;p="";tx=1;da="document.all['"+args[k]+"']";
    if(document.getElementById) {
     d="document.getElementsByName('"+args[k]+"')[0]";
     if(!eval(d)) {d="document.getElementById('"+args[k]+"')";if(!eval(d)) {d=da;}}
    }else if(document.all) {d=da;} 
    if (document.all || document.getElementById) {
     while (tx==1) {p+=".offsetParent";
      if(eval(d+p)) {x+=parseInt(eval(d+p+".offsetLeft"));y+=parseInt(eval(d+p+".offsetTop"));
      }else{tx=0;}}
     ox=parseInt(g.offsetLeft);oy=parseInt(g.offsetTop);var tw=x+ox+y+oy;
     if(tw==0 || (navigator.appVersion.indexOf("MSIE 4")>-1 && navigator.appVersion.indexOf("Mac")>-1)) {
      ox=0;oy=0;if(g.style.left){x=parseInt(g.style.left);y=parseInt(g.style.top);
      }else{var w1=parseInt(el.style.width);bx=(a<0)?-5-w1:-10;
      a=(Math.abs(a)<1000)?0:a;b=(Math.abs(b)<1000)?0:b;
      x=document.body.scrollLeft + event.clientX + bx;
      y=document.body.scrollTop + event.clientY;}}
   }else if (document.layers) {x=g.x;y=g.y;var q0=document.layers,dd="";
    for(var s=0;s<q0.length;s++) {dd='document.'+q0[s].name;
     if(eval(dd+'.document.'+args[k])) {x+=eval(dd+'.left');y+=eval(dd+'.top');break;}}}
   if(el) {e=(document.layers)?el:el.style;
   var xx=parseInt(x+ox+a),yy=parseInt(y+oy+b);
   if(navigator.appName=="Netscape" && parseInt(navigator.appVersion)>4){xx+="px";yy+="px";}
   if(navigator.appVersion.indexOf("MSIE 5")>-1 && navigator.appVersion.indexOf("Mac")>-1){
    xx+=parseInt(document.body.leftMargin);yy+=parseInt(document.body.topMargin);
    xx+="px";yy+="px";}e.left=xx;e.top=yy;}}
}

function P7_autoLayers() { //v1.1 PVII
 var g,b,k,f,args=P7_autoLayers.arguments;
 if(!document.p7setc) {p7c=new Array();document.p7setc=true;}
 for(k=0; k<p7c.length; k++) {
  if((g=MM_findObj(p7c[k]))!=null) {
   b=(document.layers)?g:g.style;b.visibility="hidden";}}
 for(k=0; k<args.length; k++) {
  if((g=MM_findObj(args[k])) != null) {
   b=(document.layers)?g:g.style;b.visibility="visible";f=false;
   for(j=0;j<p7c.length;j++) {
    if(args[k]==p7c[j]) {f=true;}}
  if(!f) {p7c[p7c.length++]=args[k];}}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function clickurl(linkTo, page) { ;
  parent.frames[1].location=linkTo;
}

function searchjs1(){ //v3.0
 if (document.form1.radiobutton1[0].checked){
 this.location.href="search/site.html";
}
 if (document.form1.radiobutton1[1].checked){
 this.location.href="search/product.html";
}
}

function searchjs2(){ //v3.0
 if (document.form1.radiobutton1[0].checked){
 this.location.href="../search/site.html";
}
 if (document.form1.radiobutton1[1].checked){
 this.location.href="../search/product.html";
}
}

function searchjs3(){ //v3.0
 if (document.form1.radiobutton1[0].checked){
 this.location.href="../../search/site.html";
}
 if (document.form1.radiobutton1[1].checked){
 this.location.href="../../search/product.html";
}
}

/* 
 *	function openPopup(sLocation, sWinName, iWidth, iHeight, oOpener, bScrollBars)
 *	open New Window as Popup
 * 	Parameters: sLocation, sWinName, iWidth, iHeight, oOpener, bScrollBars
 */

function openPopup(sLocation, sWinName, iWidth, iHeight, oOpener, bScrollBars)
{	
	var sScrollBars="no";
	if (bScrollBars != null && bScrollBars)		
		sScrollBars="yes";
	// Set the attribute property for the open javascript function
	var attrib = 'directories=no,location=no,menubar=no,status=yes,titlebar=no,toolbar=no,scrollbars=' + sScrollBars + ',resizeable=no';
	//var attrib = 'directories=no,location=no,menubar=no,status=yes,titlebar=no,toolbar=no,scrollbars=yes,resizeable=no';
	var attrib = attrib + ',width=' + iWidth + 'px,height=' + iHeight + 'px';
	var attrib = attrib + ',top=' + ((screen.availHeight - iHeight) / 2) + ',left=' + ((screen.availWidth - iWidth) / 2);
	var newWin;
	// Open window
	if (oOpener != null)	
		newWin = oOpener.open(sLocation, sWinName, attrib);
	else
		newWin = window.open(sLocation, sWinName, attrib);
		
	newWin.focus();
	return newWin;

}


/*
 * Function allows user to switch windows between CWS window and agent/member window.
 * The URL for the consultantHomeWin is now hard-coded, different deployment should have different value.
 */
function switchWindow(winObj, winName) {

	// for member homepage
	if (window.top.name == 'eServices')
	{
		window.top.opener.focus();
		return;
	}

  var features = 'menubar=no, status=yes, scrollbars=yes,resizable=yes,left=0,top=0,width='
    + screen.width*0.99 + ',height=' + (screen.height - 80);

	if (winObj == consultantHomeWin)
	{
    // agent homepage URL is hard-coded
  	consultantHomeWin = window.open('https://ssohkdev.prudential.com.hk/sso/jsp/viewAgentHome.jsp',winName,features);
  	consultantHomeWin.focus();
	}
	else
	{
  	memberHomeWin = window.open('/PruServlet?module=content&purpose=viewMemberHomepage',winName,features);
  	memberHomeWin.focus();
	}

}

/*
 * This function allows opening the input URL on its opener window
 */
function openerGoTo(url) 
{
	window.parent.opener.location = url;
    window.parent.opener.focus();
}

/*
 * This function allows opening the input URL on its parent window (CWS)
 * If the parent name is 'eServices', then it will open on the 3rd frame of the current window.
 */
function parentGoTo(url) 
{
  if (parent.name == 'cws')
    document.location = url;
  else {

    if (parent.name == 'eServices' && 'cws' == 'cws') {

      if (window.parent.opener != null && window.parent.opener.top.length == 3) {
        window.parent.opener.location = url;
        window.parent.opener.focus();
      } else {
    	  if (parent.window.opener != null) {	  
    		  parent.window.opener.location = url;
    		  parent.window.opener.focus();
    	  } else {
    		   cws = window.open(url, 'cws');
    	       cws.focus(); 
    	  }		  
      }
    } else {
      cws = window.open(url, 'cws', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no');
      cws.focus();
    }
  }  
}

function calInceptionDate()
{
    var dNow = new Date();
  	var intDay = dNow.getDate();
  	var intMth = dNow.getMonth()+ 1;  	
 	var intYear = dNow.getYear() < 1000 ? dNow.getYear() + 1900 : dNow.getYear();  	
  	
  	if (dNow.getDate() < 11) 
  		return new Date(intYear, intMth - 1, 1);
  	else
  	  if (intMth == 12)  	  	
             return new Date((intYear + 1), 1, 1);
  	  else
		return new Date(intYear,  (intMth), 1);		
}

function calANB(dateString)
{
	var dob = new Date(dateString.substring(4,8), dateString.substring(2,4)-1, dateString.substring(0,2));  
	var dIncptDate  = calInceptionDate(dob);
	
	dobYear = dob.getYear();	
	incptDateYear = dIncptDate.getYear();

	if(dobYear < 1900) dobYear += 1900;
	if(incptDateYear < 1900) incptDateYear += 1900;

	var iANB = incptDateYear  - dobYear;

	if (dob.getMonth() < dIncptDate.getMonth())
		iANB++;
	else if (dob.getMonth() == dIncptDate.getMonth() && dob.getDate() < dIncptDate.getDate())
		iANB++;

	return iANB;
}

var eleTextArea; //input textarea element
var fckNewPopup; //fckEditor popup window ref
function openFCKeditor(elementName)
{
  eleTextArea=document.getElementById(elementName);
  fckNewPopup = window.open('/FCKeditor/core/html/index.html','fckEditor');
  fckNewPopup.focus();
  setTimeout('initFCK()',2000);
}
function initFCK()
{
  fckNewPopup.init(eleTextArea.value);
}	
function saveHtml(newContent)
{
  eleTextArea.value=newContent;
}

function removeHtmlTab(string)
{
	return string.replace(/(<([^>]+)>)/ig,"");
}

//Add by Andy 2009-7-23 For Organizer
function setPosition(obj,divObj,oEvent,leftPosition, topPosition){
	
	 if (divObj.style.left < "0px"){
			//Show
    	divObj.style.top = topPosition;
    	
    	//divObj.style.left = oEvent.clientX - 80;                                
    	divObj.style.left = leftPosition;
    }else{
  		//hidden
    	divObj.style.top = -500;
    	divObj.style.left = -500;
    }
  
}
/* Added by Jimmy Iu @ 29 Jul 2009 - START */
function prudentialnetwork(){
	var url = document.getElementById('ddlPruNetwork').value;
	if (url != null && url != "") {
		var prunetwork =window.open(url, "PruNetwork", "width=1024,height=680,menubar=yes,status=yes,scrollbars=yes,resizable=yes,toolbar=yes,location=yes");
		prunetwork.focus();
	}
};

function getQueryString(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
			if (ft[0] == ji) {
				return ft[1];
		}
	}
}

// StringUtil
function StringUtil() { }
StringUtil.format = function( text )
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there��s nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi"), arguments[ token + 1 ] );
    }
    return text;
};
/* Added by Jimmy Iu @ 29 Jul 2009 - END */
/* Added by Jimmy Iu @ 20 Nov 2009 - START */
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
/* Added by Jimmy Iu @ 20 Nov 2009 - END */
