
// Initialize the comments object
Comments = function( id ) {
	this.article_id = id;
  this.comment_form = GetEl( 'comment_form' );
	this.name = GetEl( 'comment_name' );
	this.text = GetEl( 'comment_text' );
	this.button = GetEl( 'comment_button' );
	this.inner = GetEl( 'comments_inner' );
	this.order = GetEl( 'comments_sort' ).options[GetEl( 'comments_sort' ).selectedIndex].value;
  this.word = GetEl( 'recaptcha_response_field'); // captcha text
  this.challenge = GetEl('recaptcha_challenge_field');
  this.status = GetEl('status_msg');
};

// Post a comment. If either field is blank, throw an error, otherwise make ajax request
Comments.prototype.post = function() {
	var name = this.name.value;
	var text = this.text.value;
	var word = this.word.value;
	this.email = GetEl( 'comment_email' ).value;
	var has_errors = 0;
  if(word=='') {
    var f = GetEl( 'comment_recaptcha_response_field_error' );
		f.style.display = '';
    f.innerHTML = 'Please enter words below.';
		has_errors = 1;
  } else {
		GetEl( 'comment_recaptcha_response_field_error' ).style.display = 'none';
  }
	if (name == '') {
		GetEl( 'comment_name_error' ).style.display = '';
		has_errors = 1;
	} else {
		GetEl( 'comment_name_error' ).style.display = 'none';
	}
	if (text == '') {
		GetEl( 'comment_text_error' ).style.display = '';
		has_errors = 1;
	} else {
		GetEl( 'comment_text_error' ).style.display = 'none';
	}
	
	if (has_errors == 1 ) {
		this.button.disabled = false;
	} else {
		this.send();
	}
};

// Make the ajax request. Post: article_id, name, text
Comments.prototype.send = function() {
	var url = '/ajax/comments.php';
	var post = 'action=post&article_id='+this.article_id+'&name='+this.name.value+'&email='+this.email+'&text='+this.text.value
           + '&recaptcha_response_field='+this.word.value
           + '&recaptcha_challenge_field='+this.challenge.value;
	var request =  YAHOO.util.Connect.asyncRequest('POST', url, { success: this.success, argument: [this] }, post ); 
};

// Callback function after making ajax request. Build the new comment and append it to the div.
Comments.prototype.success = function( req ) {
	var obj = req.argument[0];
  var comment_error = 1;
  if(req.responseText) {
    try {
        var response = eval('(' + req.responseText + ')');
        // 1 means user name exists
        if(response['status']=='ok') {
          obj.comment_form.innerHTML = '<h4>Thank you ' + obj.name.value + ' for your comment.</h4><br /><br />';
	        obj.button.disabled = true;
          comment_error = 0;
        }
        else {
          if(response['status_msg']) {
            this.status.innerHTML = response['status_msg'];
            this.status.style.display = '';
          }
          var errors = response['errors'];
          for(var i=0; i<errors.length; i++) {
            var e_id = 'comment_' + errors[i][0] + '_error';
            var error_field = GetEl( e_id );
            if(error_field) {
              error_field.style.display = '';
              error_field.innerHTML =  errors[i][1];
            }
          }
          if(errors.length) {
            setTimeout("Recaptcha.reload();", 2000);
          }
        }
    }catch(e) { /*alert(e);*/ }
  }

  if(!comment_error) {
    var div = document.createElement( 'div' );
    div.className = 'comment';
    div.innerHTML = '<strong>' + obj.name.value + '</strong>' + '<br />' + '07/30/08 at 3:13pm' + '<hr />' + obj.text.value;
    if (obj.order == 'newest') {
      obj.inner.insertBefore( div, obj.inner.firstChild );
    } else {
      obj.inner.appendChild( div );
    }
  }
};

Comments.prototype.sortComments = function() {
	this.order = GetEl( 'comments_sort' ).options[GetEl( 'comments_sort' ).selectedIndex].value;
	
	var els = GetClass( 'comment' );
	for (var i=els.length-1; i>=0; i--) {
		this.inner.appendChild( els[i] );
	}
	
	/*
	if (opt == 'newest') {
		this.sortNewest();
	} else {
		this.sortOldest();
	}
	*/
}

Comments.prototype.sortNewest = function() {
	var els = GetClass( 'comment' );
	for (var i=els.length-1; i>=0; i--) {
		this.inner.appendChild( els[i] );
	}
};

Comments.prototype.sortOldest = function() {
	var els = GetClass( 'comment' );
	for (var i=0; i<els.length; i++) {
		this.inner.appendChild( els[i] );
	}
};
