Element.ApplicationMethods = {
  toggleActive: function(element) {
    element = $(element);
    if (element.active()) {
      element.deactivate();
    } else {
      element.activate();
    }
    return element;
  },
  active: function(element) {
    return element['application:active'];
  },
  activate: function(element) {
    element = $(element)
    if (element.active()) return element;
    element['application:inactive_text'] = element.innerHTML;
    element.innerHTML = element.getAttribute('application:active_text');
    element.addClassName(element.getAttribute('application:active_class') || 'active');
    var handler = element.getAttribute('application:on_activate');
    if (handler) eval(handler);
    element['application:active'] = true;
    return element;
  },
  deactivate: function(element) {
    element = $(element)
    if (!element.active()) return element;
    element.innerHTML = element['application:inactive_text'];
    element.removeClassName(element.getAttribute('application:active_class') || 'active');
    var handler = element.getAttribute('application:on_deactivate');
    if (handler) eval(handler);
    element['application:active'] = false;
    return element;
  },
  disableButton: function(element) {
    element = $(element);
    element.disabled = 'true';
    if (element.getAttribute('application:disabled_value')) {
      if (element.tagName == "BUTTON") {
        element['application:enabled_value'] = element.innerHTML;
        element.innerHTML = element.getAttribute('application:disabled_value');        
      } else {
        element['application:enabled_value'] = element.value;
        element.value = element.getAttribute('application:disabled_value');        
      }
    }
    return element;
  },
  enableButton: function(element) {
    element.disabled = '';
    if (element.getAttribute('application:disabled_value') && element['application:enabled_value']) {
      element.value = element['application:enabled_value'];
    }
    return element;
  }  
}
 
Object.extend(Element, Element.ApplicationMethods);
Object.extend(Element.Methods, Element.ApplicationMethods);
Element.addMethods();

var Comment = {
	validate: function(form) {
		form = $(form)
		var commentBody = $(form).down('.comment_body')
		if ($F(commentBody).gsub(/\s/, '') == '') { 
			alert('Du skal skrive noget i tekst-feltet først'); 
			$(commentBody).focus();
			return false; 
		} else { 
			return true; 
		}
	}
}

var Question = {
	validate: function(form) {
		errors = []
		form = $(form)

		var body = $(form).down('.question_body')
		if ($F(body).gsub(/\s/, '') == '') { 
			errors.push([body, 'Du skal skrive noget i tekst-feltet først'])
		}
		
		if (errors.size() > 0) {
			alert(errors.collect(function(error) { return error[1] }))
			errors[0][0].focus()
			return false; 
		} else { 
			return true; 
		}
	}
}

var CommentForm = {
	updateOtherBody: function(event) {
	  target = event.findElement()
	  commentBody = $('comment_body')
	  questionBody = $('question_body')

	  // Update the contents of the other text area with that of the target
	  if (commentBody == target) {
	    Form.Element.setValue(questionBody, $F(commentBody))
	  } else {
	    Form.Element.setValue(commentBody, $F(questionBody))
	  }
	},

	keepTextAreasInSync: function() {
		[$('comment_body'), $('question_body')].each(function(node) {
		  ['change', 'keyup'].each(function(event) {
		    Event.observe(node, event, CommentForm.updateOtherBody)
		  })
		})
	},

	toggleForms: function(checkbox) {
		questionCheckBox = $('question')
		decoy = $('question_decoy')
	  if (!decoy) { return false }

		commentForm = $('new_comment')
	  questionForm = $('new_question')

		// Decoy checkbox clicked
		if (checkbox == decoy) {
			// The user wants to create a comment
			isQuestion = false
			questionCheckBox.checked = false;
		} else {
			// The user wants to create a question
			isQuestion = true
		}

	  // Keep the decoy checkbox in sync with the real one
		decoy.checked = questionCheckBox.checked

	  if (isQuestion) {
	  	commentForm.hide()
	    questionForm.show()
	   	decoy.focus()
	  } else {
	    commentForm.show();
	    questionForm.hide();
	    questionCheckBox.focus()
	  }
	}
}
