/*
    Utility objects and functions
    version 1.0
    gpeters@gcpeters.com
    07/31/01
    
    Copyright (c) 2001 Grant Peters.
    This code is free for use, and modify 
    with the precept that this copyright information
    remains with all implementations of this source code.
    
    Thank you, and enjoy.
*/

//Add to Object object
Object.prototype.equals = function(o) {
    return (this == o);
}

// Util Object
function Util(){}

Util.prototype.launchWindow = function (htmlSrc, name, dimension, windowProps, x, y) {
    var w = dimension.getWidth();
    var h = dimension.getHeight();
    var props = 'width=' + w + ',height=' + h;
    if(windowProps) props += ',' + windowProps;
    var mw = window.open(htmlSrc,name,props);
    mw.moveTo(x,y);
    return mw;
}

Util.prototype.launchWindowCentered = function (htmlSrc, name, dimension, windowProps) {
    var x = (screen.width / 2) - (dimension.getWidth() / 2);
    var y = (screen.height / 2) - (dimension.getHeight() / 2);
    return this.launchWindow(htmlSrc, name, dimension, windowProps, x, y);
}

// Dimension object
function Dimension(width,height) {
    this.width  = width;
    this.height = height;
}
Dimension.prototype.getWidth  = function(){return this.width;}
Dimension.prototype.getHeight = function(){return this.height;}

// Param object
function Param(name,value) {
    this.name = name;
    this.value = value;
}
Param.prototype.getName = function() {return this.name;}
Param.prototype.getValue = function() {return this.value;}

// Hashtable Object
function Hashtable() {
    this.items = new Array();
}

Hashtable.prototype.put = function(key,val) {
    this.items.push(new Param(key,val));
}

Hashtable.prototype.get = function(key) {
    for(var i=0;i<this.items.length;i++)
        if(this.items[i].getName() == key)
            return this.items[i].getValue();
}

Hashtable.prototype.remove = function(key) {
    if(this.containsKey(key)) {
        for(var i=0;i<this.items.length;i++) {
            if(key == this.items[i].getName()) {
                this.items.splice(i,1);
                return;
            }
        }
    }
}

Hashtable.prototype.containsKey = function(key) {
    for(var i=0;i<this.items.length;i++)
        if(this.items[i].getName() == key)
            return true;
    return  false;
}

Hashtable.prototype.containsValue = function(val) {
    for(var i=0;i<this.items.length;i++)
        if(this.items[i].getValue() == val)
            return true;
    return  false;
}

Hashtable.prototype.keys = function() {
    var ks = new Array();
    for(var i=0;i<this.items.length;i++)
        ks.push(this.items[i].getName());
    return ks;
}

Hashtable.prototype.values = function() {
    var values = new Array();
    for(var i=0;i<this.items.length;i++)
        values.push(this.items[i].getValue());
    return values;
}

Hashtable.prototype.size = function() {return this.items.length;}

// Add methods to String Object
String.prototype.toCharArray = function() {
    var chars = new Array();
    for(var i=0;i<this.length;i++)
        chars.push(this.charAt(i));
    return chars;
}

String.prototype.fromCharArray = function(arr) {
    var s = '';
    for(var i=0;i<arr.length;i++)
        s += arr[i];
    return s;
}

String.prototype.capitalize = function() {
    var first = this.substring(0,1).toUpperCase();
    var end = this.substring(1).toLowerCase();
    return first + end;
}

String.prototype.capitalizeAll = function() {
    var delim = (arguments.length == 0) ? ' ' : arguments[0];
    var s = '';
    var words = this.split(delim);
    for(var i=0;i<words.length;i++)
        s += words[i].capitalize() + ((i == words.length - 1) ? '' : ' ');
    return s;
}
