function getJSessionId() {
  var piparit = document.cookie;
  var pos = piparit.indexOf('JSESSIONID=');
  if (pos != -1) {
    var start = pos + 11;
    var end = piparit.indexOf(';', start);
    if (end == -1) end = piparit.length;
    return piparit.substring(start, end);
  }
  return 'unkown';
}


// muotoa p-k-vvvv, erotin voi olla myös .
function showReleaseDate(releaseDate) {
  var dateAry = releaseDate.split(/-|\./);
  if (dateAry.length == 3) {
    var release = new Date();
    release.setFullYear(dateAry[2], dateAry[1] - 1, dateAry[0]);
    if (release > new Date()) Element.show('release-date');
  }
}

function showArtistLogo(logoUrl) {
  if (logoUrl != "") Element.show('artist-logo');
}

function showOldPrice() {
  $$('span.old-price').each( function(price) {
    if (!price.empty) price.show;
  }); 
}



function validateOrderForm(event) {
  // One or the other box should be checked.
  if ( !$('customer-is-adult').checked && !$('customer-is-not-adult').checked ) {
    alert("Et ole vastannut kysymykseen \n\t'Oletko täysi-ikäinen?'\n\n" +
      "You have not answered the question \n\t'Are you over 18 years old?'");
  } 
  else if ( $('customer-is-not-adult').checked && ($('guardian-email').value.strip().length < 6) ) {
    alert("Huoltajan sähköpostiosoite on liian lyhyt!\n\nParent's email address is too short!");
  } 
  else { 
    $('guardian-email').value = ''; // make sure the content is not an invalid email address!
    return true; 
  }

  event.stop();
  return false;
}

function adultCheckBoxHandler(event) {
  if (this.checked) { 
    if (this.id == 'customer-is-adult') { 
      $('customer-is-not-adult').checked = false;
    } else if (this.id == 'customer-is-not-adult') { 
      $('customer-is-adult').checked = false;
    }
  }
  $('customer-is-not-adult').checked ? $('guardian-email-row').show() : $('guardian-email-row').hide();
}

function doOrderFormValidationSetup() {
  // NOTE: change the ws<XX> ids accordingly if the questions change!!!!!

  // find elements and set ids for them.
  var reg_form = $('registration-form');

  var check_yes = reg_form.select('span#ws20 input')[0];
  check_yes.id = 'customer-is-adult';

  var check_no = reg_form.select('span#ws20 input')[1];
  check_no.id = 'customer-is-not-adult';

  var guardian_email = reg_form.select('input#ws26')[0];
  guardian_email.id = 'guardian-email';

  var guardian_email_row = guardian_email.up('tr');
  guardian_email_row.id = 'guardian-email-row';
  if ( !$('customer-is-not-adult').checked ) guardian_email_row.hide();

  // observers
  check_yes.observe('click', adultCheckBoxHandler);
  check_no.observe('click', adultCheckBoxHandler);
  $('registration-form').down('form').observe('submit', validateOrderForm);
}
