var searchTextField;

var commentForm;
var commentAuthor;
var commentBody;
var commentAuthorEmail;
var commentAuthorUrl;
var commentSubmit;
var commentValid = false;

var commentFields = new Array();

var counter;

Event.observe(document, "dom:loaded", function() {
	var documentBody = $(document.body);
	documentBody.addClassName("javascript");


	searchTextField = $("search_query");

	if (searchTextField) {
		manageSearch();
		searchTextField.observe('change', manageSearch);
		searchTextField.observe('keyup', manageSearch);
	}



	commentForm = $("comment-form");

	if (commentForm) {
		commentForm.onsubmit = manageComment;
		
		commentBody = $("comment_body");
		commentFields.push(commentBody);
		
		if (!$("comment_logged_in")) {
			commentAuthor = $("comment_author");
			commentFields.push(commentAuthor);
			
			commentAuthorEmail = $("comment_author_email");
			commentFields.push(commentAuthorEmail);
			
			commentAuthorUrl = $("comment_author_url");
			commentFields.push(commentAuthorUrl);
		}

		commentSubmit = $("comment_submit");
		commentSubmit.onclick = manageSubmitClick;

		manageComment();

		for (counter = 0; counter < commentFields.length; counter++) {
			commentFields[counter].observe('change', manageComment);
			commentFields[counter].observe('keyup', manageComment);
		}
	}
});

function manageSearch() {
	if (searchTextField.getValue() == null || searchTextField.getValue() == "") {
		searchTextField.addClassName("empty");
	} else {
		searchTextField.removeClassName("empty");
	}
}


function validateEmailAddress(emailAddress) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if(reg.test(emailAddress) == false) {
		return false;
	}
	
	return true;
}

function manageSubmitClick() {
	var focusElements = new Array();
	
	if (!$("comment_logged_in")) {
		if (commentAuthor.getValue() == null || commentAuthor.getValue() == "") {
			commentAuthor.addClassName("invalid");
			focusElements.push(commentAuthor);
		} else {
			commentAuthor.removeClassName("invalid");
		}
	
		if (commentAuthorEmail.getValue() == null || commentAuthorEmail.getValue() == "") {
			commentAuthorEmail.addClassName("invalid");
			focusElements.push(commentAuthorEmail);
		} else if (!validateEmailAddress(commentAuthorEmail.getValue())) {
			commentAuthorEmail.addClassName("invalid");
			focusElements.push(commentAuthorEmail);
		} else {
			commentAuthorEmail.removeClassName("invalid");
		}
	}
	
	if (commentBody.getValue() == null || commentBody.getValue() == "") {
		commentBody.addClassName("invalid");
		focusElements.push(commentBody);
	} else {
		commentBody.removeClassName("invalid");
	}
	
	if (focusElements.length > 0) {
		focusElements[0].focus();
		return false;
	}
	
	return true;
}

function manageComment() {
	commentValid = true;

	for (counter = 0; counter < commentFields.length; counter++) {
		if (commentFields[counter].getValue() == null || commentFields[counter].getValue() == "") {
			commentFields[counter].addClassName("empty");
		} else {
			commentFields[counter].removeClassName("empty");
		}
	}

	if (!$("comment_logged_in")) {
		if (commentAuthor.getValue() == null || commentAuthor.getValue() == "") {
			commentValid = false;
		} else {
			commentAuthor.removeClassName("invalid");
		}

		if (commentAuthorEmail.getValue() == null || commentAuthorEmail.getValue() == "") {
			commentValid = false;
		} else if (!validateEmailAddress(commentAuthorEmail.getValue())) {
			commentValid = false;
		} else {
			commentAuthorEmail.removeClassName("invalid");
		}
	}
	

	if (commentBody.getValue() == null || commentBody.getValue() == "") {
		commentValid = false;
	} else {
		commentBody.removeClassName("invalid");
	}

	if (commentValid) {
		commentSubmit.removeClassName("invalid");
	} else {
		commentSubmit.addClassName("invalid");
	}

	return commentValid;
}
