/*------------------------------------------------------------
 * stripedtable.js Version 1.0
 * Author : Kyosuke Nakamura
 * http://kyosuke.jp/
 *------------------------------------------------------------
 * 特定classのtable要素内、tr要素に対して交互にクラスをつけます。
 * className : ロールオーバーを設定するためのクラス名
 * oddlineClassName : 奇数ラインに付くクラス名
 * evanlineClassName : 偶数ラインに付くクラス名
/*------------------------------------------------------------*/
conf = {
	className : "stripedtable",
	oddlineClassName : "oddline",
	evenlineClassName : "evanline"
}




function setStripedTable(){
	var tables = getElementsByClassName(conf.className);
	for (var i=0, len=tables.length; i<len; i++){
		var table = tables[i];
		var lines = table.getElementsByTagName("tr");
		for (var j=0, llen=lines.length; j<llen; j++){

			tr = lines[j];
			if(j%2==0) {
				tr.className = conf.oddlineClassName;
			} else {
				tr.className = conf.evenlineClassName;
			}
		}
	}
}



function getElementsByClassName(name){
	var elements = new Array();
	var allElements = document.getElementsByTagName('*');
	for (var i=0, len=allElements.length; i<len; i++){
		if (allElements[i].className == name){
			elements.push(allElements[i]);
		}
	}
	return elements;
}



function ColorfulInput() {
   this.skip  = [];
   this.color = { 'blur': '#FFFFFF', 'focus': '#f5f5f5' };

   this.set = function() {
      for (var i = 0; i < document.forms.length; i++) {
         for (var f = 0; f < document.forms[i].length; f++) {
            var elm = document.forms[i][f];
            if(!this._checkSkip(elm)) continue;

            this._setColor(elm, 'focus');
            this._setColor(elm, 'blur');
         }       
      }
   }

   this._checkSkip = function(elm) {
      for(var i in this.skip) {
         if(elm.type == this.skip[i]) return false;
      }
      return true;
   }

   this._setColor = function(elm, type) { 
      var color = this.color[type];
      var event = function() { 
	  		elm.style.backgroundColor = color;
		};

      if(elm.addEventListener) {
         elm.addEventListener(type, event, false); 
      } else if(elm.attachEvent) {
         elm.attachEvent('on'+type, event); 
      } else {
         elm['on'+type] = event;
     }
   }
}

function loadSetting() {
   var colorful = new ColorfulInput;
   colorful.set();
   setStripedTable();
}


window.onload = loadSetting;
