/*  emailConverterIU.js  Stored in _IUN-Common/files/javascript

A function that runs after page load looking for double square brackets with contents like [[wmuserid,text]] 
and turns the contents into a mailto link.

------------------------------------

uses 2 regexs:
1) if there is only a userid in the brackets use the local site's default email address as the link text
2) if there are 2 things in the brackets use the second thing as the link text 
example: [[userid]] --> userid@email.edu
		 [[userid, User Id]] --> User ID

-------------------------------------

To allow sites to 'protect' multiple email address formats the following can be prepended to the userid:
n|  ..... (converts email format userid@iun.edu)
p| ..... (converts email format userid@iupui.edu)
s| ..... (converts email format userid@ius.edu)
k| ..... (converts email format userid@iuk.edu) 
b| ..... (converts email format userid@iusb.edu) 
i| ..... (converts email format userid@indiana.edu)

These work in all sites. Note that if there is no letter followed by pipe in front of the userid, the site's default email address format will be used. 

------------------------------------

TODO: If possible use xslt and regex to find double closed square brackets with stuff in it [[...]] and append after
it a no script tag like:
<noscript>&nbsp;(email this user id at wm.edu)&nbsp;</noscript>

------------------------------------

Requirements:
- jquery is used for the document.ready function only but is necessary to avoid the pageload function.

-----------------------------------

*/

$(document).ready(function(){
    //check browser for javascript    
    if (!document.getElementsByTagName) return false;
    if (!document.createElement) return false;

    page = document.body.innerHTML;

    // CASE: n| IUN (Northwest)
    page = page.replace(/\[\[[\s*[nN]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@iun.edu\">$1@iun.edu</a>");
    page = page.replace(/\[\[\s*[nN]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@iun.edu\">$2</a>");

    // CASE: p| IUPUI (Indianapolis)
    page = page.replace(/\[\[\s*[pP]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@iupui.edu?Subject="+[document.title]+"\">$1@iupui.edu</a>");
    page = page.replace(/\[\[\s*[pP]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@iupui.edu\">$2</a>");
    
    // CASE: s| IUS (Southeast)
    page = page.replace(/\[\[\s*[sS]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@ius.edu\">$1@ius.edu</a>");
    page = page.replace(/\[\[\s*[sS]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@ius.edu\">$2</a>");

    // CASE: k| IUK (Kokomo)
    page = page.replace(/\[\[\s*[kK]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@iuk.edu\">$1@iuk.edu</a>");
    page = page.replace(/\[\[\s*[kK]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@iuk.edu\">$2</a>");

    // CASE: b| IUSB (South Bend)
    page = page.replace(/\[\[\s*[bB]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@iusb.edu\">$1@iusb.edu</a>");
    page = page.replace(/\[\[\s*[bB]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@iusb.edu\">$2</a>");

    // CASE: i| IUB (Bloomington)
    page = page.replace(/\[\[\s*[iI]\|\s*([a-zA-Z0-9_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@indiana.edu\">$1@indiana.edu</a>");
    page = page.replace(/\[\[\s*[iI]\|\s*([a-zA-Z0-9_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@indiana.edu\">$2</a>");

    // CASE: site default. (DIFFERENT IN EACH SITE)
    page = page.replace(/\[\[\s*([a-zA-Z0-9\_\.-]+)\s*\,?\s*\]\]/g, "<a href=\"mailto:$1@iun.edu?\">$1@iun.edu</a>");
    page = page.replace(/\[\[\s*([a-zA-Z0-9\_\.-]+)\s*\,{1}\s*([^\]]*)\s*\]\]/g, "<a href=\"mailto:$1@iun.edu?\">$2</a>");

    document.body.innerHTML = page;
    
});
