﻿Cwo.RegisterNamespace('Cwo.Common');

function DebugObject(obj) {
    window.alert(JSON.stringify(obj).replace(/,/g, ',\n'));
}

Cwo.Common.SetCookie = function (c_name, value) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 365);
    document.cookie = c_name + '=' + value + ';expires=' + exdate.toGMTString();
};

Cwo.Common.GetCookie = function (c_name) {
    if (document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "="),
            c_end;
        if (c_start !== -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end === -1) {
                c_end = document.cookie.length;
            }
            return decodeURIComponent(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
};

function AddCommas(nStr) {
    nStr += '';
    var x = nStr.split('.'),
        x1 = x[0],
        x2 = x.length > 1 ? '.' + x[1] : '',
        rgx = /(\d+)(\d{3})/;

    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function FormatCurrency(val, ignorepence) {
    var amount = parseFloat(val), 
        num = 0;
    if (isNaN(amount)) {
        return 'NaN';
    } else {
        num = amount;
        if (ignorepence) {
            num = num.toFixed(0);
        } else {
            num = num.toFixed(2);
        }
        // AddCommas(num) now contains a currency formatted string,
        return AddCommas(num);
    }
}

// Accurately returns the type of an object, more detail than standard JS "typeof"
function CWOTypeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (typeof value.length === 'number' &&
                    !(value.propertyIsEnumerable('length')) &&
                    typeof value.splice === 'function') {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}

function Hashtable() {
    var i = 0;
    this.length = 0;
    this.Keys = [];
    this.Index = [];
    
    for (i = 0; i < arguments.length; i += 2) {
        if (typeof (arguments[i + 1]) !== 'undefined') {
            this.Keys[arguments[i]] = arguments[i + 1];
            this.length += 1;
        }
    }

    this.Remove = function (in_key) {
        var tmp_value;
        if (typeof (this.Keys[in_key]) !== 'undefined') {
            this.length -= 1;
            tmp_value = this.Keys[in_key];
            delete this.Keys[in_key];
        }
        return tmp_value;
    };

    this.GetItemAt = function (in_index) {
        return this.Keys[this.Index[in_index]];
    };

    this.GetItem = function (in_key) {
        return this.Keys[in_key];
    };

    this.Add = function (in_key, in_value) {
        if (typeof (in_value) !== 'undefined') {
            if (typeof (this.Keys[in_key]) === 'undefined') {
                this.length += 1;
            }
            this.Keys[in_key] = in_value;
            this.Index[this.length - 1] = in_key;
        }
        return in_value;
    };

    this.ContainsKey = function (in_key) {
        return typeof (this.Keys[in_key]) !== 'undefined';
    };
}

var CWO_Common_Loaded = true;
