/**
 *  The Commerce JavaScript For Front
 *  @author  fukuda -peepz inc.-
 *  @created 2009/3/6
 */

/*
 * JSONから住所を取得する
 */
var getAddressFromZipCode = function(){
	AjaxZip2.JSONDATA = '/js/zip/data';
	AjaxZip2.zip2addr('zipcode1','prefecture','city','zipcode2','address2','address1');
};
/*
 * JSONから住所を取得するForCart
 */
var getAddressFromZipCodeForCart = function(target){
	var zipcode1 = target + "[zipcode1]";
	var zipcode2 = target + "[zipcode2]";
	var prefecture = target + "[prefecture]";
	var city = target + "[city]";
	var address1 = target + "[address1]";
	var address2 = target + "[address2]";
	AjaxZip2.JSONDATA = '/js/zip/data';
	AjaxZip2.zip2addr(zipcode1,prefecture,city,zipcode2,address2,address1);
};

/*
 * カートでセレクトボックスから数量を更新する
 */
var updateQuantity = function(id){
	var product = "product_id_" + id;
	var quantity_id = "quantity_" + id;
	
	//valueを取得
	var product_id = document.getElementById(product).value;
	var selector = document.getElementById(quantity_id);
	var quantity = selector.options[selector.selectedIndex].value;
	
	//valueをセットしてサブミット
	document.updateCart.product_id.value = product_id;
	document.updateCart.quantity.value = quantity;
	document.updateCart.submit();
};

/*
 * お気に入り登録します。
 */
var addFavorite = function(url){
	var requestUrl = url;
	var ajax = new Ajax.Request(requestUrl,{method:'post',
		onComplete:function(o){
		contents = o.responseText;
		document.getElementById("message").innerHTML = contents;
		}
	});
};

/*
 * お知らせを開く
 */
var openInfo = function(id){
	var target_id = id;
	var obj = document.getElementById("info_"+target_id);
	var obj_navi = document.getElementById("info_navi_"+target_id);
	if(obj.style.display == ""){
		obj.style.display = "none";
		obj_navi.innerHTML = "[詳細]";
	}else{
		obj.style.display = "";
		obj_navi.innerHTML = "[閉じる]";
	}
	return false;
};



/**
 * セレクトボックスの値を動的に切り替える
 */
var getSubSelect = function(parent_id,id,target_id,url){
	//パラメータを初期化
	var requestUrl = "/arrival/"+url;
	var select_id = "brand_id";
	var inputName = "brand";
	var next = id + 1;
	var next2 = next + 1;
	var selector = document.getElementById(inputName+"_"+parent_id+"_"+id+"_id");
	var selectValue = selector.options[selector.selectedIndex].value;
	var selectText = selector.options[selector.selectedIndex].text;
	
	//選択した値が空だった場合はなにもしない
	if(selectValue == ""){
		for(var target_empty = target_id;target_empty < 10;target_empty++){
			document.getElementById(select_id+"_"+parent_id+"_"+target_empty).innerHTML = "";
		}
		return;
	}
	
	//選択された値からJSONをリクエスト
	new Request.JSON({
		url:requestUrl+"/"+selectValue,
		onComplete:function(categories){
			var optionItems = new Array();
			if(categories != null && categories != ""){
				optionItems.push(' &gt; <select id="'+inputName+'_'+parent_id+'_'+next+'_id" name="brand['+parent_id+'][id]" onchange="getSubSelect('+parent_id+','+next+','+next2+',\''+url+'\');">\n');
		        optionItems.push('<option value="">選択してください</option>\n');
		        for (key in categories) {
		        	if(categories[key].id != null){
		        		optionItems.push('<option value="' + categories[key].id + '">' + categories[key].name + '</option>\n');
		        	}
				}
		        optionItems.push('</select>');
		        //JSONから生成したセレクトボックスを格納
		        document.getElementById(select_id+"_"+parent_id+"_"+target_id).innerHTML = optionItems.join();
		        //それ以降のセレクトボックスがあったらそれを削除
				for(var target_empty = target_id+1;target_empty < 10;target_empty++){
					document.getElementById(select_id+"_"+parent_id+"_"+target_empty).innerHTML = "";
				}
			}else{
				//最後の選択の値をhiddenに格納。
				//最後の選択が「その他」だった場合テキストボックスを表示
				if(selectText == "その他"){
					document.getElementById(select_id+"_"+parent_id+"_"+target_id).innerHTML = " &gt; <input type='text' name='brand["+parent_id+"][other]' value='入力してください' />";
				}else{
					document.getElementById(select_id+"_"+parent_id+"_"+target_id).innerHTML = "";
				}
				//それ以降のセレクトボックスがあったらそれを削除、ないけど
				for(var target_empty = target_id+1;target_empty < 10;target_empty++){
					document.getElementById(select_id+"_"+parent_id+"_"+target_empty).innerHTML = "";
				}
			}
		}
	}).post();
};
