var selectedRow = new Array;

function initCheckRows() {
    // for every table row ...
    var rows = document.getElementsByTagName('tr');
    for ( var i = 0; i < rows.length; i++ ) {
        // ... with the class 'db_table_grid_odd_color' or 'db_table_grid_even_color' ...
        if ( 'db_table_grid_odd_color' != rows[i].className.substr(0,23) && 'db_table_grid_even_color' != rows[i].className.substr(0,24) ) {
            continue;
        }
        // ... add event listeners ...
        // ... to highlight the row on mouseover ...
        if ( navigator.appName == 'Microsoft Internet Explorer' ) {
            // but only for IE, other browsers are handled by :db_table_hover_color in css
            rows[i].onmouseover = function() {
                this.className += ' db_table_hover_color';
            }
            rows[i].onmouseout = function() {
                this.className = this.className.replace( ' db_table_hover_color', '' );
            }
        }
        // Do not set click events if not wanted
        if (rows[i].className.search(/noclick/) != -1) {
            continue;
        }
        // ... and to mark the row on click ...
        rows[i].onmousedown = function() {
            var unique_id;
            var checkbox;

            checkbox = this.getElementsByTagName( 'input' )[0];
            if ( checkbox && checkbox.type == 'checkbox' ) {
                unique_id = checkbox.name + checkbox.value;
            } else if ( this.id.length > 0 ) {
                unique_id = this.id;
            } else {
                return;
            }

            if ( typeof(selectedRow[unique_id]) == 'undefined' || !selectedRow[unique_id] ) {
                selectedRow[unique_id] = true;
            } else {
                selectedRow[unique_id] = false;
            }

            if ( selectedRow[unique_id] ) {
                this.className += ' db_table_sel_color';
            } else {
                this.className = this.className.replace(' db_table_sel_color', '');
            }

            if ( checkbox && checkbox.disabled == false ) {
                checkbox.checked = selectedRow[unique_id];
            }
        }

        // ... and disable label ...
        var labeltag = rows[i].getElementsByTagName('label')[0];
        if ( labeltag ) {
            labeltag.onclick = function() {
                return false;
            }
        }
        // .. and checkbox clicks
        var checkbox = rows[i].getElementsByTagName('input')[0];
        if ( checkbox ) {
            checkbox.onclick = function() {
                // opera does not recognize return false;
                this.checked = ! this.checked;
            }
        }
    }
}
window.onload=initCheckRows;


function checkAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 1; i < rows.length; i++ ) {
        checkbox = rows[i].getElementsByTagName( 'input' )[0];
        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
                if ( typeof(selectedRow[unique_id]) == 'undefined' || !selectedRow[unique_id] ) {
                    rows[i].className += ' db_table_sel_color';
                    selectedRow[unique_id] = true;
                }
            }
        }
    }

    return true;
}

function unCheckAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 1; i < rows.length; i++ ) {
        checkbox = rows[i].getElementsByTagName( 'input' )[0];
        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
            rows[i].className = rows[i].className.replace(' db_table_sel_color', '');
            selectedRow[unique_id] = false;
        }
    }

    return true;
}


function textCounter( field, countfield, maxlimit ) {
    if ( field.value.length > maxlimit ) {
        field.value = field.value.substring( 0, maxlimit );
        field.blur();
        field.focus();
        return false;
    } else {
        countfield.value = maxlimit - field.value.length;
    }
}


function columnRegexpFilter(r_event, r_regexp) {
    var theChar = "", r_founded = false;

    if(document.all) {
        r_event = window.event;
        theChar = String.fromCharCode(r_event.keyCode);
        if(!r_event.shiftKey) theChar = theChar.toLowerCase();
        r_founded = r_regexp.test(theChar);
        r_event.returnValue = r_founded;
    } else if (document.layers) {
        theChar = String.fromCharCode(r_event.which);
        r_founded = r_regexp.test(theChar)
    } else {
        theChar = String.fromCharCode(r_event.charCode);
        r_founded = (r_event.keyCode!=0) || r_regexp.test(theChar);
        r_event.cancelBubble = !r_founded;
    }

    return r_founded;
}


function getParent(el) {
    if (document.all)
    while (el.parentElement != null)
    if (el.tagName == 'TR') return el;
    else el = el.parentElement;
    else if (document.getElementById) {
        if (el.nodeType == 1 && el.tagName.toLowerCase() == 'tr') return el;
        else return getParent(el.parentNode);
    }
}


