/*
** wk.js - WK Javascript Library
** jgriffith
**
*/

(function($){

var WK = {
  
   ondomready: function() {
        WK.share();
        WK.links();
        if (window.analytics) {analytics.save()}
   },

   share: function(){
        if(document.getElementById("expand")){ // look for expand on the page
           var item = document.getElementById("menu");
           item.className = "hide"; // hide this by default
           document.getElementById("expand").onclick = WK.toggle; // if it gets clicked, show or hide 
        } 
   },

   toggle: function(){
        var menu = document.getElementById("menu");
        if(menu.className == "show") {
            menu.className = "hide";
        } else{
            menu.className = "show";
        } 
        return false;
   },
   
   
   links: function() {
       var links = document.getElementsByTagName('a');
       for (var x = 0;x < links.length; x++) {
           a = links[x];
   
           var pdf = false;
           if (a.href.search(/\.pdf$/) > -1) {pdf = true;}
           if (pdf) {
               if (a.className.indexOf('pdf') == -1) { a.className += ' pdf'; }
               a.onclick = pdf_popup;
           }
           

           if (a.className.indexOf('popup') > -1) { a.onclick = std_popup;}
          
           else if (a.className.indexOf('newSameWindow') > -1) { a.onclick = newSameWindow_popup; }
           else if (a.className.indexOf('new_window') > -1) { a.onclick = std_newwindow; }
   
           if (a.className.indexOf('external') > -1) { a.className += ' offsite'; }
           if (a.className.indexOf('pdf') > -1) { a.className += ' offsite'; }
           if (a.href.indexOf('http://www.hbs.edu') > -1 && a.className.indexOf('offsite') == -1) { a.className += ' offsite'; }
           if (a.href.indexOf('hbsp.') > -1 && a.className.indexOf('offsite') == -1) { a.className += ' offsite'; }
           
       }
   }
   
}

window.WK = WK;

$(document).ready(function(){WK.ondomready();});

})(jQuery);




/*
 *
 *  OLD CODE THAT WE SHOULD EVENTUALLY MOVE INTO THE ABOVE SECTION
 *
 */
 

function trace() {
}

function rewrite_emails(form) {
        if (form.e && form.e.value == 'hbswk') {
                form.e.value = form.e.value + '@hbs.edu';
        }
}

function stage_form(form){
    if (document.location.href.indexOf('webdev') > -1 || document.location.href.indexOf('webstage') > -1)
    {

        if (form.aTemplate) {
            form.aTemplate.value = form.aTemplate.value.replace(/http:\/\/hbswk\.hbs\.edu/,"http://webstage.hbswk.hbs.edu");
        }
        if (form.rTemplate) {
            form.rTemplate.value = form.rTemplate.value.replace(/http:\/\/hbswk\.hbs\.edu/,"http://webstage.hbswk.hbs.edu");
        }
        if (form.u) {
            form.u.value = form.u.value.replace(/http:\/\/hbswk\.hbs\.edu/,"http://webstage.hbswk.hbs.edu");
        }
    }
}

function ap_stopAll(num) {
    // noop
}


function checkEmail( theForm ) {
    /*    validate the email  */
    var emailId = theForm.email.value.toLowerCase();
    var cont = true;

    if ( emailId == null ||
         emailId.indexOf('@') <= 0 ||
         emailId.length < 5)
    {
        alert('Please enter a valid email address in the form X@X.X');
        cont = false;
    }
    return cont;
}

function checkCurrentEmail( theForm ) {
    /*    validate the email  */
    var emailId = theForm.CurrentEmail.value.toLowerCase();
    var cont = true;

    if ( emailId == null ||
         emailId.indexOf('@') <= 0 ||
         emailId.length < 5)
    {
        alert('Please enter a valid email address in the form X@X.X');
        cont = false;
    }
    return cont;
}


function clean_comments(form) {
    form["comment-x5"].value = clean_chars(form["comment-x5"].value);
}

function clean_chars(text) {
    var replacements = {
        "\xa0": " ",
        "\xa9": "(c)",
        "\xae": "(r)",
        "\xb7": "*",
        "\u2018": "'",
        "\u2019": "'",
        "\u201c": '"',
        "\u201d": '"',
        "\u2026": "...",
        "\u2002": " ",
        "\u2003": " ",
        "\u2009": " ",
        "\u2013": "-",
        "\u2014": "--",
        "\u2122": "(tm)"};
        for (key in replacements) {
          var rx = new RegExp(key, 'g');
          text = text.replace(rx,replacements[key])
        }
    return text
}




// finds a good spot on the screen to position the new window
function windowpos() {
    var x = window.screenX + 10 + Math.floor(Math.random()*41);
    var y = window.screenY + 10 + Math.floor(Math.random()*41);
    var pos = ",screenX="+x+",screenY="+y+",";
    return pos;
}

/// used for new window
function new_window(url) {
  nw = window.open(url, "newwindow"+Date().replace(/[^0-9]/g,''), 'width=800,height=650,directories=yes,toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes'+windowpos());
  nw.focus();
  return false;
}

/// used for pop ups
function popup(url) {
  win = window.open(url,'popupwindow', 'width=475,height=500,resizable=yes,menubar=yes,location=yes,toolbar=yes,scrollbars=yes'+windowpos());
  win.focus()
  return false;
}

/// used for pdf documents
function new_window(url) {
  win = window.open(url,'popupwindow', 'width=800,height=600,resizable=yes,menubar=yes,location=yes,toolbar=yes,scrollbars=yes'+windowpos());
  win.focus()
  return false;
}

/// used for same new window pop ups
function newSameWindow(url) {
  win = window.open(url, 'popupwindow', 'width=800,height=500,resizable=yes,location=yes,toolbar=yes,menubar=yes,scrollbars=yes'+windowpos());
  win.focus()
  return false;
}

function pdf_popup() {
    //register_click_a(this);
    return new_window(this.href);
}

function std_popup() {
    return popup(this.href);
}

function std_newwindow() {
    return new_window(this.href);
}

function newSameWindow_popup() {
    return newSameWindow(this.href);
}
