/*
###############################################################################
#
# File:     shared/js/shared.js  (content and wf)
#
# Contents: Javascript functions used by multiple areas (staff, skillnet, etc.).
#
# Linked from:
#   - most of new pages
# 
# Modifications:
#   - 2003-11-12  txe  Initial creation.
#   - 2004-01-22  txe  Added getElementWithID, setDisplayOfElementWithID.
#   - 2004-02-09  txe  Added refreshOpener().
#   - 2004-02-20  txe  Added useHugeWindow().
#   - 2004-02-25  txe  Added printWindow().
#   - 2004-10-21  txe  Added useFullWindow().
#   - 2004-10-26  txe  Added setRegion().
#   - 2004-12-17  txe  useFullWindow() tries to open with opener.
#   - 2005-02-01  txe  Added openWindow() for old pages.
#   - 2005-02-03  txe  Set resizable to YES.
#   - 2005-02-28  txe  Added currentHostIsBackEnd(), fixed size to useFullWindow().
#   - 2005-03-01  txe  Added Report Slowness link logic (not auto-execute yet).
#   - 2005-03-23  txe  Simplified addReportSlownessLink, uses window.onload.
#   - 2005-04-11  txe  Added usePopup*().
#   - 2007-01-30  txe  Added setPopupXY(), getPopup*().
#
###############################################################################
*/

// always execute //
var _popup = new Object();
//setPopupXY(10, 10, 1, 0);


//////////////////////////////////////////////////////////////////////////////// 
// FUNCTIONS
//////////////////////////////////////////////////////////////////////////////// 

function printWindow()
{
  if (window.print)
  {
    window.print();
  }
  else
  {
    alert("Select File and then Print to print");
  }
}

//////////////////////////////////////////////////////////////////////////////// 
// POPUP WINDOWS
//////////////////////////////////////////////////////////////////////////////// 

function getFirstOpener()
{
  var first = this;
  while (first.opener)
  {
    first = first.opener;
  }
  return first;
}

//////////////////////////////////////////////////////////////////////////////// 
// POPUP WINDOWS
//////////////////////////////////////////////////////////////////////////////// 


function setPopupXY(x,y,flipX,flipY) { return setPopupX(x,flipX) && setPopupY(y,flipY); }
function setPopupX(x,flipX)          { _popup.x = x; _popup.flipX = flipX; return true; }
function setPopupY(y,flipY)          { _popup.y = y; _popup.flipY = flipY; return true; }
function getPopupXForWidth(w)        { w += 8;   return (screen.width  ? (_popup.x >= 0 ? (_popup.flipX ? (screen.width  - w - _popup.x) : _popup.x) : ((screen.width  - w) / 2)) : 0); }
function getPopupYForHeight(h)       { h += 105; return (screen.height ? (_popup.y >= 0 ? (_popup.flipY ? (screen.height - h - _popup.y) : _popup.y) : ((screen.height - h) / 2)) : 0); }

//////////////////////////////////////////////////////////////////////////////// 

function getPopupDate()
{
  var date  = new Date();
  var year  = date.getFullYear();
  var month = date.getMonth() + 1;
  var day   = date.getDate();
  var yyyy  = year.toString();
  var mm    = (month < 10 ? "0" : "") + month;
  var dd    = (day   < 10 ? "0" : "") + day;
  return yyyy + mm + dd;
}

//////////////////////////////////////////////////////////////////////////////// 

function getPopupVersionOfUrl(wrappedUrl)                  { return wrappedUrl + (wrappedUrl.indexOf("?") >= 0 ? "&" : "?") + "popup=" + getPopupDate(); }
function useSmallPopup(linkElement,  name, w, h, settings) { return useSmallWindow(getPopupVersionOfUrl(linkElement.href),  name, w, h, settings); }
function useMediumPopup(linkElement, name, w, h, settings) { return useMediumWindow(getPopupVersionOfUrl(linkElement.href), name, w, h, settings); }
function useLargePopup(linkElement,  name, w, h, settings) { return useLargeWindow(getPopupVersionOfUrl(linkElement.href),  name, w, h, settings); }
function useHugePopup(linkElement,   name, w, h, settings) { return useHugeWindow(getPopupVersionOfUrl(linkElement.href),   name, w, h, settings); }
function useSmallWindow(url,         name, w, h, settings) { return useWindow(url, name, (w ? w : 400), (h ? h : 300), settings); }
function useMediumWindow(url,        name, w, h, settings) { return useWindow(url, name, (w ? w : 500), (h ? h : 400), settings); }
function useLargeWindow(url,         name, w, h, settings) { return useWindow(url, name, (w ? w : 600), (h ? h : 500), settings); }
function useHugeWindow(url,          name, w, h, settings) { return useWindow(url, name, (w ? w : 800), (h ? h : 700), settings); }

