﻿// Namespace
var IPCore = {};

// BROWSER INFO
IPCore.BrowserInfo = function()
{
    // Name
    //Get browser name
    this.Name = window.navigator.appName;
    
    //Check if browser name is Microsoft Internet Explorer
    this.IsInternetExplorer = (this.Name == 'Microsoft Internet Explorer');

    //Check if browser name is Netscape
    this.IsNetscape = (this.Name == 'Netscape');

    // Version
    //Get browser version as string
    this.VersionString = window.navigator.appVersion;

    //Get browser version as number
    this.VersionNumber = (parseFloat(this.VersionString));

    // Specific browser version 
    //Check if browser is Chrome
    this.IsChrome1 = (window.navigator.userAgent.indexOf('Chrome/1.', 0) != -1);

    //Check if browser is FireFox 3
    this.IsFireFox3 = (window.navigator.userAgent.indexOf('Firefox/3.', 0) != -1);

    //Check if browser is FireFox 3
    this.IsSafari4 = (window.navigator.userAgent.indexOf('Safari/531.', 0) != -1);

    //Check if browser is Microsft Internet Explorer 6
    this.IsInternetExplorer6 = (this.IsInternetExplorer && window.navigator.userAgent.indexOf('MSIE 6.', 0) != -1); ;

    //Check if browser is Microsft Internet Explorer 7
    this.IsInternetExplorer7 = (this.IsInternetExplorer && window.navigator.userAgent.indexOf('MSIE 7.', 0) != -1); ;
}
var Browser = new IPCore.BrowserInfo();

// REQUEST 
IPCore.Request = function()
{
}
IPCore.Request.prototype.GetQueryStringValue = function(key)
{
    if (key)
    {
        key = key.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
        var regExp = new RegExp('[\\?&]' + key + '=([^&#]*)');
        var qs = regExp.exec(window.location.href);

        if (qs == null)
        {
            return null;
        }
        else
        {
            return qs[1];
        }
    }
    else
    {
        throw new Error('No QueryString key specified.');
    }
}
var Request = new IPCore.Request();

// STRING TRIM FUNCTIONS
String.prototype.Trim = function(charToTrim)
{
    if (!charToTrim)
    {
        charToTrim = '\\s';
    }
    return this.replace(new RegExp('^' + charToTrim + '+|' + charToTrim + '+$', 'g'), '');
}
String.prototype.TrimStart = function(charToTrim)
{
    if (!charToTrim)
    {
        charToTrim = '\\s';
    }
    return this.replace(new RegExp('^' + charToTrim + '+'), '');
}
String.prototype.TrimEnd = function(charToTrim)
{
    if (!charToTrim)
    {
        charToTrim = '\\s';
    }
    return this.replace(new RegExp(charToTrim + '+$'), '');
}
// STRING COMPARE FUNCTIONS
String.prototype.StartsWith = function(value)
{
    return new RegExp('^' + value).test(this);
}

