/* reg_help.js  Help for the online registration form
   Copyright (C) 2009 - 2011  Jeffrey Hennick, o l r e g @ j e f f - h . c o m
   Part of Registration / Administation System. GLP License in ../copyleft.php.
*/
// 12/30/09 Jeff Hennick
// 1/3/10 Some simplification. Comments. Short lines per Ed. JH
// 1/8/10 getElementsByClassName IE workaround.
//        offsetWidth rather than climb tree with offsetLeft.
//        to get IE "right". Removing getTop and getLeft.
//        Add Computer College to Volunteer.  JH
// 1/12/10 Added click on checkmark to unselect per Instuctors' meeting.
//         scrollIntoView vice document.location. JH
// 1/16/10 focus() added
//         don't let help button fully off screen.  JH
// 1/17/10 When needed, make help button vertical: For Eds iPhone.  JH
// 4/17/11 Some += to +. JH

// This file has been edited to keep lines short
// There are a number of blocks like
//  if (xxx)
//   if (yyy)
//    finally_do_something;
// and blocks like
// m = xxxxx;
// m += yyyy;

var nophone = 0;  // We will check for an optional phone number
var noemail = 0;  // We will check for an optional email address
var noipa = 0;    // We will check for the optional IPA status

///////////////  API Functions ///////////////////////

// Called onload; sets up the floating "Help Me"-button
function onload_help() {
 // Position to the right edge of the main table
 if (document.all)
   var left = document.all.main_table.offsetWidth + 20; // IE only
 else var left = document.getElementById('main_table').offsetWidth + 45;

 //Now how wide is the display window?
 var myWidth = 0;
 if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
   myWidth = window.innerWidth;
  }
 else if( document.documentElement
        && ( document.documentElement.clientWidth
        || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
 }
 else if( document.body
      && ( document.body.clientWidth
      || document.body.clientHeight ) ) {  //IE 4 compatible
   myWidth = document.body.clientWidth;
 }
 // Do not let help button be totally off screen on narrow windows
 if (left + 70 > myWidth) {
  left = myWidth - 70;
  // Make Help button small, vertical, and moved to the left
  document.getElementById('help_text').innerHTML =
   "<span style='font-size:50%'>H<br />E<br />L<br />P<br />P</span>";
//alert(document.getElementById('pos_table').offsetWidth);
  // could make the display table wider for scrolling -?-
//document.getElementById('pos_table').style.width = '12in';
//alert(document.getElementById('pos_table').offsetWidth);
 }
 document.getElementById('helpbut').style.left = left + 'px';
}

