function validateFieldNotEmpty(testString, fieldName)
{
	if (testString == "") {
		formErrorMsg += "- " + fieldName + " must be filled in\n";
		return true;
	}

	return false;

}

function validateEmailField(testEmail1, testEmail2, fieldName) {

	if (testEmail1 == "") {
		formErrorMsg += '- ' + fieldName + ' must be a valid email address ("yourname@domain.com") \n';
		return true;
	}
		
	if (testEmail1.indexOf(".") > 0 && testEmail1.indexOf("@") > 0) {
	} else {
		formErrorMsg += '- ' + fieldName + ' must be a valid email address ("yourname@domain.com") \n';
		return true;
	}

	if (testEmail1 != testEmail2) {
		formErrorMsg += '- Both Email fields must match \n';
		return true;
	}
	
	return false;
		
}



function PostComment(id, type) {

//alert(id + ' ' + type);
//************** CHECK IF FIRST & IN PARMS BREAKS THE REQUEST *****************************

	formErrorMsg = ""; // needed for error salert strings from error functions, declared in FormFunctions.js

	validateFieldNotEmpty($("Name").value, "Name ");
	validateEmailField($("Email").value, $("Email").value, "Email ");
	validateFieldNotEmpty($("comment").value, "Comment ");

	if (formErrorMsg != "") {
		var displayFormErrorMsg = "PLEASE CORRECT THE FOLLOWING:\n" + formErrorMsg;
		alert (displayFormErrorMsg);
		return false;
	}


	if ($('comment').value != '') {
		var parms = "&id=" + currentVideoID + "&type=" + type + "&comment=" + encodeURIComponent($('comment').value) + "&name=" + $('Name').value + "&email=" + $('Email').value + "&action=comment" + "&qid=" + Math.random()
		var url = "CommentProcessor.php"
		//alert(url + ' ' + parms);
		new Ajax.Request(url, 
			{method:'post', parameters:parms, 
			onSuccess: function() {alert("Thanks for your comment. It will appear in a little while");$('comment').value = "";$('Name').value = "";$('Email').value = "";ShowAllComments(id, type);}})
	}

}
function PostReply(id, type, cid, tid, comment, name, email) {

//alert(id + ' ' + type + ' ' + cid + ' ' + tid + ' ' + comment + ' ' + name + ' ' + email);
//************** CHECK IF FIRST & IN PARMS BREAKS THE REQUEST *****************************

	formErrorMsg = ""; // needed for error salert strings from error functions, declared in FormFunctions.js

validateFieldNotEmpty(name, "Name ");
	validateEmailField(email, email, "Email ");
	validateFieldNotEmpty(comment, "Comment ");

	if (formErrorMsg != "") {
		var displayFormErrorMsg = "PLEASE CORRECT THE FOLLOWING:\n" + formErrorMsg;
		alert (displayFormErrorMsg);
		return false;
	}


	if (comment != '') {
		tid ++;
		var parms = "&id=" + id + "&type=" + type + "&cid=" + cid + "&tid=" + tid + "&comment=" + encodeURIComponent(comment) + "&name=" + name + "&email=" + email + "&action=reply" + "&qid=" + Math.random()
		var url = "CommentProcessor.php"
		//alert(url + ' ' + parms);
		new Ajax.Request(url, 
			{method:'post', parameters:parms, 
			onSuccess: function() {alert("Thanks for your comment. It will appear in a little while");ShowAllComments(id, type);}})
	}

}
function CheckComments(id, type) {

	var parms = "&id=" + id + "&type=" + type + "&action=check" + "&qid=" + Math.random()
	//alert(parms);
	var url = "CommentProcessor.php"
	new Ajax.Updater('comment_content', url, {method:'post', parameters:parms});


}
function FetchVideoHeader(id) {

	var parms = "&id=" + id + "&action=header" + "&qid=" + Math.random()
	//alert(parms);
	var url = "CommentProcessor.php"
	new Ajax.Updater('comment_header', url, {method:'post', parameters:parms});


}
function ShowAllComments(id, type) {

	var parms = "&id=" + currentVideoID + "&type=" + type + "&action=showall" + "&qid=" + Math.random()
	var url = "CommentProcessor.php"
	//alert(parms);
	new Ajax.Request(url, 
			{method:'post', parameters:parms,
			onSuccess: function(transport) {
				var result = transport.responseText;
				var resultArray = result.split("^");
				//alert(resultArray[1]);
				var findSubstring = resultArray[1];
				//alert(findSubstring);
				if (findSubstring.indexOf('FSN') >= 0) {
					SetVisibleBlock('new_comment_entry');
				}
				$('comment_content').innerHTML = resultArray[0];
				
				}})
	
	
	
}
