/*=============================================================================
 * jscripts.js		Mon Nov 30 16:37:11 2009		has
 *=============================================================================
 *
 *	JavaScripts:  Include this with <script src=...></script>
 *
 *=============================================================================
 */

/*
 *-----------------------------------------------------------------------------
 * Reverse a string
 *-----------------------------------------------------------------------------
 */

function strrev(str) {
   if (!str) return '';
   var revstr='';
   for (i = str.length-1; i>=0; i--)
       revstr+=str.charAt(i)
   return revstr;
}

/*
 *-----------------------------------------------------------------------------
 * Output a valid 'mailto' tag from disguised pieces.
 *-----------------------------------------------------------------------------
 *
 * The user's name is given REVERSED as first argument.
 * The second argument is 'spam@server.name'
 *
 */

function mail_to(rev,stdmail,tclass) {
	var user = strrev(rev);
	var server = stdmail.split('\@',2);
	var email = user + '@' + server[1];
	if (tclass.length == 0) {
		document.write('<A HREF="mailto:' + email + '">' + email + '</A>');
	} else {
		document.write('<A class="' + tclass + '" HREF="mailto:' + email + '">' + email + '</A>');
	}
	return true;
}		

/*
 *-----------------------------------------------------------------------------
 * Change background color of a table row
 *-----------------------------------------------------------------------------
 */

function setBG(theRow, newColor)
{
    var theCells = null;

    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    var newBG = null;

    for (var c = 0; c < rowCellsCnt; c++) {
        theCells[c].style.backgroundColor = newColor;
    }

    return true;
}

/*
 *-----------------------------------------------------------------------------
 * Change border width and color of a box object
 *-----------------------------------------------------------------------------
 */

function setBorder(box, width, color)
{
	box.style.borderStyle = 'solid';
	box.style.borderWidth = width;
	box.style.borderColor = color;
	return true;
}

/*
 *-----------------------------------------------------------------------------
 * Toggle the state of a checkbox given its ID
 *-----------------------------------------------------------------------------
 */

function toggleBox(what) {
    box = document.getElementById(what);
    box.checked = !box.checked
    return true;
}

/*
 *-----------------------------------------------------------------------------
 * Change visibility of an element given its ID (usually an image)
 *-----------------------------------------------------------------------------
 */

function showImg(thing) {
	box = document.getElementById(thing);
	box.style.visibility = 'visible';
}

function hideImg(thing) {
	box = document.getElementById(thing);
	box.style.visibility = 'hidden';
}

/*
 *-----------------------------------------------------------------------------
 * Submit a form when <return> is pressed on a field
 *-----------------------------------------------------------------------------
 */

function submitIfEnter(theField,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)  {
       theField.form.submit();
       return false;
    }
    return true;
}

/*
 *-----------------------------------------------------------------------------
 * Submit a form based on the ID
 *-----------------------------------------------------------------------------
 *
 * This a utility routine for forms buried in Iframes, etc.
 *
 */

function sendForm(fID,fAction) {
    var form = document.getElementById(fID);
    if (form) {
		form.action = fAction;
		form.submit();
		return true;
    } else {
		window.alert(fID + ' is empty or missing');
		return false;
    }
}


/*
 *-----------------------------------------------------------------------------
 * Routines for selecting rows of a table and highlighting
 *-----------------------------------------------------------------------------
 *
 *
 * author: Vincent Puglia, GrassBlade Software
 * site:   http://members.aol.com/grassblad
 */

function selectAll(formObj, isInverse) 
{
	for (var i=0;i < formObj.length;i++) 
	{
		fldObj = formObj.elements[i];
		if (fldObj.type == 'checkbox')
		{ 
			if(isInverse)
			fldObj.checked = (fldObj.checked) ? false : true;
			else fldObj.checked = true; 
		}
	}
}


/*----------------------------------------------------------------------------- 
 *
 * Select rows in tables: 
 *	Single row = mouse click
 *	Range of rows: click first row, then [SHIFT]+last row
 *	Multiple rows: [CTRL]+click
 *
 * from http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20659034.html
 *	Quincy Acklen
 *
 */

var aSelectedRows = new Array();
var oLastRow;

function selectClick(e, oRow ) {
	if(e)event=e;
	if(event.ctrlKey )
		toggleRow( oRow );
	if(event.shiftKey)
		selectRowsInbetween( oRow );
	if(!event.ctrlKey && !event.shiftKey){
		clearAllRows();
		toggleRow( oRow );
	}
	oLastRow = oRow;
}

function selectClickOne(e, oRow ) {
	clearAllRows();
	toggleRow( oRow );
	oLastRow = oRow;
}

function clearAllRows(){
	for(r in aSelectedRows) {
		aSelectedRows[r].style.backgroundColor = '';
	}
	aSelectedRows = new Array();
}

function toggleRow( oRow ) {
	if ( oRow.style.backgroundColor == 'red' ) {
		oRow.style.backgroundColor = '';
		for(i=0;i<aSelectedRows.length;i++) {
			if(aSelectedRows[i]==oRow)
				aSelectedRows.splice(i,1);
		}
	} else {
		oRow.style.backgroundColor = 'red';
		aSelectedRows[aSelectedRows.length]=oRow;
	}
}

