﻿function rol_addCommas(obj) {
    var nStr = obj.value.replace(/,/g, "");
    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');
    }
    obj.value = x1 + x2;
}

function rol_keyDownNumeric(event) {
    
    var curChar = "";
    if (window.event) {
        //backspace/tab/delete
        if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 46)
            return;
        curChar = String.fromCharCode(event.keyCode);
    }
    else {
        curChar = String.fromCharCode(event.which);
        //firefox requires 0 for tab
        if (event.which == 8 || event.which == 9 || event.which == 46 || event.which == 0)
            return;
    }
    var result = curChar.match('^[0-9]+$');
    if (!result) {
        if (window.event) {
            event.returnValue = false;
            event.cancel = true;
        }
        else {
            event.preventDefault();
            return;
        }
    }
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    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);
}

function formatCurrencyNoDollar(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    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);
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function replaceAll(text, strA, strB) {
    while (text.indexOf(strA) != -1) {
        text = text.replace(strA, strB);
    }
    return text;
}
function newWindow(mypage, myname, w, h, features) {
    try {
        if (screen.width) {
            var winl = (screen.width - w) / 2;
            var wint = (screen.height - h) / 2;
        } else { winl = 0; wint = 0; }
        if (winl < 0) winl = 0;
        if (wint < 0) wint = 0;
        var settings = 'height=' + h + ',';
        settings += 'width=' + w + ',';
        settings += 'top=' + wint + ',';
        settings += 'left=' + winl + ',';
        settings += features;
        win = window.open(mypage, myname, settings);
        win.window.focus();
    }
    catch (e) {
        window.status = e.message
    }
}

var visits = 0;

function extractCookieValue(val) {
  if ((endOfCookie = document.cookie.indexOf(";", val)) == -1) {
     endOfCookie = document.cookie.length;
  }
  return unescape(document.cookie.substring(val,endOfCookie));
}

function ReadCookie(cookiename) {
  var numOfCookies = document.cookie.length;
  var nameOfCookie = cookiename + "=";
  var cookieLen = nameOfCookie.length;
  var x = 0;
  while (x <= numOfCookies) {
        var y = (x + cookieLen);
        if (document.cookie.substring(x, y) == nameOfCookie)
           return (extractCookieValue(y));
           x = document.cookie.indexOf(" ", x) + 1;
           if (x == 0){
              break;
           }
  }
  return (null);
}

function createCookie(name, value, expiredays) {
  var todayDate = new Date();
  todayDate.setDate(todayDate.getDate() + expiredays);
  document.cookie = name + "=" + value + "; expires=" +todayDate.toGMTString() + ";"
}

function showHits() {
  userCookie = ReadCookie("_visitSite");
  if (userCookie == null)
     visits = 1;
  else
     visits = parseInt(userCookie) + 1;
     createCookie("_visitSite", visits, 30);
     document.write("You have visited this site on <b>" +visits + "</b> occasions<hr>");
}

 function addOption(strText,strVal,obj)
        {
          var elOptNew = document.createElement('option');
          elOptNew.text = strText;
          elOptNew.value = strVal;
          try {
            obj.add(elOptNew, null); // standards compliant; doesn't work in IE
          }
          catch(ex) {
            obj.add(elOptNew); // IE only
          }
        }
