﻿function formatCurrency(num) 
{
    num = num.toString().replace(/[^0-9.]/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;
    var formattedNum = "";
    while (num.length > 3) {
        formattedNum = "," + num.substring(num.length - 3) + formattedNum;
        num = num.substring(0, num.length - 3);
    }
    formattedNum = num + formattedNum;
    return (((sign) ? '' : '-') + formattedNum + '.' + cents);
}

function isValidEmail(email)
{
    email = email.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    if (email.length == 0)
        return false;
        
    // This regex is ported from UtilityFunctions.cs
    var emailRegEx = /^[A-Za-z0-9][.]?(([A-Za-z0-9_%+?=][-]?)+[.]?)*[A-Za-z0-9]@[A-Za-z0-9]?([A-Za-z0-9]+[-\.])*[A-Za-z0-9]+\.[A-Za-z]{2,4}$/;
    return emailRegEx.test(email);
}

function truncate(str, num)
{
    if(str.length > num)
    {
        str = str.substring(0, num);
    }
    
    return str;
}