// STRING PADDING FUNCTIONS
String.prototype.PadLeft = function(totalWidth, paddingChar)
{
    var re = new RegExp('.{' + totalWidth + '}$');
    var pad = '';

    if (!paddingChar)
    {
        paddingChar = ' ';
    }

    do
    {
        pad += paddingChar;
    } while (pad.length < totalWidth);

    return re.exec(pad + this)[0];
}
String.prototype.PadRight = function(totalWidth, paddingChar)
{
    var re = new RegExp('^.{' + totalWidth + '}');
    var pad = '';

    if (!paddingChar)
    {
        paddingChar = ' ';
    }

    do
    {
        pad += paddingChar;
    } while (pad.length < totalWidth);

    return re.exec(this + pad)[0];
}
// STRING VALIDATION FUNCTIONS
String.IsNullOrEmpty = function(value)
{
    var result = value == null;
    if (!result)
    {
        if (value == '')
        {
            if (typeof (value) == 'string') //needed to filter out 0 (zero)
            {
                result = true;
            }
        }
    }
    return result;
}
String.prototype.IsNumeric = function()
{
    return /^[\-+\.\d]+$/.test(this);
}
String.prototype.IsDecimal = function(allowEmpty, decimals)
{
    var result = false;
    if (!allowEmpty)
    {
        allowEmpty = false;
    }

    if (!decimals)
    {
        decimals = 2;
    }

    if (String.IsNullOrEmpty(this) && allowEmpty)
    {
        result = true;
    }
    else
    {
        if (this.IsNumeric())
        {
            result = new RegExp('^[-+\.]?(?:\\d+)(\\.\\d{0,' + decimals + '})?$').test(this);
        }
    }

    return result;
}
String.prototype.IsInt64 = function(allowEmpty)
{
    var result = false;
    if (!allowEmpty)
    {
        allowEmpty = false;
    }

    if (String.IsNullOrEmpty(this) && allowEmpty)
    {
        result = true;
    }
    else
    {
        if (this.IsNumeric())
        {
            result = /^[-+]?\d+$/.test(this);

            if (result)
            {
                var MAX_VALUE = 922337203685477500;
                var MIN_VALUE = -922337203685477500;
                var integer = parseInt(this);

                if (integer < MIN_VALUE || integer > MAX_VALUE)
                {
                    result = false;
                }
            }
        }
    }

    return result;
}
String.prototype.IsInt32 = function(allowEmpty)
{
    var result = false;
    if (!allowEmpty)
    {
        allowEmpty = false;
    }

    if (String.IsNullOrEmpty(this) && allowEmpty)
    {
        result = true;
    }
    else
    {
        if (this.IsInt64())
        {
            var MAX_VALUE = 2147483647;
            var MIN_VALUE = -2147483648;
            var integer = parseInt(this);

            if (integer < MIN_VALUE || integer > MAX_VALUE)
            {
                result = false;
            }
        }
    }

    return result;
}
String.prototype.IsInt16 = function(allowEmpty)
{
    var result = false;
    if (!allowEmpty)
    {
        allowEmpty = false;
    }

    if (String.IsNullOrEmpty(this) && allowEmpty)
    {
        result = true;
    }
    else
    {
        if (this.IsInt32())
        {
            var MAX_VALUE = 32767;
            var MIN_VALUE = -32768;
            var integer = parseInt(this);

            if (integer < MIN_VALUE || integer > MAX_VALUE)
            {
                result = false;
            }
        }
    }

    return result;
}
//format must be in the form of: dd/mm/yyyy
String.prototype.IsDate = function(allowEmpty)
{
    var result = false;
    if (!allowEmpty)
    {
        allowEmpty = false;
    }

    if (String.IsNullOrEmpty(this) && allowEmpty)
    {
        result = true;
    }
    else
    {
        var Objs = this.split("/");

        if (Objs.length == 3)
        {
            var Year = 0;
            var Month = 0;
            var Day = 0;

            if (Objs[0].IsNumeric())
            {
                Day = Objs[0];
            }

            if (Objs[1].IsNumeric())
            {
                Month = Objs[1];
            }

            if (Objs[2].IsNumeric())
            {
                Year = Objs[2];
            }

            if ((Year > 1899) && (Year < 3000) && (Month > 0) && (Month < 13) && (Day > 0) && (Day < 32))
            {
                if (Month < 8)
                {
                    if (Month % 2 == 0)
                    {
                        if (Month == 2)
                        {
                            if (Year % 4 == 0)
                            {
                                if (Day < 30)
                                    result = true
                            }
                            else
                            {
                                if (Day < 29)
                                    result = true
                            }
                        } //if (Month == 2)
                        else
                        {
                            if (Day < 31)
                                result = true
                        }
                    } //if (Month % 2 == 0)
                    else
                    {
                        if (Day < 32)
                            result = true
                    }
                } //if (Month < 8)
                else
                {
                    if (Month % 2 == 0)
                    {
                        if (Day < 32)
                            result = true;
                    } //if (Month % 2 == 0)
                    else
                    {
                        if (Day < 31)
                            result = true
                    }
                }
            }
        }
    }

    return result;
}
String.prototype.IsValidIdentity = function()
{
    var result = true;
    if (String.IsNullOrEmpty(this))
    {
        result = false;
    }
    else
    {
        if (this.length == 13)
        {
            var PartialIDnumber = this.substring(0, 12);
            if (PartialIDnumber.length == 12)
            {
                var a = 0;
                for (var i = 0; i < 6; i++)
                {
                    a += parseInt(PartialIDnumber.charAt(2 * i));
                }
                var b = 0;
                for (var i = 0; i < 6; i++)
                {
                    b = b * 10 + parseInt(PartialIDnumber.charAt(2 * i + 1));
                }
                b *= 2;
                var c = 0;
                do
                {
                    c += b % 10;
                    b = parseInt(b / 10);
                }
                while (b > 0);
                c += a;
                var d = 10 - (c % 10);
                if (d == 10) d = 0;

                if (d.toString() != this.substr(12, 1))
                {
                    result = false;
                }

                var digit = this.charAt(11);
                if (digit != '8' && digit != '9')
                {
                    result = false;
                }
            }
            else
            {
                result = false;
            }
        }
        else
        {
            result = false;
        }
    }

    return result;
}
// GENERIC FUNCTIONS
function GetClientSize(targetDocument)
{
    if (!targetDocument)
    {
        targetDocument = document;
    }

    var Size = function()
    {
        this.Width = 0;
        this.Height = 0;
    }
    Size.Width = 0;
    Size.Height = 0;

    var win = targetDocument.parentWindow ? targetDocument.parentWindow : targetDocument.defaultView;

    if (typeof (win.innerWidth) == 'number')
    {
        //Non-IE
        Size.Width = win.innerWidth;
        Size.Height = win.innerHeight;
    }
    else if (targetDocument.documentElement && (targetDocument.documentElement.clientWidth || targetDocument.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        Size.Width = targetDocument.documentElement.clientWidth /*+ parseFloat(targetDocument.body.leftMargin)*/ + parseFloat(targetDocument.body.rightMargin);
        Size.Height = targetDocument.documentElement.clientHeight /*+ parseFloat(targetDocument.body.topMargin)*/ + parseFloat(targetDocument.body.bottomMargin);
    }
    else if (targetDocument.body && (targetDocument.body.clientWidth || targetDocument.body.clientHeight))
    {
        //IE 4 compatible
        Size.Width = targetDocument.body.clientWidth /*+ parseFloat(targetDocument.body.leftMargin)*/ + parseFloat(targetDocument.body.rightMargin);
        Size.Height = targetDocument.body.clientHeight /*+ parseFloat(targetDocument.body.topMargin)*/ + parseFloat(targetDocument.body.bottomMargin);
    }
    return Size;
}

function GetClientScrollOffset(targetDocument)
{
    if (!targetDocument)
    {
        targetDocument = document;
    }

    var ScrollOffset = function()
    {
        this.X = 0;
        this.Y = 0;
    }
    ScrollOffset.X = 0;
    ScrollOffset.Y = 0;

    if (typeof (window.pageYOffset) == 'number')
    {
        //Netscape compliant
        ScrollOffset.Y = window.pageYOffset;
        ScrollOffset.X = window.pageXOffset;
    }
    else if (targetDocument.body && (targetDocument.body.scrollLeft || targetDocument.body.scrollTop))
    {
        //DOM compliant
        ScrollOffset.Y = targetDocument.body.scrollTop;
        ScrollOffset.X = targetDocument.body.scrollLeft;
    }
    else if (targetDocument.documentElement && (targetDocument.documentElement.scrollLeft || targetDocument.documentElement.scrollTop))
    {
        //IE6 standards compliant mode
        ScrollOffset.Y = targetDocument.documentElement.scrollTop;
        ScrollOffset.X = targetDocument.documentElement.scrollLeft;
    }
    return ScrollOffset;
}

