String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
};
String.prototype.pad = function(l, s, t){
    return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
        + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
        + this + s.substr(0, l - t) : this;
};

var lbl = new Object();
var msg = new Object();

lbl["email"] = "Email Address";
lbl["surname"] = "Surname";
lbl["firstname"] = "First Name";
lbl["contact"] = "Contact Number";
lbl["address"] = "Address";
lbl["birthday"] = "Date of Birth";
lbl["country"] = "Country";
lbl["nation"] = "Nationality";

msg["br"] = "\n";
msg["bullet"] = " - ";
msg["missing"] = "Please enter the following information:\n\n";
msg["invalid_surname"] = "Please enter a valid surname";
msg["invalid_firstname"] = "Please enter a valid firstname";
msg["invalid_email"] = "Please enter an valid email address";
msg["invalid_birthday_day"] = "Please enter an valid day of birth";
msg["invalid_birthday_month"] = "Please enter an valid month of birth";
msg["invalid_birthday_year"] = "Please enter an valid year of birth/ You must aged 18 or above to be our volunteer";
msg["invalid_contact"] = "Please enter an valid contact number";
msg["at_least_3_letters"] = "Please enter at least 3 letters!"; //請輸入最少3個字母!
msg["missing_seatch_text"] = "Please enter the search text!"; //請輸入要尋找的文字!

function isEmpty(str){
	if(str.trim()==""){
		return true;
	}else{
		return false;
	}
}

function isPhone(str) {
	charset = "0123456789 @-";
	var result = true;

	for (var i=0;i<str.length;i++) {
		if (charset.indexOf(str.substr(i,1))<0) {
			result = false;
			break;
		}
	}
 	return result;
}

function isDigit(str) {
	charset = "0123456789";
	var result = true;

	for (var i=0;i<str.length;i++) {
		if (charset.indexOf(str.substr(i,1))<0) {
			result = false;
			break;
		}
	}
 	return result;
}

function validatePhone(str){
	if(!isPhone(str.trim())){
		return false;
	}else if(str.trim().length < 8){
		return false;
	}
	return true;
}

function validateName(str){
	str = str.trim();
	return str.match(/^[a-z ]*$/i);
}

function validateSearch(str){
	if (str.length<3){
		alert(msg["at_least_3_letters"]);
		return false;
	}
	else if (str=="Type your search text here"){
		alert(msg["missing_seatch_text"]);
		return false;
	}
	return true;
}

function validateEmail(strEmail){
	/*
		var status = false;     
		var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
		
		if (strEmail.search(emailRegEx) == -1) {
			alert("Please enter a valid email address.");
		}else {
			//alert("Woohoo!  The email address is in the correct format and they are the same.");
			status = true;
		}
		return status;
	*/
	strEmail = strEmail.trim();
    var emailRegEx;
    emailRegEx = /^[A-Za-z0-9_\-]+(\.[A-Za-z0-9_\-]+)*@[A-Za-z0-9_\-]+(\.[A-Za-z0-9_\-]+)+$/;
    return (emailRegEx.test(strEmail));
}

function validateBirthDay(num){
	if (isNaN(parseInt(num))){
		return false;
	}else if(parseInt(num) == 0 || parseInt(num) > 31){
		return false;
	}
	return true;
}

function validateBirthMonth(num){
	if (isNaN(parseInt(num))){
		return false;
	}else if(parseInt(num) == 0 || parseInt(num) > 12){
		return false;
	}
	return true;
}

function validateBirthYear(num, lower, upper){
	if (isNaN(parseInt(num))){
		return false;
	}else if(parseInt(num) > upper || parseInt(num) < lower){
		return false;
	}
	return true;
}



//form validation functions
function updateBirthDay(obj){
	obj.value = obj.value.pad(2, "0", 0);
}
function updateBirthMonth(obj){
	obj.value = obj.value.pad(2, "0", 0);
}
function updateBirthYear(obj){
	var str = obj.value.trim();
	if(str.length == 1){
		str = "190" + str;
	}else if(str.length == 2){
		str = "19" + str;
	}else if(str.length == 3){
		str = "1" + str;
	}
	obj.value = str;
}
function submitFormEnews(){
	var formObj = document.enews_form;
	if(validateFormEnews(formObj)){
		formObj.submit();
	}	
}

