
// SC Calendar Utils

function test_alert() {
  alert("test");
}

function setDate(string, number) {
    var startDate = "";
    var endDate = "";
    var today = new Date();

    if (string == "today") {
        startDate = today;
        endDate = today;
    }
    if (string == "tomorrow") {
        startDate = addDays(today, 1);
        endDate = addDays(today, 1);
    }
    if (string == "yesterday") {
        startDate = addDays(today, -1);
        endDate = addDays(today, -1);
    }
    if (string == "next") {
        if (number > 0) {
            startDate = today;
            endDate = addDays(today, number);
        } else {
            endDate = today;
            startDate = addDays(today, number);
        }
    }
    

    if (string == "this_year") {
        var year = today.getFullYear();
        startDate = new Date(year,0,1);
        endDate = new Date(year,11,31);
    }
    if (string == "another_year") {
        year = document.getElementById('another_year').value;
        startDate = new Date(year,0,1);
        endDate = new Date(year,11,31);
        document.getElementById('another_year').value = "";
    }
    
    setDateDropdown('event_starts_on', startDate);
    setDateDropdown('event_ends_on', endDate);

    //startDateObj.value= startDate;
    //endDateObj.value= endDate;
    //mainform.submit(); 
}


function setDateDropdown(name, date) {
  document.getElementById(name+"_3i").value = date.getDate();
  document.getElementById(name+"_2i").value = date.getMonth()+1; // Add 1 because JS getMonth is 0-11
  document.getElementById(name+"_1i").value = date.getFullYear();
}


function fd(date) {
    return formatDate(date, "dd/MM/yyyy");
}

function addDays(date, days) {
    var day = date.getDate();
    var month = date.getMonth();
    var year = date.getFullYear();
    day += days;
    var newDate = new Date(year, month, day);  
    return newDate;
}

function makeSortableById(id) {
  var theTable = document.getElementById(id);
  sorttable.makeSortable(theTable);
}


/* self resizing iframe */
function resize() {
    //alert("Resizing");
    var iframe = document.getElementById('hiddenframe');
    var wrapper = document.getElementById('wrapper');
    var height = (Math.max(document.body.offsetHeight, document.body.scrollHeight)+20);
    var width = Math.max(document.body.offsetWidth, document.body.scrollWidth)+20;

    //alert("resizing to height: "+height+" and width: "+width);
    //var all_content = document.getElementById("layout_all");
    iframe.src = 'http://www.fig-gymnastics.com/resizer.html?height='+height+'&width='+width;
}


function print_result(category_id) {
  var all_divs = document.getElementsByTagName("div");
  for (i=0;i<all_divs.length;i++) {
    if (all_divs.item(i).className == 'results_container') {
      all_divs.item(i).style.display = 'none';
    } 
  } 
  var this_result = document.getElementById("results_container_"+category_id);   
  this_result.style.display =  'block';
  window.print();
  for (i=0;i<all_divs.length;i++) {
    if (all_divs.item(i).className == 'results_container') {
      all_divs.item(i).style.display = 'block';
    } 
  }
}
/* Some dropdown manipulation */
function hasOptions(obj) {
    if (obj!=null && obj.options!=null) { return true; }
    return false;
}


// -------------------------------------------------------------------
// removeAllOptions(select_object)
//  Remove all options from a list
// -------------------------------------------------------------------
function removeAllOptions(from) {
  if (!hasOptions(from)) { return; }
  for (var i=(from.options.length-1); i>=0; i--) {
    from.options[i] = null;
  }
  from.selectedIndex = -1;
}

// -------------------------------------------------------------------
// addOption(select_object,display_text,value,selected)
//  Add an option to a list
// -------------------------------------------------------------------
function addOption(obj,text,value,selected) {
  if (obj!=null && obj.options!=null) {
    obj.options[obj.options.length] = new Option(text, value, false, selected);
  }
}


function updateRoomAndBoardTypes(i) {
  updateRoomTypes(i);
  if (hotelBoardTypeRequired) {
    updateBoardTypes(i);
  }
}

function updateBoardTypes(i) {
  var hotelId = $('hotel_id_'+i).value;
  var boardTypeSelect = $('hotel_board_type_id_'+i);
  removeAllOptions(boardTypeSelect);
  addOption(boardTypeSelect, "Room only", "", "selected");
  var hotel = hotels[hotelId];
  for (hbt in hotel.hotel_board_types) {
    addOption(boardTypeSelect, hotel.hotel_board_types[hbt].name, hbt, "");
  }
}


