
<script type="text/javascript">

	//Objects to interact with server
	var CMSUtil = {
		interactWithServer: function(form, isAjax, buildObjectFunction, 
									returnFunction, action, servlet, 
									extraFields){
			if(isAjax == true && returnFunction != null){
            	var queryString = this.serializeForm(form);
				if(queryString != null){
					if(action != null){
						queryString += "&action=" + action + "&isAjax=Y";	
					}else{
            			queryString += "&isAjax=Y";
            		}
            		if(extraFields){
            			queryString += extraFields;
            		}
            		
            		var req = this.getXmlRequestObject();
					if(req != null) {
            			var handlerFunction = this.getReadyStateHandler(req, returnFunction, buildObjectFunction);
						req.onreadystatechange = handlerFunction;
						req.open("POST", servlet, true);
            			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            			req.send(queryString);
            			return false;
					}
				}
            }else{
 				this.submitUserFormNoAJAX(form, action, servlet); 
			}
		},
		
		getXmlRequestObject: function() {
			var req;
			if(window.XMLHttpRequest){
				req = new XMLHttpRequest();
			}else if(window.ActiveXObject){
				try{
					req = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
					try{
						req = new ActiveXObject("Microsoft.XMLHTTP");
					}catch(e){
               			return null;
               		}
				}
			}
    		return req;
		},
		
		getReadyStateHandler: function(req, returnFunction, buildObjectFunction) {

			return function () {

		    	// If the request's status is "complete"
    			if (req.readyState == 4) {
      
      				// Check that a successful server response was received
   		   			if (req.status == 200) {

        				// Pass the XML payload of the response to the 
        				// handler function
        				var response = "(" + req.responseText + ")";
            			response = eval(response);
						buildObjectFunction(response);	
            			returnFunction(response); 
					} else {
						// An HTTP problem has occurred
        				alert("HTTP error: "+req.status);
      				}
    			}
  			}
		},
		
		serializeForm: function(form){
			var paramString = "";
			var ampRegex = new RegExp("&", "g");
	  		var replaceAmpStr = escape("&#38;");
	  		var percentRegex = new RegExp("%", "g"); 
	   		var replacePerStr = escape("&#37;");

       		for(var i = 0;i < form.elements.length;i++){
       			var field = form.elements[i];
       			if(field.tagName === "TEXTAREA" || (field.tagName === "INPUT" && field.type !== "submit")){
       				if(field.type === "checkbox" && !field.checked){
       					 continue;
       				}
					var fieldValue = field.value;
					fieldValue = fieldValue.replace(percentRegex, replacePerStr);
	       	    	fieldValue = fieldValue.replace(ampRegex, replaceAmpStr);
       				paramString += field.name + "=" + fieldValue + "&";
           		}
       		}

			return paramString;
		},
		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 Ranking = {
		insertRanking: function(form, isAjax, returnFunction){
			CMSUtil.interactWithServer(form, isAjax, Ranking.buildRanking, returnFunction, "rankComment", "/p");
		},
		
		buildRanking: function(response){
			if(response.success == true){
				Ranking.success = true;
				Ranking.objectId = response.objectId;
				Ranking.type = response.type;
				Ranking.ipAddress = response.ipAddress;
				Ranking.rankDate = response.rankDate;
				Ranking.upRanking = response.upRanking;
				Ranking.downRanking = response.downRanking;
			} else {
				Ranking.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");
        },
        
        insertRating: function(form, isAjax, returnFunction){
			var extraQueryStr = null;
			if(isAjax){
				extraQueryStr = getFieldsForForm();
			}else{
				addFieldsToForm(form);
			}
			CMSUtil.interactWithServer(form, isAjax, Comment.buildComment, returnFunction, "insertComment", "/c", extraQueryStr);
		},
        
		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.ID = response.ID;
				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;
				User.errorMsg = response.errorMsg;
				User.errorCode = response.errorCode;
			}
		}
	}
	
	var UserContent = {
		insertContent: function(form, isAjax, returnFunction){
			var extraQueryStr = null;
			
			if(isAjax){
				extraQueryStr = getFieldsForForm();
			}else{
				addFieldsToForm(form);
			}
			UserContent.messages = null;
			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;
				UserContent.messages = response.messages;
			}
		}
	}
	
	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){
						Comment.name = null;
			Comment.website = null;
			Comment.title = null;
			Comment.comment = null;
			Comment.email = null;
			Comment.rating = null;
			Comment.ratingType = null;
			Comment.commentType = null;
			Comment.flagged = false;
			Comment.isAuthor = false;
			Comment.error = null;
			Comment.status = null;
			Comment.level = 0;
			Comment.parentID = 0;
			Comment.threadID = 0;
			Comment.id = 0;
			Comment.extID = null;		
		
			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;
				Comment.rating = response.rating;
				Comment.ratingType = response.ratingType;
				Comment.commentType = response.commentType;
				if(Comment.status == "AUTHOR"){
					Comment.isAuthor = true;
				} else if(Comment.status != "APPROVED") {
					Comment.flagged = true;
				}
			    Comment.id = response.id;
			    Comment.threadID = response.threadID;
			    Comment.parentID = response.parentID;
			    Comment.level = response.level;
			    Comment.extID = response.extID;
			} else {
				Comment.error = response.error;
			}
		},
		
		nullifyComment: function(response){
			if(response.success == true){
				Comment.comment = null;
				Comment.email = null;
				Comment.website = null;
				Comment.title = null;
				Comment.isAuthor = null;
				Comment.flagged = null;
				Comment.status = null;
				Comment.id = 0;
			    Comment.threadID = 0;
			    Comment.parentID = 0;
			    Comment.level = 0;
			    Comment.extID = null;
			} else {
				Comment.error = response.error;
			}
		}
	}	
	
	var Clickability = Clickability || {};
	Clickability.SMTK = {
		kP : 0,
		aT : 0,
		myInterval : null,
		xPos : -1,
		yPos : -1,
		firstX : -1,
		firstY : -1,
		intervals : 0,
		cid :  -1 ,
		did :  429209 ,
		d : 0,
		mT : 0
	}
	Clickability.SMTK.myInterval = window.setInterval(timedMousePos,250);
	document.onkeypress = lK;
	window.onload = rAT;

		Clickability.Comment = Comment;
		
		var p;
	var kP = 0;
	var	aT = 0;
	var myInterval = Clickability.SMTK.myInterval;
	var xPos = -1;
	var yPos = -1;
	var firstX = -1;
	var firstY = -1;
	var intervals = 0;
	var d = 0;
	var mT = 0;
	var cid =  -1 ;
	var did =  429209 ;	
	
	function getMousePos(p) {
		if(!p)var p = window.event;
		if (p.pageX || p.pageY) {
			Clickability.SMTK.xPos = p.pageX;
			Clickability.SMTK.yPos = p.pageY;
		} else if (p.clientX || p.clientY) {
			Clickability.SMTK.xPos = p.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			Clickability.SMTK.yPos = p.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	}

	
	function lK() {	
		Clickability.SMTK.kP++;
	}

	function rAT() {
		Clickability.SMTK.aT = new Date();
	}
		
	function timedMousePos() {
		document.onmousemove = getMousePos;
		if (Clickability.SMTK.xPos >= 0 && Clickability.SMTK.yPos >= 0) {
			var newX = Clickability.SMTK.xPos;
			var newY = Clickability.SMTK.yPos;
			Clickability.SMTK.intervals++;
		}
		if (Clickability.SMTK.intervals == 1) {
			Clickability.SMTK.firstX = Clickability.SMTK.xPos;
			Clickability.SMTK.firstY = Clickability.SMTK.yPos;
		} else if (Clickability.SMTK.intervals == 2) {
			clearInterval(Clickability.SMTK.myInterval);
			calcDistance(Clickability.SMTK.firstX,Clickability.SMTK.firstY,newX,newY);	
		}
	}


	function calcDistance(aX,aY,bX,bY){
		Clickability.SMTK.mT = Math.round(Math.sqrt(Math.pow(aX-bX,2)+Math.pow(aY-bY,2)));
	}
	
	function getFieldsForForm(){
		var sT = new Date();
		Clickability.SMTK.d = sT - Clickability.SMTK.aT;
		var queryString = "&";
		queryString += "mT=" + Clickability.SMTK.mT;
     	queryString += "&d=" + Clickability.SMTK.d;
     	queryString += "&kP=" + Clickability.SMTK.kP;
     	if(Clickability.SMTK.cid)queryString += "&cid=" + Clickability.SMTK.cid;
     	if(Clickability.SMTK.did)queryString += "&did=" + Clickability.SMTK.did;
     	queryString += "&socialMedia=Y";
	    return queryString;
	}
		
	function addFieldsToForm(formObj, redirect) {
		var sT = new Date();
		Clickability.SMTK.d = sT - Clickability.SMTK.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 = Clickability.SMTK.mT;
		dField.value = Clickability.SMTK.d;
		kPField.value = Clickability.SMTK.kP;
		cidField.value = Clickability.SMTK.cid;
		didField.value = Clickability.SMTK.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 insertRating(form, isAjax, returnFunction, showError){
		return submitToServer(Rating.insertRating, "Unable to insert 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 insertRanking(form, isAjax, returnFunction, showError){
		return submitToServer(Ranking.insertRanking, "Unable to insert ranking", form, isAjax, returnFunction, showError);
	}
	
	function submitToServer(method, msg, form, isAjax, returnFunction, showError){
		method(form, isAjax, returnFunction);
	
		if(isAjax){
			return false;
		} else {
			return true;
		}
	}

</script>


