﻿// JavaScript Document

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

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


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 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 checkContact(myForm) {
	cname = myForm.cname;
	phone = myForm.phone;
	email = myForm.email;
	message = myForm.message;
	verifycode = myForm.verifycode;
	var sendFlag = true;
	
	if (cname.value == "" && sendFlag){
		alert("請輸入你的姓名");
		sendFlag = false;
		cname.focus();
	}	
	
	if (phone.value == "" && sendFlag){
		alert("請輸入你的電話號碼。");
		sendFlag = false;
		phone.focus();
	}
	
	if (email.value == "" && sendFlag){
		alert("請輸入你的電郵地址");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("電郵地址不正確，請重新輸入。");
		sendFlag = false;
		email.focus();
	}

	if (message.value == "" && sendFlag){
		alert("請輸入查詢內容");
		sendFlag = false;
		message.focus();
	}
	
	if (verifycode.value == "" && sendFlag){
		alert("請輸入驗証碼");
		sendFlag = false;
		verifycode.focus();
	}
	
	if (sendFlag) {
		myForm.method = "POST";
		myForm.action += "?goSend";
		myForm.submit();
	}
}

function checkReg(myForm) {
	cname = myForm.cname;
	ename = myForm.ename;
	gender = myForm.gender;
	age = myForm.age;
	phone = myForm.phone;
	email = myForm.email;
	address = myForm.address;
	grpcourse = myForm.grpcourse;
	percourse = myForm.percourse;
	courseDate1 = myForm.courseDate1;
	courseTime1 = myForm.courseTime1;
	courseDate2 = myForm.courseDate2;
	courseTime2 = myForm.courseTime2;
	courseplace = myForm.courseplace;
	edulevel = myForm.edulevel;
	waytoknow = myForm.waytoknow;
	bookInstru = myForm.bookInstru;
	borrowInstr = myForm.borrowInstr;	
	bookcoursegrp = myForm.bookcoursegrp;
	bookcoursediv = myForm.bookcoursediv;
	courseDate3 = myForm.courseDate3;
	courseTime3 = myForm.courseTime3;
	courseDate4 = myForm.courseDate4;
	courseTime4 = myForm.courseTime4;
	bookcourseplace = myForm.bookcourseplace;
	verifycode = myForm.verifycode;

	var sendFlag = true;
	
/*	if (cname.value.Trim() == "" && sendFlag){
		alert("請輸入中文姓名");
		sendFlag = false;
		cname.focus();
	}*/
	
	if (ename.value.Trim() == "" && sendFlag){
		alert("請輸入英文姓名");
		sendFlag = false;
		ename.focus();
	}
	
	if (getCheckedValue(gender) == "" && sendFlag){
		alert("請輸入性別");
		sendFlag = false;
		gender[0].focus();
	}
	
	if (age.value.Trim() == "" && sendFlag){
		alert("請輸入年齡");
		sendFlag = false;
		age.focus();
	}	
	if (phone.value.Trim() == "" && sendFlag){
		alert("請輸入聯絡電話");
		sendFlag = false;
		phone.focus();
	}	
	if (email.value == "" && sendFlag){
		alert("請輸入你的電郵地址");
		sendFlag = false;
		email.focus();
	} else if (!checkMail(email.value) && sendFlag) {
		alert("電郵地址不正確，請重新輸入。");
		sendFlag = false;
		email.focus();
	}
	if (address.value.Trim() == "" && sendFlag){
		alert("請輸入通訊地址");
		sendFlag = false;
		address.focus();
	}	
	
/*	if (grpcourse.selectedIndex <= 0 && percourse.selectedIndex <= 0 && sendFlag)
	{
		alert("請選擇最少一個課程");
		sendFlag = false;
		grpcourse.focus();
	}

	if (courseDate1.selectedIndex <= 0 && sendFlag)
	{
		alert("請選擇上課日期");
		sendFlag = false;
		courseDate1.focus();
	}	
	if (courseTime1.value.Trim() == "" && sendFlag){
		alert("請選擇上課時間");
		sendFlag = false;
		courseTime1.focus();
	}
	if (courseDate2.selectedIndex > 0 && courseTime2.value.Trim() == "" && sendFlag){
		alert("請選擇上課時間");
		sendFlag = false;
		courseTime2.focus();
	}
	
	if (courseplace.selectedIndex <= 0 && sendFlag)
	{
		alert("請選擇上課地點");
		sendFlag = false;
		courseplace.focus();
	}
	if (edulevel.selectedIndex <= 0 && sendFlag)
	{
		alert("請選擇現時學習程度");
		sendFlag = false;
		edulevel.focus();
	}*/
	
	if (waytoknow.selectedIndex <= 0 && sendFlag)
	{
		alert("請選擇從什麼途徑認識本中心");
		sendFlag = false;
		waytoknow.focus();
	}
	
	/*if (getCheckedValue(bookInstru) == "" && sendFlag){
		alert("請選擇是否需要訂購樂器");
		sendFlag = false;
		bookInstru[0].focus();
	}
	if (getCheckedValue(borrowInstr) == "" && sendFlag){
		alert("請選擇是否需要租借樂器");
		sendFlag = false;
		borrowInstr[0].focus();
	}
	
	if (sendFlag && (bookcoursegrp.selectedIndex > 0 || myForm.bookcoursediv > 0))
	{
		if (courseDate3.selectedIndex <= 0 && sendFlag)
		{
			alert("請選擇上課日期");
			sendFlag = false;
			courseDate3.focus();
		}	
		if (courseTime3.value.Trim() == "" && sendFlag){
			alert("請選擇上課時間");
			sendFlag = false;
			courseTime3.focus();
		}
		if (courseDate4.selectedIndex > 0 && courseTime4.value.Trim() == "" && sendFlag){
			alert("請選擇上課時間");
			sendFlag = false;
			courseTime4.focus();
		}
		if (bookcourseplace.selectedIndex <= 0 && sendFlag)
		{
			alert("請選擇上課地點");
			sendFlag = false;
			bookcourseplace.focus();
		}
	}*/
	
	if (verifycode.value.Trim() == "" && sendFlag){
		alert("請輸入驗証碼");
		sendFlag = false;
		verifycode.focus();
	}

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

function checkStudy(myForm)
{
	loginID = myForm.loginID;
	password = myForm.password;
	var sendFlag = true;
	
	if (loginID.value.Trim() == "" && sendFlag){
		alert("請輸入登入名稱");
		sendFlag = false;
		loginID.focus();
	}
	if (password.value.Trim() == "" && sendFlag){
		alert("請輸入密碼");
		sendFlag = false;
		password.focus();
	}
	
	if (sendFlag) {
		myForm.action += "?goLogin";
		myForm.submit();
	}
//	return sendFlag;
}

