/* -------------------------------------------------------------
   
------------------------------------------------------------- */
function func_Location(url) {
	location.href = url;
}

function func_Submit(url) {
	document.fm.action = url;
	document.fm.submit();
}

function func_SubmitConfirm(id) {
	$("member_address_id").value = id;
	document.fm.action = "confirm.php";
	document.fm.submit();
}

function func_ChangeImage(id, file) {
	$(id).src = 'common/images/item/' + file;
}

function func_Search(kwd) {
	if(kwd == "") {
		alert("検索キーワードを入力してください");
		return false;
	} else {
		$("kwd").value = encodeURI(kwd);
		return true;
	}
}

function func_NumberFormat(str) {
	var num = new String(str).replace(/,/g, "");
	while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	return num;
}

function func_DeleteFavorite(id) {
	var conf = confirm("この商品をお気に入りから削除します。\nよろしいですか。");
	if(conf) {
		location.href = "favoriteout.php?id=" + id;
	} else {
		return false;
	}
}

function func_DeleteAddress() {
	var conf = confirm("このお届け先を削除します。\n本当によろしいですか？");
	
	if(conf) {
		$("delete_flag").value = 1;
		$("fm").action = "address.php";
		$("fm").submit();
		return true;
	} else {
		return false;
	}
}


/* -------------------------------------------------------------
   
------------------------------------------------------------- */
function func_AddRow(item, price) {
	if(item.length == 8) {
		var table = document.getElementById("tableid1").firstChild;
		
		if(!table.tagName) {
			table = document.getElementById("tableid1");
		}
		
		var rowCount = table.rows.length;
		var tr = document.createElement("tr");
		
		// no
		var td1 = document.createElement("td");
		td1.innerHTML = rowCount;
		var tx1 = document.createElement("input");
		tx1.setAttribute("type", "hidden");
		tx1.setAttribute("id", "item_flag_" + rowCount);
		tx1.setAttribute("name", "item_flag_" + rowCount);
		tx1.setAttribute("value", "0");
		td1.appendChild(tx1);
		
		// item_id
		var td2 = document.createElement("td");
		var tx2 = document.createElement("input");
		tx2.setAttribute("type", "text");
		tx2.setAttribute("id", "item_id_" + rowCount);
		tx2.setAttribute("name", "item_id_" + rowCount);
		tx2.setAttribute("size", "11");
		tx2.setAttribute("maxlength", "8");
		tx2.setAttribute("style", "ime-mode:disabled;");
		tx2.onkeyup = function(){func_SearchItem(this.value, rowCount, price); func_AddRow(this.value, price); };
		td2.appendChild(tx2);
		
		// item_name
		var td3 = document.createElement("td");
		var tx3 = document.createElement("span");
		tx3.setAttribute("id", "item_name_" + rowCount);
		td3.appendChild(tx3);
		
		// item_capacity
		var td4 = document.createElement("td");
		var tx4 = document.createElement("span");
		tx4.setAttribute("id", "item_capacity_" + rowCount);
		td4.appendChild(tx4);
		
		// item_color
		var td5 = document.createElement("td");
		var tx5 = document.createElement("span");
		tx5.setAttribute("id", "item_color_" + rowCount);
		td5.appendChild(tx5);
		
		// item_price
		var td6 = document.createElement("td");
		td6.setAttribute("align", "right");
		var tx6 = document.createElement("span");
		tx6.setAttribute("id", "item_price_" + rowCount);
		td6.appendChild(tx6);
		
		// item_number
		var td7 = document.createElement("td");
		var tx7 = document.createElement("input");
		tx7.setAttribute("type", "text");
		tx7.setAttribute("id", "item_number_" + rowCount);
		tx7.setAttribute("name", "item_number_" + rowCount);
		tx7.setAttribute("size", "5");
		tx7.setAttribute("maxlength", "3");
		tx7.setAttribute("style", "ime-mode:disabled;");
		tx7.onkeyup = function(){func_GetItemPrice(rowCount);};
		td7.appendChild(tx7);
		
		// total_price
		var td8 = document.createElement("td");
		td8.setAttribute("align", "right");
		var tx8 = document.createElement("span");
		tx8.setAttribute("id", "total_price_" + rowCount);
		td8.appendChild(tx8);
		
		//
		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
		tr.appendChild(td4);
		tr.appendChild(td5);
		tr.appendChild(td6);
		tr.appendChild(td7);
		tr.appendChild(td8);
		
		table.appendChild(tr);
		
		$("row_count").value = rowCount;
	}
}