function checkChangedRow(formName, rowNR, usedElement, oldValue, usedBgColor, usageType, isChecked) {

    var elementName = 'trpk[' + rowNR + ']';
    var rowCheckbox = formName[elementName];
    var rowElement = formName[usedElement];
    var rowElementOldValue = oldValue;

    if (rowCheckbox == null) {
        rowCheckbox = formName[elementName]
    };

    if (rowCheckbox) {
        var parentTR = getParent(rowCheckbox);
        if (usageType == 'checked') {
            if (isChecked == 'y') {
                if (rowCheckbox.checked) {
                    parentTR.className = (rowCheckbox.checked) ? 'db_table_sel_color' : usedBgColor;
                } else {
                    rowCheckbox.checked = true;
                    parentTR.className = (!rowCheckbox.checked) ? usedBgColor : 'db_table_sel_color';
                }
            } else {
                if (!rowCheckbox.checked) {
                    rowCheckbox.checked = true;
                    parentTR.className = (!rowCheckbox.checked) ? usedBgColor : 'db_table_sel_color';
                } else {
                    parentTR.className = (rowCheckbox.checked) ? 'db_table_sel_color' : usedBgColor;
                }
            }
        } else {
            if (rowElement.value != rowElementOldValue) {
                rowCheckbox.checked = true;
            } else {
                rowCheckbox.checked = false;
            }
            parentTR.className = (rowCheckbox.checked) ? 'db_table_sel_color' : usedBgColor;
        }
    }
    rowCheckbox = null;
}

//  validators
function validateDate (dateString) {
    var parsedDate = dateString.split ("-");
    if (parsedDate.length != 3) {
        return dateString;
    }
    var day, month, year ;
    year  = parsedDate[0] ;
    month = parsedDate[1] ;
    day   = parsedDate[2] ;
    var objDate = new Date(year, month-1, day);
    if (year != '0') {
        year  = objDate.getFullYear() ;
    }
    if (month != '0') {
        month = objDate.getMonth() + 1 ;
    }
    if (day != '0') {
        day = objDate.getDate() ;
    }
    dateString = year + '-' + month + '-' + day ;
    return dateString ;
}


function sysCenteredWin(file,name,properties,wprefix,hprefix) {
    centeredWindow = window.open(file,name,properties);
    centeredWindow.moveTo(screen.width/2-wprefix,screen.height/2-hprefix);
}


//  (C) Milan Svrlo, released under LGPL
var http_request = false;
function getXhttpRequest(url, onReadyStateFunction) {
    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
            }
        }
    }
    if (!http_request) {
        alert('Cannot create XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = onReadyStateFunction;
    http_request.open('GET', url, true);
    http_request.send(null);
}
/*
example of usage :
function check_count() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
alert(http_request.responseText + ' any text ...');
}
}
}
onClick="getXhttpRequest('http://server/page.php?', check_count); return false;"
*/


function trimString(string) {
    return string.replace(/^\s*|\s*$/g,"");
}


function openCenteredWindow(file,name,properties,wprefix,hprefix) {
    centeredWindow = window.open(file,name,properties);
    centeredWindow.moveTo(screen.width/2-wprefix,screen.height/2-hprefix);
}


function number_format(a, b, c, d) {
    a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
    e = a + '';
    f = e.split('.');
    if (!f[0]) {
        f[0] = '0';
    }
    if (!f[1]) {
        f[1] = '';
    }
    if (f[1].length < b) {
        g = f[1];
        for (i=f[1].length + 1; i <= b; i++) {
            g += '0';
        }
        f[1] = g;
    }
    if(d != '' && f[0].length > 3) {
        h = f[0];
        f[0] = '';
        for(j = 3; j < h.length; j+=3) {
            i = h.slice(h.length - j, h.length - j + 3);
            f[0] = d + i +  f[0] + '';
        }
        j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
        f[0] = j + f[0];
    }
    c = (b <= 0) ? '' : c;
    return f[0] + c + f[1];
}



// 3D Party JS
// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
        keyStr.charAt(enc3) + keyStr.charAt(enc4);
    } while (i < input.length);

    return output;
}

function decode64(input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + String.fromCharCode(chr1);

        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }
    } while (i < input.length);

    return output;
}



function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}