﻿// 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 checkdate(input) {
	var validformat=/^\d{4}\-\d{2}\-\d{2}$/; //Basic check for format validity
	var returnval=false;
	if(input.value != "") {
		if (!validformat.test(input.value))
		{
			alert("不正確日期，請輸入YYYY-MM-DD");
		}
		else{ //Detailed check for valid date ranges
			var yearfield=input.value.split("-")[0];
			var monthfield=input.value.split("-")[1];
			var dayfield=input.value.split("-")[2];
			var dayobj = new Date(yearfield, monthfield-1, dayfield);
			if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
				alert("不正確日期，請重新輸入。");
			else
				returnval=true;
		}
		return returnval;
	} else {
		return true;
	}
}

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;
	password = myForm.password;
	password2 = myForm.password2;
	name1 = myForm.name1;
	phone = myForm.phone;
	birthday = myForm.birthday;
	email = myForm.email;
	agree = myForm.agree;
	verifycode = myForm.verifycode;

	var sendFlag = true;
	
	if (loginId.value.Trim() == "" && sendFlag){
		alert("請輸入登入名稱。");
		sendFlag = false;
		loginId.focus();
	}
	
	if (password.value.length < 6 && sendFlag){
		alert("請輸入最少六位英文字母或數字的密碼。");
		sendFlag = false;
		password.focus();
	} else if (password.value != password2.value)
	{
		alert("再輸入密碼不相符，請重新輸入。");
		sendFlag = false;
		password.focus();
	}
	
	if (name1.value.Trim() == "" && sendFlag){
		alert("請輸入姓名。");
		sendFlag = false;
		name1.focus();
	}
	
	if (email.value.Trim() == "" && sendFlag){
		alert("請輸入電郵地址。");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("電郵地址格式不正確，請重新輸入。");
		sendFlag = false;
		email.focus();
	}	
	
	if (sendFlag) {
		if (!checkdate(birthday))
		{
			sendFlag = false;
			birthday.focus();
		}
	}
	
	if (!agree.checked && sendFlag){
		alert("請點選同意我們的使用條款");
		sendFlag = false;
		agree.focus();
	}
	
	if (verifycode.value == "" && sendFlag){
		alert("請輸入驗証碼");
		sendFlag = false;
		verifycode.focus();
	}

	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goReg";
		myForm.submit();
	}
}

function checkMngCat(myForm){
	fname2 = myForm.fname2;
	
	if (fname2.value.Trim() == "" && sendFlag)
	{
		alert("請輸入分類名稱。");
		sendFlag = false;
		fname2.focus();
	}
	
	var sendFlag = true;	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goCat";
		myForm.submit();
	}

}

function checkBlog(myForm) {
	title = myForm.title;
	var sendFlag = true;
	
	if (title.value.Trim() == "" && sendFlag)
	{
		alert("請輸入主題。");
		sendFlag = false;
		title.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goPost";
		myForm.submit();
	}
}

function checkReply(myForm){
	myForm.method = "POST";
	myForm.action += "&goReply";
	myForm.submit();
}

function checkEdit(myForm) {
	password = myForm.passwordo;
	password2 = myForm.password;
	name1 = myForm.name1;
	agree = myForm.agree;
	verifycode = myForm.verifycode;

	var sendFlag = true;
	
	if (password.value.Trim() == "" && sendFlag)
	{
		alert("請輸入密碼以更改資料。");
		sendFlag = false;
		password.focus();
	}
	
	if (password2.value.length < 6 && password2.value.length > 0 && sendFlag){
		alert("請輸入最少六位英文字母或數字的密碼。");
		sendFlag = false;
		password2.focus();
	}
	
	if (name1.value.Trim() == "" && sendFlag){
		alert("請輸入姓名。");
		sendFlag = false;
		name1.focus();
	}
	
	if (!agree.checked && sendFlag){
		alert("請點選同意我們的使用條款");
		sendFlag = false;
		agree.focus();
	}
	
	if (verifycode.value == "" && sendFlag){
		alert("請輸入驗証碼");
		sendFlag = false;
		verifycode.focus();
	}

	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goEdit";
		myForm.submit();
	}
}

function checkMama(myForm) {	
	username = myForm.username;
	password = myForm.password;
	email = myForm.email;
	cname = myForm.cname;
	ename = myForm.ename;
//	idno = myForm.idno;
	phone = myForm.phone;
	preg = myForm.preg;
	age = myForm.age;
	yyear = myForm.yyear;
	ymonth = myForm.ymonth;
	yday = myForm.yday;
	
	digit = /[^0-9]/;

	var sendFlag = true;
	
	if (username.value.Trim() == "" && sendFlag){
		alert("請輸入登入名稱。");
		sendFlag = false;
		username.focus();
	}
	
	if (password.value.length < 6 && sendFlag){
		alert("請輸入最少六位英文字母或數字的密碼。");
		sendFlag = false;
		password.focus();
	}
	
	if (email.value.Trim() == "" && sendFlag){
		alert("請輸入電郵地址。");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("電郵地址格式不正確，請重新輸入。");
		sendFlag = false;
		email.focus();
	}	
	
	if (cname.value.Trim() == "" && sendFlag){
		alert("請輸入中文姓名。");
		sendFlag = false;
		cname.focus();
	}	
	if (ename.value.Trim() == "" && sendFlag){
		alert("請輸入英文姓名。");
		sendFlag = false;
		ename.focus();
	}
	
	/*if (idno.value.Trim() == "" && sendFlag){
		alert("請輸入香港身分證號碼頭4個數字。");
		sendFlag = false;
		idno.focus();
	}
	if (digit.test(idno.value.Trim()) && sendFlag){
		alert("身分證號碼不正確，請重新輸入");
		sendFlag = false;
		idno.focus();
	}*/
	
	if (phone.value.Trim() == "" && sendFlag){
		alert("請輸入聯絡電話。");
		sendFlag = false;
		phone.focus();
	}	
	
	if (getCheckedValue(preg) == "" && sendFlag)
	{
		alert("請選擇是否孕媽媽。");
		sendFlag = false;
		preg[0].focus();
	}
	if (getCheckedValue(preg) == "不是" && sendFlag)
	{
		if (age.value.Trim() == ""){
			alert("請輸入BB年齡。");
			sendFlag = false;
			age.focus();
		}	
	}
	if (getCheckedValue(preg) == "是" && sendFlag)
	{
		if (yyear.selectedIndex <= 0 || ymonth.selectedIndex <= 0 || yday.selectedIndex <= 0)
		{
			alert("請輸入完整預產期。");
			sendFlag = false;
			yyear.focus();
		}
	}
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goReg";
		myForm.submit();
	}	
}