//
function func_SearchItem(item, i, price) {
	if(item.length == 8) {
		var filename = "ajax_search.php?item=" + item + "&price=" + price;
		
		new Ajax.Request(
			filename, 
			{ method: 'get', onComplete: function(objHttp){
				func_GetItem(objHttp, i);
			}
		});
	} else {
		$("item_flag_" + i).value = 0;
		$("item_name_" + i).innerHTML = "";
		$("item_capacity_" + i).innerHTML = "";
		$("item_color_" + i).innerHTML = "";
		$("item_price_" + i).innerHTML = "";
		$("item_number_" + i).value = "";
		$("total_price_" + i).innerHTML = "";
	}
}

function func_GetItem(objHttp, i) {
	var strItem = objHttp.responseText;
	var aryItem = strItem.split("\t");
	
	if(aryItem[0] != "該当商品はありません") {
		$("item_flag_" + i).value = aryItem[3];
		$("item_name_" + i).innerHTML = aryItem[0];
		$("item_capacity_" + i).innerHTML = aryItem[1];
		$("item_color_" + i).innerHTML = aryItem[2];
		$("item_price_" + i).innerHTML = "\\" + func_NumberFormat(aryItem[3]);
		$("item_number_" + i).value = 1;
		$("total_price_" + i).innerHTML = "\\" + func_NumberFormat(aryItem[3]);
	} else {
		$("item_name_" + i).innerHTML = aryItem[0];
	}
}

function func_GetItemPrice(i) {
	var item_price = $("item_price_" + i).innerHTML;
	var item_number = $("item_number_" + i).value;
	
	item_price = item_price.replace("\\", "");
	item_price = item_price.replace(",", "");
	
	var sub_total_price = item_price * item_number;
	
	$("total_price_" + i).innerHTML = "\\" + func_NumberFormat(sub_total_price);
}









/* -------------------------------------------------------------
   cart
------------------------------------------------------------- */
function func_CartIn(id, number, price) {
	if(isNaN(number) || number == "") {
		alert("数量を数値でご入力ください");
	} else {
		location.href = "../cart/cartin.php?id=" + id + "&number=" + number + "&price=" + price;
	}
}

function func_DisableMemberAddress(flag) {
	if(flag) {
		//$("jp_member_address_name").disabled = true;
		//$("jp_member_address_name").style.background = "#efefef";
	} else {
		
	}
}

function func_SubmitPayment(id, flag) {
	$("member_address_id").value = id;
	$("member_worldwide_flag").value = flag;
	document.fm1.action = "payment.php";
	document.fm1.submit();
}

function func_SetPaymentID(flag) {
	$("payment_flag").value = flag;
}


function func_ExtractExport(flag, id) {
	var filename = "export.php?worldwide_flag=" + flag;
	new Ajax.Request(
		filename, 
		{ method: 'get', onComplete: function(objHttp){
			func_DispExport(objHttp, flag, id);
		}
	});
}

function func_DispExport(objHttp, flag, id) {
	var strText = objHttp.responseText;
	var aryRows = strText.split(/\r\n|\r|\n/);
	$("s").removeChild($("s").firstChild);
	
	
	var select = document.createElement("select");
	select.setAttribute("id", "export_id");
	select.setAttribute("name", "export_id");
	$("s").appendChild(select);
	
	for(i=0; i<aryRows.length; i++) {
		if(aryRows[i] != "") {
			aryRow = aryRows[i].split(",");
			var option = document.createElement("option");
			option.setAttribute("value", aryRow[0]);
			if(aryRow[0] == id) { option.setAttribute("selected", "true"); }
			var text = document.createTextNode(aryRow[1]);
			option.appendChild(text);
			select.appendChild(option);
		}
	}
	
	/*
	for(i=0; i<aryRows.length; i++) {
		if(aryRows[i] != "") {
			var aryRow = strRows[i].split(",");
			var opt = document.createElement("option");
			opt.setAttribute("value", aryRow[0]);
			if(aryRow[0] == id) { opt.setAttribute("selected", "true"); }
			var text = document.createTextNode(aryRow[1]);
			opt.appendChild(text);
			$("export_id").appendChild(opt);
		}
	}
	*/
	
}








function func_Order(yy, mm, dd) {
	var date = yy + "-" + mm + "-" + dd;
	location.href = "index.php?date=" + date;
}