



/**
 * Sets the carrent focus to the element with this id.
 * @param elementId The id of the element to focus on.
 */
function setFocus(elementId) {
    if ( (elementId != null) && (elementId.length > 0) ) {
        var ele = document.getElementById(elementId);
        if ( ele != null ) {
            ele.focus();
            ele.select();
        }
    }
}

/**
 * Allows only decimal numbers to be entered into this field.
 */
function decimalsOnly(field, event) {
    return numbersOnly(field, event, true, false);
}

/**
 * Does not allow any non-numeric characters to be entered into this field.
 */
function numbersOnly(field, event, decimalAllowed, signAllowed) {
    var key;
    var keychar;

    if (window.event) { 
       key = window.event.keyCode;
    } else if (event) {
       key = event.which;
    } else {
       return true;
    }
    //alert("Key pressed! ASCII-value: " + key + ":" + keychar);
    keychar = String.fromCharCode(key);
    
    var accept = false;
    // control keys
    if ((key===null) ||
        (key===0) ||
        (key==8) ||
        (key==9) ||
        (key==13) ||
        (key==27)) {
        accept = true;
    } else if((event.ctrlKey) && ((key==99) || (key==118) || (key==120))) {
        
        accept = true;
    } else if ((("0123456789").indexOf(keychar) > -1)) {
        accept = true;
    } else if (decimalAllowed && keychar == "." && field.value.indexOf(".") == -1) {
        accept = true;
    } else if (signAllowed && (keychar=="-" || keychar=="+") && field.value.length==0) {
        accept = true;
    }
    return accept;
}

function ghostText(text, show, element) {
    if ( show ) {
        if ( element && (element.value == '') ) {
            element.value = text;
        }
    } else {
        if ( element && (element.value == text) ) {
            element.value = '';
        }
    }
}

function forward(href) {
    window.location = href;
}

/**
 * Popup Window
 */
function puw(url, name, props) {
    if ( name == null ) {
       var d = new Date();
       name = "puid" + d.getTime();
    }
    if ( props == null ) {
        props = "toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1";
    }
    return window.open(url, name, props);
}

/**
 * Add trim functions to String object.
 */
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

/**
 * Import another Javascript source file.
 */
function importjs(path) {
    if (document.createElement && document.getElementsByTagName) {
        var s = document.createElement('script');
        var h = document.getElementsByTagName('head');
        if (s && h.length) {
            s.type = "text/javascript";
            s.src = path;
            h[0].appendChild(s);
        }
    } else {
        document.write("<" + "scr" + "ipt type=\"text/j" + "ava" + "scr" + "ipt\"" +
            " src=\"" + path + "\">" + "</" + "scr" + "ipt" + ">");
    }
    
}
    
/**
 * Check if javascript file has been loaded.
 */
function isLoaded(path) {
    var scripts = document.getElementsByTagName("script");
    for ( var i = 0; i < scripts.length; i++ ) {
        var src = scripts[i].src;
        var indx = src.indexOf(path);
        if ( (indx != -1) && (indx == (src.length - path.length)) ) {
            return true;
        }
    }
    return false;
}

/**
 * Imports this javascript file only if it has not already been imported.
 */
function smartImportjs(path) {
    if ( !isLoaded(path) ) {
        importjs(path);
    }
}

/**
 * Gets the fuly qullified path to the servlet found a the page.URL
 */
function getServletPage(path) {
    return '' + file + '';
}

/**
 * Gets the fuly qullified path to the servlet found a the page.URL
 */
function getServletAction(path) {
    return '/' + file + '.do';
}