

function getBaseURL() {
  locstr = window.location.href;
  locpath = locstr.substring(0, locstr.lastIndexOf("/"));
  return locpath+"/";
}
var siteLocate = getBaseURL();

function checkPass()
      {

    //Store the password field objects into variables ...
    var pass1 = document.getElementById('pass1');
    var pass2 = document.getElementById('pass2');
    //Store the Confimation Message Object ...
    var message = document.getElementById('confirmMessage');
    //Set the colors we will be using ...
    var goodColor = '#66cc66';
    var badColor = '#ff6666';
    //Compare the values in the password field 
    //and the confirmation field

    if(pass1.value == pass2.value){
        //The passwords match. 
        //Set the color to the good color and inform
        //the user that they have entered the correct password 
        pass2.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = 'Passwords Match!'
    }else{
        //The passwords do not match.
        //Set the color to the bad color and
        //notify the user.
        pass2.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = 'Passwords Do Not Match!'
    }
}


function checkMedsReg()
{ 
   
   if (document.getElementById('no_med_conditions').checked==true) 
        {         
             var x = document.getElementById("medical_cond_element");
             x.style.display = "block";
        }

   if (document.getElementById('no_med_conditions').checked==false) 
            {         
                var x = document.getElementById("medical_cond_element");
                x.style.display = "none";
            }             
}
  
function checkGaurdian()
{

dobRaw = document.getElementById('date_of_birth').value;
//defLoad = document.getElementById('default_dob').value;
if (dobRaw.length == 0)
        {         
             var x = document.getElementById("gaurdian_display");
             x.style.display = "none";
             var y = document.getElementById("no_gaurdian_display");
             y.style.display = "block";
             //alert("Loaded");
        }
        else {
                  changeDate();
             }


}

function changeDate()
{
  var   todayDate = document.getElementById('default_dob').value; 
  var   inputDate = document.getElementById('date_of_birth').value;
  
  var todayParts = todayDate.split('-');
  var inputParts = inputDate.split('-');
  
  cutOffYearNum = todayParts[0]-16;
  
  var cutOffYear = new Date(cutOffYearNum, todayParts[1] - 1, todayParts[2]);
  var inputRebuilt = new Date(inputParts[0], inputParts[1] - 1, inputParts[2]);
  

    if( (new Date(cutOffYearNum, todayParts[1] - 1, todayParts[2]).getTime() < new Date(inputParts[0], inputParts[1] - 1, inputParts[2]).getTime()))
    {
                 var x = document.getElementById("gaurdian_display");
                 x.style.display = "block";
                 var y = document.getElementById("no_gaurdian_display");
                 y.style.display = "none";
                 //alert("a:today="+todayDate+" & Input = "+inputRebuilt+" cutt off="+cutOffYear);
                 var unSix = 0;
                 document.getElementById('is_under_sixteen').value = unSix;
    }

    if( (new Date(cutOffYearNum, todayParts[1] - 1, todayParts[2]).getTime() >= new Date(inputParts[0], inputParts[1] - 1, inputParts[2]).getTime()))
    {
                 var x = document.getElementById("gaurdian_display");
                 x.style.display = "none";
                 var y = document.getElementById("no_gaurdian_display");
                 y.style.display = "block";
                 //alert("b:today="+todayDate+" & Input = "+inputRebuilt+" cutt off="+cutOffYear);
                 var unSix = 1;
                 document.getElementById('is_under_sixteen').value = unSix;
    }
}

function dupGaurdPh()
{
   var mobPhone = document.getElementById("mob_phone").value;
   document.getElementById('guardian_mob_phone').value = mobPhone;
}
function dupAuthPh()
{
   var mobPhone = document.getElementById('guardian_mob_phone').value;
   document.getElementById("mob_phone").value = mobPhone;
}


function dupGaurdEm()
{
   var mobPhone = document.getElementById("email_add").value;
   document.getElementById('guardian_email_add').value = mobPhone;
}
function dupAuthEm()
{
   var mobPhone = document.getElementById('guardian_email_add').value;
   document.getElementById("email_add").value = mobPhone;
}


function checkConsent()
{ 
   
   if (document.getElementById('guardian_consent_1').checked==true || document.getElementById('guardian_consent_2').checked==true) 
        {         
             var x = document.getElementById("submit");
             x.style.display = "block";
        }

   if (document.getElementById('guardian_consent_1').checked==false && document.getElementById('guardian_consent_2').checked==false) 
            {         
                var x = document.getElementById("submit");
                x.style.display = "none";
            }             
}


function checkDomReg()
{
   if (document.getElementById('check_attn').checked==true) 
        {         
             var x = document.getElementById("participants_book");
             x.style.display = "block";
        }

   if (document.getElementById('check_attn').checked==false) 
            {         
                var x = document.getElementById("participants_book");
                x.style.display = "none";
            } 

}

