﻿/* Last modified on 2006-11-26 by roal */

function test_first_input_tag() {
	/* Prints information about the first "input" tag's attributes */
	/* Useful to test the behaviour of different browsers */
	/* Works in IE, Mozilla and Opera */
	var message = "";
	var attributesArray = document.getElementsByTagName("input")[0].attributes;
	var trueAttributesArray = new Array();
	var attributesNr = attributesArray.length;
	message += "The first 'input' tag has " + attributesNr + " total attributes,\n";
	for (j = 0; j < attributesNr; j++) {
		var attributeValue = attributesArray[j].nodeValue;
		if (attributeValue) {
			trueAttributesArray.push(attributesArray[j]);
		}
	}
	attributesArray = ("");

	attributesNr = trueAttributesArray.length;
	message += "with the following " + attributesNr + " set to a true value:\n\n";
	var typeIndex = -1;
	for (j = 0; j < attributesNr; j++) {
		var attributeName = trueAttributesArray[j].nodeName;
		var attributeValue = trueAttributesArray[j].nodeValue;
		if (typeIndex < 0 && (attributeName == "type" || attributeName == "TYPE")) {
			/* Opera catches the nodeName in UPPERcase! */
			typeIndex = j;
		}
		message += j + ": ";
		message += attributeName + " = ";
		message += attributeValue + "\n";
	}
	if (typeIndex >= 0) {
		message += "\nIt's type is '" + trueAttributesArray[typeIndex].nodeValue + "'";
	} else {
		message += "\nNo type set!";
	}
	alert(message);
	return false;
}

function get_tag_numbers(tagName, attName, attValue) {
	/* This is a powerful and generally useful JS function to generate DHTML using standards compliant DOM  */
	/* Finds all tags with the name 'tagName' that have an attribute name 'attName' set to the value 'attValue' */
	/* and returns their 'tagNr' numbers usable by getElementsByTagName(tagName)[tagNr] */
	/* Written by Robert Allerstorfer on 2005-08-05 */
	/* Successfully tested on: IE 6, Firefox 1-2, Opera 7, Safari 2 */
	/* Usage example: */
	/* <a href="#dummy" onclick="alert(get_tag_numbers('input', 'type', 'checkbox'));">Show input type='checkbox' elements</a> */
	var attNameUc = attName.toUpperCase();
	var message = "";
	var tagNrsArray = new Array();
	var matches = 0;
	for (i = 0; i < document.getElementsByTagName(tagName).length; i++) {
		var attributesArray = document.getElementsByTagName(tagName)[i].attributes;
		var attributesNr = attributesArray.length;
		for (j = 0; j < attributesNr; j++) {
			var attributeName = attributesArray[j].nodeName;
			var attributeValue = attributesArray[j].nodeValue;
			if (attributeValue == attValue && (attributeName == attName || attributeName == attNameUc)) {
				tagNrsArray[matches] = i;
				matches++;
				break;
			}
		}
	}
/*
	message += "\n" + matches + " '" + tagName + "' tags with the following numbers contain the '";
	message += attributeName + "=" + attributeValue + "' attribute:\n";
	for (i = 0; i < matches; i++) {
		message += tagNrsArray[i] + " ";
	}	
	alert(message);
*/
	return(tagNrsArray);
}

function unhide_spamprotected() {
	/* A modern and smart way to unhide spam protected email addresses and links */
	/* Needs get_tag_numbers(tagName, attName, attValue) */
	/* Written by Robert Allerstorfer on 2005-08-05 */
	/* Last updated on 2006-11-26 */
	/* Successfully tested on: IE 6, Firefox 1-2, Opera 7, Safari 2 */
	/* Usage example: */
	/* <body onload="unhide_spamprotected();"> */
	/* <a href="#spamprotected" name="spamprotected" title="enable JavaScript to unhide spam protected email address">username(AT)domain(dot)tld</a> */
	/* <a href="#spamprotected" name="spamprotected" title="enable JavaScript to unhide spam protected email address">otheruser(AT)otherdomain(dot)tld</a> */
	var tagName = 'a';
	var tagNrsArray = get_tag_numbers(tagName, 'class', 'spamprotected');
	var message = "";
	for (i = 0; i < tagNrsArray.length; i++) {
		var tagNr = tagNrsArray[i];
		var displayAddress = document.getElementsByTagName(tagName)[tagNr].firstChild.nodeValue;
		realAddress = displayAddress.replace(/\(dot\)/g, ".");
		realAddress = realAddress.replace(/\(AT\)/g, "@");
		message += displayAddress + " -> " + realAddress + "\n";
		document.getElementsByTagName(tagName)[tagNr].firstChild.nodeValue = realAddress;
		document.getElementsByTagName(tagName)[tagNr].setAttribute("href", "mailto:" + realAddress);
		document.getElementsByTagName(tagName)[tagNr].setAttribute("title", "click to mail");
	}
/*
	alert(message);
*/
}

