/*
// this function averages 4.67ms per call
function changecss(theClass,element,value) {	//documentation for this script at http://www.shawnolson.net/a/503/  thanks shawn!
	var cssRules = setcssrules();
	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
			if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
				document.styleSheets[S][cssRules][R].style[element] = value;
			}
		}
	}
}*/


function setcssrules() {
	var cssRules;
	if (document.all) {
		cssRules = 'rules';
	}
	else if (document.getElementById) {
		cssRules = 'cssRules';
	}
	return cssRules;
}



// This function averages 5.45ms per call 
function changecss(theClass,element,value) {	//documentation for this script at http://www.shawnolson.net/a/503/  thanks shawn!
	var cssRules = setcssrules();
	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 1; R < document.styleSheets[S][cssRules].length; R++) { // the first rule (rule 0) is always the document type definition (ie @charset "UTF-8"); this will halt in IE & Safari
			if (document.styleSheets[S][cssRules][R].selectorText.indexOf(theClass,0) == 0) {
				document.styleSheets[S][cssRules][R].style[element] = value;
			}
		}
	}
}

function togglecssdisplay(theClass) {	//documentation for this script at http://www.shawnolson.net/a/503/  thanks shawn!
	var cssRules = setcssrules();
	var displayed = false;
	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 1; R < document.styleSheets[S][cssRules].length; R++) { // the first rule (rule 0) is always the document type definition (ie @charset "UTF-8"); this will halt in IE & Safari
			if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
				if(document.styleSheets[S][cssRules][R].style.display == "none") {
					document.styleSheets[S][cssRules][R].style.display = "block";
					displayed = true;
				} else {document.styleSheets[S][cssRules][R].style.display = "none";}
			}
		}
	}
	return displayed;
}

/*
// This function averages 6.1ms per call
function changecss(theClass,element,value) {	//documentation for this script at http://www.shawnolson.net/a/503/  thanks shawn!
	var cssRules = setcssrules();
	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
			if (document.styleSheets[S][cssRules][R].selectorText.match(theClass) != null) {
				document.styleSheets[S][cssRules][R].style[element] = value;
			}
		}
	}
}*/

// this script changes the elements inline style, but runs extremely fast ie 0.1ms vs the method below.
// use this script if you have a bunch of stuff running on the page.
function HideContentStyle(divID) {
	document.getElementById(divID).style.display = "none";
	}
function ShowContentStyle(divID) {
	document.getElementById(divID).style.display = "block";
	}
function ContentNoStyle(divID) {
	document.getElementById(divID).style.display = "";
	}
function ReverseContentDisplayStyle(divID) {
	if (document.getElementById([divID]).style.display == null) {alert(document.getElementById([divID]).style.display);}
	if(document.getElementById([divID]).style.display == "none") { document.getElementById([divID]).style.display = "block"; }
	else { document.getElementById([divID]).style.display = "none"; }
	}
function ReverseContentDisplayNoStyle(divID) {
	if(document.getElementById(divID).style.display == "none") { document.getElementById(divID).style.display = ""; }
	else { document.getElementById(divID).style.display = "none"; }
	}
function ReverseInnerHTML(divID,beginningHTML,endingHTML) {
	if(document.getElementById(divID).innerHTML == beginningHTML) { document.getElementById(divID).innerHTML = endingHTML; }
	else { document.getElementById(divID).innerHTML = beginningHTML; }
	}
function toggleBackgroundImage(theClass,element) {
	var cssRules = setcssrules();
	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 1; R < document.styleSheets[S][cssRules].length; R++) { // the first rule (rule 0) is always the document type definition (ie @charset "UTF-8"); this will halt in IE & Safari
			if (document.styleSheets[S][cssRules][R].selectorText.indexOf(theClass,0) == 0) {
				var oldRule = document.styleSheets[S][cssRules][R].style.cssText; // gets the text
				var pattern = new RegExp(":","gi"); // match pattern to extract the string
				var elementValueStringBeginning = oldRule.match(pattern); // assigns string to variable
				pattern.test(oldRule); // gets position (in oldRule) of end matched substring
				pattern2 = new RegExp(";","g") // looks for the first colon in oldRule
				pattern2.test(oldRule); // and records the index
				var elementValueStringEnd = oldRule.slice(pattern.lastIndex+1,pattern2.lastIndex-1)
				if(elementValueStringEnd.match("top","gi")){
						document.styleSheets[S][cssRules][R].style[element] = elementValueStringEnd.replace("top","bottom");
					}
				else {
					document.styleSheets[S][cssRules][R].style[element] = elementValueStringEnd.replace("bottom","top");
				}
			}
		}
	}
}
function toggleTurnDownTriangle(divID,element) {
			if (document.getElementById([divID]) != null) {
				var backgroundPosition = document.getElementById([divID]).style.background;
				if(backgroundPosition.match("top","gi"))
					document.getElementById([divID]).style.background = backgroundPosition.replace("top","bottom");
				else
					document.getElementById([divID]).style.background = backgroundPosition.replace("bottom","top");
			}
}
// This function swaps button images for client approval
function  swapbuttonimage(theClass,element,firstFileName,secondFileName) {
	var cssRules = setcssrules();
	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 1; R < document.styleSheets[S][cssRules].length; R++) { // the first rule (rule 0) is always the document type definition (ie @charset "UTF-8"); this will halt in IE & Safari
			if (document.styleSheets[S][cssRules][R].selectorText.indexOf(theClass,0) == 0) {
				var oldRule = document.styleSheets[S][cssRules][R].style.cssText; // gets the text
				var pattern = new RegExp("(url[(]){1}[^]+(?=[)] )","gi"); // match pattern to extract the string "url(...)"
				var elementValueStringBeginning = oldRule.match(pattern); // assigns string to variable
				pattern.test(oldRule); // gets position (in oldRule) of end matched substring
				pattern2 = new RegExp(";","g") // looks for the first colon in oldRule
				pattern2.test(oldRule); // and records the index
				
				var elementValueStringEnd = oldRule.slice(pattern.lastIndex+1,pattern2.lastIndex-1);
				var compareFileName = "url("+firstFileName;
				if (elementValueStringBeginning == compareFileName) {
					document.styleSheets[S][cssRules][R].style[element] = "transparent url("+secondFileName+")"+elementValueStringEnd;
					}
				else {
					document.styleSheets[S][cssRules][R].style[element] = "transparent "+compareFileName+")"+elementValueStringEnd;
				}
				
			}
		}
	}
}
function removeBLINKTags(DivID){
 	if(document.getElementById && document.getElementById(DivID)){
 		var strInputCode = document.getElementById(DivID).innerHTML;
 	 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
   		// alert("Input code:\n" + strInputCode + "\n\n");
 		var strTagStrippedText = strInputCode.replace(/<\/blink>|<blink>/g, "");
		
		document.getElementById(DivID).innerHTML = strTagStrippedText;
 		// alert("Output text:\n" + strTagStrippedText);	
   // Use the alert below if you want to show the input and the output text
   		// alert("Input code:\n" + strInputCode + "\n\nOutput text:\n" + strTagStrippedText);	
 	}	
}