function selectRowsInbetween( oRow ) {
	//if(!event.ctrlKey)
	//   clearAllRows();
	var aRows = oRow.parentNode.parentNode.rows;
	from = Math.min(oLastRow.rowIndex, oRow.rowIndex);
	to = Math.max(oRow.rowIndex, oLastRow.rowIndex);
	for(var i=0; i< aRows.length; i++ )
		if(i >= from && i <= to)
		if ( aRows[i].style.backgroundColor != 'red' ) {
			aRows[i].style.backgroundColor = 'red';
			aSelectedRows.push(aRows[i]);
		}
}

// - Attach the selector routines to a table - 
function setMouse( tabID, oneOnly ){
	theTab = document.getElementById(tabID);
	oLastRow = theTab.rows[0];
	theTab.style.cursor = 'default';
	theTab.onselectstart = function(){return false};
	if (oneOnly) {
		for(i=0;i<theTab.rows.length;i++)
			theTab.rows[i].onmousedown=function(event){selectClickOne(event, this)}; 
	} else {
		for(i=0;i<theTab.rows.length;i++)
			theTab.rows[i].onmousedown=function(event){selectClick(event, this)}; 
	}
}

// - Put list of selected rows in the hidden element with name 'f_selRows' -
function getSelect(theForm){
	theSel = new Array();
	for(r in aSelectedRows)
		theSel[theSel.length]=aSelectedRows[r].rowIndex;  
	theSel.sort();
	selVal = '';
	for(i=0;i<theSel.length;i++)
			selVal+=' '+theSel[i];
	theForm.f_selRows.value=selVal.substr(1);
}

// - Return the comma-separated list of IDs for the selected rows -
function getSelectID(){
	var theSel = new Array();
	var sep ='';
	for(r in aSelectedRows) {
		if (aSelectedRows[r].id !== undefined) {
			theSel[theSel.length]=aSelectedRows[r].id;
		}
	}
	theSel.sort();
	selVal = '';
	for(i=0;i<theSel.length;i++) {
		selVal+=sep+theSel[i];
		sep = ',';
	}
	return selVal;
}

// - Inserts a list of option items to a <select> list given by its ID -
function putOptions(optList,listID) {
	var dstList = document.getElementById(listID);
	if (!dstList) {
		window.open("domviewer.html");
//		document.writeln("<br>putOptions: listID '" + listID + "' is empty<br>");
	} else {
		for(var count = dstList.options.length - 1; count >= 0; count--) {
			dstList.options[count] = null;
		}
		for(var i = 0; i < optList.length; i++) {
			if (optList[i] != null)
			dstList.options[i] = new Option(optList[i].text, optList[i].value );
		}
	}
}

// - Cancels event bubble-up on mouse click -
function noClick(evt) {
	var e = (typeof evt != 'undefined') ? evt : event;
	e.cancelBubble = true;
}

/*
 *-----------------------------------------------------------------------------
 * Routines for folding and unfolding outlines (Klapplisten)
 *-----------------------------------------------------------------------------
 *
 */

function klSwitch(node) {
	var divs = node.getElementsByTagName("div");
	var hdg = divs[0];
	var box = divs[1];
	if (node.className == 'klzu') {
		node.className = 'klauf';
		box.style.display = 'block';
		hdg.style.paddingTop = '8px';
		hdg.style.fontWeight="bold";
		hdg.style.fontSize="1.3em";
	} else {
		node.className = 'klzu';
		box.style.display = 'none';
		hdg.style.paddingTop = 0;
		hdg.style.fontWeight="normal";
		hdg.style.fontSize="1em";
	}
	return true;
}

/*
 *-----------------------------------------------------------------------------
 * Popup window with an image
 *-----------------------------------------------------------------------------
 *
 */

var imgwindow = ''
function imgpopup(url,xtra) {
if (imgwindow && !imgwindow.closed && imgwindow.location ) {
    imgwindow.location.href = url;
    imgwindow.focus(); }
else {
    imgwindow=window.open(url,'htmlname',xtra);}
}
/*	--- This can be used for the <body onUnload...> tag --- */
function imgpoptidy() {
if (imgwindow && !imgwindow.closed && imgwindow.location ) {
   imgwindow.close(); }
}

/*
 *-----------------------------------------------------------------------------
 * Put stuff selected in a child window (eg. a popup) to a parent
 *-----------------------------------------------------------------------------
 *
 *	putSelToParent(childID, parentID)
 *
 *	childID = id of a form
 *	parentID = id of a forms variable in the parent window to receive data.
 */

function putSelToParent(childID, parentID) {
	var src = document.getElementById(childID);
	var list = getSelectID(src); 
	var dst = window.opener.document.getElementById(parentID);
	dst.value=list;	
	return list;
}

/*	--- Put a value to a parent tag ---	*/
function putValToParent(val,parentID) {
	var dst = window.opener.document.getElementById(parentID);
	dst.value = val;
	return true;
}

/*
 *=============================================================================
 *		DEBUGGING
 *=============================================================================
 *
 *-----------------------------------------------------------------------------
 * Dump the properties of an object
 *-----------------------------------------------------------------------------
 */

function dump_props(obj, obj_name) {
   var result = ""
   for (var i in obj) {
      result += obj_name + "." + i + " = " + obj[i] + "<BR>"
   }
   result += "<HR>"
   return result
}