function uncheck_checkboxes() {
	/* Unchecks all checkboxes (removes the "checked" attribute from all <input type="checkbox" /> elements) */
	/* Needs get_tag_numbers(tagName, attName, attValue) */
	/* Written by Robert Allerstorfer on 2005-08-05 */
	/* Successfully tested on: IE 6, Firefox 1, Opera 7, Safari 2 */
	/* Usage example: */
	/* <a href="#dummy" onclick="uncheck_checkboxes();">Disable all</a> */
	var tagName = 'input';
	var attName = 'checked';
	var tagNrsArray = get_tag_numbers(tagName, 'type', 'checkbox');
	for (i = 0; i < tagNrsArray.length; i++) {
		var tagNr = tagNrsArray[i];
		var myTag = document.getElementsByTagName(tagName)[tagNr];
		if (Browser == "MSIE" || Browser == "Opera") {
			myTag.setAttribute(attName, false);
		} else {
			myTag.removeAttribute(attName);
			/* Does not work in Mozilla when checkbox has been changed directly, so we need old-style JavaScript: */
			myTag.checked = false;
		}
	}
}

function check_checkboxes() {
	/* Checks all checkboxes (sets the "checked" attribute from all <input type="checkbox" /> elements to "checked") */
	/* Needs get_tag_numbers(tagName, attName, attValue) */
	/* Written by Robert Allerstorfer on 2005-08-05 */
	/* Successfully tested on: IE 6, Firefox 1, Opera 7, Safari 2 */
	/* Usage example: */
	/* <a href="#dummy" onclick="check_checkboxes();">Enable all</a> */
	var tagName = 'input';
	var attName = 'checked';
	var attValue = 'checked';
	var tagNrsArray = get_tag_numbers(tagName, 'type', 'checkbox');
	for (i = 0; i < tagNrsArray.length; i++) {
		var tagNr = tagNrsArray[i];
		var myTag = document.getElementsByTagName(tagName)[tagNr];
		myTag.setAttribute(attName, attValue);
		/* Does not work in Mozilla when checkbox has been changed directly, so we need old-style JavaScript: */
		myTag.checked = true;
	}
}

function invert_checkboxes() {
	/* Invert current status of all checkboxes */
	/* Needs get_tag_numbers(tagName, attName, attValue) */
	/* Written by Robert Allerstorfer on 2005-08-05 */
	/* Successfully tested on: IE 6, Firefox 1, Opera 7, Safari 2 */
	/* Usage example: */
	/* <a href=\"#dummy\" onclick=\"invert_checkboxes();\">Invert selection</a> */
	var tagName = 'input';
	var tagNrsArray = get_tag_numbers(tagName, 'type', 'checkbox');
	for (i = 0; i < tagNrsArray.length; i++) {
		var tagNr = tagNrsArray[i];
		var myTag = document.getElementsByTagName(tagName)[tagNr];
		myTag.checked = (! myTag.checked);
	}
}

function checkbox_switch() {
	/* Needs uncheck_checkboxes() and check_checkboxes() */
	/* Written by Robert Allerstorfer on 2005-08-05 */
	/* Successfully tested on: IE 6, Firefox 1, Opera 7, Safari 2 */
	/* Usage example: */
	/* <a href="#dummy" onclick="checkbox_switch();"><span id="checkbox-switch" title="Check &lt;-&gt; Uncheck all checkboxes">Disable all</span></a> */
	var modus1 = "Disable all";
	var modus2 = "Enable all";
	var myTag = document.getElementById('checkbox-switch');
	var switchModus = myTag.firstChild.nodeValue;
	if (switchModus == modus1) {
		uncheck_checkboxes();
		myTag.firstChild.nodeValue = modus2;
	} else {
		check_checkboxes();
		myTag.firstChild.nodeValue = modus1;
	}
}

