

// workaround for old browsers
if (document.all && !document.getElementById) {
    document.getElementById = function(id) {
	return document.all[id];
    }
}

// Like firstChild, but skips over text and other non-element nodes.
function firstChildElement(element) {
    var child = element.firstChild;
    while (child && child.nodeType != 1) {
	child = child.nextSibling;
    }
    return child;
}

// Like nextSibling, but skips over text and other non-element nodes.
function nextSiblingElement(element) {
    var sibling = element;
    do {
	sibling = sibling.nextSibling;
    } while (sibling && sibling.nodeType != 1);
    return sibling;
}

// Like previousSibling, but skips over text and other non-element nodes.
function previousSiblingElement(element) {
    var sibling = element;
    do {
	sibling = sibling.previousSibling;
    } while (sibling && sibling.nodeType != 1);
    return sibling;
}

// This function should be called from a submit button\'s onClick.
function testJavascript(button) {
    document.cookie = 'haveJavascript=1; path=/';
    return true;
}

// Move focus to first text field in first form of document, if possible.
function firstFormFocus()
{
    try {
	for (var i = 0; i < document.forms.length; ++i) {
	    for (var j = 0; j < document.forms[i].elements.length; ++j) {
		var e = document.forms[i].elements[j];
		if ((e.type=="text" || e.type=="textarea" || e.type=="password" ||
		    e.type=="file") && !e.disabled && !e.readOnly)
		{
		    e.focus();
		    return;
		}
	    }
	}
    } catch (e) {
	// ignore
    }
}

function frameload(frame, location)
{
    if (frame.contentDocument) { // W3C standard DOM
	framedoc = frame.contentDocument;
    } else if (frame.contentWindow) { // IE 5.5 and IE 6
	framedoc = frame.contentWindow.document;
    } else if (frame.document) { // IE 5
	framedoc = frame.document;
    }

    framedoc.location.replace(location);
}

function frameloadById(id, location)
{
    var frame;
    if (document.frames) { // IE 5 Mac
	frame = document.frames[id];
    } else { // standard
	frame = document.getElementById(id);
    }
    frameload(frame, location);
}

function clearHint(element, hint)
{
    if (element.value == hint) {
	element.value = "";
	element.style.color = "#000"; // XXX should not be hardcoded
    }
}

function deemphasizeHint(element)
{
    element.style.color = "#999"; // gray XXX this should not be hardcoded
}

// This function should be called from an onload event to enable a form that
// may have hints or errors.
// (Note that onload is called on the initial page load, and in some but not
// all browsers, when returning to a page e.g. via browser\'s back button.)
function enableForm(form)
{
    for (var i = 0; i < form.elements.length; ++i) {
	var e = form.elements[i];

	// Note, e.value != e.defaultValue if browser replaced server\'s
	// default values with browser-remembered values.

	if (e.className) {
	    if (e.value != e.defaultValue) {
		// Clear spurious errors.
		if (e.className.indexOf("formerror") >= 0) {
		    clearFormError(e);
		}
	    } else {
		// If element has a hint value, de-emphasize it.
		if (e.className.indexOf("hint") >= 0) {
		    deemphasizeHint(e);
		}
	    }
	}
    }
    return true;
}

// Because a form\'s onreset event may be called before the browser actually
// resets the input fields, we must reset the fields ourselves before calling
// deemphasizeHint() or countTextLength().
function resetForm(form)
{
    for (var i = 0; i < form.elements.length; ++i) {
	var e = form.elements[i];
	e.value = e.defaultValue;
	if (e.className && e.className.indexOf("hint") >= 0)
	    deemphasizeHint(e);
	countTextLength(e);
    }
    return true;
}

