// Javascript to process ticket applications
// maxopt is the maximum number of options (=tickets in any one category)

var maxopt = 9;
var maxcopt = 49;
var sum = 0;

// routine to calculate the total (regular) ticket cost
// also packs the ticket details into a single string
// can be invoked when select box changes or elsewhere

function calc_cost()
   {
   var gigfull_cost = 40;
   var gigconc_cost = 30;
   var dayfull_cost = 70;
   var dayconc_cost = 50; 
   var weekfull_cost = 130;
   var weekconc_cost = 95;
   var famfull_cost = 280;
   var i;
   var sumstr = 'Px';
   
//i = document.tform.week_full.selectedIndex;
//alert (i);
//alert (document.tform.week_full.options[i].value);
//alert (document.tform.ticket_cost.value);

   sum = 0;
   
   i = document.tform.gig_full.selectedIndex;
   if (i>=0 && i<=maxopt)
      {
      sum = sum + i * gigfull_cost;
      sumstr = sumstr + ' ' + i.toString();
      }
   i = document.tform.gig_conc.selectedIndex;
   if (i>=0 && i<=maxopt)
      {
      sum = sum + i * gigconc_cost;
      sumstr = sumstr + ' ' + i.toString();
      }

   i = document.tform.sat_full.selectedIndex;
   if (i>=0 && i<=maxopt)
      {
      sum = sum + i * dayfull_cost;
      sumstr = sumstr + ' ' + i.toString();
      }
   i = document.tform.sat_conc.selectedIndex;
   if (i>=0 && i<=maxopt)
      {
      sum = sum + i * dayconc_cost;
      sumstr = sumstr + ' ' + i.toString();
      }

   i = document.tform.sun_full.selectedIndex;
   if (i>=0 && i<=maxopt)
      {
      sum = sum + i * dayfull_cost;
      sumstr = sumstr + ' ' + i.toString();
      }
   i = document.tform.sun_conc.selectedIndex;
   if (i>=0 && i<=maxopt)
      {
      sum = sum + i * dayconc_cost;
      sumstr = sumstr + ' ' + i.toString();
      }

   i = document.tform.week_full.selectedIndex;
   if (i>=0 && i<=maxopt)
      {
      sum = sum + i * weekfull_cost;
      sumstr = sumstr + ' ' + i.toString();
      }
   i = document.tform.week_conc.selectedIndex;
   if (i>=0 && i<=maxopt)
      {
      sum = sum + i * weekconc_cost;
      sumstr = sumstr + ' ' + i.toString();
      }

   i = document.tform.fam_full.selectedIndex;
   if (i>=0 && i<=maxopt)
      {
      sum = sum + i * famfull_cost;
      sumstr = sumstr + ' ' + i.toString();
      }

   var sstr = sum.toString();
   if (sstr.indexOf('.') < 0) sstr = sstr + '.00';
   document.tform.ticket_cost.value = '$' + sstr;
 
 //alert ('at end of calc_cost sum,sumstr,length = ' + sum + ' ' + sumstr + ' ' + sumstr.length);
   
   return sumstr;
   }


// routine to calculate the total (choir) ticket cost
// also packs the ticket details into a single string
// can be invoked when input box changes or elsewhere

function calc_ccost()
   {
   var gigconc_cost = 30;
   var weekchoir_cost = 95;
   var minnct = 6;
   var i, s;
   var sumstr = 'Cx 0';

   sum = 0;

   i = document.ctform.gig_conc.selectedIndex;
   if (i > 0) i = i + minnct - 1;
   if (i>=0 && i<=maxcopt)
      {
      sum = sum + i * gigconc_cost;
      s = i.toString();
      if (i < 10) s = ' ' + s;
      sumstr = sumstr + s;
      }

   i = document.ctform.choir_weekend.selectedIndex;
   if (i > 0) i = i + minnct - 1;
   if (i>=0 && i<=maxcopt)
      {
      sum = sum + i * weekchoir_cost;
      s = i.toString();
      if (i < 10) s = ' ' + s;
      sumstr = sumstr + ' 0 0 0 0 0' + s + ' 0';
      }

   var sstr = sum.toString();
   if (sstr.indexOf('.') < 0) sstr = sstr + '.00';
   document.ctform.ticket_cost.value = '$' + sstr;
 
 //alert ('at end of calc_cost sum,sumstr,length = ' + sum + ' ' + sumstr + ' length = ' + sumstr.length);
   
   return sumstr;
   }


