﻿//控制是否可以送出訂單(須等最後的結算明細秀出來後才會被設定為true
var bCanSendOrder = false;

//按下修改
function doModify(pid, qty_id)
{
	var qty = document.getElementById(qty_id).value;
	window.location.href = 'shopping.php?pid=' + pid + '&qty=' + qty + '&act=Modify';
}

//顯示或隱藏指定id的div(visible=true:顯示; visible=false:隱藏)
function showDiv(divid, visible)
{
	var d = document.getElementById(divid);
	if(d != null)
	{
		if(visible)
			d.style.display = 'inline';	// or block
		else
			d.style.display = 'none';
	}
}

String.prototype.Trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.LTrim = function()
{
	return this.replace(/(^\s*)/g, "");
}

String.prototype.RTrim = function()
{
	return this.replace(/(\s*$)/g, "");
}

function isEmail(email)
{
	if (email == "") return false;
	reEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return reEmail.test(email);
}

function showStep_1()
{
	showDiv('step_1', true);
	showDiv('step_2', false);
	showDiv('step_3', false);
	showDiv('step_4', false);
	showDiv('step_5', false);
}

function showStep_2()
{
	showDiv('step_1', false);
	showDiv('step_2', true);
	showDiv('step_3', false);
	showDiv('step_4', false);
	showDiv('step_5', false);
	
	//驗證特約廠商的程式
	setPasswdStatus();
	showDiv('span_validating', false);
	showDiv('span_validate_ok', false);
	showDiv('span_validate_fail', false);
}

function showStep_3()
{
	if(document.getElementById('step_2_UserName').value.Trim() == "")
	{
		alert("請填寫姓名！");
		document.getElementById('step_2_UserName').focus();
		return;
	}
	
	if(document.getElementById('step_2_shipping_sel').value.Trim() == "0")
	{
		alert("請選擇寄送方式");
		document.getElementById('step_2_shipping_sel').focus();
		return;
	}
	
	if(document.getElementById('step_2_Address').value.Trim() == "")
	{
		alert("請填寫地址！");
		document.getElementById('step_2_Address').focus();
		return;
	}
	
	if(document.getElementById('step_2_Zip').value.Trim() == "")
	{
		alert("請填寫郵遞區號！");
		document.getElementById('step_2_Zip').focus();
		return;
	}
	else if(document.getElementById('step_2_Zip').value.Trim().length != 3 &&
		document.getElementById('step_2_Zip').value.Trim().length != 5)
	{
		alert("郵遞區號應為3碼或5碼！");
		document.getElementById('step_2_Zip').focus();
		return;
	}
	
	if(document.getElementById('step_2_Phone').value.Trim() == "")
	{
		alert("請填寫聯絡電話！");
		document.getElementById('step_2_Phone').focus();
		return;
	}
	
	var email = document.getElementById('step_2_EMail').value;
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)))
	{
		alert("電子郵件格式錯誤請確實填寫！");
		document.getElementById('step_2_EMail').focus();
		return;
	}
	
	if (document.getElementById('step_2_SpCustomer').value != '0')
	{
		//不驗證，要求要輸入即可
		if(document.getElementById('step_2_Passwd').value.Trim() == "")
		{
			alert("請填寫" + document.getElementById('hint').innerHTML + "！");
			document.getElementById('step_2_Passwd').focus();
			return;
		}
		else
			showStep_3a();
			
		//有選特約廠商，要驗證
		/*
		showDiv('span_validating', true);
		validateSpecialCustomer(document.getElementById('step_2_SpCustomer').value, document.getElementById('step_2_Passwd').value);*/
	}
	else
	{
		//沒有選特約廠商，直接跳下一步
		showStep_3a();
	}
}

function showStep_3a()
{
	//驗證特約廠商OK
	if (document.getElementById('step_2_SpCustomer').value != '0')
	{
		showDiv('span_validating', false);
		showDiv('span_validate_ok', true);
	}
	
	showDiv('step_1', false);
	showDiv('step_2', false);
	showDiv('step_3', true);
	showDiv('step_4', false);
	showDiv('step_5', false);
	
	//取得最後的購物車資料
	document.getElementById('step_3_BuyItemList').innerHTML = '<img src="../images/ajax-loader.gif">載入中...';
	bCanSendOrder = false;
	var cus_id = "0";
	cus_id = document.getElementById('step_2_SpCustomer').value;
	selshipping= document.getElementById('step_2_shipping_sel').selectedIndex;
	seleted_value=document.getElementById('step_2_shipping_sel').value;
	getShoppingList(cus_id, seleted_value);
	
	//複製step2填的資料至step3
	document.getElementById('step_3_UserName').innerHTML = document.getElementById('step_2_UserName').value;
	document.getElementById('step_3_Address').innerHTML = document.getElementById('step_2_Address').value;
	document.getElementById('step_3_Zip').innerHTML = document.getElementById('step_2_Zip').value;
	document.getElementById('step_3_Phone').innerHTML = document.getElementById('step_2_Phone').value;
	document.getElementById('step_3_EMail').innerHTML = document.getElementById('step_2_EMail').value;
	document.getElementById('step_3_Remark').innerHTML = document.getElementById('step_2_Remark').value.replace(/\n/g, '<br />');
	document.getElementById('step_3_shipping').innerHTML=document.getElementById('step_2_shipping_sel').options[selshipping].text;
}

