﻿/*
String Extension Methods
*/

String.prototype.isAlpha = function()
{
    return (this.match(/[A-Za-z]/g) ? true : false);
}

String.prototype.isNumeric = function()
{
    return (this.match(/^\d+$/) ? true : false);
}

String.prototype.isSpecial = function()
{
    return (this.match(/[^A-Za-z0-9]/g) ? true : false);
}

String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g, '');
}

String.prototype.ltrim = function()
{
    return this.replace(/^\s+/, "");
}

String.prototype.rtrim = function()
{
    return this.replace(/\s+$/, "");
}

String.prototype.isEmail = function()
{
    var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    var matches = rx.exec(this);
    return (matches != null && this == matches[0]);
}

String.prototype.isURL = function()
{
    var rx = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-\\+ ./?%:&=#\\[\\]]*)?");
    var matches = rx.exec(this);
    return (matches != null && this == matches[0]);
}

String.prototype.contains = function(t)
{
    return this.indexOf(t) >= 0 ? true : false;
}

String.prototype.beginsWith = function(t, i)
{
    if (i == false)
    {
        return (t == this.substring(0, t.length));
    }
    else
    {
        return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
    }
}

String.prototype.endsWith = function(t, i)
{
    if (i == false)
    {
        return (t == this.substring(this.length - t.length));
    }
    else
    {
        return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());
    }
}

/* 
Checks a string is a valid date.  
Allowed formats are:
dd/mm/yyyy
mm/dd/yyyy
*/
String.prototype.isValidDate = function(dateFormat)
{
    var delimiter = dateFormat.substring(2, 3);
    var ddStart = dateFormat.substring(0, 2) == "dd" ? true : false;

    var datePartPattern = "^([0-9]{2})" + delimiter + "([0-9]{2})" + delimiter + "([0-9]{4})$";

    var ddPos = 1;
    var mmPos = 2;
    var yyPos = 3;

    if (!ddStart)
    {
        ddPos = 2;
        mmPos = 1;
    }

    var IsoDateRe = new RegExp(datePartPattern);
    var matches = IsoDateRe.exec(this);
    if (!matches) return false;

    var ddVal = matches[ddPos];
    var mmVal = matches[mmPos];
    var yyVal = matches[yyPos];

    var composedDate = new Date(yyVal, (mmVal - 1), ddVal);

    return (
        (composedDate.getMonth() == parseInt(mmVal - 1)) &&
        (composedDate.getDate() == parseInt(ddVal)) &&
        (composedDate.getFullYear() == parseInt(yyVal))
    );
}

String.prototype.isMod10 = function()
{
    var valid = "0123456789"
    var len = this.length;
    var bNum = true;
    var iCCN = this;
    var sCCN = this;
    var iCCN;
    var iTotal = 0;
    var bResult = false;
    var digit;
    var temp;
    iCCN = sCCN.replace(/^\s+|\s+$/g, '');      // strip spaces
    for (var j = 0; j < len; j++)
    {
        temp = "" + iCCN.substring(j, j + 1);
        if (valid.indexOf(temp) == "-1")
        {
            bNum = false;
        }
    }

    if (!bNum)
    {
        return false;
    }

    iCCN = parseInt(iCCN);
    if (len == 0)
    {                                           /* nothing, field is blank */
        bResult = true;
    }
    else
    {
        if (len >= 15)                           //15 or 16 for Amex or V/MC
        {
            for (var i = len; i > 0; i--)
            {
                digit = "digit" + i;
                calc = parseInt(iCCN) % 10;     //right most digit
                calc = parseInt(calc);
                iTotal += calc; 	            //parseInt(cardnum.charAt(count))i:\t" + calc.toString() + " x 2 = " + (calc *2) +" : " + calc2 + "\n";

                i--;
                digit = "digit" + i;

                iCCN = iCCN / 10; 	            // subtracts right most digit from ccNum
                calc = parseInt(iCCN) % 10; // step 1 double every other digit
                calc2 = calc * 2;

                switch (calc2)
                {
                    case 10: calc2 = 1; break; //5*2=10 & 1+0 = 1
                    case 12: calc2 = 3; break; //6*2=12 & 1+2 = 3
                    case 14: calc2 = 5; break; //7*2=14 & 1+4 = 5
                    case 16: calc2 = 7; break; //8*2=16 & 1+6 = 7
                    case 18: calc2 = 9; break; //9*2=18 & 1+8 = 9
                    default: calc2 = calc2; 	//4*2= 8 &   8 = 8  -same for all lower numbers
                }

                iCCN = iCCN / 10; 	            // subtracts right most digit from ccNum
                iTotal += calc2;
            }

            if ((iTotal % 10) == 0)
            {
                bResult = true;
            }
            else
            {
                bResult = false;
            }
        }
    }

    return bResult;
}

/*
Number Extension Methods
*/