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 txt = elementById('txtChildName1');
  if (txt) 
    if (txt.value.trim() != "")
    {
      childCount++;
      if (freeBabyAge != 0) 
      {
        try 
        {
          var dt = new Date(elementById('txtChildBirthdate1').value.trim());
          dt.fixShortYear();
          dt.addMonths(freeBabyAge);
          if (dt > new Date(classDate))
            babyCount++;
        }
        catch (e) 
        {
        }
      }
    }
  txt = elementById('txtChildName2')
  if (txt)
    if (txt.value.trim() != "") 
    {
      childCount++;
      if (freeBabyAge != 0) 
      {
        try 
        {
          var dt = new Date(elementById('txtChildBirthdate2').value.trim());
          dt.fixShortYear();
          dt.addMonths(freeBabyAge);
          if (dt > new Date(classDate))
            babyCount++;
        }
        catch (e) 
        {
        }
      }
    }
  txt = elementById('txtChildName3');
  if (txt)
    if (txt.value.trim() != "") 
    {
      childCount++;
      if (freeBabyAge != 0) 
      {
        try 
        {
          var dt = new Date(elementById('txtChildBirthdate3').value.trim());
          dt.fixShortYear();
          dt.addMonths(freeBabyAge);
          if (dt > new Date(classDate))
            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(index, id)
{
  var ctrl = document.getElementById(id);
  if (ctrl.oldValue != ctrl.value)
    {
      showClassesEx(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(index) {
  try {
    var prevValue = "";

    var inputs = document.getElementsByName("rbClass" + index);
    if (inputs) {
      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 cmbLoc = elementByIdEx(index, "cmbLOC");
    var cmbClt = elementByIdEx(index, "cmbCLT");
    var loc = cmbLoc.value;
    var clt = cmbClt.value;

    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";
    }
      
    loc = cmbLoc.value;
    clt = cmbClt.value;

    if (!loc || !clt) {
      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 k = 0;
    var cell0 = k++;
    var cell1 = k++;
    var cell2 = k++;
    if (hasRegularClasses)
      var cell3 = k++;
    var cell4 = k++;
    var cell5 = k++;
    var cell6 = k++;

    var cell = row.insertCell(cell0);
    cell.innerHTML = "&nbsp;";
    cell = row.insertCell(cell1);
    cell.appendChild(document.createTextNode("Location"));
    cell = row.insertCell(cell2);
    cell.appendChild(document.createTextNode(className));
    if (hasRegularClasses) 
    {
      cell = row.insertCell(cell3);
      cell.appendChild(document.createTextNode("Schedule"));
    }
    cell = row.insertCell(cell4);
    cell.appendChild(document.createTextNode("Start Date"));
    cell = row.insertCell(cell5);
    cell.appendChild(document.createTextNode("Duration"));
    cell = row.insertCell(cell6);
    cell.appendChild(document.createTextNode(teacherRoleName));
    var bIsIE = String(navigator.userAgent).search(/MSIE/) != -1;
    var rowCount = 1;
    
    for(var i = 0; i < locations.length; i++) 
    {
      if (loc == "-1" || locations[i][0] == loc)
      {
        for(j = 0; j < classes[i].length; j++) 
        {
          if (clt != "-1" && (classes[i][j][7] != clt))
            continue;

          row = table.insertRow(rowCount++);
          cell = row.insertCell(cell0);
          cell.align = "center";

          var childCount = getNonSiblingBabyChildCount(classes[i][j][3]); 

          if (classes[i][j][5] < childCount) 
            cell.innerHTML = "&nbsp;";
          else 
            cell.innerHTML = "<input name='rbClass" + index + "' type='radio' value='" + classes[i][j][0] + "'>";
          cell = row.insertCell(cell1);
          cell.align = "center";
          cell.innerHTML = locations[i][1];
          cell = row.insertCell(cell2);
          
          var descrText = classes[i][j][1];
          var re = /(<*[^>](\')|(\"))cls_/g;
          var replaceBy = '$1cls_' + index + '_';
          descrText = descrText.replace(re, replaceBy);
          
          cell.innerHTML = descrText;
          if (hasRegularClasses) 
          {
            cell = row.insertCell(cell3);
            cell.innerHTML = classes[i][j][2];
          }
          cell = row.insertCell(cell4);
          cell.innerHTML = classes[i][j][3];
          cell = row.insertCell(cell5);
          cell.innerHTML = classes[i][j][6];
          cell = row.insertCell(cell6);
          cell.innerHTML = classes[i][j][4];
        }
      }
    }
    
    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 = elementsByName("rbClass");
    if (inputs) {
      for(var i = 0; i < inputs.length; i++)
        if (inputs[i].value == prevValue) {
          inputs[i].checked = true;
          break;
        }
    }
  }
  catch (e)
  {

  }
}


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);
}

