// GENERAL METHODS

function ge(id) {
	return document.getElementById(id);
}
function openUrl(url) {
	window.location.href = url;
}
function newUrl(url,vars) {
	if(typeof(vars) == "undefined") vars = "";
	var w = window.open(url,"",vars);
}
function display(id,show) {
    if(id == null) return;
    if(typeof(id) != "string") { 
        id.style.display = show ? "" : "none";  
        return; 
    }
    var e = ge(id);
    if(e == null) return;
    e.style.display = show ? "" : "none";
}
function toggle(id) {
	if(id == null) return;
    if(typeof(id) != "string") {
		id.style.display = id.style.display == "" ? "none" : "";	
		return;
	}
	var e = ge(id);
    if(e == null) return;
    e.style.display = e.style.display == "" ? "none" : "";
}

// IMAGE OVER
function imageOver(img) {
	img.src = img.src.replace(".gif","-over.gif");
}

function imageOut(img) {
	img.src = img.src.replace("-over","");
}

// LIST CLASS

function List(multiple,data,delim) {
    this.mult = multiple == null ? false : multiple;    
    this.list = new Array();
    if(data != null) {
        if(delim == null) delim = ",";
        if(data.startsWith(delim)) data = data.substring(1);
        if(data.endsWith(delim)) data = data.substring(0,data.length-1);
        var a = data.split(delim);
        for(var i = 0; i < a.length; i++) this.add(a[i]);
    }
}
List.prototype.contains = function(s) {
    for(var i = 0; i < this.list.length; i++) {
        if(this.list[i] == s) return true;
    }
    return false;
}
List.prototype.add = function(s) {         
    if(s != null && (this.mult || !this.contains(s))) {
        this.list[this.list.length] = s;
    }            
}
List.prototype.remove = function(s) {
    var a = [];   
    for(var i = 0; i < this.list.length; i++) {
        if(this.list[i] != s) a[a.length] = this.list[i];
    }    
    this.list = a;
}
List.prototype.count = function(s) {
    return this.list.length;
}
List.prototype.csv = function() {
    return this.delim(",");
}
List.prototype.delim = function(delim) {
    var s = "";
    for(var i = 0; i < this.list.length; i++) {
        if(this.list[i] != "") {
            if(s != "") s += delim;
            s += this.list[i];
        }
    }
    return s;
}

// STRING CLASS

String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g,"");
}
String.prototype.after = function(delim) {
    if(delim == null || delim == "") return this;
    var index = this.indexOf(delim);
    return index > -1 ? this.substring(index + delim.length,this.length) : this;    
}
String.prototype.afterLast = function(delim) {
    if(delim == null || delim == "") return this;
    var index = this.lastIndexOf(delim);
    return index > -1 ? this.substring(index + delim.length,this.length) : this;     
}
String.prototype.before = function(delim) {
    if(delim == null || delim == "") return this;
    var index = this.indexOf(delim);
    return index > -1 ? this.substring(0,index) : this;    
}
String.prototype.beforeLast = function(delim) {
    if(delim == null || delim == "") return this;
    var index = this.lastIndexOf(delim);
    return index > -1 ? this.substring(0,index) : this;    
}
String.prototype.startsWith = function(s) {
    if(s.length == 0) return true;
    if(s.length > this.length) return false;
    return this.indexOf(s) == 0;
}
String.prototype.endsWith = function(s) {	
    if(s.length == 0) return true;	
    if(s.length > this.length) return false;
    return this.slice(this.length - s.length) == s;	
}
String.prototype.truncate = function(length,suffix) {
    if(this.length <= length) return this;            	
    return this.substring(0,length) + (suffix == null ? "" : suffix);
}
