////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                //
// users.js                                                                                       //
// THIS JAVASCRIPT FILE PROVIDES VARIOUS FUNCTIONS USED IN THE APPLICATION (CHECKING FORMS,       //
// HIGHLIGHT SELECTED ROWS IN INDEX TABLES, TRIM TEXTS, ETC.)                                     //
//                                                                                                //
////////////////////////////////////////////////////////////////////////////////////////////////////

	function Trim(TRIM_VALUE){
		if(TRIM_VALUE.length<1){ return""; }
		TRIM_VALUE=RTrim(TRIM_VALUE);
		TRIM_VALUE=LTrim(TRIM_VALUE);
		if(TRIM_VALUE==""){ return ""; }
		else{ return TRIM_VALUE; }
	}

	function RTrim(VALUE){
		var w_space=String.fromCharCode(32);
		var v_length=VALUE.length;
		var strTemp="";
		if(v_length<0){ return""; }
		var iTemp=v_length -1;
		while(iTemp>-1){
			if(VALUE.charAt(iTemp)==w_space){}
			else{
				strTemp = VALUE.substring(0,iTemp +1);
				break;
			}
			iTemp = iTemp-1;
		}
	return strTemp;
	}

	function LTrim(VALUE){
		var w_space=String.fromCharCode(32);
		if(v_length<1){ return""; }
		var v_length=VALUE.length;
		var strTemp="";
		var iTemp=0;
		while(iTemp<v_length){
			if(VALUE.charAt(iTemp)==w_space){}
			else{
				strTemp = VALUE.substring(iTemp,v_length);
				break;
			}
			iTemp = iTemp + 1;
		}
	return strTemp;
	}

	function isNumber(nr) {
  	if(isNaN(Trim(nr)) || Trim(nr).length==0) return false;
  	return true;
	}

/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;

/**
 * Sets/unsets the pointer and marker in browse mode (for highlighted rows within a index table)
 *
 * @param   object    the table row
 * @param   integer   the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    //  Pointer and mark feature are disabled or the browser can't get the
    //  row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    //  Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    //  Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    //  ... with DOM compatible browsers except Opera that does not return
    //      valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    //  ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3


    //  ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0)
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    //  Defines the new color
    //  Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // deactivated onclick marking of the checkbox because it's also executed
            // when an action (like edit/delete) on a single item is performed. Then the checkbox
            // would get deactived, even though we need it activated. Maybe there is a way
            // to detect if the row was clicked, and not an item therein...
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    //  Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    //  Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = false;
        }
    } // end

    //  Sets the new color...
    if (newColor) {
        var c = null;
        //  ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        //  ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end

    return true;
} // end of the 'setPointer()' function

function popUP(mypage, myname, w, h, scroll, titlebar) {
     var winl = (screen.width - w) / 2;
     var wint = (screen.height - h) / 2;
     winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',unresizable'
     win = window.open(mypage, myname, winprops)
     if (parseInt(navigator.appVersion) >= 4) { 
	 win.window.focus();
     }
}  

function OLiframeContent(src, width, height, name, frameborder) {
 return ('<iframe src="'+src+'" width="'+width+'" height="'+height+'"'
 +(name!=null?' name="'+name+'" id="'+name+'"':'')
 +(frameborder!=null?' frameborder="'+frameborder+'"':'')
 +' scrolling="auto">'
 +'<div>[iframe not supported]</div></iframe>');
}

function OLobjectContent(data, width, height, name) {
 return ('<object data="'+data+'" width="'+width+'" height="'+height+'"'
 +(name?' name="'+name+'" id="'+name+'"':'')+' type="text/html">'
 +'<div>[object not supported]</div></object>');
}

function explodeArray(item,delimiter) {
  tempArray=new Array(1);
  var Count=0;
  var tempString=new String(item);

  while (tempString.indexOf(delimiter)>0) {
    tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
    tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1); 
    Count=Count+1
  }

  tempArray[Count]=tempString;
  return tempArray;
}

function validateInteger( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid integer number.
PARAMETERS:
   strValue - String to be tested for validity
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
	var objRegExp  = /(^-?\d\d*$)/;
	return objRegExp.test(strValue);
}

function replace(string,text,by) {
// Replaces text with by in string
	var strLength = string.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) return string;

	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0,txtLength))) return string;
	if (i == -1) return string;

	var newstr = string.substring(0,i) + by;

	if (i+txtLength < strLength)
		newstr += replace(string.substring(i+txtLength,strLength),text,by);

	return newstr;
}