﻿//============================================================================
//  CwikStrings.js
//----------------------------------------------------------------------------
//  Copyright © 2008 Sovenix, Inc.  All rights reserved.
//  Updated August 7, 2008
//============================================================================

String.prototype.left = function (i)
{
    return this.substring (0, i);
}

String.prototype.formatCurrency = function ()
{
    var i = parseFloat (this.replace(/[\,\$]/g, ""));
    var s = new String ();

    if (isNaN (i))
    {
        i = 0.0;
    }
    
    s = (i < 0) ? "-" : "";
    
    i = new String (parseInt ((Math.abs (i) + 0.005) * 100) / 100);
    
    if (i.indexOf (".") < 0)
    {
        i = i + ".00";
    }
    
    else if (i.indexOf (".") == (i.length - 2))
    {
        i = i + "0";
    }
    
    return s + i;
}

String.prototype.formatPhoneNumber = function (phoneFormats, phonePrefix)
{
    var exps = new CwikExpressions (phoneFormats);
    var exp;
    
    var temp = this.getBasePhone (phonePrefix);
    
    if (temp != "")
    {
        if (exp = exps.getMatch (temp))
        {
            temp = exp.replace (temp);
        }  
    }
    
    return temp;
}

String.prototype.formatPostalCode = function (postalFormats)
{
    var exps = new CwikExpressions (postalFormats);
    var exp;
    
    var temp = this.getAlphaNumerics ();
    
    if (temp != "")
    {
        if (exp = exps.getMatch (temp))
        {
            temp = exp.replace (temp);
        }
    }
    
    return temp;
}

String.prototype.getAlphaNumerics = function ()
{
    return this.replace (/\W+/g, "");
}

String.prototype.getBasePhone = function (prefix)
{
    var s = this.getAlphaNumerics ();
    
    s = s.replace (/^(0|1)+/, "");
    s = s.replace (new RegExp ("^" + prefix), "");
    
    return s;
}

String.prototype.isInteger = function ()
{
    return this.trim ().search (/^\-?\d+$/) != (-1);
}

String.prototype.isValidCurrency = function ()
{
    var i = this.replace(/[\,\$]/g, "");
    
    return isNaN (parseFloat (i)) ? false : true;
}

String.prototype.isValidDateTime = function ()
{
    return isNaN (Date.parse (this)) ? false : true;
}

String.prototype.isValidDomain = function ()
{
    return this.search (/^(\w([\w\-]*\w)?\.)+(\w{2}|com|edu|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\.?$/) != (-1);
}

String.prototype.isValidEmail = function ()
{
    var s = this.split("@");
    
    if (s.length != 2)
        return false;
    if (s[1].isValidDomain () == false)
        return false;
    else
        return s[0].search (/^\w+([\.\-]\w+)*$/) != (-1);
}

String.prototype.isValidPhoneNumber = function (phoneFormats, phonePrefix)
{
    var exps = new CwikExpressions (phoneFormats);
    var exp;
    
    var temp = this.getBasePhone (phonePrefix);
    
    if (temp != "")
    {
        if (exp = exps.getMatch (temp))
        {
            return true;
        }  
    }
    
    return false;
}

String.prototype.isValidPostalCode = function (postalFormats)
{
    var exps = new CwikExpressions (postalFormats);
    var exp;
    
    var temp = this.getAlphaNumerics ();
    
    if (temp != "")
    {
        if (exp = exps.getMatch (temp))
        {
            return true;
        }
    }
    
    return false;
}

String.prototype.pad = function (length, character, left, clip)
{
    var result = this;
    
    while (result.length < length)
    {
        if (left)
            result = character + result;
        else
            result = result + character;
    }
    
    if ((result.length > length) && (clip))
    {
        if (left)
            result = result.right (length);
        else
            result = result.left (length);
    }
    
    return result;
}

String.prototype.replaceAll = function (find, replace)
{
    return this.split (find).join (replace);
}

String.prototype.right = function (i)
{
    return this.substring (this.length - i);
}

String.prototype.trim = function ()
{
    return this.replace (/(^\s*)|(\s*$)/g, "");
}

