/*
* $Id: cs_persData_birthDate.js,v 1.3 2009/08/19 12:46:52 annette Exp $ 
*/
function isChild(participantNo){
    $('#isChild' + participantNo).get(0).value = "true";
    $('#isAdult' + participantNo).get(0).value = "false";
    //console.log("birthdates are initialized. '#isChild' + participantNo = "+ '#isChild' + participantNo);
  }
function adjustBirthDateSelects(obj){
  var thisDate = $(obj).get(0).value.split('.');
  //console.log("month is " + thisDate[0]);
  var thisMonth = parseInt(thisDate[0], 10);
  var thisYear =  parseInt(thisDate[1], 10);
  setBirthDateDays(thisMonth, thisYear, $(obj).get(0).selectedIndex, $(obj).get(0).length, $(obj).get(0).id);
  //console.log("thisMonth " +thisMonth);
  }	

function dateStringToArr(dateString){
  var dateArray = dateString.split('.');
  dateArray[0] =  parseInt(dateArray[0], 10);
  dateArray[1] = parseInt(dateArray[1], 10);
  
  return dateArray; 
}

function getEarliestBirthDate(dateString, age){

var startDate = toDateObj(dateString);
startDate.setDate(startDate.getDate()+1);
startDate.setYear(startDate.getFullYear() - age -1);
return startDate.toDateStringDE();
}

function setBirthDateDays(theMonth, theYear, selectedIndex, length, theId){
  var firstDay = 1;
  var startDate = $('#startDate').text().split(' ');
  var theDate = dateStringToArr(getEarliestBirthDate(startDate[1], age));
  var participantNo = theId.slice(14);
  
  //console.log("participantNo " + participantNo); 
  
  if(selectedIndex == 1){
    firstDay = theDate[0];
  }
  var numDays = getDays(theMonth - 1, theYear);
  if(selectedIndex == 0){
    firstDay = 1;
    numDays = 31;
  }
  
  if(selectedIndex == (length - 1)){
    numDays = dateStringToArr(startDate[1]);
    numDays = numDays[0];
  }
  var birthDaySelect = $('#birthDay'+participantNo).get(0);
  
  for(i = birthDaySelect.length-1; i > 0; i--){
    //console.log("removing " + birthDaySelect.options[i].value);
    birthDaySelect.remove(i);
  }
  drawBirthDateDays(participantNo, firstDay, numDays);
  
  var preselectedDay = "";
  if($('#day'+participantNo).get(0).value != "---")
    preselectedDay = parseInt($('#day'+participantNo).get(0).value, 10);
  else preselectedDay = $('#day'+participantNo).get(0).value;
  //console.log("typeof(preselectDay) " +typeof(preselectDay));
  var firstPosDay = parseInt(birthDaySelect[1].value, 10);
  var newPreselectedDay = "";
  
  var lastPosDay = parseInt(birthDaySelect[birthDaySelect.length-1].value, 10);
  if(preselectedDay < firstPosDay){
    birthDaySelect.selectedIndex = 1;
    newPreselectedDay = birthDaySelect[1].value;
    //console.log(preselectedDay + " -- firstPosDay " + firstPosDay);
  }
  else if(preselectedDay > lastPosDay){
    birthDaySelect.selectedIndex = birthDaySelect.length-1;
    newPreselectedDay = birthDaySelect[birthDaySelect.length-1].value;
    //console.log(preselectedDay + " -- lastPosDay" + lastPosDay);
  }
  else {
    var theIndex = "";
    for(var i = 0; i < birthDaySelect.length; i++){
      if(birthDaySelect[i].value == preselectedDay){
        birthDaySelect.selectedIndex = i;
        theIndex = i;
      }
    }
    newPreselectedDay = birthDaySelect[theIndex].value;
    //console.log('newPreselectedDay = birthDaySelect[theIndex].value ' + newPreselectedDay);
  }
  $('#day'+participantNo).get(0).value = newPreselectedDay;
  
  
}
function drawBirthDateDays(participantNo, day, numDaysInMonth){
 //console.log("drawBirthDateDays - month has "+ numDaysInMonth + " days. First day is "+ day);
  var birthDaySelect = $('#birthDay'+participantNo).get(0);
  var index = 1;
  for(i = day; i <= numDaysInMonth; i++){
    if(i < 10)
        i = '0' + i;
      value = i;
  //console.log("add option " + i);
      birthDaySelect[index]= new Option(value, value);
      index++;
  }
}
function initBirthDateSelects(participantNo, age){
// get startDate and split it in day, month, year   
  var startDate = $('#startDate').text().split(' ');
  var theDate = dateStringToArr(getEarliestBirthDate(startDate[1], age));
  
  var day =  parseInt(theDate[0], 10);
  var month = parseInt(theDate[1], 10);
  var year = parseInt(theDate[2], 10);
  var numDaysInMonth = getDays(month-1, year);

  drawBirthDateDays (participantNo, 1, 31);
  
  var value = "";
  var birthMonthYearSelect = $('#birthMonthYear' + participantNo).get(0);
  var index = 1;

  for(var i = month;i <= 12;i++){
    if(i < 10)
      i = '0' + i;
    value = i + "." + year;
    birthMonthYearSelect[index]= new Option(value, value);
    index++;
  }
  
  for(var i = 1;i <= month;i++){
    if(i < 10)
      i = '0' + i;
    
    value = i + "." + eval(year+1);
    birthMonthYearSelect[index]= new Option(value, value);
    index++;
  }
  birthMonthYearSelect[index]= new Option('anderes Datum', 'anderes Datum');
  $('#birthDay' + participantNo).show();
  $('#birthMonthYear' + participantNo).show();
  isChild(participantNo);
}
function openBirthDateInformation(){
      $("#InnerDimensions").html($('#birthDateInformation').html());
      openPopup();
    }
function checkForSelectOtherDate(obj){
  var selectLength = obj.length;
  if(obj.selectedIndex == selectLength - 1){
    openBirthDateInformation();
    obj.selectedIndex = 0;
  }
}


