// Speleology Order Form
// Copyright 2006 David Gibson

// 4/7/07 added issues 10-11, deleted two reasons

/*
reasons = new Array('Author or contributor to this issue.', 
  		    'Advertiser seeking sample copy.', 
		    'BCA member whose copy has not arrived.', 
		    'Regular overseas correspondent or reviewer.', 
		    'UK regional correspondent.', 
		    'Journal exchange direct with the editor.', 
		    'Other (please give reason below).' ) ;
*/

reasons = new Array('Author or contributor to this issue.', 
		    'BCA member whose copy has not arrived.', 
		    'Regular overseas correspondent or reviewer.', 
		    'UK regional correspondent.', 
		    'Journal exchange direct with the editor.') ;

  
  var reason_number, label_type;
  var mag = 'Speleology ';  // note trailing space


function initialise()
{
  var j=0, start=4; // so first issue is 5
  form.issue.options[j++] = new Option(mag + (j+start) 	+ ' (Dec 2005)', 	(j+start));  // issue 5
  form.issue.options[j++] = new Option(mag + (j+start) 	+ ' (Apr 2006)', 	(j+start));  // issue 6
  form.issue.options[j++] = new Option(mag + (j+start) 	+ ' (Aug 2006)', 	(j+start));  // issue 7
  form.issue.options[j++] = new Option(mag + (j+start)	+ ' (Dec 2006)', 	(j+start));  // issue 8
  form.issue.options[j++] = new Option(mag + (j+start) 	+ ' (Apr 2007)', 	(j+start));  // issue 9
  form.issue.options[j++] = new Option(mag + '10/11'   	+ ' (Aug/Dec 2008)', 	(j+start));  // issue 10-11
  form.issue.options[j++] = new Option(mag + (j+start+1)+ ' (Aug 2008 (est.))', 	(j+start+1));  // issue 12
  
  j=0;
  form.reason[j++].value = (j-1);
  form.reason[j++].value = (j-1);
  form.reason[j++].value = (j-2);
  form.reason[j++].value = (j-1);
  form.reason[j++].value = (j-1);
  // form.reason[j++].value = (j-1);
  // form.reason[j++].value = (j-1);  // 7 reasons

  resetform();
  resetStaffBits();
}

function resetform()
{
  form.reset();
  form.issue.options[form.issue.options.length-1].selected = true;
  reason_number = form.reason.length-1;
  form.reason[reason_number].checked = true;
  form.destination[1].checked = true;
}

function resetStaffBits()
{
  form.mailroom.value = 'd.gibson';
  form.label[0].checked = true;
  label_type = form.label[0].value;
} 

function prepareAddress(addition)
{
  if (addition != '') { $summary += addition + "\n"; } 
}  

function BCAwarn()
{
  window.alert("If you are claiming a free issue because \n" +
               "of non-delivery of your BCA member\'s copy \n" + 
               "you must type your BCA membership number \n" +
               "in the message box below.") ;
}               
  
function checkForm()
{

// Check the info in form, and process the buttons in form2

  if (form.debug.value == "%%") 
  {
    form.name.value = "David Gibson";
    form.street_line1.value = "12 Well House Drive";
    form.street_line2.value = "";
    form.posttown.value = "LEEDS";
    form.postcode.value = "LS8 4BX";
  }

  var s = "";
  if (form.name.value == "") { s += "- Name not given.\n"; }

  var addressComplete =  ((form.street_line1.value  != "") & (form.posttown.value != "")) | (form.overseas.value != "");
  var addressDuplicate = ((form.street_line1.value  != "") & (form.posttown.value != "")) & (form.overseas.value != "");
  
  if (!addressComplete) 
  { s += "- Address is missing or incomplete.\n"; }
  
  if (addressDuplicate) 
  { s += "- Address appears twice. \n  Please delete one entry.\n"; }
  
  
  if ((reason_number == form.reason.length-1) & ( (form.message.value == "(none)") | (form.message.value == "") ) )
  { s += "- Reason for Request not selected \n  OR message box is blank"; }
  
  if ((reason_number == form.reason.length-5) & ( (form.message.value == "(none)") | (form.message.value == "") ) )
  { s += "- Please type your BCA membership \n  number in message box"; }

                                     
  if (s != "")
  {
    window.alert("Some information is missing from \nthis form...\n\n" + s);
    return false;
  }

  // published = new Date(2007,3,1,0,0,0,0);
  // window.alert(published.toUTCString());
  
  published = new Date();
  // window.alert('Debug: ' + published.getFullYear() + '  ' + published.getYear())
  published = 5 + 3*(published.getFullYear() - 2006) + Math.floor((published.getMonth() + 1)/4);

  s =  "The most recent issue is #" + published + "\n";
  s += "Issue requested is #" + (form.issue.selectedIndex + 5) + "\n\n";
  
  if ( (form.issue.selectedIndex + 5) > published)
  {
    s += "The issue you have requested has not \n" +
         "been published yet. Please wait until \n" +
         "the publication date before ordering.";

    window.alert(s);
    return false;
  }


  $summary = form.name.value + "\n";
  
  prepareAddress(form.street_line1.value);
  prepareAddress(form.street_line2.value);
  prepareAddress(form.posttown.value);
  prepareAddress(form.postcode.value);
  prepareAddress(form.overseas.value);
  
  if ( (form.overseas.value != "") & (form.destination[0].checked == false) )
  {
    if (!window.confirm('You have completed an address in the Overseas box\n' + 
                        'but the destination is not set to Airmail. Do you want to proceed?\n')) return false;
  }                      

  $summary += "\nRequested Magazine: " + mag + form.issue.options[form.issue.selectedIndex].value;
  $summary += "; Copies: " + (form.copies.selectedIndex + 1) + "\n";
  $summary += "Reason: " + reasons[reason_number] + "\n";
  if (form.message.value =="") form.message.value = "(none)";
  $summary += "Message: " + form.message.value + "\n";
  for (j=0; !form.destination[j].checked; j++) ; // no statements in loop; j is global
  destination_number = j;  // j was global
  $summary += "Destination: " + form.destination[destination_number].value + "\n";
  $summary += "Originator: " + form.originator.value + "\n\n";
  
  // $summary += "Staff use: Mailroom %" + form.mailroom.value + "% label-format %" + label_type + "%\n";

  $summary = "YOUR ORDER DETAILS\n" +
             "==================\n\n" + $summary;
    
  if ( !window.confirm($summary) ) return false;
    
  var today = new Date();
  var digits = (Math.floor(today.getTime()/1000) - 800000000) % 10000;
  digits = '0000' + digits;
  digits = digits.substr(digits.length - 4);
  form2.ref.value  = "HE06-" + digits + "/0";
  form2.javascript.value = jsversion;
  form2.order.value = $summary + "\n";

  // Cant be bothered to write a routine to parse $summary for info on Payment page so...
  form2.mailroom.value = form.mailroom.value;
  form2.label.value = label_type;
  form2.destination.value = form.destination[destination_number].value;

  form2.submit(); 
}


function staffonly(s)
{
  if (form.auth.value != 'caraway')
  {
    alert("This is for staff use only");
    resetStaffBits()
  }
  else 
  {
    // if (s != 'debug') { label_type = s;}
    if (form.label[0].checked) { label_type = form.label[0].value;}
    if (form.label[1].checked) { label_type = form.label[1].value;}
    if (s=='A4')
       { alert('A4 is not currently an option. \nThe Radio Button values are ignored'); }
  }
}    
    
  