var xmlhttp = false;
//var http = "http://shopping.worldfamily.com.tw/";
//var https = "https://shopping.worldfamily.com.tw/";
var http = "";
var https = "";
function showPage(page)
{
	if (document.all("process").style.visibility == "visible") retrun;

	if (page == "LeadsSave")
	{
		if (!checkLeads()) return;
		if (!checkAddress("Address","連絡")) return;
		if (document.all("Mobile").value.length < 10)
		{
			alert("行動電話號碼錯誤");
			document.all("Mobile").focus();
			return;
		}
		var parameter;
		parameter = "page=" + document.all("Page").value;
		parameter += "&Email=" + document.all("Email").value;
		parameter += "&Name=" + document.all("Name").value;
		if (document.all("Sex")[0].checked)
			parameter += "&Sex=2";
		else
			parameter += "&Sex=1";
		parameter += "&Year=" + document.all("Year").value;
		parameter += "&Month=" + document.all("Month").value;
		parameter += "&Day=" + document.all("Day").value;
		parameter += "&TelO=" + document.all("TelO").value;
		parameter += "&TelH=" + document.all("TelH").value;
		parameter += "&Mobile=" + document.all("Mobile").value;
		parameter += "&zip_code=" + document.all("zipcode").value;
		parameter += "&Address=" + document.all("Address").value;

	  	xmlhttp = newXmlHttp();
		xmlhttp.open("POST","page.aspx",false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
			{
				document.all("send").disabled = false;
				if ( xmlhttp.responseText == "success")
				{
					window.location.href = "page.aspx?page=LeadsFb";
				}
				else
				{
					alert(xmlhttp.responseText);
				}
			}
		}
		document.all("send").disabled = true;
		xmlhttp.send(parameter);
	}
	else if (page == "login")
	{
		if (!isEmail(document.all("account"))) return;
		if (document.all("Password").value == "")
		{
			alert("密碼錯誤");
			return;
		}

		var parameter = "page=" + page + "&Email=" + document.all("account").value + "&Password=" + document.all("Password").value;
	  	xmlhttp = newXmlHttp();
		xmlhttp.open("POST","page.aspx",false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
			{
				if ( xmlhttp.responseText == "success")
				{
					if (window.location.href.indexOf("logout") == -1)
						window.location.reload();
					else
						window.location.href = http + "page.aspx";
				}
				else
				{
					document.all("Password").value = "";
					alert(xmlhttp.responseText);
				}
			}
		}
		xmlhttp.send(parameter);
	}
	else if (page == "forget")
	{
		if (!isEmail(document.all("account"))) return;

		var parameter = "page=" + page + "&Email=" + document.all("account").value;
	  	xmlhttp = newXmlHttp();
		xmlhttp.open("POST","page.aspx",false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
			{
				document.all("account").value = "";
				alert(xmlhttp.responseText);
			}
		}
		xmlhttp.send(parameter);
	}	
	else if (page == "search")
	{
		var low,high;
		var index = document.all("selectPrice").selectedIndex;
		if (index == 1)
			low = "1";
		else
			low = parseInt(document.all("selectPrice").options[index-1].value) + 1;
		high = document.all("selectPrice").options[index].value;
		window.location.href = http + "page.aspx?page=search&Type=search&name=search&low=" + low + "&high=" + high;
	}
	else if (page == "shopdata_add")
	{
		var parameter;
		parameter = "page=" + page;

		if (document.all("Name").value == "")
		{
			alert("請輸入中文姓名");
			document.all("Name").focus();
			return;
		}
		if (document.all("Email").value == "")
		{
			alert("請輸入Email");
			document.all("Email").focus();
			return;
		}
		if (document.all("Tel_d").value == "")
		{
			alert("請輸入日間連絡電話");
			document.all("tel_d").focus();
			return;
		}
		if (document.all("Tel_n").value == "")
		{
			alert("請輸入夜間連絡電話");
			document.all("tel_n").focus();
			return;
		}
		if (document.all("Mobile").value == "")
		{
			alert("請輸入行動電話");
			document.all("Mobile").focus();
			return;
		}
		if (document.all("Mobile").value.length < 10)
		{
			alert("行動電話號碼錯誤");
			document.all("Mobile").focus();
			return;
		}
		if (document.all("zipcode").value == "")
		{
			alert("請輸入郵遞區號");
			document.all("zip_code").focus();
			return;
		}
		if (!checkAddress("Address","連絡")) return;

		parameter += "&Email=" + document.all("Email").value;
		parameter += "&Name=" + document.all("Name").value;
		if (document.all("Sex",0).checked)
			parameter += "&Sex=2";
		else
			parameter += "&Sex=1";
		parameter += "&Tel_d=" + document.all("Tel_d").value;
		parameter += "&Tel_n=" + document.all("Tel_n").value;
		parameter += "&Mobile=" + document.all("Mobile").value;
		parameter += "&zip_code=" + document.all("zipcode").value;
		parameter += "&Address=" + document.all("Address").value;

		if (document.all("shop_Name").value == "")
		{
			alert("請輸入收件人姓名");
			document.all("shop_Name").focus();
			return;
		}
		if (document.all("shop_Mobile").value == "")
		{
			alert("請輸入收件人電話");
			document.all("shop_Mobile").focus();
			return;
		}
		if (document.all("zipcode1").value == "")
		{
			alert("請輸入收件人郵遞區號");
			document.all("zip_code1").focus();
			return;
		}
		if (!checkAddress("shop_Address","收件人")) return;

		parameter += "&shop_Name=" + document.all("shop_Name").value;
		parameter += "&shop_Mobile=" + document.all("shop_Mobile").value;
		parameter += "&zip_code1=" + document.all("zipcode1").value;
		parameter += "&shop_Address=" + document.all("shop_Address").value;

	  	xmlhttp = newXmlHttp();
		xmlhttp.open("POST",https + "page.aspx",false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
			{
				if ( xmlhttp.responseText == "success")
				{
					window.location.href = https + "page.aspx?page=shoppay";
				}
				else
				{
					alert(xmlhttp.responseText);
				}
			}
		}
		xmlhttp.send(parameter);
	}
	else if (page == "shopdata_pay")
	{
		var parameter;
		var cardno = (document.all("cardno1").value+document.all("cardno2").value+document.all("cardno3").value+document.all("cardno4").value);
		parameter = "Page=" + page;
		parameter += "&Name=" + document.all("Name").value;
		if (document.all("pay_type",0).checked)
			parameter += "&pay_type=2";
		else
		{
			parameter += "&pay_type=3";
			if (!checkCard("cardno1",cardno)) return;
			if (!checkCardDue("due_year","due_month")) return;
			if (!checkID("id")) return;

		}
		parameter += "&id=" + document.all("id").value;
		if (!document.all("gui_type",0).checked)
		{
			parameter += "&gui_type=2";
		}
		else
		{
			if (document.all("gui_no").value == "")
			{
				alert("請輸入統一編號");
				document.all("gui_no").focus();
				return;
			}
			if (!checkGUI("gui_no")) return;
			if (document.all("gui_title").value == "")
			{
				alert("請輸入發票抬頭");
				document.all("gui_title").focus();
				return;
			}
			parameter += "&gui_type=3";
		}
		if (document.all("zipcode").value == "")
		{
			alert("請輸入郵遞區號");
			document.all("zip_code").focus();
			return;
		}
		if (!checkAddress("gui_address","發票")) return;

		if (document.all("chkGive",0).checked)
		{
			parameter += "&chkGive=Y";
		}
		else
		{
			parameter += "&chkGive=N";
		}
		parameter += "&gui_no=" + document.all("gui_no").value;
		parameter += "&gui_title=" + document.all("gui_title").value;
		parameter += "&gui_address=" + document.all("gui_address").value;
		parameter += "&zip_code=" + document.all("zipcode").value;
		parameter += "&card_no=" + cardno;	
		parameter += "&due_date=" + (document.all("due_year").value+"/"+document.all("due_month").value);

	  	xmlhttp = newXmlHttp();
		xmlhttp.open("POST",https + "page.aspx",true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
			{
				showProcess("process");
				if ( xmlhttp.responseText == "success")
				{
					window.location.href = http + "page.aspx?page=shopfb";
				}
				else
				{
					alert(xmlhttp.responseText);
				}
			}
		}
		showProcess("process");
		xmlhttp.send(parameter);
	}
	else if (page == "contact_send")
	{
		if (!isEmail(document.all("email"))) return;
		var parameter = "page=" + page + "&Email=" + document.all("email").value;
		parameter += "&name=" + document.all("name").value;
		parameter += "&tel="+document.all("tel").value;
		parameter += "&notes="+document.all("usernote").value;

	  	xmlhttp = newXmlHttp();
		xmlhttp.open("POST","page.aspx",false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
			{
				if ( xmlhttp.responseText == "success")
				{
					window.location.href = http + "page.aspx?page=contactfb";
				}
				else
				{
					alert(xmlhttp.responseText);
				}
			}
		}
		xmlhttp.send(parameter);
	}	
	else if (page == "savepass")
	{
		if (document.all("newpass1").value != document.all("newpass2").value)
		{
			alert("兩組新密碼不相同 !!");
			document.all("newpass1").focus();
			return;
		}
		if (!isAlpha(document.all("newpass1").value))
		{
			alert("新密碼含有不屬於數字或英文字母的字元 !! 請重新輸入");
			document.all("newpass1").focus();
			return;
		}
		var parameter = "page=" + page + "&oldpass=" + document.all("oldpass").value;
		parameter += "&newpass=" + document.all("newpass1").value;

	  	xmlhttp = newXmlHttp();
		xmlhttp.open("POST","page.aspx",false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
			{
				if ( xmlhttp.responseText == "success")
				{
					window.location.href = http + "page.aspx?page=changepassword_fb";
				}
				else
				{
					alert(xmlhttp.responseText);
				}
			}
		}
		xmlhttp.send(parameter);
	}	
	else
	{
		if (page == "shopdata")
			window.location.href = https + "page.aspx?page=" + page;
		else
			window.location.href = http + "page.aspx?page=" + page;
	}
}
function showProduct(type,name)
{
	if (document.all("process").style.visibility == "visible") retrun;

	window.location.href = http + "page.aspx?page=product&Type=" + type + "&name=" + name;
}
function showDetails(no)
{
	if (document.all("process").style.visibility == "visible") retrun;

	window.location.href = "page.aspx?page=Details&No=" + no;
}
function openBanner(url)
{
	window.open(url);
}
function addCart(no,name,price,type)
{
	var parameter = "page=addcart&No=" + no + "&prodname=" + name + "&price=" + price + "&type=" + type;
  	xmlhttp = newXmlHttp();
	xmlhttp.open("POST","page.aspx",false);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
		{
			if (xmlhttp.responseText == "success")
				window.location.href = "page.aspx?page=cart";
			else
				alert(xmlhttp.responseText);
		}
	}
	xmlhttp.send(parameter);
}
function checkLeads()
{
	if (!isEmail(document.all("Email"))) return false;
	if (document.all("Name").value == "")
	{
		alert("姓名不可空白");
		document.all("Name").focus();
		return false;
	}
	if (document.all("Year").value == "")
	{
		alert("生日年份不可空白");
		document.all("Year").focus();
		return false;
	}
	if (document.all("Month").value == "")
	{
		alert("生日月份不可空白");
		document.all("Month").focus();
		return false;
	}
	if (document.all("Day").value == "")
	{
		alert("生日日期不可空白");
		document.all("Day").focus();
		return false;
	}
	if (!CheckDate(document.all("Year"),document.all("Year").value+document.all("Month").value+document.all("Day").value)) return false;
	if (document.all("TelO").value == "")
	{
		alert("白天電話不可空白");
		document.all("TelO").focus();
		return false;
	}
	if (document.all("TelH").value == "")
	{
		alert("夜間電話不可空白");
		document.all("TelH").focus();
		return false;
	}
	if (document.all("Mobile").value == "") 
	{
		alert("行動電話不可空白");
		document.all("Mobile").focus();
		return false;
	}
	if (document.all("Country").value == "") 
	{
		alert("市區鄉鎮不可空白");
		document.all("Country").focus();
		return false;
	}
	if (document.all("Address").value == "") 
	{
		alert("地址不可空白");
		document.all("Address").focus();
		return false;
	}
	return true;
}

