// 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;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
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 "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function checkContactUs(myForm) {
	contactPerson = myForm.contactPerson;
	email = myForm.email;
	tel = myForm.tel;
	fax = myForm.fax;
	message = myForm.message;
	var sendFlag = true;

	if (contactPerson.value == "" && sendFlag){
		alert("Please input your name. 請輸入姓名。");
		sendFlag = false;
		contactPerson.focus();
	}
	
	if (email.value.Trim() == "" && sendFlag){
		alert("Please input your email. 請輸入電郵。");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value.Trim()) && sendFlag) {
		alert("invalid email. Please input again. 輸入電郵格式不正確。請重新輸入。");
		sendFlag = false;
		email.focus();
	}
		
	if (tel.value == "" && sendFlag){
		alert("Please input your telephne no. 請輸入電話號碼。");
		sendFlag = false;
		tel.focus();
	}
		
	if (message.value == "" && sendFlag){
		alert("Please input your message. 請輸入查詢內容。");
		sendFlag = false;
		message.focus();
	}
		
	if (sendFlag) {
		document.getElementById('btnReset').disabled = "disabled";
		document.getElementById('btnSubmit').disabled = "disabled";
	}
	
	return sendFlag;
}

function checkSubscribe(myForm) {
	email = myForm.email;
	var sendFlag = true;

	if (email.value.Trim() == "" && sendFlag){
		alert("Please input your email. 請輸入電郵。");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value.Trim()) && sendFlag) {
		alert("invalid email. Please input again. 輸入電郵格式不正確。請重新輸入。");
		sendFlag = false;
		email.focus();
	}

	if (sendFlag) {
		document.getElementById('btnSubmit').disabled = "disabled";
	}
	
	return sendFlag;
}

function checkCart(myForm)
{
	name1 = myForm.name1;
	name2 = myForm.name2;
	address1 = myForm.address1;
	country = myForm.country;
	phone = myForm.phone;
	email = myForm.email;
	var sendFlag = true;	
	if (name1.value.Trim() == "" && sendFlag){
		alert("請填寫收件人姓氏");
		name1.focus();
		sendFlag = false;
	}	
	if (name2.value.Trim() == "" && sendFlag){
		alert("請填寫收件人名字");
		name2.focus();
		sendFlag = false;
	}
	if (address1.value.Trim() == "" && sendFlag){
		alert("請填寫收件人地址");
		address1.focus();
		sendFlag = false;
	}	
	if (country.selectedIndex < 1 && sendFlag) {
		alert("請選擇你的國家");
		country.focus();
		sendFlag = false;
	}
	if (phone.value.Trim() == "" && sendFlag) {
		alert("請填寫你的電話");
		phone.focus();
		sendFlag = false;
	}
	if (email.value.Trim() == "" && sendFlag){
		alert("請輸入電郵。");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value.Trim()) && sendFlag) {
		alert("輸入電郵格式不正確。請重新輸入。");
		sendFlag = false;
		email.focus();
	}	
	if (sendFlag){
		myForm.action = 'check_out.php?toSave';		
		myForm.method = "POST";
		myForm.submit();
	}
}
