/* Utlities for manipulating the properties of HTML DOM objects - very useful
Visit www.alistapart.com for the lowdown under DHTML */



var isNav4, isNav6, isIE4;

/*
 * Browser version snooper; determines your browser
 * (Navigator 4, Navigator 6, or Internet Explorer 4/5)
 */
function setBrowser()
{
    if (navigator.appVersion.charAt(0) == "4")
    {
        if (navigator.appName.indexOf("Explorer") >= 0)
        {
            isIE4 = true;
        }
        else
        {
            isNav4 = true;
        }
    }
    else if (navigator.appVersion.charAt(0) > "4")
    {
        isNav6 = true;
    }
}

// following code from browser sniffer from webreference.com
    var agt=navigator.userAgent.toLowerCase();
    isMac = (agt.indexOf("mac")!=-1);
	
	/*
 *
 * Given a selector string, return a style object
 * by searching through stylesheets. Return null if
 * none found
 *
 */
function getStyleBySelector( selector )
{
    if (!isNav6)
    {
        return null;
    }
    var sheetList = document.styleSheets;
    var ruleList;
    var i, j;

    /* look through stylesheets in reverse order that
       they appear in the document */
    for (i=sheetList.length-1; i >= 0; i--)
    {
        ruleList = sheetList[i].cssRules;
        for (j=0; j<ruleList.length; j++)
        {
            if (ruleList[j].type == CSSRule.STYLE_RULE &&
                ruleList[j].selectorText == selector)
            {
                return ruleList[j].style;
            }   
        }
    }
    return null;
}


/*
 *
 * Given an id and a property (as strings), return
 * the given property of that id.  Navigator 6 will
 * first look for the property in a tag; if not found,
 * it will look through the stylesheet.
 *
 * Note: do not precede the id with a # -- it will be
 * appended when searching the stylesheets
 *
 */
function getIdProperty( id, property )
{
    if (isNav6)
    {
        var styleObject = document.getElementById( id );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            if (styleObject[property])
            {
                return styleObject[ property ];
            }
        }
        styleObject = getStyleBySelector( "#" + id );
        return (styleObject != null) ?
            styleObject[property] :
            null;
    }
    else if (isNav4)
    {
        return document[id][property];
    }
    else
    {
        return document.all[id].style[property];
    }
}

/*
 *
 * Given an id and a property (as strings), set
 * the given property of that id to the value provided.
 *
 * The property is set directly on the tag, not in the
 * stylesheet.
 *
 */
function setIdProperty( id, property, value )
{
    if (isNav6)
    {
        var styleObject = document.getElementById( id );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            styleObject[ property ] = value;
        }
    }
    else if (isNav4)
    {
        document[id][property] = value;
    }
    else if (isIE4)
    {
         document.all[id].style[property] = value;
    }
}








/*Code for opening popup Online Help
************************************************************/
//ALERT!!!!!  the use of the variable "addressCode" in this code is for the prototype only - use absolute file 
//references in the live system...
//This function has its own file in the live system....

window.onhelp=openHelp;
function openHelp()
{
var screenX=window.screen.availWidth;
var screenY=window.screen.availHeight;
var xSize=screenX/2+100;
var ySize=screenY-35;
var xpos=screenX/2-115;
var configString="toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+xSize+",height="+ySize+", left="+xpos+", top=0"
//alert("xSize= "+xSize+" ySize= "+ySize+" xpos= "+xpos);
if (!window.addressCode)  addressCode="";
if (window.onlineHelp) 
	{
	if (!onlineHelp.closed) 
		{
		onlineHelp.focus();
		return false;
		}
	}
onlineHelp = window.open(addressCode+"miscellaneous/help/index.htm","helpScreen",configString);
onlineHelp.focus();
return false;
}

//A new set of standard utilities - will try to bring them into domUtisl when I'm happy with them...


