// @cvs-id $Id: lib.js,v 1.10 2005/02/01 15:32:22 nsadmin Exp $ $Name:  $
// Copyright 2004 MedTouch LLC

function getByID( id, doc )
{
    if ( doc == null ) doc = 'document';

    if (eval(doc).getElementById) {
	// Newer browsers
	return eval(doc).getElementById(id);
    } else if (eval(doc).all) {
	// IE4
	return eval(doc).all[id];
    } else if (eval(doc).layers) {
	// NS4
	return eval(doc).layers[id];
    } else {
	alert( 'We couldn\'t recognize your browser.  Please use a different one or upgrade to the newest version.' );
	window.close();
    }
}

function changeColor( id, color ) {
    var tag = getByID( id );
    if (tag.style)
	tag.style.backgroundColor = color;
    else if (tag.document && tag.document.bgColor)
	tag.document.bgColor = color;
}
 
var changedColor = '#ffddee';
var unchangedColor = 'white';

function markChanged( id ) {
    changeColor('row.'+id, changedColor);
} 

function markUnchanged( id ) {
    changeColor('row.'+id, unchangedColor);
} 

// onSubmit handlers for double-click protection
 
function refuseDoubleClick()
{
        alert('It looks like this form has already been submitted.  In order to prevent duplicate or broken data, we are refusing this submission.  You may re-load this page to re-submit');
        return false;
}
 
function testDoubleClick()
{
        return confirm('It looks like this form has already been submitted. If you re-submit it it could result in duplicate, or even broken, data.  Do you wish to proceed?');
}

var doubleClickProtect = false;

// If submitted once, use one of the above functions.
function checkDoubleClick( force )
{
    if ( doubleClickProtect ) {
	if ( force ) {
	    return refuseDoubleClick();
	} else {
	    return testDoubleClick();
	}
    } else {
	doubleClickProtect = true;
	return true;
    }
}

// Utilities for making alternating colors
var colorchanger = 0;
function makeCol (Color1, Color2)
{
    colorchanger++
    if (colorchanger % 2 == 0) {
	colorchanger = 0;
	document.write ("<TR bgcolor = "+ Color1 + ">");
    } else {
	document.write ("<TR bgcolor = "+ Color2 + ">");	
	
    }
}

function lastmakeCol (Color1, Color2)
{
    if (colorchanger % 2 == 0) {
	colorchanger = 0;
	document.write ("<TR bgcolor = "+ Color1 + ">");
    } else {
	document.write ("<TR bgcolor = "+ Color2 + ">");
    }
}

// Form validation (by paulm@oho.com)

function test_item_name (item_name, thisform)
{
    return (test_item (thisform[item_name]));

}

function set_id (item_name, id, thisform)
{
    if (!thisform[item_name].id) {
	thisform[item_name].id = id;
    }

}

function test_item (item)
{
var i;
/* alert (item.type + "  " + item.name); */
    
if (item.type == "text" || item.type == "textarea" || item.type=="password") {
    if (item.value != "") {
	return true;
    } else {
	return false;
    }
} else if (item.type == "checkbox" || item.type == "radio") {
    return item.checked;
} else if (item.type == "select-one") {
    for (i = 0; i < item.length; i++) 
    {
	if (item[i].selected && item[i].value != "") {
		return true;
	}
    }
    return false;
} else {
    for (i = 0; i < item.length; i++) 
    {
	if (test_item(item[i])) {
		return true;
	}
    }
}
}

function test_items (thisform)
{
    var i;	
    var emptyitems = "";
    for (i=0; i < thisform.length; i++) {
	
//	thisform[i].id = "TEST";
	if (thisform[i].id != "" && thisform[i].id.substring(thisform[i].id.length - 2) == "rq" ) {
	    if (!thisform[i].id) {
		var j;
		var item = thisform[i];
		for (j=0; i < item.length; j++) 
		{		
		    if (item[j].id != "") 
		    {
		        if (test_item_name (item.name, thisform))
			{

			} else {
			    if (thisform[i].id) {
				emptyitems += thisform[i].id;
			    } else {
			 	emptyitems += thisform[i].name;
			    }
			    emptyitems += "\n";
		
			}
		    }
		}
	     } else {
		if (test_item_name (thisform[i].name, thisform)) {

		} else {
		    if (thisform[i].id) {
			changeColor (thisform[i].id, changedColor);
			thisform[i].onfocus=function onfocus(event) {
			    changeColor(this.id,unchangedColor);
			};
			emptyitems += thisform[i].id.substring(0, thisform[i].id.length - 2);
		    } else {
			emptyitems += thisform[i].name;
		    }
		    emptyitems += "\n";
		}
	    }
	}
    }
    if (emptyitems != "") {
	alert ("You need to enter information for the following items: \n\n" + emptyitems);
	return false;
    } else {
	return true;
    }
}

// Display the source of the document (including everything that has been
// written into it via DHTML)
// tigre@ybos.net 2004-12-17

function showCurrentSource (doc, returnWin) {
    if (!doc) doc = document;
    var newWin = window.open(null,
			     'source_view',
			     'width=800,height=600,resizable=yes,toolbar=no,location=on,scrollbars=yes,status=no');
    newWin.document.write('<code><pre>\n'+doc.body.innerHTML.replace(/</g, '&lt;')+'</pre></code>');
    if (returnWin) {
	return newWin;
    }
}

// Snagged these two functions from http://www.quirksmode.com/js/findpos.html

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
	while (obj.offsetParent)
	{
	     curleft += obj.offsetLeft
	     obj = obj.offsetParent;
	}
    }
    else if (obj.x)
	curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
	while (obj.offsetParent)
	{
	     curtop += obj.offsetTop
	     obj = obj.offsetParent;
	}
    }
    else if (obj.y)
	curtop += obj.y;
    return curtop;
}

// radioGetVal

function radioGetVal(form, fieldName) {
    var radio = form[fieldName];
    var buttons = radio.length;
    var i = 0;
    var selection;
    while (i < buttons)  {
	if (radio[i].checked) {
	    selection = radio[i].value;
	    break;
	}
	i++;
    }
    return selection;
}