function updateRoomTypes(i) {
  var hotelId = $('hotel_id_'+i).value;
  var roomTypeSelect = $('hotel_room_type_id_'+i);
  removeAllOptions(roomTypeSelect);
  addOption(roomTypeSelect, "--SELECT--", "", "selected");
  var hotel = hotels[hotelId];
  for (hrt in hotel.hotel_room_types) {
    addOption(roomTypeSelect, hotel.hotel_room_types[hrt].name, hrt, "");
  }
}

/* Hotel cost calculation */
var roomsAvailable = new Array();
roomsAvailable["Premier Inn"] =  [];
roomsAvailable["Guoman Hotel"] =  6;

function update_prices(index) {
  try {
    var numberOfNights = $('departure_day_'+index).value - $('arrival_day_'+index).value;
    var hotelId = $('hotel_id_'+index).value;
    var numberOfPeople = $('number_of_people_'+index).value;
    var hotelRoomTypeId = $('hotel_room_type_id_'+index).value;
    var pricePppn = hotels[hotelId].hotel_room_types[hotelRoomTypeId].price;
    if (hotelBoardTypeRequired) {
      var hotelBoardTypeId = $('hotel_board_type_id_'+index).value;
      if (hotelBoardTypeId) {
        var boardPppn = hotels[hotelId].hotel_board_types[hotelBoardTypeId].price;
        pricePppn += boardPppn;
      } else {
        // no board selection - assume none
      }
    } else {
    }

    var totalPrice = pricePppn * numberOfNights * numberOfPeople;

    $('number_of_nights_'+index).value = numberOfNights;
    $('number_of_nights_text_'+index).innerHTML = numberOfNights;
    $('price_pppn_'+index).value = pricePppn;
    $('price_pppn_text_'+index).innerHTML = pricePppn.toFixed(2);
    $('total_price_'+index).value = totalPrice;
    $('total_price_text_'+index).innerHTML = totalPrice.toFixed(2);
  } catch(e) {}
}


var maxAccommodationRequests = 30;

function update_people(index) {
  var this_new_or_existing = new_or_existing[index];
  var index_if_needed = (this_new_or_existing == 'existing') ? existing_ids[index] : "";
  if (acceptsNominativeAccommodationRequests) {
    var inputBoxes = "";
    var numberOfPeople = $('number_of_people_'+index).value;
    for (var i=0; i<numberOfPeople; i++) {
      inputBoxes += "<input type='text' name='registration["+this_new_or_existing+"_accommodation_request_attributes]["+index_if_needed+"][new_accommodation_request_name_attributes][][name]' class='accommodation_request_name' style='width:210px'><br/>"; 
    }
    $('accommodation_request_names_'+index).innerHTML = inputBoxes; 
  }
}



Event.onReady(function() {
    /*
  for (var i=0; i<maxAccommodationRequests; i++) {
    eval("Event.observe('hotel_id_'+"+i+", 'change', function(e) { updateRoomAndBoardTypes("+i+"); return false; });");
    eval("Event.observe('hotel_room_type_id_'+"+i+", 'change', function(e) { update_prices("+i+"); return false; });");
    eval("Event.observe('number_of_people_'+"+i+", 'change', function(e) { update_prices("+i+"); return false; });");
    eval("Event.observe('arrival_day_'+"+i+", 'change', function(e) { update_prices("+i+"); return false; });");
    eval("Event.observe('departure_day_'+"+i+", 'change', function(e) { update_prices("+i+"); return false; });");
    if (hotelBoardTypeRequired) {
      eval("Event.observe('hotel_board_type_id_'+"+i+", 'change', function(e) { update_prices("+i+"); return false; });");
    }
  }
  */
});


function check_maximum_apparatus(maximum) {
  count = maximum;
  for (var i=0; i<4; i++) {
    if ($('titles_'+i).value.length > 0 || $('authors_'+i).value.length || $('performed_by_'+i).value.length) {
      count++;
    }
  }
  if (count>12) {
    alert("A maximum of 12 apparatus can be used by individual gymnasts, but you have submitted "+count+" music entries.");
    return false;
  } else {
    return true;
  }
}

function show_photo(id) {
  var photo_el = document.getElementById('photo_'+id);
  var photo_url = document.getElementById('photo_url_'+id).innerHTML;
  var photo_object = document.getElementById('photo_object_'+id);
  photo_object.src = photo_url;
  photo_el.style.display = "block";

}

function hide_photo(id) {
  var photo_el = document.getElementById('photo_'+id);
  photo_el.style.display = "none";
}


function update_athlete_select(index) {
  alert(index);  

}