//////////////////////////////////////////////////////////////////////////////// 

function useFullWindow(url, name)
{
  var isBackEnd = currentHostIsBackEnd();
  var width     = 800;
  var height    = (isBackEnd ? 800 : 600);
  return useWindow(url, name, width, height, "directories=1,location=1,menubar=1,resizable=1,scrollbars=1,status=1,toolbar=1");
}              

//////////////////////////////////////////////////////////////////////////////// 

function useWindow(url, name, w, h, settings)
{
  name         = (name     ? name     : "_blank");
  settings     = (settings ? settings : "scrollbars=yes,resizable=yes");
//  var x        = (screen.width  ? ((screen.width  - w) / 2) : 0);
//  var y        = (screen.height ? ((screen.height - h) / 2) : 0);
  var x        = getPopupXForWidth(w);
  var y        = getPopupYForHeight(h);
  var features = "height=" + h + ",width=" + w + ",left=" + x + ",top=" + y + "," + settings;
  var win      = window.open(url, name, features);
  win.focus();
  return false;
}

//////////////////////////////////////////////////////////////////////////////// 



// DEPRECATED, but needed by some of the older pages
function openWindow(url, name, w, h, settings)
{
  w = (w ? w : 520);
  h = (h ? h : 500);
  useWindow(url, name, w, h, settings);
}

//////////////////////////////////////////////////////////////////////////////// 

function refreshOpener()
{
  if (opener && opener.document && opener.document.location)
  {
    opener.document.location = opener.document.location + "";
  }          
}


//////////////////////////////////////////////////////////////////////////////// 
// FORM FIELDS
//////////////////////////////////////////////////////////////////////////////// 

// add robustness later //

function getFieldValue(name)
{
  var field = getField(name);
  return field.value;
}      

function setFieldValue(name, value)
{
  var field = getField(name);
  field.value = value;
  return value;
}

function getField(name) { return eval("document.forms[0]." + name); }


//////////////////////////////////////////////////////////////////////////////// 
// ELEMENTS
//////////////////////////////////////////////////////////////////////////////// 

function getElementWithID(id)
{
  var element = null;
  if (document.getElementById)
  {
    element = document.getElementById(id);
  }
  else
  {
    alert("getElementByID() not supported");
  }
  return element;
}

//////////////////////////////////////////////////////////////////////////////// 

function setDisplayOfElementWithID(id,display)
{
  var element = getElementWithID(id);
  var style   = (element && element.style ? element.style : element);
  if (style)
  {
    style.display = display;
  }
}

////////////////////////////////////////////////////////////////////////////////

function showElementWithID(id) { setDisplayOfElementWithID(id,"block"); } // make more robust later //
function hideElementWithID(id) { setDisplayOfElementWithID(id,"none");  } // make more robust later //


////////////////////////////////////////////////////////////////////////////////
// REPORT SLOWNESS LINK
////////////////////////////////////////////////////////////////////////////////

function createReportSlownessLinkHtml()
{
  var emailTarget  = "travis@careerperfect.com,admin@careerperfect.com";
  var emailSubject = escape("Reporting Slowness in: " + document.location.href);
  var emailBody    = escape("The following url was slow:\n\n  " + document.location.href + "\n\n[add any other comments here]\n\n");
  var linkUrl      = "mailto:" + emailTarget + "?subject=" + emailSubject + "&body=" + emailBody;
  return "\n<div id='cpReportSlowness'><a href='" + linkUrl + "'>Report Slowness</a></div>\n";
}

//////////////////////////////////////////////////////////////////////////////// 

function addReportSlownessLinkToBody() { document.body.innerHTML = createReportSlownessLinkHtml() + document.body.innerHTML; }
function addReportSlownessLink()       { window.onload = addReportSlownessLinkToBody; }

//////////////////////////////////////////////////////////////////////////////// 
// always executes
// if (currentHostIsBackEnd()) { window.onload = addReportSlownessLink; }


////////////////////////////////////////////////////////////////////////////////
// MISC
////////////////////////////////////////////////////////////////////////////////

function setRegion(region,partnerid)
{
  setFieldValue("channelid", region);
  setFieldValue("partnerid", partnerid);
}

////////////////////////////////////////////////////////////////////////////////

function currentHostIsBackEnd()
{
  var currentUrl = document.location.href;
  return (currentUrl.indexOf("admin.careerperfect.com")   >= 0) 
      || (currentUrl.indexOf("writers.careerperfect.com") >= 0);
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
