// JScript File

function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}
function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function textboxMultilineMaxNumber(txt, maxLen) {
    try {
        if (txt.value.length > (maxLen - 1)) return false;
    } catch (e) {
    }
}


function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}
function addCss(cssCode) {
    var styleElement = document.createElement('style');
    styleElement.type = 'text/css';
    if (styleElement.styleSheet) {
        styleElement.styleSheet.cssText = cssCode;
    } else {
        styleElement.appendChild(document.createTextNode(cssCode));
    }
    document.getElementsByTagName('head')[0].appendChild(styleElement);
}

function addCssFile(cssFile) {
    var styleElement = document.createElement('link');

    styleElement.type = 'text/css';
    styleElement.rel = 'stylesheet';
    styleElement.href = cssFile;
    document.getElementsByTagName('head')[0].appendChild(styleElement);
}

function addRssLink(fileName) {

    var styleElement = document.createElement('link');
    styleElement.type = 'application/rss+xml';
    styleElement.rel = 'alternate';
    styleElement.href = fileName;
    styleElement.title = 'RSS';


    document.getElementsByTagName('head')[0].appendChild(styleElement);
}


function getKeyCode(e) {
    if (window.event)
        return window.event.keyCode;
    else if (e)
        return e.which;
    else
        return null;
}

function keyRestrict(e, validchars) {
    var key = '', keychar = '';
    key = getKeyCode(e);
    if (key == null) return true;
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    validchars = validchars.toLowerCase();
    if (validchars.indexOf(keychar) != -1)
        return true;
    if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27)
        return true;
    return false;
}



function showElement(id) {
    var element = document.getElementById(id);
    element.style.display = '';
}

function hideElement(id) {
    var element = document.getElementById(id);
    element.style.display = 'none';
}

function toggleElement(id) {
    var element = document.getElementById(id);

    if (element.style.display == '')
        hideElement(id);
    else
        showElement(id);
}

function linearSearchArray2(searchS, arraySA, dimension) {
    var I = 0;
    var minI = 0;
    var maxI = arraySA.length - 1;
    var s = "";
    var foundB = false;

    I = minI - 1;
    while ((I < maxI) && (!(foundB))) {
        I = I + 1;
        s = arraySA[I][dimension];
        foundB = (searchS == s);
    }

    if (foundB) {
        return (I);
    } else {
        return (-1); // some negative number indicating not found
    }
}

function _moveRowDown(currentRowCriteria) {

    if (currentRowCriteria == null) return null;

    var newRow = currentRowCriteria.cloneNode(true);

    currentRowCriteria.parentNode.insertBefore(newRow, currentRowCriteria.nextSibling.nextSibling);
    currentRowCriteria.parentNode.removeChild(currentRowCriteria);

    return newRow;
}


function _moveRowUp(currentRowCriteria) {

    if (currentRowCriteria == null) {
        return null;
    }

    var newRow = currentRowCriteria.cloneNode(true);

    currentRowCriteria.parentNode.insertBefore(newRow, currentRowCriteria.previousSibling);
    currentRowCriteria.parentNode.removeChild(currentRowCriteria);

    return newRow;
}

function insertOption(list, text, value) {
    var y = document.createElement('option');
    y.text = text;
    y.value = value;

    try {
        list.add(y, null); // standards compliant
    }
    catch (ex) {
        list.add(y); // IE only
    }
}

function clearList(list) {
    var index = list.length - 1;
    while (index >= 0) {
        list.remove(index--);
    }
}

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+$/, "");
}

function isValueInRange(value, minValue, maxValue) {
    if ((value < minValue) || (value > maxValue)) {
        return false;
    } else {
        return true;
    }
}


// Return the abolute position of the element
// WARNING: The webpage where I took this script "says" it could return
// the wrong location with complicated css designs.
function getAbsolutePosition(element) {
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
        var tmp = getAbsolutePosition(element.offsetParent);
        r.x += tmp.x;
        r.y += tmp.y;
    }
    return r;
};

//This returns the left and top coordinates of the element sent
//as the first argument. If the second argumentis a literal false,
//it returns the position relative to its parent, otherwise it returns the
//position relative to the body (includes the body offsets as well).

function getAbsolutePosition2(who, wch) {
    var L = 0, R = 0;
    var pa = who;

    while (pa.parentNode) {
        L += (pa.offsetLeft) ? pa.offsetLeft : 0;
        R += (pa.offsetTop) ? pa.offsetTop : 0;

        if (pa == document.body || wch === false) break;
        pa = pa.parentNode;
    }
    var A = [L, R];
    return (wch === 1 || wch === 2) ? A[wch] : A;
}


// Another way to find the absolute location
function getAbsolutePosition3(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

/*****************/
/* ImagePreloader */
/*****************/

function ImagePreloader(images, call_back) {
    // store the call-back
    this.call_back = call_back;

    // initialize internal state.
    this.nLoaded = 0;
    this.nProcessed = 0;
    this.aImages = new Array;

    // record the number of images.
    this.nImages = images.length;

    // for each image, call preload()
    for (var i = 0; i < images.length; i++)
        this.preload(images[i]);
}

ImagePreloader.prototype.preload = function(image) {
    // create new Image object and add to array
    var oImage = new Image;
    this.aImages.push(oImage);

    // set up event handlers for the Image object
    oImage.onload = ImagePreloader.prototype.onload;
    oImage.onerror = ImagePreloader.prototype.onerror;
    oImage.onabort = ImagePreloader.prototype.onabort;

    // assign pointer back to this.
    oImage.oImagePreloader = this;
    oImage.bLoaded = false;

    // assign the .src property of the Image object
    oImage.src = image;
}

ImagePreloader.prototype.onComplete = function() {
    this.nProcessed++;
    if (this.nProcessed == this.nImages) {
        this.call_back(this.aImages, this.nLoaded);
    }
}

ImagePreloader.prototype.onload = function() {
    this.bLoaded = true;
    this.oImagePreloader.nLoaded++;
    this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function() {
    this.bError = true;
    this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function() {
    this.bAbort = true;
    this.oImagePreloader.onComplete();
}


/********************************/
/* Effects - Based on Script.aculo.us */
/********************************/

function blendimage(divid, imageid, imagefile, secs) {

    //set the current image as background
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";

    //make image transparent
    Element.hide(imageid);

    //make new image
    document.getElementById(imageid).src = imagefile;

    Effect.Appear(imageid, { duration: secs });
}

/********** LOG *****************/
function log(message) {
    if (!log.window_ || log.window_.closed) {
        var win = window.open("", null, "width=400,height=200," +
                              "scrollbars=yes,resizable=yes,status=no," +
                              "location=no,menubar=no,toolbar=no");
        if (!win) return;
        var doc = win.document;
        doc.write("<html><head><title>Debug Log</title></head>" +
                  "<body></body></html>");
        doc.close();
        log.window_ = win;
    }
    var logLine = log.window_.document.createElement("div");
    logLine.appendChild(log.window_.document.createTextNode(message));
    log.window_.document.body.appendChild(logLine);
}

function getUrlParameter(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
