﻿/*
* Is JavaScript Library v1.0
*
* Copyright (c) 2010 Danny Rendle
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Date: 2010-02-17 12:43:10 -0500 (Wed, 17 Feb 2010)
* Revision: 9
*
* Usage: if(Is.EmailAddress(email)) */

(function() {
    if (window.Is)
        return;
    /* helpers */
    function parseDate(s, d) {
        d = d || "/"; // default to /
        var re = new RegExp("^(\\d{1,2})" + d + "(\\d{1,2})" + d + "(\\d{4})$");
        var parts = re.exec(s);
        if (parts.length != 4) {
            return NaN;
        }
        else {
            return new Date(parseInt(parts[3], 10), parseInt(parts[2], 10) - 1, parseInt(parts[1], 10));
        }
    }
    function trim(s) {
        return s.replace(/^\s+/, "").replace(/\s+$/, "");
    }
    window.Is = {
        OnlyNumbers: function(s) {
            // Only digits
            return /^\d*$/.test(s);
        }
        , OnlyLetters: function(s) {
            // Only characters a to z
            return /^[a-z]*$/i.test(s);
        }
        , Name: function(s) {
            // Only name valid characters
            return /^[-a-z\']*$/i.test(s);
        }
        , PhoneNumber: function(s) {
            // Optional +XX followed by an optional space, followed by an optional bracketed set of numbers, followed by any combination of spaces and numbers
            return /^\+?\d* ?(\(\d*\))?[ \d]*$/.test(s);
        }
        , UKPostcode: function(s) {
            return /^([a-z]{1,2}\d{1,2}[a-z]?|[G][I][R]) ?\d[a-z]{2}$/i.test(s);
        }
        , EmailAddress: function(s) {
            return /^[a-z0-9][^\(\)\<\>\@\,\;\:\\\"\[\]]*\@[a-z0-9][a-z0-9\-\.]*\.[a-z]{2,}$/i.test(s); // thanks to visibone browser book ?+ À-ßà-ÿ ???
        }
        , Currency: function(s, sy) { // s = string, sy = currency symbol
            sy = sy || "£"; // default to £
            return new RegExp("^" + sy + "?\\d+\\.\\d{1,2}$").test(s); // symbol is optional
        }
        , Date: function(date) {
            var intDay, intMonth, intYear, re, re1, aryParts;
            re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;

            if (trim(date).length == 0 || !re.test(date)) {
                return false;
            }
            else {
                re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
                re1 = /^0/;
                aryParts = re.exec(date);
                intDay = parseInt(aryParts[1].replace(re1, ''));
                intMonth = parseInt(aryParts[2].replace(re1, ''));
                intYear = parseInt(aryParts[3].replace(re1, ''));
                re = /^0/;

                function newDate(dteYear) {
                    this.m1 = this.m3 = this.m5 = this.m7 = this.m8 = this.m10 = this.m12 = 31;
                    this.m4 = this.m6 = this.m9 = this.m11 = 30;
                    this.m2 = (dteYear % 4 == 0) ? ((dteYear % 100 == 0) ? ((dteYear % 1000 == 0) ? 29 : 28) : 29) : 28;
                }

                dteDate = new newDate(intYear);
                return (intDay <= dteDate['m' + intMonth.toString().replace(re, '')]);
            }
        }
        , FutureDate: function(date, delimiter) {
            var dte = parseDate(date, delimiter);
            return !isNaN(dte) && (dte.valueOf() > new Date().valueOf());
        }
    };
})();