// product.js

function ChangePage(rec_cnt) {
	//alert(document.URL);
	var url_str=document.URL;
	Act_data=url_str.split("/");

	var new_action=Act_data[Act_data.length - 1];

	//alert("new action="+new_action);

	document.pageform.action=new_action;
	document.pageform.FV_offset.value=rec_cnt;
	document.pageform.submit();
}

function ChangeCategory(myid) {
	with(document.CategoryForm) {
		ViewCategoryID.value=myid;
		submit();
	}
}

// for order

function OrderNow(myform,myID,myType) {
	with(myform) {

		ItemInfo.value=myType+"^"+myID+"^1";

		submit();
	}
}


function KeepShop(myform,my_url) {
	with(myform) {
		if(! UpdateShopQuantity(myform))
		{
			return false;
		}

		UpdType.value="UpdateQuantity";
		ReturnURL.value=my_url;
		submit();
	}
}

function GoPay(myform) {
	with(myform) {

		if(! UpdateShopQuantity(myform))
		{
			return false;
		}

		UpdType.value="Finish";
		submit();
	}
}

function CheckIsNumber(myObj) {

	var myQuantity=parseInt(myObj.value,10);
	if (isNaN(myQuantity) || myQuantity<=0)
	{
		alert('購買數量必須是正整數！');
		myObj.focus();
		myObj.select();

		return false;
	}
	else
	{
		myObj.value=myQuantity;
	}

	return true;
}

function UpdateShopQuantity(myform){
	with(myform){
		var QuantityStr="";
		if(isNaN(Quantity.length))
		{
			QuantityStr=Quantity.value;
			if(! CheckIsNumber(Quantity))
			{
				return false;
			}
		}
		else
		{
			var inx=0;
			for(inx=0;inx<Quantity.length;inx++)
			{
				if(! CheckIsNumber(Quantity[inx]))
				{
					return false;
				}

				if(QuantityStr.length==0)
				{
					QuantityStr = Quantity[inx].value;
				}
				else
				{
					QuantityStr = QuantityStr + "," + Quantity[inx].value;
				}
			}
		}
		//alert("QuantityStr=["+QuantityStr+"]");
		ItemInfo.value=QuantityStr;
		return true;
		//myform.action="/product/shop_upd.php";
		//submit();
	}
}//end UpdatShopQuantity

function CheckQuantity(myObj) {

	if(! CheckIsNumber(myObj))
	{
		return false;
	}

	return;
}

function CheckAllQuantity(myform) {
	with(myform){
		UpdateShopQuantity(myform);
		UpdType.value="UpdateQuantity";
		return true;
	}
}

function DeletItemFromList(myItem) {
 	with(document.ShopForm){
		if (confirm("是否確定從購物車中刪除？"))
		{
			UpdType.value="Delete";
			ItemInfo.value=myItem;
			submit();
		}
 	}
}


function CheckOrderForm(myform){
  with (myform) {

	if (Contact.value.length == 0 ) {
		alert("請填寫收貨人真實姓名，方便連絡送貨事宜。")
		Contact.focus();
		return false;
	}

	if (PhoneD.value.length == 0 ) {
		alert("請填寫收貨人白天聯絡電話！")
		PhoneD.focus();
		return false;
	}

	if (PhoneN.value.length == 0 ) {
		alert("請填寫收貨人夜間聯絡電話！")
		PhoneN.focus();
		return false;
	}

	if (Mobile.value.length == 0 ) {
		alert("請填寫收貨人手機號碼！")
		Mobile.focus();
		return false;
	}

	if (Address.value.length == 0 ) {
		alert("請填寫送貨地址！")
		Address.focus();
		return false;
	}

	if(Invoice[1].checked)
	{
		if (InvoiceNumber.value.length == 0 ) {
			alert("三聯式發票必須填寫統一編號。")
			InvoiceNumber.focus();
			return false;
		}

		if (InvoiceTitle.value.length == 0 ) {
			alert("三聯式發票必須填寫發票抬頭。")
			InvoiceTitle.focus();
			return false;
		}
	}

	if(!PayType[0].checked && !PayType[1].checked)
	{
		alert("請選擇付款方式！")
		PayType[0].focus();
		return false;
	}

  }
	return true;
}


function ShowAlertMessage(myType) {

	if(myType=="0")
	{
		alert("訂購程序尚未完成，請按下 [訂單確認] 按鈕完成訂購程序！");
	}
	else if(myType=="1")
	{
		alert("訂購程序尚未完成，請按下 [進入刷卡畫面] 按鈕完成付款程序！");
	}

	return;
}





// OLD
function ChangeProductPrice(myform) {
	with(myform) {

		var myMaterial=Material.options[Material.selectedIndex].value;
		var myMaterialText=Material.options[Material.selectedIndex].text;

		document.getElementById("Price").innerHTML = "<font size='2' color='red'>US $" + eval("Price_"+myMaterial)+"<br>("+myMaterialText+")</font>";

	}

}



function Buy(myItemNo) {
	with(document.ListForm) {

		BuyItemNo.value=myItemNo;

		ItemInfo.value="AS^"+BuyItemNo.value+"^^^^"+BuyQuantity.value;
		//alert(ItemInfo.value);

		submit();
	}
}


function Pay(myform,myURL) {
	with(myform) {
		if(Payment[0].checked)
		{
			action=myURL;
		}

		return true;
	}
}

function ReTryPay(myform,myURL) {
	with(myform) {
		//action=myURL;
		return true;
	}
}

