﻿function formatNumber (obj, decimal) 
{ 
     //decimal  - the number of decimals after the digit from 0 to 3 
     //-- Returns the passed number as a string in the xxx,xxx.xx format. 
       anynum=eval(obj.value); 
       divider =10; 
       switch(decimal)
       { 
            case 0: 
                divider =1; 
                break; 
            case 1: 
                divider =10; 
                break; 
            case 2: 
                divider =100; 
                break; 
            default:       //for 3 decimal places 
                divider =1000; 
        }  
  
       workNum=Math.abs((Math.round(anynum*divider)/divider)); 
  
       workStr=""+workNum 
  
       if (workStr.indexOf(".")==-1){workStr+="."} 
  
       dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0 
       pStr=workStr.substr(workStr.indexOf(".")) 
  
       while (pStr.length-1< decimal){pStr+="0"} 
  
       if(pStr =='.') pStr =''; 
  
       //--- Adds a comma in the thousands place.    
       if (dNum>=1000) { 
          dLen=dStr.length 
          dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen) 
       }  
  
       //-- Adds a comma in the millions place. 
       if (dNum>=1000000) { 
          dLen=dStr.length 
          dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen) 
       } 
       retval = dStr + pStr 
       //-- Put numbers in parentheses if negative. 
       if (anynum<0) {retval="("+retval+")";} 
  
    //You could include a dollar sign in the return value. 
      //retval =  "$"+retval 
      obj.value = retval; 
}

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; 
}

function specialAlert(){
    alert('specialAlert');
}

function OnGetSelectedReportValues(selectedValues) {
    // ... Implement required actions ...
    // Example:
    var numTotal = 0;
    if (selectedValues.length > 0) {
        for (i = 0; i < selectedValues.length; i++) {
            numTotal += selectedValues[i]
        }
    }
    var ccy = hfBudget.Get("CurrencySymbol")
    var numBudgetRem = new Number(Number(hfBudget.Get("ExternalBudget")) - numTotal);
    var numBudgetPRRem = new Number(Number(hfBudget.Get("ExternalProRataBudget")) - numTotal);
    lblRemainingAnnualBudgetValue.SetText(ccy + addCommas(numBudgetRem.toFixed(0))); 
    try {
        lblRemainingProRataBudgetValue.SetText(ccy + addCommas(numBudgetPRRem.toFixed(0))); 
    }
    catch (err) {
    }
}

function GetSelectionInfo(inputElement) {
    var start, end;
    if (ASPxClientUtils.ie) {
        var range = document.selection.createRange();
        var rangeCopy = range.duplicate();
        range.move('character', -inputElement.value.length);
        range.setEndPoint('EndToStart', rangeCopy);
        start = range.text.length;
        end = start + rangeCopy.text.length;
    } else {
        start = inputElement.selectionStart;
        end = inputElement.selectionEnd;
    }
    return { startPos: start, endPos: end };
}

var sessionTimeout = 20;  //"<%= Session.Timeout %>";
var sessionStart = new Date().getTime();  //21 //"<%= Session.Timeout %>";
function DisplaySessionTimeout() {
    if (typeof (lblSessionTime) == 'undefined') return; // label not visible on form
    //lblSessionTime.SetText(""); 
    //return;
    //assigning minutes left to session timeout to Label
    //document.getElementById("<%= lblSessionTime.ClientID %>").innerText = sessionTimeout;
    //sessionTimeout = sessionTimeout - 1;
    var elapsed = parseInt((new Date().getTime() - sessionStart) / 60000);
    lblSessionTime.SetText("[NOTE: There are " + (sessionTimeout - elapsed) + " minutes until the current Session times out.]");
    //if session is not less than 0
    if (elapsed >= 0) {
    //call the function again after 1 minute delay
        window.setTimeout("DisplaySessionTimeout()", 60000);
        var d = new Date(); 
        var startTime = d.getTime();
        if ((sessionTimeout - elapsed) == 1) {
            if (confirm("There has been no activity for some time.\nClick 'OK' if you wish to continue your session,\nor click 'Cancel' to log out.\nIf you are unable to respond to this message\nwithin 1 minute you will be logged out automatically.")) {
                //post the page to itself
                d = new Date(); 
                var endTime = d.getTime();
                var diff = endTime - startTime;
//                alert("startTime = " + startTime + "\nendTime = " + endTime + "\ndiff = " + diff);
                if (diff > 60000) {
                    alert("The Simulator session has timed out so you must login again to continue.");
                    document.location.href = getPage();//"Login.aspx";
                }
                else {
                    document.location.href = getPage();
                }
            }
            else {
                document.location.href = getPage();//"Login.aspx";
            }
        }
    }
    else {
        //show message box
        alert("The Simulator session has timed out so you must login again to continue.");
    }
}

function getPage(){   
    var url = window.location.pathname;   
    // reads the last 'slash' and takes text after it - as a pagename   
    return url.substring(url.lastIndexOf('/') + 1);   
}

