//Objects to interact with server
		   var CMSUtil = {
			   interactWithServer: function(form, isAjax, buildObjectFunction, 
										   returnFunction, action, servlet, 
										   extraFields){
				   if(isAjax == true && returnFunction != null){
					var queryString = Form.serialize(form);
					   if(queryString != null){
						   if(action != null){
							   queryString += "&action=" + action + "&isAjax=Y";	
						   }else{
							queryString += "&isAjax=Y";
						}
						if(extraFields){
							queryString += extraFields;
						}
						var handlerFunction = function(originalRequest){
							var response = "(" + originalRequest.responseText + ")";
							response = eval(response);
							   buildObjectFunction(response);	
							returnFunction(response);    
						}    
						var myAjax = new Ajax.Request(servlet,
							{method: 'post', parameters: queryString, onComplete: handlerFunction}
						);
					}
				}else{
					this.submitUserFormNoAJAX(form, action, servlet); 
				   }
			   },
			submitUserFormNoAJAX: function(form, action, servlet){
				   if(servlet == null) return null;
				  form.action = servlet;
				if(action != null) {
					   var actionField = document.createElement('input');
					   actionField.setAttribute('name', 'action');
						actionField.setAttribute('type', 'hidden');
						actionField.value = action;
					   form.appendChild(actionField);
			   }
				   form.submit();
			   }
		}

		var Action = {
			attachAction: function(form, isAjax, returnFunction){
				var action = "";
				if(isAjax){
					action = "attachActionAJAX";
				} else {
					action = "attachAction";
				}
				CMSUtil.interactWithServer(form, isAjax, Action.buildAction, returnFunction, action, "/s");
			},
			deleteAction: function(form, isAjax, returnFunction){
				var action = "";
				if(isAjax){
					action = "deleteActionAJAX";
				} else {
					action = "deleteAction";
				}
				CMSUtil.interactWithServer(form, isAjax, Action.deletedAction, returnFunction, action, "/s");
		  },
			buildAction: function(response){
				if(response.success == true){
					Action.actionID = response.actionID;
					Action.contentID = response.contentID;
					Action.commentID = response.commentID;
					Action.contentTypeID = response.contentTypeID;
					Action.actionDate = response.actionDate;
					Action.userID = response.userID;
					Action.actionType = response.actionType;
					Action.slugLine = response.slugLine;
				} else {
					Action.error = response.error;
				}
			},
			deletedAction: function(response){
				if(response.success == true){
					Action.actionID = response.actionID;
				} else {
					Action.error = response.error;
				}
			}
		}
		
		var Rating = {
			addRating: function(form, isAjax, returnFunction){
				var action = "";
				if(isAjax){
					action = "addRatingAJAX";
				} else {
					action = "addRating";
				}
				CMSUtil.interactWithServer(form, isAjax, Rating.buildRating, returnFunction, action, "/s");
		  },
			buildRating: function(response){
				if(response.success == true){
					Rating.id = response.id;
					Rating.rating = response.rating;
					Rating.upvote = response.upvote;
					Rating.downvote = response.downvote;
					Rating.type = response.type;
					Rating.date = response.date;
					Rating.ipAddress = response.ipAddress;
				}else{
					Rating.error = response.error;
				}
			}
		}

		var User = {
			createUser: function(form, isAjax, returnFunction){	
			   var action = "";
				if(isAjax){
					action = "createProfileAJAX";
				} else {
					action = "createProfile";
				}
			 CMSUtil.interactWithServer(form, isAjax, User.buildUser, returnFunction, action, "/s");
			},
			loginUser: function(form, isAjax, returnFunction){
				var action = "";
				if(isAjax){
					action = "doLoginProfileAJAX";
				} else {
					action = "doLoginProfile";
				}
				CMSUtil.interactWithServer(form, isAjax, User.buildUser, returnFunction, action, "/s");
			},
			updateUser: function(form, isAjax, returnFunction){
				var action = "";
				if(isAjax){
					action = "updateProfileAJAX";
				} else {

					action = "updateProfile";
				}
				CMSUtil.interactWithServer(form, isAjax, User.buildUser, returnFunction, action, "/s");
			},
			updateRegistration: function(form, isAjax, returnFunction){
				var action = "";
				if(isAjax){
					action = "updateRegistrationAJAX";
				} else {
					action = "updateReg";
				}
				CMSUtil.interactWithServer(form, isAjax, User.buildUser, returnFunction, action, "/s");
			},
			updateProfilePassword: function(form, isAjax, returnFunction){
				var action = "";
				if(isAjax){
					action = "updateProfilePasswordAJAX";
				} else {
					action = "updateProfilePassword";
				}
				CMSUtil.interactWithServer(form, isAjax, User.buildUser, returnFunction, action, "/s");
			},
			insertProfilePic: function(form, isAjax, returnFunction){
				var action = "";
				if(isAjax){
					//not implemented as of yet
					return;
					//action = "updateImageAJAX";
				} else {
					action = "updateImage";
				}
				CMSUtil.interactWithServer(form, isAjax, User.buildUser, returnFunction, action, "/s");
			},
			buildUser: function(response){
				if(response.success == true){
					User.username = response.username;
					User.email = response.email;
					User.firstName = response.firstName;
					User.lastName = response.lastName;
					User.birthDate = response.birthDate;
					User.createDate = response.createDate;
					User.title = response.title;
					User.company = response.company;
					User.address1 = response.address1;
					User.address2 = response.address2;
					User.city = response.city;
					User.state = response.state;
					User.zipcode = response.zipcode;
					User.country = response.country;
					User.custom1 = response.custom1;
					User.custom2 = response.custom2;
					User.custom3 = response.custom3;
					User.custom4 = response.custom4;
					User.custom5 = response.custom5;
					User.custom6 = response.custom6;
					User.custom7 = response.custom7;
					User.custom8 = response.custom8;
					User.custom9 = response.custom9;
				}else{
					User.error = response.error;
				}
			}
		}

		var UserContent = {
			insertContent: function(form, isAjax, returnFunction){
				var extraQueryStr = null;
				if(isAjax){
					extraQueryStr = getFieldsForForm();
				}else{
					addFieldsToForm(form);
				}
				CMSUtil.interactWithServer(form, isAjax, UserContent.buildContent, returnFunction, extraQueryStr, "/d");
			},
			editContent: function(form, isAjax, returnFunction){
				CMSUtil.interactWithServer(form, isAjax, UserContent.buildContent, returnFunction, null, "/d");
			},
			buildContent: function(response){
				if(response.success == true){
					for (var i=0; i< response.fields.length; i++){
						var field = response.fields[i];
						UserContent[field] = response[field];
					}
					UserContent.contentID = response.contentID;
					UserContent.contentType = response.contentType;
					UserContent.title = response.title;
					UserContent.status = response.status;
				} else {
					UserContent.error = response.error;
				}
			}
		}

		var Comment = {
			insertComment: function(form, isAjax, returnFunction){
				var extraQueryStr = null;
				if(isAjax){
					extraQueryStr = getFieldsForForm();
				}else{
					addFieldsToForm(form);
				}
				CMSUtil.interactWithServer(form, isAjax, Comment.buildComment, returnFunction, "insertComment", "/c", extraQueryStr);
			},
			deleteComment: function(form, isAjax, returnFunction){
				var extraQueryStr = null;
				if(isAjax){
					extraQueryStr = getFieldsForForm();
				}else{
					addFieldsToForm(form);
				}
				CMSUtil.interactWithServer(form, isAjax, Comment.nullifyComment, returnFunction, "deleteComment", "/c", extraQueryStr);
			},
			updateComment: function(form, isAjax, returnFunction){
				var extraQueryStr = null;
				if(isAjax){
					extraQueryStr = getFieldsForForm();
				} else {
					addFieldsToForm(form);
				}
				CMSUtil.interactWithServer(form, isAjax, Comment.buildComment, returnFunction, "updateComment", "/c", extraQueryStr);
			},
			buildComment: function(response){
				if(response.success == true){
					Comment.id = response.id;

					Comment.name = response.name;
					Comment.comment = response.comment;
					Comment.email = response.email;
					Comment.website = response.website;
					Comment.title = response.title;
					Comment.isAuthor = response.isAuthor;
					Comment.flagged = response.flagged;
					Comment.status = response.status;
					if(Comment.status == "AUTHOR"){
						Comment.isAuthor = true;
					} else if(Comment.status != "APPROVED") {
						Comment.flagged = true;
					}
				} else {
					Comment.error = response.error;
				}
			},
			nullifyComment: function(response){
				if(response.success == true){
					Comment.id = response.id;
					Comment.comment = null;
					Comment.email = null;
					Comment.website = null;
					Comment.title = null;
					Comment.isAuthor = null;
					Comment.flagged = null;
					Comment.status = null;
				} else {
					Comment.error = response.error;
				}
			}
		}	

		var p;
		var kP = 0;
		var	aT = 0;
		var myInterval = window.setInterval(timedMousePos,250);
		var xPos = -1;
		var yPos = -1;
		var firstX = -1;
		var firstY = -1;
		var intervals = 0;
		var d = 0;
		var mT = 0;
		document.onkeypress = lk;
		window.onload = rAT;
		

		function getMousePos(p) {
			if(!p)var p = window.event;
			if (p.pageX || p.pageY) {
				xPos = p.pageX;
				yPos = p.pageY;
			} else if (p.clientX || p.clientY) {
				xPos = p.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
				yPos = p.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			}
		}

		function lk() {	
			kP++;
		}

		function rAT() {
			aT = new Date();
		}
		
		function timedMousePos() {
			document.onmousemove = getMousePos;
			if (xPos >= 0 && yPos >= 0) {
				var newX = xPos;
				var newY = yPos;
				intervals++;
			}
			if (intervals == 1) {
				firstX = xPos;
				firstY = yPos;
			} else if (intervals == 2) {
				clearInterval(myInterval);
				calcDistance(firstX,firstY,newX,newY);	
			}
		}

		function calcDistance(aX,aY,bX,bY){
			mT = Math.round(Math.sqrt(Math.pow(aX-bX,2)+Math.pow(aY-bY,2)));
		}

		function getFieldsForForm(){
			var sT = new Date();
			d = sT - aT;
			var queryString = "&";
			queryString += "mT=" + mT;
			queryString += "&d=" + d;
			queryString += "&kP=" + kP;
			if(cid)queryString += "&cid=" + cid;
			if(did)queryString += "&did=" + did;
			queryString += "&socialMedia=Y";
			return queryString;
		}
	
		function addFieldsToForm(formObj, redirect) {
			var sT = new Date();
			d = sT - aT;
			var mTField = document.createElement('input');
			var dField = document.createElement('input');
			var kPField = document.createElement('input');
			var cidField = document.createElement('input');
			var didField = document.createElement('input');
			var socialMedia = document.createElement('input');
	
			socialMedia.setAttribute('name','socialMedia');
			socialMedia.setAttribute('type', 'hidden');
	
			mTField.setAttribute('name','mT');
			mTField.setAttribute('type', 'hidden');
 	  
			dField.setAttribute('name', 'd');
			dField.setAttribute('type', 'hidden');
  
			kPField.setAttribute('name', 'kP');
			kPField.setAttribute('type', 'hidden');
	
			cidField.setAttribute('name', 'cid');
			cidField.setAttribute('type', 'hidden');
	
			didField.setAttribute('name', 'did');
			didField.setAttribute('type', 'hidden');	
 	
			if(redirect != null) {
				var pathField = document.createElement('input');
				pathField.setAttribute('name', 'path');
				pathField.setAttribute('type', 'hidden');
				pathField.value = redirect;
				formObj.appendChild(pathField);
			}
			mTField.value = mT;
			dField.value = d;
			kPField.value = kP;
			cidField.value = cid;
			didField.value = did;
			socialMedia.value = "Y";

			formObj.appendChild(dField);
			formObj.appendChild(kPField);
			formObj.appendChild(mTField);
			formObj.appendChild(cidField);
			formObj.appendChild(didField);
			formObj.appendChild(socialMedia);
		}		

		//helper functions

		function insertComment(form, isAjax, returnFunction, showError){
			return submitToServer(Comment.insertComment, "Unable to insert comment", form, isAjax, returnFunction, showError);
		}

		function updateComment(form, isAjax, returnFunction, showError){
			return submitToServer(Comment.updateComment, "Unable to update comment", form, isAjax, returnFunction, showError);
		}
	
		function deleteComment(form, isAjax, returnFunction, showError){
			return submitToServer(Comment.deleteComment, "Unable to delete comment", form, isAjax, returnFunction, showError);
		}
	
		function createUser(form, isAjax, returnFunction, showError){
			return submitToServer(User.createUser, "Unable to create user", form, isAjax, returnFunction, showError);
		}
	
		function updateUser(form, isAjax, returnFunction, showError){
			return submitToServer(User.updateUser, "Unable to update user", form, isAjax, returnFunction, showError);
		}
	
		function loginUser(form, isAjax, returnFunction, showError){
			return submitToServer(User.loginUser, "Unable to login user", form, isAjax, returnFunction, showError);
		}
	
		function insertUserContent(form, isAjax, returnFunction, showError){
			return submitToServer(UserContent.insertContent, "Unable to insert content item", form, isAjax, returnFunction, showError);
		}
	
		function editUserContent(form, isAjax, returnFunction, showError){
			return submitToServer(UserContent.editContent, "Unable to edit content item", form, isAjax, returnFunction, showError);
		}

		function insertProfilePic(form, isAjax, returnFunction, showError){
			return submitToServer(User.insertProfilePic, "Unable to upload profile photo", form, isAjax, returnFunction, showError);
		}
	
		function addRating(form, isAjax, returnFunction, showError){
			return submitToServer(Rating.addRating, "Unable to add rating", form, isAjax, returnFunction, showError);
		}

		function attachAction(form, isAjax, returnFunction, showError){
			return submitToServer(Action.attachAction, "Unable to attach action", form, isAjax, returnFunction, showError);
		}
	
		function deleteAction(form, isAjax, returnFunction, showError){
			return submitToServer(Action.deleteAction, "Unable to delete action", form, isAjax, returnFunction, showError);
		}

		function submitToServer(method, msg, form, isAjax, returnFunction, showError){
			try{
				method(form, isAjax, returnFunction);
			} catch(e) {
				if(showError) alert(e);
				alert(msg);
			}
			if(isAjax){
				return false;
			} else {
				return true;
			}
		}

