//global variables for all three examples
var colors = new Array('#ffffff','#dddddd','#aaaaaa','#888888');
var bgcolor = 255; 			//starting bgcolor in decimal
var steps = 10;				//the factor, a distance between colors
var down = true;			//direction, if going up or down when calculating bgcolor value
var switchingPoint = 160;	//the numeric value where the foreground color need to change
var fgColorHi = "white";
var fgColorLo = "black";
/****************************************************************************************/
function alternateByClass(){
	//method = document.methodSelector.selector[document.methodSelector.selector.selectedIndex].value;	
	if(document.getElementsByTagName){							//check that browser has capabilities
		var tables = document.getElementsByTagName("table");	//get all tables i the document
		var re = /\s/g;
		for(j = 0 ; j < tables.length ; j++)
		{
			var table =  tables[j];
			var cn = trimAll(table.className.toLowerCase());
			if(cn == "alternating")								//if the class has class = "alternating", set alternating rowcolors
			{
				var rows = table.getElementsByTagName("tr");	//get all table rows
				for(i = 0; i < rows.length; i++){				//alternate styles			
					//manipulate rows	
					//if(method == "doAlternate") doAlternate(rows[i], i);
					//if(method == "doMultiple") doMultiple(rows[i], i);
					//if(method == "doGradient") doGradient(rows[i]);
					doAlternate(rows[i], i);
				}
			}
		}
	}
}
/****************************************************************************************/
function alternateById(id){
	//method = document.methodSelector.selector[document.methodSelector.selector.selectedIndex].value;	
	if(document.getElementById){						//check that browser has capabilities
		var table = document.getElementById(id);		//get just the selected table not all of them
		var rows = table.getElementsByTagName("tr");	//get all table rows
		for(i = 0; i < rows.length; i++){				//alternate styles			
			//manipulate rows	
			//if(method == "doAlternate") doAlternate(rows[i], i);
			//if(method == "doMultiple") doMultiple(rows[i], i);
			//if(method == "doGradient") doGradient(rows[i]);
			doAlternate(rows[i], i);
		}
	}
}
/****************************************************************************************/
function doAlternate(row, i){
	if(i % 2 == 0){
		row.className = "even";
	}else{
		row.className = "odd";
	}
}
/****************************************************************************************/
function doMultiple(row, i){
	row.style.backgroundColor = colors[i % colors.length];
}
/****************************************************************************************/
function doGradient(row){
	useColor = document.methodSelector.color[document.methodSelector.color.selectedIndex].value;
	//build bgcolor string	
	if (useColor == "red")
		bgcolorValue = "ff" + padHex() + bgcolor.toString(16) + padHex() + bgcolor.toString(16);
	if (useColor == "green")
		bgcolorValue = padHex() + bgcolor.toString(16) + "ff" + padHex() + bgcolor.toString(16);
	if (useColor == "blue")
		bgcolorValue = padHex() + bgcolor.toString(16) + padHex() + bgcolor.toString(16) + "ff";
	
	row.style.backgroundColor = "#" + bgcolorValue;
		
	if(down && (bgcolor-steps) > 0){	//if subtracting, prevent negative values
		bgcolor = (bgcolor - steps);
	}else{							
		bgcolor = (bgcolor + steps);
		down = false;
	}
	if(bgcolor > 255){					//prevent too high values
		bgcolor = (bgcolor - steps);
		down = true;
	}
	if(bgcolor < switchingPoint){		//change color of text (foreground color) if below a certain value (160)
		row.style.color = fgColorHi;
	}else{
		row.style.color = fgColorLo;
	}
}
/****************************************************************************************/
function padHex(){
	return (bgcolor < 16) ? "0" : "";
}
/****************************************************************************************/
