/* 
This file contains some custom JS that requires prototype and scriptaculous

Made by Geoffrey Donaldson http://www.afws.com.au

*/

function editInTable(beforeElem, id){
	editor = "<tr class='nonedit' id='edit_form_"+id+"'></tr>";
  
  new Insertion.After(beforeElem, editor);
}

function showInTable(editElem){
  Effect.BlindDown($(editElem))
}

function closeInTable(editElem){
  Element.remove($(editElem))
}

function loadingAjax(id) {
  Element.show($('loader_'+id));
}

function finishAjax(id) {
  Element.hide($('loader_'+id));
}

function errorAjax(id) {
  alert('Error '+request.status+'!'); 
  finishAjax(id);
}

// Will get the ID from the booking, event auto compleate.
function getSelectionId(text, li) {
    $("event_id_"+li.id).value = li.attributes.getNamedItem("eid").value;
    $("full_cost_"+li.id).value = li.attributes.getNamedItem("cost").value;
    $("booking_event_desc_"+li.id).innerHTML = li.innerHTML;
    event_cost(li.id, li.attributes.getNamedItem("cost").value);
    $("booking_event_"+li.id).value = "";
}

function set_date(id, date, string) {
  $('date_'+id).value = date;
  Element.update('date_string_'+id, string);
  RedBox.close();
}

function choose_event(b_id, e_id, name, cost) {
  $("event_id_"+b_id).value = e_id;
  Element.update("chosen_event_"+b_id, name);
  $("event_select_"+b_id).innerHTML = "";
  $("full_cost_"+b_id).value = cost;
  event_cost(b_id, cost);
}

function event_cost(b_id, cost) {
  if (cost == null){
    cost = $("full_cost_"+b_id).value
  }
  discount = $("booking_discount_"+b_id).value
  v = Math.ceil(cost - (cost*(discount/100)));
  $("amount_due_"+b_id).value = v;
}

function dismiss() {
  RedBox.close()
  alert("Developer note: \n\
  Dismiss was called, this is a defunct call, please use 'RedBox.close()'.")
}

/**
 * Returns the value of the selected radio button in the radio group
 * 
 * @param {radio Object} or {radio id} el
 * OR
 * @param {form Object} or {form id} el
 * @param {radio group name} radioGroup
 */
function $RF(el, radioGroup) {
	if($(el).type == 'radio') {
		var radioGroup = $(el).name;
		var el = $(el).form;
	} else if ($(el).tagName.toLowerCase() != 'form') {
		return false;
	}
	return $F(Form.getInputs($(el),'radio', radioGroup).find(
		function(re) {return re.checked;}
	));
}