// Submit the form, with all blank and hint-valued input fields disabled.
// This significantly reduces the length of a GET URL in the common case of an
// advanced search where many fields are left blank.
function submitWithoutBlanks(form)
{
    var n = 0;
    var disabledInputs = new Array();
    var suffixes = new Array();
    suffixes[0] = "_op";
    suffixes[1] = "_units";

    for (var i = 0; i < form.elements.length; ++i) {
	var e = form.elements[i];
	if (e.value == "" || // blank input
	    (e.className && e.className.indexOf("hint") >= 0 &&
	    e.value == e.defaultValue)) // still has its hint value
	{
	    // disable this element
	    e.disabled = true;
	    disabledInputs[n++] = e;

	    // disable corresponding suffixed elements
	    for (var j = 0; j < suffixes.length; ++j) {
		var e2 = document.getElementsByName(e.name + suffixes[j]);
		for (var k = 0; k < e2.length; ++k) {
		    if (!e2[k]) continue;
		    if (e2[k].name.indexOf(e.name) > 0) {
			// avoid MSIE bug where getElementsByName(str) returns
			// elements whose name CONTAINS str as a substring,
			// not just exact matches.
			continue;
		    }
		    e2[k].disabled = true;
		    disabledInputs[n++] = e2[k];
		}
	    }
	}
    }

    // Now that blank inputs are disabled, submit the form
    form.submit();

    // Re-enable the inputs, so they\'re not still disabled if user returns to
    // this page (e.g. via browser\'s Back button)
    for (var i = 0; i < disabledInputs.length; ++i) {
	disabledInputs[i].disabled = false;
    }

    return false; // prevent browser from submitting again
}

function clearFormError(element) {
    // restore the input field\'s default style
    element.style.backgroundColor = "#F8F8F8";
    element.style.borderColor = "#BBBBBB";
    // We can not use getElementsByName(), because the HTML spec and IE
    // in particular do not recognize the name attribute on <li> elements.
    var items = document.getElementsByTagName("li");
    if (items) {
	// clear the error messages
	for (var i = 0; i < items.length; i++) {
            if (items[i].className &&
		items[i].className.indexOf("error_" + element.name) >= 0)
	    {
		items[i].style.textDecoration = "line-through";
	    }
	}
    }
    return true;
}

function test_cliserv() {
    if (document.forms.length == 0) return;
    var testframe = document.createElement('iframe');
    testframe.id = "testframe";
    testframe.style.width = "0px";
    testframe.style.height = "0px";
    testframe.style.visibility = "hidden";
    // add testframe as a child of the first FORM element
    document.forms[0].appendChild(testframe);

    if (document.getElementById('testframe')) {
	var inputs = document.getElementsByName('cliserv');
	if (inputs) {
	    for (i=0; i < inputs.length; i++)
		inputs[i].disabled = false;
	}
    }
}

// create a length counter for an input field
function createFieldCounter(fieldname, maxlen) {
    // if (fieldname == "name") alert("version 2");
    var inputs = document.getElementsByName(fieldname);
    if (inputs) {
	for (i=0; i < inputs.length; i++) {
	    inputs[i].onkeyup = function() { countTextLength(this); };
	    inputs[i].onmouseup = function() { delayedCountTextLength(this); };

	    var parent = inputs[i].parentNode;
	    var next = inputs[i].nextSibling;

	    var node = document.createElement("span");
	    node.className = "counter";

	    var counterNode = document.createElement("span");
	    counterNode.id = "counter_" + fieldname;
	    counterNode.appendChild(document.createTextNode(inputs[i].value.length));

	    var maxlenNode = document.createElement("span");
	    maxlenNode.id = "maxlen_" + fieldname;
	    maxlenNode.appendChild(document.createTextNode(maxlen));

	    node.insertBefore(counterNode, null);
	    node.insertBefore(document.createTextNode("/"), null);
	    node.insertBefore(maxlenNode, null);

	    parent.insertBefore(node, next);
	}
    }
}

// set the counter value to the length of input.value, and make
// it red if length has exceeded maxlen.
function countTextLength(input) {
    var counterNode = document.getElementById("counter_" + input.name);
    var maxlenNode = document.getElementById("maxlen_" + input.name);
    if (counterNode && maxlenNode) {
	counterNode.firstChild.nodeValue = input.value.length;
	if (input.value.length > maxlenNode.firstChild.nodeValue)
	    counterNode.style.color = "#FF0000";
	else
	    counterNode.style.color = "#000000";
    }
}

// execute countTextLength after a 1ms delay.
function delayedCountTextLength(element) {
    setTimeout(function() { countTextLength(element); }, 1);
}