function checkTrade(myForm) {
	cat = myForm.cat;
	title = myForm.title;
	size = myForm.size;
	brand = myForm.brand;
	photo1 = myForm.photo1;
	description = myForm.description;
	
	var sendFlag = true;
	
	if (cat.selectedIndex <= 0 && sendFlag){
		alert("Please select a category.");
		sendFlag = false;
		cat.focus();
	}
	
	if (title.value.Trim() == "" && sendFlag){
		alert("Please enter a title.");
		sendFlag = false;
		title.focus();
	}
	
	if (size.selectedIndex <= 0 && sendFlag){
		alert("Please select the size.");
		sendFlag = false;
		size.focus();
	}
	
	if (brand.selectedIndex <= 0 && sendFlag){
		alert("Please select the brand.");
		sendFlag = false;
		brand.focus();
	}
	
	if (photo1.value.Trim() == "" && sendFlag){
		alert("Please choose a photo to upload.");
		sendFlag = false;
		photo1.focus();
	}
	
	if (description.value.Trim() == "" && sendFlag){
		alert("Please enter some description.");
		sendFlag = false;
		description.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goEdit";
		myForm.submit();
	}
}

function checkTrade2(myForm) {
	cat = myForm.cat;
	title = myForm.title;
	size = myForm.size;
	brand = myForm.brand;
	description = myForm.description;
	
	var sendFlag = true;
	
	if (cat.selectedIndex <= 0 && sendFlag){
		alert("Please select a category.");
		sendFlag = false;
		cat.focus();
	}
	
	if (title.value.Trim() == "" && sendFlag){
		alert("Please enter a title.");
		sendFlag = false;
		title.focus();
	}
	
	if (size.selectedIndex <= 0 && sendFlag){
		alert("Please select the size.");
		sendFlag = false;
		size.focus();
	}
	
	if (brand.selectedIndex <= 0 && sendFlag){
		alert("Please select the brand.");
		sendFlag = false;
		brand.focus();
	}
	
	if (description.value.Trim() == "" && sendFlag){
		alert("Please enter some description.");
		sendFlag = false;
		description.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "&goPost";
		myForm.submit();
	}
}


function checkPM(myForm) {
	var title = myForm.title;
	var content = myForm.content;	
	var sendFlag = true;
	
	if (title.value.Trim() == "" && sendFlag){
		alert("Please enter a subject.");
		sendFlag = false;
		title.focus();
	}

	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goPM";
		myForm.submit();
	}
}

function checkLogin(myForm) {
	loginID = myForm.loginID;	
	password = myForm.password;	
	var sendFlag = true;
	
	if (loginID.value.Trim() == "" && sendFlag){
		alert("請輸入用戶名稱");
		sendFlag = false;
		loginID.focus();
		return false;
	}

	if (password.value.Trim() == "" && sendFlag){
		alert("請輸入密碼");
		sendFlag = false;
		password.focus();
		return false;
	}
		
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action = "member_login.php?goLogin";
		return sendFlag;
	}	
}


function checkPost(myForm) {
	title = myForm.title;
	content = myForm.content;
	
	var sendFlag = true;

	if (title.value.Trim() == "" && sendFlag){
		alert("Please enter a subject.");
		sendFlag = false;
		title.focus();
	}

	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goPost";
		myForm.submit();
	}
}

function checkContact(myForm)
{
	name = myForm.name;
	company = myForm.company;
	email = myForm.email;
	tel = myForm.tel;
	message = myForm.message;

	var sendFlag = true;

	if (name.value == "" && sendFlag){
		alert("Please enter your name.");
		sendFlag = false;
		name.focus();
	}
	
	if (company.value == "" && sendFlag){
		alert("please enter your company");
		sendFlag = false;
		company.focus();
	}
	
	if (email.value == "" && sendFlag){
		alert("please enter your email");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("Invalid email format, please re-enter.");
		sendFlag = false;
		email.focus();
	}
	
	if (tel.value == "" && sendFlag){
		alert("please enter your phone no.");
		sendFlag = false;
		tel.focus();
	}

	if (message.value == "" && sendFlag){
		alert("Please enter your message.");
		sendFlag = false;
		message.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action = "successmail.php";
		myForm.submit();
	}
}

function checkFg(myForm)
{
	email = myForm.email;	
	var sendFlag = true;
		
	if (email.value == "" && sendFlag){
		alert("請輸入你的電郵");
		email.focus();
		return;
	} else if (!checkMail(email.value) && sendFlag) {
		alert("電郵地址不正確，請重新輸入");
		email.focus();
		return;
	}	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goSend";
		myForm.submit();
	}
}


