﻿
// Function to get reference for element (using getElementById in case el is string)
function $(el) { if(isString(el)) return document.getElementById(el); else return el; }

// Create a javascript class, where constructor uses init
function createClass() {
	return function(args) {
		this.init(args);
    }
}

// Extend a javascript class, while storing a reference for its parent instance 
function extendClass(baseClass) {
	return function(args) {
		this.init(args);
	    args = isDefined(this.baseParameters) ?getValue(this.baseParameters) :args;
		this.base = new baseClass(args);
	}
}

// Create a singleton class
function createSingleton(klass) {
	klass = createClass();
	klass.instance = null;
	klass.getInstance = function(args) {
		if(klass.instance == null) {
			klass.instance = new klass(args);
		}
		return klass.instance;
	};
	return klass;
}

// Determine whether value is initialized
function isDefined(val) {
	return typeof(val) != "undefined" && val != null;
}

// Determine whether value is empty
function isEmpty(val) {
	if(isDefined(val))
		return val.length == 0;
	else
		return true;
}

// Determine whether value is a string 
function isString(val) {
	return typeof(val) == "string";
}

// Determine whether value is a number/numeric
function isNumeric(val) {
	return typeof(val) == "number";
}

// Determine whether value is a array
function isArray(val) {
	return typeof(val) == "object" && typeof(val.length) != "undefined";
}

// Determine whether value is hashed object
function isHashed(val) {
	return typeof(val) == "object" && typeof(val.length) == "undefined";
}

// Determine whether value is a function
function isFunction(val) {
	return typeof(val) == "function";
}

// Determine whether value is a date or not
function isDate(val) {
    return val instanceof Date;
}

// Get value
function getValue(val, args) {
	if(isFunction(val))
		return val(args);
	else
		return val;
}

// Convert hashed to array
function convertToArr(ht) {
    if(!isArray(ht)) {
        var arr = new Array();
        for(var c in ht)
            arr.push(ht[c]);
        return arr;
    }
    else
        return ht;
}