//activating/de-activating text-inputs according to a radio-button selection
//method = true/"activate" or false/"de-activate"
//selection method #1 - select text input  by name value of radio button
//selection method #2 - if #1 fails to find element the script affects all text inputs, select lists and text areas in the same table row as the radio button
//actOnCheckedItemOnly is optional - forces only the checked radio button to effect an action - useful when the radio buttons are on the same row and operate on a single set of input elements
function radioChoosesTextInput(src,method,actOnCheckedItemOnly)
{
	var checkedItemFlag=false;
	if (typeof actOnCheckedItemOnly!="undefined" && actOnCheckedItemOnly) checkedItemFlag=true;
	var thisForm=src.form;
	var thisRadio=thisForm.elements[src.name];
	if (method=="activate") method=true;
	else if (method=="de-activate") method=false;
	if (typeof method!="boolean" || !thisRadio.length) return false;
	var textInput=new Array();
	//collecting inputs
	for (var i=0; i<thisRadio.length;i++)
	{
		textInput.length=0;
		if (thisForm.elements[thisRadio[i].value])
		{
			textInput[0]=thisForm.elements[thisRadio[i].value];
		}
		else 
		{
			var tr=getParentElementByTagName(thisRadio[i],"TR");
			textInput=getElementsByCondition(tr,"INPUT","{if (arguments[0].type=='text') return true; else return false;}");
			var selectEl=tr.getElementsByTagName("SELECT");
			if (selectEl.length>0) 
			{
				for (var j=0; j<selectEl.length;j++)
				{
					textInput[textInput.length]=selectEl[j];
				}
			}
			var textareaEl=tr.getElementsByTagName("TEXTAREA");
			if (textareaEl.length>0) 
			{
				for (var j=0; j<textareaEl.length;j++)
				{
					textInput[textInput.length]=textareaEl[j];
				}
			}
		}
		//acting on inputs
		if ((thisRadio[i].checked && method) || (!thisRadio[i].checked && !method && !checkedItemFlag))
		{
			for (var j=0; j<textInput.length; j++)
			{
				//alert(textInput[j].tagName);
				textInput[j].disabled=false;
				textInput[j].style.backgroundColor="white";
			}
		}
		else if ((!thisRadio[i].checked && method && !checkedItemFlag) || (thisRadio[i].checked && !method))
		{
			for (var j=0; j<textInput.length; j++)
			{
				//alert(textInput[j].tagName);
				textInput[j].disabled=true;
				textInput[j].style.backgroundColor="#F0F0F0";
			}
		}
	}
}

//cross-browser tool for extracting the inner text of a node
function getInnerText(el) {
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				//str += ts_getInnerText(cs[i]);
				str += getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}

//cross-browser tool for updating the inner text of a node
function putInnerText(el,string) {
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) el.innerText=string;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) 
	{
		if (cs[i].nodeType==3) 
		{
				cs[i].nodeValue=string;
				break;
		}
	}
}

//works like getElementsByTagName, but with extra search condition - can be fed a string of text or a reference to another function as the conditional argument
function getElementsByCondition(src,tagName,condition)
{
	var el=src.getElementsByTagName(tagName);
	if (typeof condition =="string") 
	{
		eval("var thisCondition=function()"+condition);
	}
	else if (typeof condition =="function") 
	{
		var thisCondition=condition;
	}
	else return false;
	var test; var final_el=new Array();
	for (var i=0; i<el.length; i++)
	{
		test=thisCondition(el[i]);
		if (test) final_el[final_el.length]=el[i];
	}
	return final_el
}

//backtracks up through the DOM until it finds a parent node of the appropriate tag name
function getParentElementByTagName(obj,tag)
{
	do 
	{
		obj=obj.parentNode;
		if (!obj.nodeName || obj.nodeName=="BODY") return false;
	}
	while (obj.nodeName!=tag);
	return obj;
}


//activates/de-activates on/off button pairs - a useful function
//all buttons are assumed to exist as standard buttonNameOn/buttonNameOff pairs of objects
function activateButtons(action)
{
	var offAction=false;
	var onAction=false;
	if (arguments.length>1) 
	{
		var buttons=new Array(arguments.length-1);
		for (var i=0; i<arguments.length-1; i++)
		{
			buttons[i]=arguments[i+1];
		}
	}
	if (action=="on")
	{
		offAction="none";
		onAction="block";
	}
	else if (action=="off")
	{
		offAction="block";
		onAction="none";
	}
	else return alert('the defined action needs to be either \"on\" or \"off\" ');
	for (var i=0; i<buttons.length; i++)
	{
		var test=window.document.getElementById(buttons[i]+"On");
		if (test) setIdProperty(buttons[i]+"On","display",onAction);
		test=window.document.getElementById(buttons[i]+"Off");
		if (test) setIdProperty(buttons[i]+"Off","display",offAction);
	}
}


function addEventHandler(obj,func,evnt)
{
	if (isIE4) //IE variants
	{
		
	}
	else if (isNav6) //NS variants
	{
		
	}
}


function readCookie(cookieName)
{
	var nameEQ = cookieName + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
		{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
		{
			
			var temp= c.substring(nameEQ.length,c.length);
			return temp;
		}
		}
	return null;

	}

function writeCookie(cookieName,cookieValue,days)
{
	if (typeof days !="number")days=30;
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
  	}
	else expires = "";
	document.cookie = cookieName+"="+cookieValue+expires+"; path=/";
}