// Copyright David Gibson, 2007

// 09 dec 2006: added makeCallto and verifyHuman 
// 21 dec 2006: altered to avoid repeated messages when hovering over links
// 23 feb 2007: altered so that verification is on click, not on mouseover
//              added default_tld
// saved 4/9/08: This version includes window.alert('wrong!');
// saved 13/7/09: This version includes window.alert('Security code missing or wrong code entered');
// 30 Oct 2009: This version copied to all locations

// this file contains the functions

// function verifyHuman()
// function makeURL(user, domain, tld) 
// function makeURLwithSubject(user, subject, domain, tld) 
// function makeCallto(s2,s1,type) 
// function PDFwarning()
// function DOCwarning()    Used for forms
// function DOC2warning()   Used for general documents

// Usage: this file is to be copied to the html root, as follows.
// ONLY the following line should be edited to tailor the usage.

// default_domain = 'bcra';  default_tld = 'org.uk';
// use for bcra.org.uk, caves.org.uk, caves.org.uk/payments/creg, bcra.org.uk/creg/heyphone

// default_domain = 'caves';  default_tld = 'org.uk';
// use for mcrosolv and mcroslv/hawksmoor

// default_domain = 'british-caving';  default_tld = 'org.uk';
// use for caves.org.uk/payments/payments/bca

   default_domain = 'hidden-earth';  default_tld = 'org.uk';
// use for hidden-earth.org.uk

// default_domain = 'mrsl-research';  default_tld = 'com';
// use for mrsl-research.com, plus adema, adris, docs, emtech, mintos, nemaeq, rainow


failed_url = 'http://caves.org.uk/mailto_failed.html';
js_url = 'javascript:verifyHuman()';

var userprompt = '', user_response='';
var today = new Date();
var security = (Math.floor(today.getTime()/1000) - 8E8) % 1E6;
var tries = 0;
var tries_limit = 2; // need 2 tries because of popup window alert in MSIE 7


/* Originally, ONMOUSEOVER called makeURL, which did a security check using 
   verifyHuman. However, this is annoying for the user, and it would be better
   for the security check to be done by ONCLICK.  To achieve that without
   needing to re-write each <A> tag requires ONMOUSEOVER to set a Javascript URL
*/ 


function verifyHuman()
    {
      // window.alert('tries='+tries);
      if (user_response==security)
        {
        // do nothing if the user has already entered the security code
        }
      else
        {
          if (tries++ < tries_limit)   
            {
              s = 'To use the email or phone links on this page,  \n' +
              'you must enter a security code at the next prompt.\n' +
              'The purpose is to attempt to verify that you are \n' +
              'a human and not a spam-bot.' ;
              window.alert(s);

              s = 'To use the email links on this page, please enter the security code ' + security ;  
              user_response = window.prompt(s,'');
              
              if (user_response==security)  // added 4-sep-8
              {
                s = 'The email and phone links on this page have \n' +
                'now been enabled and should function when you \n' +
                'click on them.';
                window.alert(s);
              }
              else
              {
                window.alert('Security code missing or wrong code entered');
              }
            }
          if (tries >= tries_limit)  
             // if user has now been given two tries, force a failure.  This happens even if the
             // second try was correct, but in this case, the js_url is never used because the 
             // test in makeURL of (user_response != security) is false.
            {
              js_url = failed_url;
            }
        }
      // return (user_response==security); 
      // originally, return a true/false flag here. Now, nothing must be returned
      // or else the javascript URL writes the value to a fresh document.
    }        


function makeURL(user, domain, tld) 
    {
      // if (!verifyHuman()) return (failed_url);  
      // verifyHuman no longer returns a value, so the above line is replaced by the following,
      // in which js_url is EITHER the javascript URL or the address of the 'failed' message page.
      if (user_response != security) return (js_url);
      
      if (domain == null) domain = default_domain ;
      if (tld == null)  tld = default_tld ;
      return ("mai" + "lto:" + user + "@" + domain + "." + tld) ;
    }

function makeURLwithSubject(user, subject, domain, tld) 

   {
      // if (!verifyHuman()) return (failed_url);
      if (user_response != security) return (js_url);
      if (subject == null) subject = "no subject"
      if (domain == null) domain = default_domain ;
      if (tld == null)  tld = default_tld ;
      return ("mai" + "lto:" + user + "@" + domain + "." + tld + "?subject=" + subject) ;
    }

function makeCallto(s2,s1,type) 
    {
      // if (!verifyHuman()) return (failed_url);
      if (user_response != security) return (js_url);
      if (type==null)  type = "";
      if (type=='fax') type = ";type=fax";
      return ("callto:" + s1 + "-" + s2 + type) ;
    }


function PDFwarning()
   {
   return window.confirm(
   'Warning: Some versions of Adobe Acrobat will not \n' +
   'run correctly within MS Internet Explorer (a feature \n' +
   'known as \'in-place activation\'). If you have problems \n' +
   'you should try saving the PDF instead of opening it.\n\n' +

   'If you continue, your browser will try to open the \n' +
   'document using \'in-place activation\'. If you wish to \n' +
   'SAVE it to disc instead of OPENing it, click cancel, \n' +
   'and then right-click the link.' );
   }

function DOCwarning()
   {
   return window.confirm(
   'A Microsoft Word document is useful if you want to type \n' +
   'your responses directly into the document, but please be \n' +
   'aware that Word documents with advanced formatting \n' +
   'do not easily transport from one computer to another. \n' +
   'You may therefore experience layout problems with this \n' +
   'document which you may need to deal with.\n\n' +

   'If you continue, your browser will try to open the Word \n' +
   'document using \'in-place activation\'. If you wish to SAVE \n' +
   'it to disc instead of OPENing it, click cancel, and then \n' +
   'right-click the link.' );
   }

function DOC2warning()
   {
   return window.confirm(
   'Please note that Microsoft Word documents with advanced \n' +
   'formatting do not easily transport from one computer to \n' +
   'another. You may therefore experience layout problems \n' +
   'with this document and we advise that you download the \n' +
   'PDF instead.\n\n' +

   'If you continue, your browser will try to open the Word \n' +
   'document using \'in-place activation\'. If you wish to SAVE \n' +
   'it to disc instead of OPENing it, click cancel, and then \n' +
   'right-click the link.' );
   }