// JavaScript Document
function checkForm(formName) {
	var inputID = "";
	var errorOccured = false;
	$("span.error").fadeOut();
	$("form[name="+formName+"] input[required=true]").each(function() {
		if ($(this).val() == "") {
			inputID = ($(this).attr("id"));
			$("span#label_"+inputID+"").addClass("error");
			errorOccured = true;
		}		
	});		
	
	$("form[name="+formName+"] textarea[required=true]").each(function() {
		if ($(this).html() == "") {
			inputID = ($(this).attr("id"));
			$("span#label_"+inputID+"").addClass("error");
			errorOccured = true;
		}		
	});		
	
	
	if (errorOccured) {
		//start: display the error panel at the top of the page//
		return(false);
	}
	else {
		return(true);
	}
}

function clearForm(formName) {
	$("form[name="+formName+"] input").each(function() {
		$(this).val("");
	});			
	$("form[name="+formName+"] select").each(function() {
		$(this).val("");
	});				

	$("form[name="+formName+"] textarea").each(function() {
		$(this).html("");
	});					
}


function submitAjaxForm(postData, returnFunction) {	
	var serialisedObject = JSON.stringify(postData);	
	$.ajax({
		type: "POST",
		url: postData.action,
		data: "jsonObject="+ serialisedObject,					
		success: function(returnObject){
			var returnObject = JSON.parse(returnObject);
			if (returnObject != null) {		
				returnFunction(returnObject);
			}
		}
	});				
}	

function htmlEncode(theString) {
	/*start: I had to add this function to transmit strings containing html data via JSON objects (for Ajax requests) */
	theString = encodeURIComponent(theString);
	theString = escape(theString);	
	return(theString);
}
