// This function takes the obfuscated links the CAEDM obfuscation PHP
// function caedm_obfuscate() left behind, and fixes them so the user
// doesn't know they were messed up...
function caedm_obfuscation_clarify(){
 var anchors=document.getElementsByTagName("a"); // Get all the anchor tags.
 var number_of_anchors=anchors.length; // Find out how many there are.
 for(anchor=0;anchor<number_of_anchors;anchor++){ // Loop through them.
  if(anchors[anchor].className.indexOf('caedm-email')==0){ // We're only worried about those with the class 'caedm-email'.
   var full_url=anchors[anchor].getAttribute('href'); // Grab the href.
   var url_prefix='http://www.et.byu.edu/obfuscation/index.php?'; // This is what we expect the URL to start with.
   if(full_url.indexOf(url_prefix)!=0) return false; // If the URL's not what we're expecting, bail out.
   var get_request=full_url.substring(url_prefix.length); // Strip out the prefix, leaving just the request part.
   var get_parts=get_request.split('&'); // Split into individual variable/value pairs
   
   var address_get_prefix='address='; // Here's how the address get piece should start.
   var link_text_get_prefix='linktext='; // Here's how the link text get piece should start.

   var number_of_get_parts=get_parts.length; // Find the number of pieces to the get request (that is, the number of get variables that we were given)
   var obfuscated_address=''; // This will hold the obfuscated address at then end
   var obfuscated_link_text=''; // And this will hold the obfuscated link text, if any

   // Go through each of the get parts and get the appropriate values into the appropriate variables
   for(part=0;part<number_of_get_parts;part++){
    if(get_parts[part].indexOf(address_get_prefix)==0) obfuscated_address=get_parts[part].substring(address_get_prefix.length); // If it's the address, strip out the variable name and so on and get the value into obfuscated_address
    if(get_parts[part].indexOf(link_text_get_prefix)==0) obfuscated_link_text=get_parts[part].substring(link_text_get_prefix.length); // If it's the link text, strip out the variable name and so on and get the value into obfuscated_link_text
   }

   var address=''; // This will hold the actual, unobfuscated email address
   obfuscated_address_length=obfuscated_address.length; // Get the length of the obfuscated address
   // Loop through, two characters at a time, and decode the characters
   // (they're two digit hexadecimal ASCII codes, perfect for decoding via unescape).
   for(character_position=0;character_position<obfuscated_address_length;character_position+=2){
    address+=unescape("%"+obfuscated_address.substr(character_position,2)); 
   }

   var link_text=''; // Same thing for the linktext, if it exists.
   obfuscated_link_text_length=obfuscated_link_text.length;
   for(character_position=0;character_position<obfuscated_link_text_length;character_position+=2){
    link_text+=unescape("%"+obfuscated_link_text.substr(character_position,2));
   }

   anchors[anchor].setAttribute('href','mailto:'+address); // Then fix the href to be a mailto to the address
   if(link_text==''){ // And make link text content link_text (if it exists) or the address (otherwise)
    anchors[anchor].firstChild.data=address;
   }else{
    anchors[anchor].firstChild.data=link_text;
   }
  }
 }
}

// Nothing left to do but tell the page to run the script as soon as everything's loaded up!
window.onload=function(){caedm_obfuscation_clarify();}
