var clientIdsArr = new Array();

function strltrim() {
  return this.replace(/^\s+/, "");
}

function strrtrim() {
  return this.replace(/\s+$/, "");
}

function strtrim() {
  return this.ltrim().rtrim();
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;

function dateAddMonths(months) {
  var month = this.getMonth() + months;
  var year = this.getYear();
  year = year % 100;
  year += (year < 70) ? 2000 : 1900;
  year += Math.floor((month - 1) / 12);
  month = (month - 1) % 12 + 1;
  this.setYear(year);
  this.setMonth(month);
}

function dateFixShortYear() {
  var year = this.getYear();
  year = year % 100;
  year += (year < 70) ? 2000 : 1900;
  this.setYear(year);
}

Date.prototype.addMonths = dateAddMonths;
Date.prototype.fixShortYear = dateFixShortYear;


function getNonSiblingBabyChildCount(classDate) 
{
  // This method returns number of entered children
  // excluding siblings that are less than freeBabyAge months old.
  // If no children entered, methods returns 1 
  // because at least one child will be entered later.
  var childCount = 0;
  var babyCount = 0;
  var clsDate = new Date(classDate);
  
  var ids = 
  [
    ['txtChildName1', 'txtChildBirthdate1', 'cmbChild1'],
    ['txtChildName2', 'txtChildBirthdate2', 'cmbChild2'],
    ['txtChildName3', 'txtChildBirthdate3', 'cmbChild3']
  ];

  for (var i = 0; i < ids.length; i++)
  {
    var txt     = elementById(ids[i][0]);
    var txtDate = elementById(ids[i][1]);
    var cmb     = elementById(ids[i][2]);
    
    var dt;
    
    if ((!txt || txt.value.trim() == "") && (!cmb || cmb.value.trim() == "")) 
      continue;
    
    childCount++;
    
    if (freeBabyAge == 0) 
      continue;
    
    try 
    {
      var dt = null;
      if (txtDate)
      {
         dt = new Date(txtDate.value.trim());
         dt.fixShortYear();
      }
      
      if (dt != null)
      {
        dt.addMonths(freeBabyAge);

        if (dt > clsDate)
          babyCount++;
      }
      
    } catch (e) {}
  }
  
  if (childCount == 0)
    return 1;
  if (babyCount == childCount)
    return 1;
    
  return childCount - babyCount;
}

function showBlock(id)
{  
  elementById(id).style.display = 'block';
}

function hideBlock(id)
{
  elementById(id).style.display = 'none';
}

function saveInitialValue(id)
{
  var ctrl = document.getElementById(id);
  ctrl.oldValue = ctrl.value;
}

function elementById(id)
{
  var c = document.getElementById(id);
  
  if (c) return c;
  
  var prefix = "";
  
  if (clientIdPart && clientIdPart != "undefined") 
    prefix = clientIdPart + "_";
    
  return document.getElementById(prefix + id);
}

function showClassesWithCheckEx(clsList, index, id)
{
  var ctrl = document.getElementById(id);
  if (ctrl.oldValue != ctrl.value)
  {
    showClassesEx(clsList, index);
  }
}


function elementByIdEx(index, id)
{
  var c = document.getElementById(id);
  
  if (c) return c;
  
  var prefix = "";
  var clientPartExt = clientIdsArr[index];
 
  if (clientPartExt && clientPartExt != "undefined") 
    prefix = clientPartExt + "_";
  
  return document.getElementById(prefix + id);
}

function registerSelectorId(index, clientId)
{
    clientIdsArr[index] = clientId;
}

function showClassesEx(clsList, index, resetselected) 
{
  try 
  {
    var prevValue = "";

    var inputs = document.getElementsByName("rbClass" + index);
    if (inputs && !resetselected) {
      for(var i = 0; i < inputs.length; i++)
        if (inputs[i].checked) {
          prevValue = inputs[i].value;
          break;
        }
    }

    var btn = elementByIdEx(index, "btnShowClasses");
    btn.style.cssText = "";

    var table = document.getElementById("ClassesGrid" + index);
    var table2 = elementByIdEx(index, "ClassesGrid");

    if (table || table2) 
    {
      if (table)
        table.parentNode.removeChild(table);
      
      if (table2)
        table2.parentNode.removeChild(table2);
        
      var btnClr = elementByIdEx(index, "btnClearClasses")
      if (btnClr)
        btnClr.style.display = "none";
    }
    
    var cmbFil1 = elementByIdEx(index, "cmbFilter1");
    var cmbFil2 = elementByIdEx(index, "cmbFilter2");
    var cmbFil3 = elementByIdEx(index, "cmbFilter3");

    var f1 = !cmbFil1 ? "-1" : cmbFil1.value;
    var f2 = !cmbFil2 ? "-1" : cmbFil2.value;
    var f3 = !cmbFil3 ? "-1" : cmbFil3.value;  
    
    // show select filter message 
    if (!f1 || !f2 || !f3) 
    {
      elementByIdEx(index, "lblNoClasses").style.cssText = "font-weight: bold;";
      elementByIdEx(index, "lblNoMatchingClasses").style.cssText = "display: none;";
      btn.style.cssText = "display: none;";
      return;
    }
        
    // Create table and insert header row 
    table = document.createElement("table");
    table.id = "ClassesGrid" + index;
    table.style.cssText = "border-width:1px;border-style:solid;width:100%;border-collapse:collapse;";
    table.rules = "all";
    table.cellSpacing = 0;
    table.cellPadding = 4;
    table.border = 1;
    table.className = "TableWithBorders";
    
    var row = table.insertRow(0);
    row.align = "center";
    row.className = "TableHeader";
    
    var cell = row.insertCell(0);
    cell.innerHTML = "&nbsp;";
    
    for(var i = 0; i < headers.length; i++)
    {
      cell = row.insertCell(1 + i);
      cell.appendChild(document.createTextNode(headers[i]));
    }

    var rowCount = 1;
    
    for(var i = 0; i < clsList.length; i++)
    {
      if (!checkFilter(f1, filter1, i) || !checkFilter(f2, filter2, i) || !checkFilter(f3, filter3, i))
        continue;  

      row = table.insertRow(rowCount++);
      cell = row.insertCell(0);
      cell.align = "center";       
      
      var clsId          = clsList[i][0];
      var availableSeats = clsList[i][1];
      var clsStartDate   = clsList[i][2];
      var childCount     = getNonSiblingBabyChildCount(clsStartDate); 
      
      if (availableSeats < childCount) 
        cell.innerHTML = "&nbsp;";
      else 
        cell.innerHTML = "<input name='rbClass" + index + "' type='radio' value='" + clsId + "' />";
      
      for(var j = 0; j < headers.length; j++)
      {
        cell = row.insertCell(1 + j);
        cell.innerHTML = clsList[i][j + 3];
        cell.style.cssText = column_styles[j];
      }  
    }
    
    elementByIdEx(index, "lblNoClasses").style.cssText = "display: none;";    
    if (table.rows.length > 1)
    {
      elementByIdEx(index, "classesGridContainer").appendChild(table);
      elementByIdEx(index, "lblNoMatchingClasses").style.cssText = "display: none;";
      var btnClr = elementByIdEx(index, "btnClearClasses")
      if (btnClr) 
        btnClr.style.display = "inline";
    }
    else
    {
      elementByIdEx(index, "lblNoMatchingClasses").style.cssText = "font-weight: bold;";
    }
    
    btn.style.cssText = "display: none;";

    inputs = document.getElementsByName("rbClass" + index);
    if (inputs) {
      for(var i = 0; i < inputs.length; i++)
        if (inputs[i].value == prevValue) {
          inputs[i].checked = true;
          break;
        }
    }
  }
  catch (e) { }
}

function checkFilter(value, filter, clsIndex)
{
  // select option 
  if (value == "")
    return false;
  // any option
  if (value == "-1")
    return true;
  
  // select classes matching this filter
  var cls = filter[value];
  if (!cls || cls.length == 0)
    return false;
  
  for(var i = 0; i < cls.length; i++)
    if (cls[i] == clsIndex)
      return true;
  
  return false;
}

function elementById(id)
{
  var c = document.getElementById(id);
  
  if (c) return c;
  
  var prefix = "";
  
  if (clientIdPart && clientIdPart != "undefined") 
    prefix = clientIdPart + "_";
    
  return document.getElementById(prefix + id);
}

function showStudentBirthDate(cmbChildId, txtDateId)
{
  var cmb = document.getElementById(cmbChildId);
  var txt = document.getElementById(txtDateId);
  
  if (!cmb || !students || !txt)
    return;
  
  var dt = students[cmb.value];
  
  if (!dt)
  {
    txt.value = '';
    return;
  }

  var year = (dt.getFullYear() % 100) + "";
  var month = (dt.getMonth() + 1) + "";
  var day = dt.getDate() + "";
  
  day = day.length == 1 ? ("0" + day) : day;
  month = month.length == 1 ? ("0" + month) : month;
  year = year.length == 1 ? ("0" + year) : year;
  
  txt.value = month + "/" + day + "/" + year;
}