// simple test for a legal email address
// must be at least x@y.z and the domain can only have hyphen and alphanumerics

function isemail (s)
   {
   var i = s.indexOf('@');
   if (i < 1) return false;
   if (i > (s.length-4)) return false;
   var dn = s.substring(i+1);
   if (dn.length < 4) return false;
   if (dn.search(/[^a-zA-Z0-9\.-]/) >= 0) return false;
   i = dn.indexOf('.');
   if (i < 1) return false;
   if (i > (dn.length-1)) return false;
   return true;
   }


// routine called when the regular ticket selling form is submitted
// check that the name and address fields contain something and that some tickets were ordered
// return true (will submit form) if OK otherwise false (supress form submission)

function checktform()
   {
   var ss = calc_cost();
   var f = document.tform;
   
//alert (ss);

   if (ss.search(/[1-9]/) < 0)
      {
      alert ('No tickets ordered');
      return false;
      }
   if (sum <= 0)
      {
      alert ('No tickets selected');
      return false;
      }
   if (f.first_name.value.search(/[a-zA-Z]/) < 0)
      {
      alert ('Your name is required');
      f.first_name.focus();
      return false;
      }
   if (f.last_name.value.search(/[a-zA-Z]/) < 0)
      {
      alert ('Your name is required');
      f.last_name.focus();
      return false;
      }

   var t1 = f.address1.value.search(/[a-zA-Z]/) < 0;
   var t2 = f.mobile.value.search(/[0-9]/) < 0;
   var t3 = f.phone.value.search(/[0-9]/) < 0;
   if (t1 && t2 && t3)
      {
      alert ('An alternate contact method is required' + '\n\n' + '(in case there is a problem with your email address)');
      f.address1.focus();
      return false;
      }

   if (!isemail(f.email.value))
      {
      alert ('Your valid email address is required' + '\n\n' + '(This is our primary method for contacting you and confirming your ticket purchase)');
      f.email.focus();
      return false;
      }

   f.cost.value = sum.toString();
   f.details.value = ss;
   
//alert ('document.tform.cost.value = ' + document.tform.cost.value);
//alert ('document.tform.details.value = ' + document.tform.details.value);

   return true;
   }


// routine called when the choir ticket selling form is submitted
// check that the name and address fields contain something and that some tickets were ordered
// return true (will submit form) if OK otherwise false (supress form submission)

function checkctform()
   {
   var ss = calc_ccost();
   var f = document.ctform;
   
// alert (ss);

   if (ss.search(/[1-9]/) < 0)
      {
      alert ('No tickets ordered');
      return false;
      }
   if (sum <= 0)
      {
      alert ('No tickets selected');
      return false;
      }
   if (f.first_name.value.search(/[a-zA-Z]/) < 0)
      {
      alert ('Your name is required');
      f.first_name.focus();
      return false;
      }
   if (f.last_name.value.search(/[a-zA-Z]/) < 0)
      {
      alert ('Your name is required');
      f.last_name.focus();
      return false;
      }
   if (f.address1.value.search(/[a-zA-Z]/) < 0)
      {
      alert ('Your postal address is required (so that we can post the tickets to you)');
      f.address1.focus();
      return false;
      }
   if (f.choir_name.value.search(/[a-zA-Z]/) < 0)
      {
      alert ('Your choir name is required');
      f.choir_name.focus();
      return false;
      }
   if (!isemail(f.email.value))
      {
      alert ('Your valid email address is required' + '\n\n' + '(If we need to contact you we will use email)');
      f.email.focus();
      return false;
      }
   f.cost.value = sum.toString();
   f.details.value = ss;
   
//alert ('document.tform.cost.value = ' + document.tform.cost.value);
//alert ('document.tform.details.value = ' + document.tform.details.value);

   return true;
   }


// if a search string is found then display an appropriate dialogue box

function doMessage (msg)
   {
   if (msg.length < 1) return;
   if (msg.indexOf('ERR') >= 0)
      {
      msg = msg.replace(/%20/g, ' ');
      alert (msg + '\n\n' + 'We have a problem - Please notify DFoV support (support@dfov.org.au)');
      }
   else (msg.indexOf('Invalid') >= 0)
      {
      msg = msg.replace(/%20/g, ' ');
      alert (msg + '\n\n' + 'Please contact DFoV support (support@dfov.org.au) if you are having problems purchasing tickets');
      }
   }

