/*----------------------------------------------------------------
Project     : NSA Website
Program     : nsa.js
Purpose     : Provide safe email addresses and decryption
Last Update : 12 Jan 2012
------------------------------------------------------------------*/
function get_address(p_cName)
// Build an email address
// (Internal function for this script)
{
  var aDef = new Array()
  aDef[0] = ".org.uk";
  aDef[1] = "nsa"
  aDef[2] =  "@";
  aDef[3] = "cr";

  return p_cName + aDef[2] + aDef[1] + aDef[3] + aDef[0];
}
//------------------------------------------------------------------
function get_committee()
// Build email addresses for committee
// (Internal function for this script)
{
  var aCommittee = new Array();
  var cAddress = '';
  var i;

  // Set up committee names
  aCommittee[0]= "Chairman";
  aCommittee[1]= "Secretary";
  aCommittee[2]= "Treasurer";
  aCommittee[3]= "RingingMaster";
  aCommittee[4]= "DeputyRingingMaster";
  aCommittee[5]= "BellAdviser";
  aCommittee[6]= "PealSecretary";
  aCommittee[7]= "Education";
  aCommittee[8]= "PublicRelations";
  aCommittee[9]= "CCRep1";
  aCommittee[10]= "CCRep2";
  aCommittee[11]= "CCRep3";
  aCommittee[12]= "CM1";
  aCommittee[13]= "CM2";
  aCommittee[14]= "CM3";
  aCommittee[15]= "CM4";

  // Build list of addresses
  cAddress = get_address(aCommittee[0]);
  for(i=1; i<aCommittee.length; i++)
    {cAddress += ',' + get_address(aCommittee[i]);}

  return cAddress;
}
//------------------------------------------------------------------
function nsa_mail(p_cName, p_cSubject, p_cDisplay)
// Write an email link
{
  var cAddr = '';
  var cSubject = '';

  // Check if committee
  if(p_cName.toUpperCase() == 'COMMITTEE')
  {
    // Set up addresses for committee members
    if(p_cDisplay == '') {p_cDisplay = 'E-mail all committee members';}
    cAddr = 'mailto:' + get_committee();
  }else
  {
    // Set up address for an individual
    if(p_cDisplay == '') {p_cDisplay = get_address(p_cName);}
    cAddr = 'mailto:' + get_address(p_cName);
  }//endif

  // Set up the subject
  if(p_cSubject != '') {cSubject = '?subject=' + p_cSubject;}

  // Write link
  document.write('<a href="' + cAddr + cSubject + '" title="Click here to send e-mail">' + p_cDisplay + '</a>');
}
//------------------------------------------------------------------
function nsa_bend(p_cText,p_cType)
// Decrypt a string with option to use as email link
{
  var nLen = 0;
  var x = 0;
  var nSeed = 0;
  var nOffset = 0;
  var nChr = 0;
  var cOut = '';

  // Decode
  nLen = p_cText.length;
  for(x=0; x<nLen; x=x+2){
    // Get next character
    nChr = Number(p_cText.substr(x,2));
    // Check if 1st chracter of 5 chr group
    if(x%5 == 0){
      // Use every 5th character as a new seed and reset offset
      nSeed = nChr;
      nOffset = 5;
    }else{
      // Decrypt current character
      nChr = nChr - nSeed - nOffset;
      if(nChr < 1){nChr += 96;}
      cOut += String.fromCharCode(nChr + 31);
      nOffset += 4;
    }//endif
  }//endfor

  // Write link
  if(p_cType == 'E'){
    document.write('<a href="mailto:' + cOut + '?subject=From NSA Web Site" title="Click here to send e-mail">'
                   + cOut + '</a>');
  }else{
    document.write(cOut);
  }//endif
}
//------------------------------------------------------------------

