﻿// JavaScript Document
String.prototype.Trim = function() { 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 

function checkMail(email) {
	var x = email;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function checkReg(myForm) {
	loginid = myForm.loginid;
	pwd = myForm.pwd;
	pwd2 = myForm.pwd2;
	title = myForm.title;
	membername = myForm.membername;
	address = myForm.address;
	age = myForm.age;
	email = myForm.email;
	phone = myForm.phone;
	disclaimer = myForm.disclaimer;
	var sendFlag = true;
	
	if (loginid.value.Trim() == "" && sendFlag){
		alert("請輸入你的登入名稱。\nPlease enter a login ID.");
		sendFlag = false;
		login.focus();
	}

	if (pwd.value.Trim() == "" && sendFlag){
		alert("請輸入密碼。\nPlease enter a password.");
		sendFlag = false;
		pwd.focus();
	}
	else if (pwd.value.Trim() != pwd2.value.Trim() && sendFlag){
		alert("密碼和確認密碼不相同，請重新輸入。\nPassword is not identical to confirm password. Please re-enter.");
		sendFlag = false;
		pwd.focus();
	}
	if (getCheckedValue(title) == "" && sendFlag){
		alert("請輸入你的稱謂。\nPlease select your salutation.");
		sendFlag = false;
		title[0].focus();
	}
	if (membername.value.Trim() == "" && sendFlag){
		alert("請輸入你的姓名。\nPlease enter your name.");
		sendFlag = false;
		membername.focus();
	}	
	if (age.value.Trim() == "" && sendFlag){
		alert("請輸入你的年齡。\nPlease enter your age.");
		sendFlag = false;
		age.focus();
	}	
	if (address.value.Trim() == "" && sendFlag){
		alert("請輸入你的地址。\nPlease enter your address.");
		sendFlag = false;
		address.focus();
	}	
	if (email.value.Trim() == "" && sendFlag){
		alert("請輸入你的電郵地址\nPlease enter your email address.");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("電郵地址不正確，請重新輸入。\nInvalid email address, please re-enter.");
		sendFlag = false;
		email.focus();
	}	
	if (phone.value.Trim() == "" && sendFlag){
		alert("請輸入你的電話\nPlease enter your phone no.");
		sendFlag = false;
		phone.focus();
	}
	if (!disclaimer.checked && sendFlag)
	{
		alert("請點選空格表示您已閱讀及接受免責聲明和私隱條例。\nPlease check the disclaimer checkbox.");
		sendFlag = false;
		disclaimer.focus();
	}
					
	if (sendFlag) {
		myForm.method = "POST";
		myForm.submit();
	}
}

function checkWarranty(myForm) {
	uname = myForm.uname;
	address = myForm.address;
	phone = myForm.phone;
	email = myForm.email;
	prodId = myForm.prodId;
	serial = myForm.serial;
	dateOfPurchase =myForm.dateOfPurchase;
	retailer =myForm.retailer;
	receiptNo =myForm.receiptNo;
	retailerAddr =myForm.retailerAddr;		
	dopyear =myForm.dopyear;
	dopmonth =myForm.dopmonth;	
	dopday =myForm.dopday;	
	

	
	agree =myForm.agree;	
	var sendFlag = true;
	
	if (uname.value.Trim() == "" && sendFlag){
		alert("請輸入您的姓名。\nPlease enter your name.");
		sendFlag = false;
		uname.focus();
	}
	
	if (address.value.Trim() == "" && sendFlag){
		alert("請輸入您的地址。\nPlease enter a password.");
		sendFlag = false;
		address.focus();
	}
	
	if (phone.value.Trim() == "" && sendFlag){
		alert("請輸入你的電話號碼。\nPlease enter your telephone no.");
		sendFlag = false;
		phone.focus();
	}	
	
	if (email.value.Trim() == "" && sendFlag){
		alert("請輸入你的電郵地址\nPlease enter your email address.");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("電郵地址不正確，請重新輸入。\nInvalid email address, please re-enter.");
		sendFlag = false;
		email.focus();
	}	

	if (prodId.selectedIndex <=0 && sendFlag){
		alert("請選擇產品型號。\nPlease select the product model.");
		sendFlag = false;
		prodId.focus();
	}	
	
	if (serial.value.Trim() == "" && sendFlag){
		alert("請輸入產品序號。\nPlease enter the product serial no.");
		sendFlag = false;
		serial.focus();
	}	

	yearfield = dopyear.options[dopyear.selectedIndex].value;
	if (dopyear.selectedIndex < 0  && sendFlag){
		alert("請輸入你的購買日期。\nPlease enter the date of purchase.");
		sendFlag = false;
		dopyear.focus();
	}	
	monthfield = dopmonth.options[dopmonth.selectedIndex].value;
	if (dopmonth.selectedIndex < 0  && sendFlag){
		alert("請輸入你的購買日期。\nPlease enter the date of purchase.");
		sendFlag = false;
		dopmonth.focus();
	}
	dayfield = dopday.options[dopday.selectedIndex].value;	
	if (dopday.selectedIndex < 0  && sendFlag){
		alert("請輸入你的購買日期。\nPlease enter the date of purchase.");
		sendFlag = false;
		dopday.focus();
	}
	var dayobj = new Date(yearfield, monthfield, dayfield);
	var today = new Date();
	
	if (((dayobj.getMonth()!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)) && sendFlag)
	{
		alert("不正確日期, 請重新輸入");	
		sendFlag = false;
		dopyear.focus();
	}
	else
	{
		dateOfPurchase.value = yearfield + '-' + (parseInt(monthfield)+1) + '-' + dayfield;
	}
	
	if (retailer.value.Trim() == "" && sendFlag){
		alert("請輸入購買商舖名稱\nPlease enter the retailer name.");
		sendFlag = false;
		retailer.focus();
	}
	
	if (receiptNo.value.Trim() == "" && sendFlag){
		alert("請輸入發票號碼\nPlease enter the receipt no.");
		sendFlag = false;
		receiptNo.focus();
	}
	
	if (retailerAddr.value.Trim() == "" && sendFlag){
		alert("請輸入購買商舖地址\nPlease enter the retailer address.");
		sendFlag = false;
		retailerAddr.focus();
	}
	
	if (!agree.checked && sendFlag)
	{
		alert("請點選空格表示您已閱讀及接受保養服務條款。\nPlease check the disclaimer checkbox.");
		sendFlag = false;
		agree.focus();
	}
					
	if (sendFlag) {
		myForm.method = "POST";
		myForm.submit();
	}
}


function checkEdit(myForm) {
	loginid = myForm.loginid;
	pwd = myForm.pwd;
	pwd2 = myForm.pwd2;
	title = myForm.title;
	membername = myForm.membername;
	address = myForm.address;
	age = myForm.age;
	email = myForm.email;
	phone = myForm.phone;
	var sendFlag = true;
	
	if (loginid.value.Trim() == "" && sendFlag){
		alert("請輸入你的登入名稱。\nPlease enter a login ID.");
		sendFlag = false;
		login.focus();
	}

	if (pwd.value.Trim() == "" && sendFlag){
		alert("請輸入密碼。\nPlease enter a password.");
		sendFlag = false;
		pwd.focus();
	}
	else if (pwd.value.Trim() != pwd2.value.Trim() && sendFlag){
		alert("密碼和確認密碼不相同，請重新輸入。\nPassword is not identical to confirm password. Please re-enter.");
		sendFlag = false;
		pwd.focus();
	}
	if (getCheckedValue(title) == "" && sendFlag){
		alert("請輸入你的稱謂。\nPlease select your salutation.");
		sendFlag = false;
		title[0].focus();
	}
	if (membername.value.Trim() == "" && sendFlag){
		alert("請輸入你的姓名。\nPlease enter your name.");
		sendFlag = false;
		membername.focus();
	}	
	if (age.value.Trim() == "" && sendFlag){
		alert("請輸入你的年齡。\nPlease enter your age.");
		sendFlag = false;
		age.focus();
	}	
	if (address.value.Trim() == "" && sendFlag){
		alert("請輸入你的地址。\nPlease enter your address.");
		sendFlag = false;
		address.focus();
	}	
	if (email.value.Trim() == "" && sendFlag){
		alert("請輸入你的電郵地址\nPlease enter your email address.");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("電郵地址不正確，請重新輸入。\nInvalid email address, please re-enter.");
		sendFlag = false;
		email.focus();
	}	
	if (phone.value.Trim() == "" && sendFlag){
		alert("請輸入你的電話\nPlease enter your phone no.");
		sendFlag = false;
		phone.focus();
	}
	if (sendFlag) {
		myForm.method = "POST";
		myForm.submit();
	}
}

function checkLogin(myForm){
	var login = myForm.login;
	var password = myForm.pwd;	
	var sendFlag = true;

	if (login.value.Trim() == "" && sendFlag){
		alert("請輸入登入名稱\nPlease enter Login ID.");
		sendFlag = false;
		login.focus();
	}
	if (password.value.Trim() == "" && sendFlag){
		alert("請輸入密碼\n Please enter password");
		sendFlag = false;
		password.focus();
	}
	if (sendFlag)
	{
		myForm.action += "?login";
		myForm.submit();
	}

}

function checkContact(myForm) {
	cname = myForm.cname;
	email = myForm.email;
	content = myForm.content;
	
	var sendFlag = true;

	if (cname.value.Trim() == "" && sendFlag){
		alert("請輸入你的姓名");
		sendFlag = false;
		cname.focus();
	}
	
	if (email.value.Trim() == "" && sendFlag){
		alert("請輸入你的電郵地址");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("電郵地址不正確，請重新輸入。");
		sendFlag = false;
		email.focus();
	}
	
	if (content.value.Trim() == "" && sendFlag){
		alert("請輸入你的留言內容");
		sendFlag = false;
		content.focus();
	}	
					
	if (sendFlag) {
		myForm.method = "POST";
		myForm.submit();
	}
}

