// Global vars
var verified=0;
var showingCheckin=false;
var myShow;

function doneLoading() {
  var tdEvents = document.getElementById('menu_events');
  if (tdEvents == null) {
    return;
  }
  else if (typeof __newsDate != 'number' || __newsDate <= 0) {
    return;
  }
  else {
    if (jQuery.cookie('newsDate') == null) {
      jQuery.cookie('newsDate', (__newsDate-1), { path: '/' });
      return;
    }
    if (jQuery.cookie('newsDate') < __newsDate) {
      jQuery.cookie('newsDate', __newsDate, { path: '/' });
      flashEvents(8);
    }
  }
}

function doHome() {
  document.location='http://www.kaivalya-yoga.com/';
}

function showCheckin() {
  jQuery("#checkin").fadeIn("slow");  
}

function hideCheckin() {
  jQuery("#checkin").fadeOut("slow");  
}

function hiliteCheckin() {
  var bc = jQuery("#checkin_button").css('background-color');
  var c = jQuery("#checkin_button").css('color');
  jQuery("#checkin_button").css({
    'background-color': c,
    'color': bc
  });
}

function unhiliteCheckin() {
  var bc = jQuery("#checkin_button").css('background-color');
  var c = jQuery("#checkin_button").css('color');
  jQuery("#checkin_button").css({
    'background-color': c,
    'color': bc
  });
}

//##############################################################################

function flashEvents(counter) {
  var tdEvents = document.getElementById('menu_events');
  if (tdEvents == null) {
    return;
  }
  if (counter <= 0) {
    tdEvents.style.backgroundColor = null;
    tdEvents.style.color = null;
    return;
  }
  if (counter % 2) {
    tdEvents.style.backgroundColor = 'black';
    tdEvents.style.color = 'white';
  }
  else {
    tdEvents.style.backgroundColor = null;
    tdEvents.style.color = null;
  }
  setTimeout('flashEvents('+(counter-1)+')', 120);
}

//##############################################################################

function setStudent(isStudent) {
  var student_email_container=document.getElementById('student_email_container');
  var student_table=document.getElementById('student_table');
  var non_student_table=document.getElementById('non_student_table');
  if (student_email_container==null ||
      student_table==null ||
      non_student_table==null) {
    alert('Error loading student tables');
    return;
  }
  if (isStudent) {
    student_email_container.style.visibility = 'visible';
    student_table.style.display = 'block';
    non_student_table.style.display = 'none';
    initStudentEmail();
  }
  else {
    student_email_container.style.visibility = 'hidden';
    student_table.style.display = 'none';
    non_student_table.style.display = 'block';
  }
}

function clearStudentEmail() {
  var student_email=document.getElementById('student_email');
  if (student_email==null) return;
  student_email.style.color = 'black';
  student_email.style.backgroundColor = null;
  student_email.value='';
  clearVerified();
}

function clearVerified() {
  var student_email_status=document.getElementById('student_email_status');
  if (student_email_status==null) return;
  student_email_status.innerHTML = '';
  student_email_status.style.color = 'black';
  setVerified(0);
}

function initStudentEmail() {
  var student_email=document.getElementById('student_email');
  if (student_email==null) return;
  student_email.style.color = 'gray';
  student_email.value='Enter a valid .edu email address';
  clearVerified();
}

function checkInitStudentEmail() {
  var student_email=document.getElementById('student_email');
  if (student_email==null) return;
  if (student_email.value.trim()=='') initStudentEmail();
}

function alertEmail() {
  var student_email=document.getElementById('student_email');
  if (student_email==null) return;
  student_email.style.backgroundColor='pink';
  alert('Please verify email before purchase');
}

function doSubmit(button) {
  var parentForm = button.parentNode;
  parentForm.submit();
}

// If you are looking at this code, then you know this is very easy to
// circumvent.  We decided not to put a lot of effot into trying to thwart
// those attempts.  We believe honesty is the best policy.  Thanks.
function verifyEmail(address) {
  if (address == null) {
    var student_email = document.getElementById('student_email');
    address = student_email.value;
  }
  var student_email_status=document.getElementById('student_email_status');
  if (student_email_status==null) return;
  student_email_status.innerHTML = 'Verifying...';
  student_email_status.style.color = 'blue';

  var data = {
    email: escape(address)
  }
  jQuery.getJSON('verify_js.php', data, setVerifiedEmail);
}

function setVerifiedEmail(success) {
  var student_email_status=document.getElementById('student_email_status');
  if (student_email_status==null) return;
  if (success) {
    student_email_status.innerHTML = 'Verified';
    student_email_status.style.color = 'green';
    setVerified(1);
  }
  else {
    student_email_status.innerHTML = 'Failed';
    student_email_status.style.color = 'red';
    setVerified(0);
  }
}

