/* *
 * our CommentsFormCtl
 */
CommentsCtl = function() {

	/* * 
	 * register events, if any
	 */
	this.register = function() {
		//nothing to do...
	}
		
	/* * 
	 * things to do after controller loads, if anything
	 */
	this.onload = function() {
		if(! BFInstance.noxss) {
			this.xssComments();
		}
		//alert('wa');
		this.c.anonymouse = false;	
	}
	
	this.comment = function()  {
		$('comment_message').innerHTML = '';
		$('comment_message').hide();
		$('submit_comment').disabled = true;
		$('submit_comment').value="Posting comment...";
		$('comment').removeClassName('highlight');
		$('comment_name').removeClassName('highlight');
		$('comment_email').removeClassName('highlight');
		$('comment_message').innderHTML = '';
		$('comment_message').hide();
		commerr = false;
		if(this.c.anonymouse) {
			var comment_name = $('comment_name').value.replace(/^\s+|\s+$/g,'');
			var comment_email = $('comment_email').value.replace(/^\s+|\s+$/g,'');
			if( ! comment_name || comment_name == '') {
				$('comment_name').addClassName('highlight');
				commerr = true;				
			}
			if( ! comment_email || comment_email == '') {
				$('comment_email').addClassName('highlight');
				commerr = true;				
			}
		}
		var comment_value = $('comment').value.replace(/^\s+|\s+$/g,'');
		if(!  comment_value || comment_value == '') {
			$('comment').addClassName('highlight');
			commerr = true;									
		} 
		
		if(commerr) {
		  $('comment_message').addClassName('error');
			$('comment_message').innerHTML = 'Please complete the highlighted field(s)';
			$('comment_message').show();
			$('submit_comment').disabled = false;
			$('submit_comment').value="comment";					
		} else {
			var params = 'proxy_uri=/Comment&comment=' + escape( comment_value );			
			params += '&pagekey='	+ escape( this.c.pagekey );
			params += '&atco_session='	+ escape( this.getCookie('atco_session') );
			params += '&host=' + escape( window.location.hostname );
			params += '&site_name=' + escape( this.c.site_name );
			params += '&comment_url=' + escape( document.location.href );
			// title
			var title = this.stripHTML( document.getElementsByTagName('title')[0].innerHTML );
			params += '&title=' + escape(title);	
			if(this.c.anonymouse) {
				params += '&anonymouse=true';	
				params += '&name=' + escape( comment_name );	
				params += '&email=' + escape( comment_email );	
				params += '&url=' + escape( $('comment_url').value );	
			}
			
			// Get policy/terms checkbox
			var rform = $('commentForm');
			var Eacceptterms = rform['acceptterms2'];
			var termsanswer = 'no';
			if(Eacceptterms) {
				var acceptterms = $F(Eacceptterms);
				if(acceptterms !== undefined) termsanswer = 'yes';
			}
			params += '&acceptterms=' + termsanswer;
			
			var uri = this.c.root_url;
			var cr = function(res) {
				var me = BFInstance.getController('CommentsCtl');
				me.commentRes(res);
			}		
			new Ajax.Request(uri, {method:'post', parameters:params, onSuccess:cr, onFailure:this.comErr});	
		}
	}
	
	this.commentRes = function(resp) {
		//alert( resp.responseText );
		var resp_ob = eval('(' + resp.responseText + ')');
		$('submit_comment').disabled = false;
		$('submit_comment').value="Submit Comment";
		if(resp_ob['duplicate']) {
			$('comment').value = '';	
 			$('comment_message').removeClassName('success');
			$('comment_message').innerHTML = 'You have already posted this comment.';
		} else if (resp_ob['banned']) {
			$('comment').value = '';	
			$('comment_message').removeClassName('success');
			$('comment_message').innerHTML = 'Sorry, but we no longer accept comments posted from this IP address.';
		} else if (resp_ob['notaccepted']) {
			$('accept_terms_error').show();
			return;
		} else {
  		  $('comment').value = '';	
		  $('comment_message').removeClassName('error');
		  $('accept_terms_error').hide();
		  $('comment_message').addClassName('success');
			$('comment_message').innerHTML = 'Your comment has been posted.';					
		}
		if( $('comment_preview') ) {
			$('comments').removeChild( $('comment_preview') );
		}
		var comments = resp_ob['comments'];
		//DP_Debug.dump(comments);
		var parms = this.c;
		parms['comments_response'] = {comments:comments};
		parms['preview'] = false;
		var out = this.parseTemplate('comments_template', parms);
		$('comments').innerHTML += out;					
		$('comment_message').show();
	}
	

	this.resetForm = function(hide) {
		if(hide) {
			$('email_form').hide();
			$('login_form').show();		
		} else {
			$('reset_fields').show();			
			$('email_form').show();
			$('login_form').hide();					
			$('reset_message').hide();
		}
	}
	
	this.resetPassword = function() {
		var email = $('reset_email').value.strip();
		if(email == '') {
			$('reset_message').addClassName('error');
			$('reset_message').show();
			$('reset_message').innerHTML='Please enter an email address.';
		} else {
			var params = 'proxy_uri=/Recover&email=' + escape(email);			
			var uri = this.c.root_url;
			var er = function(res) {
				BFInstance.getController('CommentsCtl').resetResponse(res);
			}
			$('reset_password_button').disabled = true;
			new Ajax.Request(uri, {method:'get', parameters:params, onSuccess:er, onFailure:this.comErr});			
		}
	}	
	
	this.resetResponse = function(resp) {
		var resp_ob = eval('(' + resp.responseText + ')');
		//DP_Debug.dump(resp_ob);	
		$('reset_password_button').disabled = false;

		if(resp_ob.reset == true ) {
			//alert('wa');
			$('reset_message').className = 'message';
			$('reset_message').innerHTML = 'Your password has been reset and emailed to you. &nbsp;' +
											'<a href="" onclick="BFInstance.getController(\'CommentsCtl\').resetForm(true); return false;">' +
											'<b>Login</b>.</a>'; 
			$('reset_message').show();
			$('reset_fields').hide();
		} else {
			$('reset_message').className = 'message error';
			$('reset_message').innerHTML = 'We were unable to find an account with the email address you entered.';
			$('reset_message').show();
			//$('reset_fields').hide();				
		}
	}
	
	this.gotCommentsRes = function(resp_ob) {
		var parms = this.c;
		parms['comments_response'] = resp_ob;
		parms['preview'] = false;
		var out = this.parseTemplate('comments_template', parms);
		$('comments').innerHTML = out;
		if(resp_ob.comments && resp_ob.comments.length > 0) {			
			$('comments_header').innerHTML += ' (' + resp_ob.comments.length + ')';
		}	
	}
	
	this.xssComments = function() {
		var script = document.createElement('script');
		script.src = this.c.xsshost + '/Comments?pagekey=' + escape(this.c.pagekey);
		// IE7 doesnÕt like this: document.body.appendChild(script);
		// Instead use:
		document.getElementsByTagName('head')[0].appendChild(script);
	}
  	
	this.comErr = function(resp) {
		alert('error: ' + resp.responseText);
	}
	
}