function doAdd(){
	if(!isEmptyField('contactEmail')){
		var email = document.getElementById('contactEmail').value;
		if(!isValidEmail(email)){
			alert("'" + email + "' is an invalid email!");
			return;
		}
	}

	if(isEmptyField('title')){
		alert("Please enter title!");
		return;
	}

	if(isEmptyField('message')){
		alert("Please enter comment!");
		return;
	}	
	if(isEmptyField('verifyCode')){
		alert("Please enter the verify code!");
		return;
	}	
	document.getElementById('guestBookForm').submit();
}

function doDelete(url){
	var ok = confirm("Are you sure you want to delete this comment?");
	if(ok){
		document.location=url;
	}
}

function doIPAction(url, action){
	var ip = "";
	if(action == "addBlockIP"){
		if(isEmptyField("ip4add")){
			return;
		}
		ip = document.getElementById("ip4add").value;
	}else if(action == "removeBlockedIP"){
		ip = document.getElementById("ip4remove").value;
	}
	if(ip != ""){
		document.location = url + "?action="+action+"&ip="+ip;
	}
}

//remove the front and back spaces of the given string
function stripSpace(str){ 
   //remove the front white space
    for (var i = 0; i < str.length; i++){
       if (str.charAt(i) !=' '){ break;}  
     }
    var s = str.substring(i, str.length);

   //remove the back white space
    for (var j = s.length - 1; j >= 0; j--){ 
       if (s.charAt(j) != ' '){ break;}
    }
   return s.substring(0, j+1); 
}



//check if the field value is empty
function isEmptyField(fieldId){
	var field = document.getElementById(fieldId);
	if (stripSpace(field.value).length == 0){
		field.focus();
		field.value = "";
		return true;
	}else{
		return false;
	}
}

function isNumber(str){ 
	var digits = "0123456789";
	for(var i = 0; i < str.length; i++){
		if(digits.indexOf(str.charAt(i)) < 0){
			return false;
		}
	}
	return true;
}


function isValidEmail(str) {
	//var emailFilter=/^.+@.+\..{2,3,4}$/;
	var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (!(emailFilter.test(str)) || str.match(illegalChars)) { 
		   return false;
	}
	
	return true;
}

