// *******************************************************************
// cdsUtilities.js
//
// Version 2.0
// Copyright (c) 2008 Catalog Data Solutions. All Rights Reserved.
//
// Utility classes for CDS javascript applications
// *******************************************************************

// -----------------------------------------------
// Namespace
//
// Class implementing namespaces and packages for CDS
// Syntax: cds.namespace("package1.package2.etc");
// -----------------------------------------------

// Creates root package
if (typeof cds == "undefined" || !cds) {
	var cds = {};
}

// Creates packages for the given namespace path
// NOTE: Reserved words don't work for package names in some browsers.
cds.namespace = function(packageName) {
	var plist = packageName.split(".");
	var o = cds;

	for (var i = 0; i < plist.length; i++) {
		if (plist[i] == "cds") continue;
		if (o[plist[i]] == null) o[plist[i]] = {};
		o = o[plist[i]];
	}

	return o;
};

// Posts a form with action=to and all inputs from obj
function sendPost(to, obj, target, method) {
    var f = document.createElement("form");
    f.method = (method == null) ? "post" : method;
    f.action = to;
    if (target != null) f.target = target;
    for (var k in obj) {
        if (obj[k] instanceof Array) {
            for (var i = 0; i < obj[k].length; i++) {
                var inp = document.createElement("input");
                f.appendChild(inp);
                inp.setAttribute("name", k);
                inp.setAttribute("value", obj[k][i]);
            }
        } else {
            var inp = document.createElement("input");
            f.appendChild(inp);
            inp.setAttribute("name", k);
            inp.setAttribute("value", obj[k]);
        }
    }
    document.body.appendChild(f);
    f.submit();
    document.body.removeChild(f);
}

function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            var endstr = document.cookie.indexOf(";", j);
            if (endstr == -1) endstr = document.cookie.length;
            return unescape(document.cookie.substring(j, endstr));
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }

    return null;
}

// Trims leading and trailing whitespace from a string
String.prototype.trim = function () {
	return this.replace(/^\s+|\s+$/g, "");
};

String.prototype.entityify = function() {
	return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;").replace(/\'/g, "&apos;");
};

function typeOf(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 isEmpty(o) {
	var i, v;
	if (typeOf(o) === 'object') {
		for (i in o) {
			v = o[i];
			if (v !== undefined && typeOf(v) !== 'function') {
				return false;
			}
		}
	}
	return true;
}

String.prototype.quote = function () {
	var c, i, l = this.length, o = '"';
	for (i = 0; i < l; i += 1) {
		c = this.charAt(i);
		if (c >= ' ') {
			if (c === '\\' || c === '"') {
				o += '\\';
			}
			o += c;
		} else {
			switch (c) {
			case '\b':
				o += '\\b';
				break;
			case '\f':
				o += '\\f';
				break;
			case '\n':
				o += '\\n';
				break;
			case '\r':
				o += '\\r';
				break;
			case '\t':
				o += '\\t';
				break;
			default:
				c = c.charCodeAt();
				o += '\\u00' + Math.floor(c / 16).toString(16) +
					(c % 16).toString(16);
			}
		}
	}
	return o + '"';
};

String.prototype.supplant = function (o) {
	return this.replace(/{([^{}]*)}/g,
		function (a, b) {
			var r = o[b];
			return typeof r === 'string' || typeof r === 'number' ? r : a;
		}
	);
};