function helpme(where) { // Called by Help Me-button with blank "where"
          // Could be called with a specific location
 close_help(); // Close any priviously open help block

 // Now we are going to step down the document until we get to a field
 // that needs action. We there give the var where its value.
 // Then we will provide help for it.

 // The first ones are required fields
 if (where == undefined)
  if (document.getElementById('Ecom_ShipTo_Postal_Name_First').value == '')
   where = 'name';

 if (where == undefined)
  if (document.getElementById('Ecom_ShipTo_Postal_Name_Last').value == '')
   where = 'name';

 if (where == undefined)
  if (document.getElementById('Ecom_ShipTo_Postal_Street_Line1').value == '')
   where = 'address';

 // Now we get to some optional fields.  These will give a choice in help
 // of skipping this check on the next pass, via the "noxxxxx" variables.
 if (where == undefined)
  if (!nophone
   && document.getElementById('Ecom_ShipTo_Telecom_Phone_Number').value == '')
    where = 'phone';

 if (where == undefined)
  if (!noemail
   && document.getElementById('Ecom_ShipTo_Postal_Email').value == '')
    where = 'email';

 if (where == undefined) if (!noipa) {
  // javascript and DOM give no easy way to get this
  var ipao = document.getElementsByName('IPA');
  var ipac = 0; // So step through them all but last one
   for (var i = 0; i <ipao.length -1; i++) {
    if (ipao[i].checked) ipac = 1; // See if it is checked
   }
   if (!ipac) where = 'ipa'; // Only the last one, None, is checked
 }

 // Next we see if any class has been checked or not
 if (where == undefined) {
   var found_one = 0; // None, yet
   for (var i = 0; ;i++) { // Look at each line
    if (! document.getElementById('i' + i)) break; // No class number: End
    if (document.getElementById('i' + i).checked) found_one = 1; // Got one!
   }
   if (!found_one) where = 'class';
 }

 // Are we in one of the bottom "submit" stages?
 if (where == undefined)
  if (document.getElementById('submit_1').className == 'showit')
   where = 'sub_1';

 if (where == undefined)
  if (document.getElementById('submit_2').className == 'showit')
   where = 'sub_2';

 if (where == undefined)
  if (document.getElementById('submit_3').className == 'showit')
   where = 'sub_3';

 // So, now we know what help is needed, let's get its message
 var a = 'you'; // Where we should position the scroll
 var m = '';  // The help message
 var f = ''; // The field to get focus

 switch (where) { // each "case" below checks against this variable
                  // until it finds a match
  case 'name':
   m = "We need your First Name, and Last Name to register you.";
   f = 'Ecom_ShipTo_Postal_Name_First';
  break;
  case 'address':
   m = "We need your Shell Point Address (Unit Number and Court)";
   m += " to register you.";
   f = 'Ecom_ShipTo_Postal_Street_Line1';
  break;
  case 'phone':
   m = "If we have a phone number,"
     + " when there is a change we can notify you faster.<br />"
     + "It is optional."
     + " <input type=button onclick='nophone=1;helpme();'"
     + " value='No Phone number to be listed' />";
   f = 'Ecom_ShipTo_Telecom_Phone_Number';
  break;
  case 'email':
   m = "If we have an email address,"
     + " we will send a confirmation of this registration.<br />"
     + "It is optional."
     + "<input type=button onclick='noemail=1;helpme();'"
     + "value='No Email to be listed' />";
   f = 'Ecom_ShipTo_Online_Email';
  break;
  case 'ipa':
   m = "If you are a Computer College volunteer as one of these,"
     + " there will be no charge! Consider volunteering!"
     + "<br /><input type=button onclick='noipa=1;helpme();'"
     + " value='Not a volunteer' />";
  break;
  case 'class':
   m = "To register you for a course(s), you must select a course(s).<br />"
     + "To do so,"
     + " click on the small box on the line with the course you want.<br />"
     + "It will then show a checkmark.<br /><br />"
     + "Clicking on a checkmark will erase it and NOT register you for it.<br />";
     + "You can see a description of any course by clicking on its title.";
   a = 'class_block';
  break;
  case 'sub_1':
   m = "OK! Now we will verify the information before sending.<br />"
     + 'Click the "Next"-button.';
   a = 'prior';
  break;
  case 'sub_2':
    m = 'Almost done.<br />'
      + 'If your information is correct, and these are the courses you'
      + ' want to regester for,<br />Click the "OK Send"-button.'
      + '<br />If you want to add other course(s),'
      + ' click the "Return for Changes"-button.'
   a = 'prior';
  break;
  case 'sub_3':
   m = 'All Done.<br />Unless there is an error message saying otherwise,'
     + ' you are registered.<br />'
     + 'If you gave an email address, confirmation has been sent to you.'
     + '<br />It is safe to close this web page now.';
   a = 'prior';
  break;
  default:
   alert('Unknown position: ' + where);
   return;
 }
 var hw = 'help_' + where;
 hdiv = document.getElementById(hw);
 m += "<br /><br />"
   + "<input type=button onclick='close_help(\"" + hw + "\")'"
   + " value='OK' />";
 hdiv.innerHTML = m;
 if (a !='') document.getElementById(a).scrollIntoView(true);
 if (f !='') document.forms[0].elements[f].focus();
}

function close_help(id) { // Close any open help block
 // by name
 if (id != undefined) document.getElementById(id).innerHTML = "";
 else {
  // all as a list, recursively

  if (document.getElementsByClassName) {
    var h_list = document.getElementsByClassName('help_disp');
  }
  else { // IE workaround hack
    var h_list = new Array();
    var j = 0;
    var i = 0;
    var a = document.getElementsByTagName("div");
    while (element = a[i++]) {
      if (element.className == "help_disp") {
       h_list[j++] = element;
        }
    }
  }
  for (var i = 0; i < h_list.length; i++) {
   close_help(h_list[i].id);
  }
 }
}

///////////////  helper Functions ///////////////////////