function getActiivtyListLoad(activityTypeID, activityID)
{

    functJS = 105;
    var xhttp; 
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) 
    {
    const serverQuote = document.getElementById("activity_show");   
    serverQuote.innerHTML = this.responseText; 
    }
    
  };
    xhttp.open("GET",siteLocate+"js/remote_posting.js.php?activ_type_id="+activityTypeID+"&activity_id="+activityID+"&funct_js="+functJS, true);
    xhttp.send(); 
    
}

function getActivityListChange()
{

    functJS = 105;
   /* document.getElementById('activity_show').innerHTML = "it worked";*/
    activityTypeID = document.getElementById('activ_type_id').value;
    activityID =0;
    var xhttp; 
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) 
    {
        const serverQuote = document.getElementById("activity_show");   
        serverQuote.innerHTML = this.responseText; 
    }
    
    };
    xhttp.open("GET",siteLocate+"js/remote_posting.js.php?activ_type_id="+activityTypeID+"&activity_id="+activityID+"&funct_js="+functJS, true);
    xhttp.send(); 
  
  /*document.getElementById('change_book_msg').innerHTML = "it worked";
  alert("Seelction working!");*/
  /*location.reload(); 
  setTimeout(function(){location.reload()}, 500);*/
}

function checkMeds()
{ 
 /*  
if(document.getElementById('no_med_conditions').checked==true)
    {
        alert('Its working');
    }
*/
   
   if (document.getElementById('no_med_conditions').checked==true) 
        {         
             var x = document.getElementById("medical_cond_element");
             x.style.display = "block";
        }

   if (document.getElementById('no_med_conditions').checked==false) 
            {         
                var x = document.getElementById("medical_cond_element");
                x.style.display = "none";
            } 
            /*
    if (document.getElementById('no_med_conditions_n').checked==false) 
        {         
             var x = document.getElementById("medical_cond_element");
             x.style.display = "block";
        }
   if (document.getElementById('no_med_conditions_n').checked==true)
                var x = document.getElementById("medical_cond_element");
                x.style.display = "none";
            } 
*/


            
}





function emailIsValid (email) 
{
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
}

function emailIsValidStrong(email) 
{
   return email.match(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)
}


function checkEmailLogin ()
{
     var email = document.getElementById('email_add').value;
     
      if (emailIsValid(email)) 
      {
          
           if (emailIsValidStrong(email)) 
           {
                // email is fine alert("email fine");
                  url=getBaseURL()+"js/remote_posting.js.php";
                  $.ajax({
                   type: "POST",
                   url: url,
                   data: {
                   "email_add"     : email,
                   "check_email_exists" : 101,
                 },
                   success: function(data) {
                  if (data["ok"])
                  {          
                      document.getElementById('email_add').value ="";
                      alert("Email already exists, please try a different email address");
                  } 
                  else
                  {
                      
                  }
                }
                });

           }
           else
           {
              document.getElementById('email_add').value ="";
              alert("Please fill in valid email address.");
              document.getElementById('email_add').focus();
           }
           
             // alert("email fine2");
      } 
      else 
      {
        document.getElementById('email_add').value ="";
        alert("Please fill in valid email address");
        document.getElementById('email_add').focus();
      } 
      
}

  function searchFunction() {
    var input, filter, tables, i, j, tr, td, txtValue, found;
    input = document.getElementById("searchInput");
    filter = input.value.toUpperCase();
    tables = document.getElementsByClassName("table_onsite_display_2");
    document.getElementById("searchResults").innerHTML = "";
    found = false;
    $numFound = tables.length;
    console.log("tables = "+tables.length);
    for (i = 0; i < tables.length; i++) {
        tr = tables[i].getElementsByTagName("tr");
        for (j = 0; j < tr.length; j++) {
            td = tr[j].getElementsByTagName("td");
            for (var k = 0; k < td.length; k++) {
                txtValue = td[k].textContent || td[k].innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    if (!found) {
                        found = true;
                        tr[j].scrollIntoView({ behavior: "smooth", block: "center" });
                    }
                    tr[j].classList.add("highlight");
                    break; // Highlight only once per row
                }
            }
        }
    }

    if (!found) {
        document.getElementById("searchResults").innerHTML = "<p>No results found.</p>";
    }
}

function setPlacesValues(totalPlaces, filledPlaces)
{

    freeSlots = totalPlaces - filledPlaces;
    var slotsData = "<div style='text-align:center; height:30px; width:100%; font-weight:600;'>Total places = "+totalPlaces+", Filled places = "+filledPlaces+", Free slots ="+freeSlots+"</div>";
    console.log("slotsData == "+slotsData);
    document.getElementById("report_slot_numbers").innerHTML = slotsData;

}