function submitFormSearch(){
	var formObj = document.search_form;
	if(validateSearch(formObj.search_text.value.trim())){
		formObj.submit();
	}	
}

function validateFormEnews(formObj){
	//formObj = enews_form
	var isValid = true;
	
	if (formObj.email.value=='') {
		isValid = false;
		alert(msg["missing"] + lbl["email"]);
	} else if (!validateEmail(formObj.email.value)) { 
		isValid = false;
		alert(msg["invalid_email"]);
	}
	return isValid;
}
function validateFormAct(formObj){
	var aMsg = msg["missing"];
	var isValid = true;
	
	//first check missing input
	if (isEmpty(formObj.surname.value)){
		aMsg += msg["bullet"] + lbl["surname"] + msg["br"];
		isValid = false;
	}
	if (isEmpty(formObj.firstname.value)){
		aMsg += msg["bullet"] + lbl["firstname"] + msg["br"];
		isValid = false;
	}
	if (isEmpty(formObj.contactnumber.value)){
		aMsg += msg["bullet"] + lbl["contact"] + msg["br"];
		isValid = false;
	}
	if (isEmpty(formObj.email.value)){
		aMsg += msg["bullet"] + lbl["email"] + msg["br"];
		isValid = false;
	}
	if (!isEmpty(formObj.birth_day.value) || !isEmpty(formObj.birth_month.value) || !isEmpty(formObj.birth_year.value)){
		if(isEmpty(formObj.birth_day.value) || isEmpty(formObj.birth_month.value) || isEmpty(formObj.birth_year.value)){
			aMsg += msg["bullet"] + lbl["birthday"] + msg["br"];
			isValid = false;
		}
	}
	if(isValid){
		//second check invalid input
		if(!validateName(formObj.surname.value)){
			aMsg = msg["invalid_surname"];
			isValid = false;
		}else if(!validateName(formObj.firstname.value)){
			aMsg = msg["invalid_firstname"];
			isValid = false;
		}else if(!validatePhone(formObj.contactnumber.value)){
			aMsg = msg["invalid_contact"];
			isValid = false;
		}else if(!validateEmail(formObj.email.value)){
			aMsg = msg["invalid_email"];
			isValid = false;
		}else if(!isEmpty(formObj.birth_day.value) && (!isDigit(formObj.birth_day.value) || !validateBirthDay(formObj.birth_day.value))){
			aMsg = msg["invalid_birthday_day"];
			isValid = false;
		}else if(!isEmpty(formObj.birth_month.value) && (!isDigit(formObj.birth_month.value) || !validateBirthMonth(formObj.birth_month.value))){
			aMsg = msg["invalid_birthday_month"];
			isValid = false;
		}else if(!isEmpty(formObj.birth_year.value) && (!isDigit(formObj.birth_year.value) || !validateBirthYear(formObj.birth_year.value, 1921, 1991))){
			aMsg = msg["invalid_birthday_year"];
			isValid = false;
		}
	}
	if(!isValid){
		alert(aMsg);
	}
	return isValid;
}

function validateFormContact(formObj){
	var aMsg = msg["missing"];
	var isValid = true;
	
	//first check missing input
	if (isEmpty(formObj.surname.value)){
		aMsg += msg["bullet"] + lbl["surname"] + msg["br"];
		isValid = false;
	}
	if (isEmpty(formObj.firstname.value)){
		aMsg += msg["bullet"] + lbl["firstname"] + msg["br"];
		isValid = false;
	}
	if (isEmpty(formObj.contactnumber.value)){
		aMsg += msg["bullet"] + lbl["contact"] + msg["br"];
		isValid = false;
	}
	if (isEmpty(formObj.email.value)){
		aMsg += msg["bullet"] + lbl["email"] + msg["br"];
		isValid = false;
	}
	if(isValid){
		//second check invalid input
		if(!validateName(formObj.surname.value)){
			aMsg = msg["invalid_surname"];
			isValid = false;
		}else if(!validateName(formObj.firstname.value)){
			aMsg = msg["invalid_firstname"];
			isValid = false;
		}else if(!validatePhone(formObj.contactnumber.value)){
			aMsg = msg["invalid_contact"];
			isValid = false;
		}else if(!validateEmail(formObj.email.value)){
			aMsg = msg["invalid_email"];
			isValid = false;
		}
	}
	if(!isValid){
		alert(aMsg);
	}
	return isValid;
}
