/**
 * NBR.CheckAndCookie
 * クッキーからチェックボックスにチェックする
 * チェックされたチェックボックスの値をクッキーに保持する
 *
 * @version 1.2 [2008/12/04]
 * @see cookie.js
 * @see NBR_moveInputValue.js
 */
if(typeof NBR == 'undefined') var NBR = function(){};

NBR.CheckAndCookie = function(formID, aInputNames) {
	
	// 設定
	this.highlightColor = "#FFFFFF";
	this.highlightBoxExt = "_bg";
	// 必須属性となる隠しチェックボックスのid
	this.hiddenCheckboxID = "catalog-check";
	
	if(document.getElementById(formID) == null) return false;
	this.hiddenCheckbox = document.getElementById(this.hiddenCheckboxID);
	// チェックされたcheckboxの数を記憶する変数
	this.nCheckTotal = 0;
	this.formID = formID;
	this.oCookie = {};
	this.oInputArray = {};
	
	if(typeof aInputNames != 'undefined') {
		var nLength = aInputNames.length;
		for(i=0; i<nLength; i++) {
			var aInputs = document.getElementsByName(aInputNames[i]);
			this.oInputArray[aInputNames[i]] = aInputs;
			document.getElementById(aInputNames[i] + "_hidden").value = "";
		}
	}
	
	this.cid = "";
	this.init();
};

NBR.CheckAndCookie.prototype.init = function() {
	this.checkQuery();
	this.checkFromCookie();
	this.checks();
	this.setEventHandler();
}

NBR.CheckAndCookie.prototype.checkQuery = function() {
	var sURL = document.location.href;
	if(sURL.indexOf("?") > -1) {
		var sQuery = sURL.split("?")[1];
		this.cid = sQuery.split("cid=")[1];
		if(document.getElementById(this.cid) != null) {
		} else {
			this.cid = "";
		}
	}
};

NBR.CheckAndCookie.prototype.checkFromCookie = function() {
	var cookieStr = jQuery2.cookie('catalog');
	
	if(cookieStr != null) {
		this.oCookie = NBR.CheckAndCookie.sCookieTooCookie(cookieStr);
	}
	
}

NBR.CheckAndCookie.prototype.checks = function() {
	if(this.cid != null && this.cid != "") {
		this.oCookie[this.cid] = 1;
		jQuery2.cookie("catalog", NBR.CheckAndCookie.oCookieTosCookie(this.oCookie));
	}
	for(p in this.oCookie) {
		if(this.oCookie[p] > 0) {
			var input = document.getElementById(p);
			if(typeof this.oInputArray[input.name] != 'undefined') {
				input.checked = true;
				this.nCheckTotal++;
				document.getElementById(input.name + "_" + p + this.highlightBoxExt).style.backgroundColor = this.highlightColor;
				this.hiddenCheckbox.checked = true;
				NBR.moveInputValue(input);
			}
		}
	}
	if(this.nCheckTotal > 0) {
		this.hiddenCheckbox.checked = true;
	}
}
/*
NBR.CheckAndCookie.prototype.checkAll = function(bool) {
	var elements = document.forms[0].elements;
	var nLength = elements.length;
	for( i = 0; i < nLength; i++) {
		elements[i].checked = bool;
		if(elements[i].checked) {
			document.getElementById("sec_" + elements[i].id).style.backgroundColor = this.highlightColor;
			oCookie[elements[i].id] = 1;
		} else {
			document.getElementById("sec_" + elements[i].id).style.backgroundColor = "";
		}
	}
	if(bool) jQuery2.cookie('catalog', oCookieTosCookie(oCookie));
	else jQuery2.cookie('catalog', null);
}
*/

NBR.CheckAndCookie.prototype.setEventHandler = function() {
	var elements = document.forms[this.formID].elements;
	var nLength = elements.length;
	for( i = 0; i < nLength; i++) {
		var thisObj = this;
		if(typeof this.oInputArray[elements[i].name] != 'undefined') {
			elements[i].prevChecked = false;
			elements[i].onclick = function() {
				if(this.checked) {
					document.getElementById(this.name + "_" + this.id + thisObj.highlightBoxExt).style.backgroundColor = thisObj.highlightColor;
					thisObj.oCookie[this.id] = 1;
					thisObj.nCheckTotal++;
				} else {
					document.getElementById(this.name + "_" + this.id + thisObj.highlightBoxExt).style.backgroundColor = "";
					thisObj.oCookie[this.id] = 0;
					thisObj.nCheckTotal--;
				
				}
				var str = NBR.CheckAndCookie.oCookieTosCookie(thisObj.oCookie);
				jQuery2.cookie("catalog", str);
				
				NBR.moveInputValue(this);
				//alert(thisObj.nCheckTotal);
				if(thisObj.nCheckTotal > 0) {
					thisObj.hiddenCheckbox.checked = true;
				} else {
					thisObj.hiddenCheckbox.checked = false;
				}
				//alert(thisObj.isChecked(this.name));
			}
		}
	}
}


NBR.CheckAndCookie.sCookieTooCookie = function(s) {
	var o = {};
	var aCookies = s.split(",");
	for(i = 0; i < aCookies.length; i++) {
		var aCookie = aCookies[i].split("=");
		o[aCookie[0]] = aCookie[1];
	}
	return o;
}

NBR.CheckAndCookie.oCookieTosCookie = function(o) {
	var s = "";
	for(p in o) {
		s += p + "=" + o[p] + ",";
	}
	s = s.substr(0,s.length-1);
	return s;
}

//いずれかひとつでもチェックが入っているかどうかのチェック
/*
NBR.CheckAndCookie.prototype.isChecked = function(name) {
	if(typeof oCheckboxList[name] == 'undefined') {
		this.oCheckboxList[name] = document.getElementsByName(name);
	}
	if(this.oCheckboxList[name].length > 0)
		for(i=0; i<aCheckboxes.length; i++) {
			if(aCheckboxes[i].checked) return true;
	}
	return false;
}
*/