
//DISUCSSION MANAGER CLASS
	var DiscussionManager = new Class({
		initialize: function(discName,discInterval,this_movie_id){
			console.log('New DiscussionManager',discName,discInterval,this_movie_id);
			this.file_mod_date="";
			this.movie_id=this_movie_id;
			this.discName=discName; //discName is the same name as the form name
			this.discussionGetter = this.getLatestDiscussion.periodical(discInterval,this);
		},
		setObjProperty: function(propName,propValue) {
			this[propName]=propValue;
		},
		populateInitialFields: function() {
			//loop through field values (set in html) in format: field_N_initial
			//and populate fields in format: field_1
			//until there are no more fields to populate
				var counter=1;
				var this_field_data=this['field_'+counter+'_initial'];
				while (this_field_data){
					this_field_name="field_"+counter;
					this.updateField(this_field_name,this_field_data);
					console.log(this_field_data);
					counter++;
					this_field_data=this['field_'+counter+'_initial'];
				}
		},
		catchEnter: function(event) {
			if (event && event.keyCode == 13){
    			this.submitNewComment();
				console.log('return clicked for',this.discName);
				return false;
  			}else{
    			return true;
			}
		},
		clearIfHasDefaultValues: function(fieldID) {
			if (this.fieldHasInitialValue(fieldID)){
				this.updateField(fieldID,'');
				return(true);
			}else if (this.fieldHasErrorValue(fieldID)){
				this.updateField(fieldID,'');
				return(true);
			}
			return false;
		},
		fieldHasErrors: function(fieldID) {
			var error_msg=this[fieldID+'_error'];
			
			if (this.fieldIsEmpty(fieldID)){
				this.updateField(fieldID,error_msg)
				return (true);
				
			}else if (this.fieldHasInitialValue(fieldID)){
				this.updateField(fieldID,error_msg)
				return(true);
				
			}else if (this.fieldHasErrorValue(fieldID)){
				return(true);
			}
			return false;
			
		},
		fieldHasErrorValue: function(fieldID) {
			if ($(this.discName)[fieldID].value==this[fieldID+'_error']){
				return true;
			}
			return false;
		},
		fieldHasInitialValue: function(fieldID) {
			if ($(this.discName)[fieldID].value==this[fieldID+'_initial']){
				return true;
			}
			return false;
		},
		fieldIsEmpty: function(fieldID) {
			if ($(this.discName)[fieldID].value==""){
				return true;
			}
			return false;
		},
		updateField: function(fieldID,newCopy) {
			$(this.discName)[fieldID].value=newCopy;
		},
		updateScreen: function(newCopy) {
			var thisScreenName=this.discName+'_copy';
			$(thisScreenName).innerHTML=newCopy;
		},
		getLatestDiscussion: function() {
			new Ajax('dp.php', {
				method: 'post',
				postBody: 'act=get_cur_discussion&ev=discusser&file_mod_date='+this.file_mod_date+'&discName='+this.discName+"&movie_id="+this.movie_id,
				onComplete: this.processDiscussionFile.bind(this)
			}).request();
		},
		allFieldsValidate: function() {
			//loop through all fields and run fieldHasErrors on each one
				var counter=1;
				var this_field_ID="field_"+counter;
				while ($(this.discName)[this_field_ID]){ //resolves false when the field doesnt exist
					if (this.fieldHasErrors(this_field_ID)){
						return false;
					}
					counter++;
					this_field_ID="field_"+counter;
				}
				return true;
		},
		submitNewComment: function() {
			if ( this.allFieldsValidate() ){
				this.sendCommentToServer();
				console.log('SENDING');
			}
		},
		sendCommentToServer: function() {
			var this_qString=$(this.discName).toQueryString()+'&act=write_new&discName='+this.discName+"&movie_id="+this.movie_id;
			console.log(this_qString);
			new Ajax('dp.php', {
				method: 'post',
				postBody: this_qString,
				onComplete: this.processDiscussionFile.bind(this)
			}).request();
		},
		processDiscussionFile: function(req) {
			console.log('processDiscussResults has been completed');
			var myObject = Json.evaluate(req);
			console.log(myObject);
			if (myObject.result_code=="post_successful"){
				this.updateField('field_1','');
			}
			if ((myObject.result_code=="is_new_discussion") || (myObject.result_code=="post_successful")){
				this.updateScreen(myObject.result_data);
				this.file_mod_date=myObject.file_mod_date;
			}
		}
	});
	
