/*=============================================================================
 * jscripts.js		Sun Jun 20 18:03:37 2010		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;
}

/*
 *-----------------------------------------------------------------------------
 * 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;
    }
}

/*
 *-----------------------------------------------------------------------------
 * 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
}

/*
 *-----------------------------------------------------------------------------
 * 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 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){
    theTab = document.getElementById(tabID);
    oLastRow = theTab.rows[0];
    theTab.style.cursor = 'default';
    theTab.onselectstart = function(){return false}; 
    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;
}

/*
 *-----------------------------------------------------------------------------
 * 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(); }
}

/*
 *=============================================================================
 *		DEBUGGING
 *=============================================================================
 */