function setVerified(success) {
  verified=success;

  var student_email=document.getElementById('student_email');
  if (student_email==null) return;

  var student_table=document.getElementById('student_table');
  if (student_table==null) return;

  // Set the button actions
  var inputs=student_table.getElementsByTagName('input');
  for (var i=0; i<inputs.length; i++) {
    var input = inputs[i];
/*
    if (success && input.type=='button') {
      input.type = 'submit';
      input.onclick = null;
    }
    else if (input.type=='submit') {
      input.type = 'button';
      input.onclick = alertEmail;
    }
*/
    if (success && input.type=='submit') {
      input.style.display = 'block';
    }
    else {
      input.style.display = 'none';
    }
  }

  // Set the merchant field values
  var inputs=student_table.getElementsByTagName('input');
  for (var i=0; i<inputs.length; i++) {
    var input = inputs[i];
    if (input.name=='email' || input.name=='merchant_defined_field_1') {
      if (success) input.value=student_email.value;
      else input.value='';
    }
  }

}

//##############################################################################

function setTraining(kytt_id) {
  var divs = document.getElementsByTagName('div');
  for (var i=0; i<divs.length; i++) {
    if (divs[i].id.match('kytt_')) {
      if (divs[i].id.match(kytt_id)) {
        divs[i].style.display = 'block';
      }
      else {
        divs[i].style.display = 'none';
      }
    }
  }
}

//##############################################################################

function askDiscountCode(formId, amount, hash) {
  var formElem = document.getElementById(formId);
  if (formElem==null) {
    alert('Form error');
    return;
  }
  var baseValue = formElem.order_description.value;
  var baseParts = baseValue.split(' (');
  baseValue = baseParts[0];
  formElem.order_description.value = baseValue;
  var ask = confirm('Apply discount code?');
  if (!ask) {
    formElem.submit();
    return;
  }
  code = prompt('Enter discount code:');
  if (code=='') {
    formElem.submit();
    return;
  }
  setDiscountCode(formId, code, amount, hash);
}

function setDiscountCode(formId, code, amount, hash) {
  var formElem = document.getElementById(formId);
  if (formElem==null) {
    alert('Error applying discount code');
    return;
  }
  if (formElem.order_description==null ||
      formElem.amount==null ||
      formElem.hash==null) {
    alert('Error applying discount code');
    return;
  }
  formElem.order_description.value += ' ('+code+')';
  formElem.amount.value = amount;
  var hashParts = formElem.hash.value.split('|');
  var oldHash = hashParts.pop();
  var newHash = '';
  for (var i=0; i<hashParts.length; i++) {
    newHash += hashParts[i]+'|';
  }
  newHash += hash;
  formElem.hash.value = newHash;
  formElem.submit();
}

//##############################################################################

function showCalories() {
  var type = document.getElementById('caloriesType');
  var weight = document.getElementById('caloriesWeight');
  var weightUnits = document.getElementById('caloriesWeightUnits');
  var time = document.getElementById('caloriesTime');
  var timeUnits = document.getElementById('caloriesTimeUnits');
  var result = document.getElementById('caloriesResult');

  if (isNaN(weight.value)) {
    alert('Please enter a valid weight');
    weight.focus();
    return false;
  }

  if (isNaN(time.value)) {
    alert('Please enter a valid time');
    time.focus();
    return false;
  }

  var hr = time.value;
  if (timeUnits.value!=1) {
    hr = hr / 60;
  }
  if (hr>12 || hr<=0) {
    alert('Please enter a valid time');
    timeUnits.focus();
    return false;
  }

  var kg = weight.value;
  if (weightUnits.value!=1) {
    kg = weight.value / 2.20462262;
  }
  if (kg>182 || kg<22) {
    alert('Please enter a valid weight');
    weightUnits.focus();
    return false;
  }

  var cb = Math.round((kg * type.value) * hr);
  var wb = (Math.round(cb / 77.16) / 100) + ' kg';
  if (weightUnits.value!=1) {
    wb = (Math.round(cb / 35.00) / 100) + ' lb';
  }
  $(result).css('color', 'black');
  $(result).css('font-weight', 'bold');
  result.innerHTML = cb+' Cal';// (' + wb + ')';
}

//##############################################################################

function hilite(element) {
  if (typeof(element)=='string') element=document.getElementById(element);
  if (!element) return;
  element.src='/images/home_s.png';
}

function unhilite(element) {
  if (typeof(element)=='string') element=document.getElementById(element);
  if (!element) return;
  element.src='/images/home.png';
}

function hiliteNav(element) {
  if (typeof(element)=='string') element=document.getElementById(element);
  if (!element) return;
  var baseClassName = element.className;
  element.className = baseClassName + ' ' + baseClassName + '_h';
}

function unhiliteNav(element) {
  if (typeof(element)=='string') element=document.getElementById(element);
  if (!element) return;
  var baseClassNames = element.className.split(' ');
  element.className = baseClassNames[0];
}

//##############################################################################

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, '');
};