function addQuantity(name,selected)
{
	for(i=1;i<=10;i++)
	{
		var NewQuantity = document.createElement("option");
		NewQuantity.text = i;
		NewQuantity.value = i;
		if (i==selected) NewQuantity.selected = true;
		document.all(name,0).add(NewQuantity);	
	}
}
function getClick(obj)
{
	var Node = window.event.srcElement.parentNode.parentNode.parentNode;
	if (!obj.checked)
	{
		var parameter = "page=delCart&no=" + Node.childNodes[1].firstChild.nodeValue;

	  	xmlhttp = newXmlHttp();
		xmlhttp.open("POST","page.aspx",false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
			{
				if ( xmlhttp.responseText == "success")
				{
					Node.removeNode(true);
					getTotalPrice();
				}
				else
				{
					alert(xmlhttp.responseText);
				}
			}
		}
		xmlhttp.send(parameter);
	}
}
function getChange(obj)
{
	var Node = window.event.srcElement.parentNode.parentNode;
	var parameter = "page=updateCart&no=" + Node.childNodes[1].firstChild.nodeValue + "&quantity=" + (window.event.srcElement.selectedIndex + 1);

  	xmlhttp = newXmlHttp();
	xmlhttp.open("POST","page.aspx",false);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
		{
			if ( xmlhttp.responseText == "success")
			{
				getTotalPrice();
			}
			else
			{
				alert(xmlhttp.responseText);
			}
		}
	}
	xmlhttp.send(parameter);
}
function getTotalPrice()
{
	var price = 0;
	var quanity,unitPrice,portage;
	var free = false;
	for (i=2;i<document.getElementById("tbCart").firstChild.childNodes.length;i++)
	{
		quanity = document.getElementById("tbCart").firstChild.childNodes[i].childNodes[3].firstChild.selectedIndex+1;
		unitPrice = document.getElementById("tbCart").firstChild.childNodes[i].childNodes[4].childNodes[1].firstChild.nodeValue;
		price += (quanity * unitPrice)
		if (document.getElementById("tbCart").firstChild.childNodes[i].childNodes[5].firstChild.scr == "4")
			free = true;
	}
	if (price > 1000 || price == 0)
		portage = 0;
	else
	{
		if (free)
			portage = 0;
		else
			portage = 100;
	}
	document.getElementById("tbPrice").firstChild.childNodes[0].childNodes[2].childNodes[1].firstChild.nodeValue = portage;
	document.getElementById("tbPrice").firstChild.childNodes[1].childNodes[2].childNodes[1].firstChild.nodeValue = price + portage;
}
function copyData()
{
	document.all("shop_name").value = document.all("name").value;
	document.all("shop_mobile").value = document.all("mobile").value;
	document.all("city1").selectedIndex = document.all("city").selectedIndex;
	Add_Country('City1','Country1','');
	document.all("country1").selectedIndex = document.all("country").selectedIndex;
	document.all("shop_address").value = document.all("address").value;
	document.all("zipcode1").value = document.all("zipcode").value;
}
function getSelectIndex(select,key)
{
	for(var i=0;i<document.all(select).length;i++)
	{
		if (document.all(select).options[i].value.indexOf(key) != -1)
		{
			document.all(select).selectedIndex = i;	
			break;
		}
	}
}
function setZipCode(zip,country,div)
{
	document.all(zip).value = document.all(country).value.substr(1,3);
	document.all(div).style.visibility = "hidden";
}
function showObj(obj)
{
	if (document.all(obj).style.visibility == "visible")
		document.all(obj).style.visibility = "hidden";
	else
		document.all(obj).style.visibility = "visible";
}
function showProcess(obj)
{
	if (document.all(obj).style.visibility == "hidden")
	{
		document.all(obj).style.top = (screen.height-document.all(obj).height)/2;
		document.all(obj).style.left = (screen.width-document.all(obj).width)/2;
		document.all(obj).style.visibility = "visible";
	}
	else
	{
		document.all(obj).style.visibility = "hidden";
	}
}
function Add_selectMonth(obj)
{
	for(i=1;i<=12;i++)
	{
		var month = document.createElement("option");
		month.text = i;
		month.value = i;
		if (i < 10)
		{
			month.text = "0" + month.text;
			month.value = "0" + month.value;
		}
		document.all(obj,0).add(month);	
	}
}
function Add_selectYear(obj)
{
	var dt = new Date();
	for(i=0;i<10;i++)
	{
		var newYear = document.createElement("option");
		newYear.text = dt.getFullYear() + i;
		newYear.value = dt.getFullYear() + i;
		document.all(obj,0).add(newYear);	
	}
}
function checkCardDue(objYear,objMonth)
{
	var dt = new Date();
	var year = dt.getFullYear();
	var month = dt.getMonth() + 1;
	if (month < 10) month = "0" + month;
	var due = document.all(objYear).value + document.all(objMonth).value;
	if ((year.toString() + month.toString()) > due)
	{
		alert("信用卡到期日已過");
		document.all(objMonth).focus();
		return false;
	}
	return true;
}
function clearGui()
{
	document.all("gui_no").value = "";
	document.all("gui_title").value = "";
//	document.all("gui_address").value = "";
//	document.all("zipcode").value = "";
}
function clearCard()
{
	document.all("name").value = "";
	document.all("id").value = "";
	document.all("cardno1").value = "";
	document.all("cardno2").value = "";
	document.all("cardno3").value = "";
	document.all("cardno4").value = "";
	document.all("due_month").selectedIndex = -1;
	document.all("due_year").selectedIndex = -1;
}
function checkAddress(obj,msg)
{
	//window.alert(document.all(obj).value);
	
	if (document.all(obj).value == "")
	{
		alert("請輸入" + msg + "地址");
		document.all(obj).focus();
		return false;
	}
	if (document.all(obj).value.substr(2,1) != "縣" && document.all(obj).value.substr(2,1) != "市")
	{
		alert("請輸入" + msg + "地址的(縣,市)資料");
		document.all(obj).focus();
		return false;
	}
	//if (document.all(obj).value.indexOf("鄉") == -1 && document.all(obj).value.indexOf("鎮") == -1 && document.all(obj).value.indexOf("區") == -1 && document.all(obj).value.indexOf("市") < 5)
	//{
	//	alert("請輸入" + msg + "地址的(市,區,鄉,鎮)資料");
	//	document.all(obj).focus();
	//	return false;
	//}
	return true;
}
function getCityCountry(obj,obj1,obj2)
{
	if(document.all(obj).value == "")
	{
		var city = new Array();
		var country = new Array();
		city = document.all(obj1).value.split(" ",2);
		country = document.all(obj2).value.split(" ",2);
		if (document.all(obj1).selectedIndex == -1)
			document.all(obj).value = "";
		else
		{
			if (document.all(obj2).selectedIndex == 0)
				document.all(obj).value = city[0];
			else
				document.all(obj).value = city[0] + country[1];		
		}
	}
}