//Toggle visibility of element
function toggleVisibility(theElement){
	if(document.getElementById(theElement).style.display == "none"){
		document.getElementById(theElement).style.display = "block"
	}else{
		document.getElementById(theElement).style.display = "none"
	}
}

//Hide current object
function hideThis(theElement){
	document.getElementById(theElement).style.display = "none";
}

//Form field validation
function fieldCheck(){

	//Input fields
	var nameField = document.getElementById('name');
	var emailField = document.getElementById('email');
	var commField = document.getElementById('comment');

	//Hide feedback divs
	document.getElementById('commentErr').style.display = "none";
	document.getElementById('emailErr').style.display = "none";
	document.getElementById('nameErr').style.display = "none";

	//Check each field
 	if(checkName(nameField)){ //Check name
		if(checkEmail(emailField)){ //Check email
			if(checkComment(commField)){ //Check Comment
				//alert('All tests successful');
				document.newComment.submit();
			}else{ //If comment fails
				document.getElementById('commentErr').style.display = "block";
			}
		}else{ //If email fails
			document.getElementById('emailErr').style.display = "block";
		}
	}else{ //If name fails
		document.getElementById('nameErr').style.display = "block";
	}
}

//Form validation, check email
function checkEmail(elem){
	var validEmail = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; //Standard email check
	if(elem.value.match(validEmail)){
		return true;
	}else{
		return false;
	}
}

//Form validation, check name
function checkName(elem){
	var validName = /^[\sa-zA-Z0-9]+$/; //Contains letters and numbers only (a-zA-Z0-9), allow spaces (/s)
	if(elem.value.match(validName)){
		return true;
	}else{
		return false;
	}
}

//Form validation, check comments
function checkComment(elem){
	var validName = /^[\s\?\;\:\'\"\,\.\!\-a-zA-Z0-9]+$/; //Contains letters and numbers only (a-zA-Z0-9), allow spaces (/s) and normal punctuation (?;:'",.!-)
	if(elem.value.match(validName)){
		return true;
	}else{
		return false;
	}
}

//Build query string to post vars in destination url
function buildQueryString(){
	selectedItems = ""; //Create new var to hold CSV list of selected items
	for(i=0; i< document.commentList.length; i++){
		if(document.commentList[i].checked == true){
			selectedItems += document.commentList[i].name + ",";
		}
	}
	//alert("Selected Items: " + selectedItems);
	window.location.href = "comment-delete.php?selectedItems="+selectedItems;
}

//Add sort method to the URL of admin page
function sortResultsBy(sortMethod, searchString){
	//alert ('sorting');
	window.location.href = "index.php?sortMethod="+sortMethod+"&searchString="+searchString;
}

