// TRIMIN METHODS

function imageOver(id) {
	var img = ge(id);
	if(img == null) return;
	img.src = img.src.replace(id,id + "_over");
}

function imageOut(id) {
	var img = ge(id);
	if(img == null) return;
	img.src = img.src.replace(id + "_over",id + "");
}

// 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 newClass(e,c) {
	e.oClass = e.className;
	e.className = c;
}

function oldClass(e) {
	if(e.oClass != null) e.className = e.oClass;
}

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";
}

// 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);
}

// LIST CLASS

function List(data,delim,mult) {
    this.mult = mult == null ? false : mult;    
    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() {
    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;
}

// MISC

function focus(id) {
	if($(id) == null) return;
	try {
		$(id).focus();
		$(id).select();
	} catch(ex) {}
}

function esc(s) {
	if(typeof(s) != "string") return s;
    return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "&#39;").replace(/>/g, "&gt;").replace(/</g, "&lt;");
}
