// @cvs-id $Id: lib.js,v 1.11 2006/04/19 20:33:42 nsadmin Exp $ $Name: bridge-release-2_6_0 $
// Copyright 2004-2006 MedTouch LLC

// Cross-browser utilities

var agt = navigator.userAgent.toLowerCase();
var is_mac = (agt.indexOf("mac") != -1);
// Cross-browser utilities

var isMac = (agt.indexOf("mac") != -1);
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1);
var isOpera = (agt.indexOf("opera") != -1);
var isNs = (navigator.appName.indexOf("Netscape") != -1);
var isGecko = (agt.indexOf("gecko") != -1);
var isKonqueror = (agt.indexOf("konqueror") != -1);
var isSafari = (agt.indexOf("safari") != -1);
var isIE = ((agt.indexOf("msie") != -1) && !isOpera && (agt.indexOf("webtv") == -1));
// An alternative version.  Which is better?
//var isIE  = (navigator.appVersion.indexOf("MSIE") != -1);
var is_ie = isIE;
var ieP = isIE;
// Deprecated definition, shouldn't be a problem
// var ieP = document.all;


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();
    }
	return "";
}


function getXMLRequest () {
  if (window.ActiveXObject  && !is_mac) {
    return new window.ActiveXObject("Msxml2.XMLHTTP");
  } else if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  }
}

// Useful color changing functions to highlight changed data or fields that
// need to be changed

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 = '#FFEADF';
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 + " " + item);*/
    
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;
	}
    }
}
return false;
}

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

// Encode the values of the fields listed in an array into a URLstring

function fieldsToQuery (arr) {
  var query = "";
  var field;
  for (var i = 0; i < arr.length; i++) {
    field = getByID(arr[i]);
    if (field.type != "checkbox" || field.checked) {
      value = field.value;
    } else {
      value = "";
    }
    if (i > 0) query += "&";
    query += encodeURIComponent(arr[i]) + "=" + encodeURIComponent(value);
  }
  return query;
}


// Flash stuff?  Who put this in?

var MM_contentVersion = 6;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
                var words = navigator.plugins["Shockwave Flash"].description.split(" ");
            for (var i = 0; i < words.length; ++i)
            {
                if (isNaN(parseInt(words[i])))
                continue;
                var MM_PluginVersion = words[i];
            }
        var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0
   && (navigator.appVersion.indexOf("Win") != -1)) {
        document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
        document.write('on error resume next \n');
        document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
        document.write('</SCR' + 'IPT\> \n');
}
// added by BD 1/20
function openNewWin(page, win_name, width, height, scrollbars, resize) {
        if(scrollbars) {
		if (resize) {
			var remote = window.open(page,win_name,"width=" + width + ",height=" + height + ",scrollbars=yes,resizable=yes,status=no,location=no");
		} else {
        	        var remote = window.open(page,win_name,"width=" + width + ",height=" + height + ",scrollbars=yes,resizable=no,status=no,location=no");
		}
	} else {
		if (resize) {
	                var remote = window.open(page,win_name,"width=" + width + ",height=" + height + ",scrollbars=no,resize=yes,status=no,location=no");
		} else {
		        var remote = window.open(page,win_name,"width=" + width + ",height=" + height + ",scrollbars=no,resize=no,status=no,location=no");
		}
        }
	remote.focus();
}
 
// added by BD 1/20
function randQuote() {
        var index = Math.floor(Math.random() * 12);
        quote = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
        document.write('<img src="/images/quotes/quote-' + quote[index] + '.gif" border="0" width="476" height="77">');
}

// Select an entry in a select box by value
function selectByValue(select, value)
{
    for (var i = 0; i < select.length; i++)
    {
        if (select[i].value == value)
        {
            select.selectedIndex = i;
            return true;
        }
    }
    return false;
}

// Toggle an Objects visibility
function toggle2(objid) {
  var obj = getByID(objid);
  if (obj.style.display == "none")
    obj.style.display = '';
  else
    obj.style.display = "none";
}


// Get Select Value
function getSelectedValue(select) {
    return select.options[select.selectedIndex].value;
}

// Enable/Disable all inputted form elements
function form_enable(elements, enable)
{
    for (var i = 0; i < elements.length; i++)
    {
        var obj = elements[i];
        obj.disabled = !enable;
    }
}

// Copy all values from one list of elements to another
function form_copy_values(source_elements, dest_elements)
{
    // Copy each element individually
    for (var i = 0;
         i < source_elements.length && i < dest_elements.length;
         i++)
    {
        // Don't copy if the elements have different types
        var source = source_elements[i];
        var dest   =   dest_elements[i];
        if (source.type != dest.type)
            continue;

        // Different types copy in different ways
        if (source.type == "text" ||
            source.type == "textarea" ||
            source.type == "hidden" ||
            source.type == "password")
        {
            dest.value = source.value;
        }
        else
        if (source.type == "checkbox" ||
            source.type == "radio")
        {
            dest.checked = source.checked;
        }
        else
        if (source.type == "select-one")
        {
            // Copy the select contents
            dest.length = 0;
            for (var j = 0; j < source.length; j++)
                dest[dest.length++] = new Option(source[j].text,
                                                 source[j].value);

            // Copy the selected index
            dest.selectedIndex = source.selectedIndex;
        }
        // FIXME: Add support for multiple select, which no one uses
    }
}

// Strip all non-numbers from the text box
function only_numbers(textbox)
{
    textbox.value = textbox.value.replace(/\D/, "");
}

// Useful for creating a pop-up notification of some sort

function newWindow(url, name, width, height) {
  var newwin = window.open(url,name,
			   (width ? 'width=' + width + ',' : '') +
			   (height ? 'height=' + height + ',' : '') +
			   'resizable=yes, titlebar=no,toolbar=no,location=no,scrollbars=yes,status=no');
  newwin.focus();
  return newwin;
}

// global stuff for tracking multiple editors
var imageChooserWindow = new Array();
var fileChooserWindow = new Array();
var editorObjectID = new Array();

function fileChooserEditorID(win) {
  for (id in fileChooserWindow) {
    if (fileChooserWindow[id] == win) return id;
  }
}