function showStep_3b()
{
	showDiv('span_validating', false);
	showDiv('span_validate_fail', true);
}

function showStep_4()
{
	if (bCanSendOrder == false)
	{
		alert('等待購買明細載入中，請稍後...');
		return;
	}
	
	showDiv('step_1', false);
	showDiv('step_2', false);
	showDiv('step_3', false);
	showDiv('step_4', true);
	showDiv('step_5', false);
	sendOrder(document.getElementById('step_2_UserName').value, document.getElementById('step_2_Address').value, 
		document.getElementById('step_2_Zip').value, document.getElementById('step_2_Phone').value, 
		document.getElementById('step_2_EMail').value, document.getElementById('step_2_Remark').value, 
		document.getElementById('step_2_SpCustomer').value, document.getElementById('step_2_Passwd').value,
		document.getElementById('step_2_shipping_sel').value);
}

//選擇特約廠商的話，顯示驗證欄位
function setPasswdStatus()
{
	if (document.getElementById('step_2_SpCustomer').value != '0')
	{
		showDiv('span_passwd', true);
		var sel = document.getElementById('step_2_SpCustomer');
		var len = sel.options.length;
		for(var i=0; i<len; i++)
		{
			if(sel.options[i].selected == true)
			{
				var sel2 = document.getElementById('step_2_SpCustomer_Hint');
				document.getElementById('hint').innerHTML = sel2.options[i].text;
				return;
			}
		}
	}
	else
		showDiv('span_passwd', false);
}

//送出訂單
function sendOrder(username, address, zip, phone, email, remark, cid, passwd, shipping)
{
	var url = "shopping_code.php";
	request.open("POST", url, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.onreadystatechange = finishOrder;
	request.send("username="+username+"&address="+address+"&zip="+escape(zip)+"&phone="+escape(phone)+"&email="+escape(email)+"&remark="+remark+"&cid="+cid+"&passwd="+passwd+"&shipping="+shipping);
}

function finishOrder()
{
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			//var rt = request.responseText;
			var rt = eval('(' + request.responseText + ')');
			
			//確認回傳結果
			if(rt.result != 'OK')
			{
				showDiv('step_1', false);
				showDiv('step_2', false);
				showDiv('step_3', false);
				showDiv('step_4', false);
				showDiv('step_5', false);
				showDiv('step_6', true);
				
				alert(rt.msg);
			}
			else
			{
				var orderno = rt.orderno;
				var authID = rt.authid;
	
				document.getElementById('OrderNo').innerHTML = orderno;
				document.getElementById('AuthID').innerHTML = authID;
				
				showDiv('step_1', false);
				showDiv('step_2', false);
				showDiv('step_3', false);
				showDiv('step_4', false);
				showDiv('step_5', true);
				showDiv('step_6', false);
			}
		}
	}
}

function showshipinfo()
{
	if (document.getElementById('step_2_shipping_sel').value==1)
	{
		document.getElementById('shipinfo').innerHTML="";
	}
	if (document.getElementById('step_2_shipping_sel').value==2)
	{
		document.getElementById('shipinfo').innerHTML="消費滿千元免手續費";
	}
	if (document.getElementById('step_2_shipping_sel').value==3)
	{
		document.getElementById('shipinfo').innerHTML="須先轉帳，全家未提供貨到付款服務，消費滿千元免手續費，請至<a href='http://www.famiport.com.tw/shop.html' target='blank'> FarmiPort </a>查詢收件店名或店號並填入備註欄";
	}
	if (document.getElementById('step_2_shipping_sel').value==4)
	{
		document.getElementById('shipinfo').innerHTML="須先轉帳，7-11未提供貨到付款服務，消費滿千元免手續費，請至<a href='http://emap.pcsc.com.tw/01.htm' target='blank'> 7-11網頁 </a>查詢收件店名或店號並填入備註欄";
	}
	
}