// index.js
//------------------------------
// Called by index.html
//------------------------------

/***********************************
 * Global variables                * 
 * (Set in external.js)            * 
 *                                 *    
 * Used in addfav(), mailThisUrl() *
 *   homepage                      *
 * Used in addfav()                * 
 *   is_ie, hometitle              *
 * Used in testNews()              *
 *   news_date                     *
 ***********************************/

function greet() {
  var todaydate = new Date();
  todaydate.setTime(todaydate.getTime());

  var houris = todaydate.getHours();
  var greet_welcgif = (houris >= 17) ? "evening.gif"
                    : (houris >= 12) ? "afternoon.gif"
                    : "morning.gif";
 
  document.getElementById('welc').src='images/display/welc_anim.gif';
  document.getElementById('greet').src='images/display/' + greet_welcgif;
} //------------------------------

function auPreload(fname) {
  if (!fname)
    { fname = "audio/minuet.mid"; }
  document.getElementById("aupreload").innerHTML = '<embed src="' + fname + '" loop=false autostart=false hidden>';
} //------------------------------

function testNews() {
  // Global variable: news_date
  
  document.getElementById("newsavail").innerHTML =
          (Math.ceil((new Date() - news_date) / (1000*60*60*24)) > 7)
  	? '<b>No news available</b>'
  	: '<b>News available</b>';
} //------------------------------

// ***** CURRENTLY UNUSED 11/01/2009 ****
function loadCalendar() {
  var now = new Date();
  var year = now.getYear().toString().substr(2);
  var monthno = now.getMonth() + 1;
  monthno = (monthno < 10) ? '0' + monthno : monthno;

  location.href = 'calendar' + year + monthno + '.html';
} //------------------------------

function addfav() {
  // Global variables: hometitle, homepage  
  var is_ie = (navigator.appVersion.indexOf('MSIE') != -1);
  var appver, is_minor, is_major, text;
  
  if (is_ie) {
    appver = navigator.appVersion;
    is_minor = parseFloat(appver.substring(appver.indexOf('MSIE') + 5,
                                           appver.indexOf(';',appver.indexOf('MSIE')) ));
    is_major = parseInt(is_minor,10); 
  }

  if (is_ie && is_major >= '4') 
    { window.external.AddFavorite(homepage,hometitle); }       
  else 
    { alert("To add our site to your favorites - Press (Ctrl+D)"); }  
} //------------------------------

function playSound(fname) {
  if (!fname)
    { fname = "audio/minuet.mid"; }
  var x = document.getElementById("sound");
  x.innerHTML = (!x.innerHTML ) ? '<embed src="' + fname + '" loop=false autostart=true hidden>' : '';
} //------------------------------

function loadIframe(id,sPath) {
  var x = document.getElementById(id);

  if (x.style.display != 'block') {
    x.style.display = 'block';
    x.src = sPath;
  }
  else
    { x.style.display = 'none'; }
} //------------------------------

function checkEmailAddress(field) {
  // the following expression in () after 'match' must be all on one line...
  // suffixes not added: .cat .mobi .nato
  if (field.value.match(/(^(\S+@)\S+(((\.ac)|(\.aero)|(\.arpa)|(\.biz)|(\.co)|(\.com)|(\.coop)|(\.edu)|(\.firm)|(\.gov)|(\.info)|(\.int)|(\.jobs)|(\.mil)|(\.museum)|(\.name)|(\.nom)|(\.net)|(\.org)|(\.pro)|(\.store)|(\.travel)|(\.web))(\.\S{2})?)$)/i))
    { return true; }
  else {  
    if (confirm('Are you sure that this address is correct?\n'
                + 'OK for Yes.\n'
                + 'Cancel to retry.'))
      { return true; }    
    else { 
      field.focus();
      field.select();
      return false; 
    }     
  } // end if(field.value.match ....
} //------------------------------

function mailThisUrl() {
  var addr = document.eMailer.address;
  var subj = "I thought this might interest you...";
  var text = "Here is an interesting Website: ";

  if (checkEmailAddress(addr))
    { window.location = 
            "mailto:" + addr.value
  	  + "?subject=" + subj
	  + "&body="    + text + "%0d%0a"
	  + this.document.title + "%0d%0a"
	  + "(" + this.location.href + ")"; }
} //------------------------------