// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function mark_for_destroy(element) {
	$(element).next('.should_destroy').value = 1;
	$(element).up('.credential').hide();
}

function challengeClick(element, formField) {
	
	var userList = new String();
	userList = $F(formField).strip();

	var userArray = new Array();

	if (!userList.blank()) {
		userList.scan(/\d+/, function(s) { 
			userArray.push(s)
		});
	}


	var userId = element.id.gsub(/[^0-9]/,'');
	
	if (userArray.include(userId)) {
		// Look to see if the user id is already in the array, if so, remove it and deselect the element
		$(element).removeClassName('selected');
		userArray = userArray.without(userId);
	}
	else {
		// Otherwise, add it, and select the element
		userArray.push(userId);
		$(element).addClassName('selected');
	}
	$(formField).value = userArray.join();
}

function give_answer(answerElt, answer, flashcardSetId) {
  answerElt = $(answerElt).addClassName('selected');
  
  $('flashcardAnswers').childElements().each(function(c) {
    c.firstDescendant().writeAttribute('onclick', 'return false');
  });
  
	$('answer').value = answer;
  var answerEltId = answerElt.up().identify();
	var f = $('flashcard_form'),
	        formParams = Form.serialize(f) + '&correct_answer_id=' + CORRECT_ANSWER_ID + '&your_answer_id=' + answerEltId;
	
	submitUrl = '/games/flashcards/'+flashcardSetId+'/answer';

	new Ajax.Request(submitUrl, {asynchronous:true, evalScripts:true, parameters:formParams}); return false;
}