/*
###############################################################################
#
# File:     content/js/content.js
#
# Contents: Javascript used by front-end pages.
#
# Used by:
#   - content/htm/*.htm
#
# Requires:
#   - shared/js/shared.js
#
# Notes:
#   - need to update openPollsPage() after refactoring.
#
# Modifications:
#   - 2005-07-12  txe  Initial creation (copied from client/js).
#   - 2005-09-21  txe  Finished initial creation.
#   - 2006-11-28  txe  Added hostPrefix support to redirectToMaster().
#   - 2007-01-30  txe  Moved popups to upper right.
#   - 2007-02-19  txe  Moved popups further away from upper right.
#   - 2007-12-31  txe  Updated getPopupVersionOfUrl().
#   - 2008-05-06  txe  Added forcePopupWithSize().
#   - 2009-04-23  txe  Added nudge functions.
#   - 2009-04-29  txe  Changed nudge delay to 60 seconds.
#
###############################################################################
*/

//always execute //

setPopupXY(100, 100, 1, 0); // upper right //


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

// 2008-05-06 txe start //
function forcePopupWithSize(size)
{
	var temp  = new Object(); 
	temp.href = document.location.href.replace(/forcePopup\=[a-zA-Z0-9]+/, ""); 
	if (temp.href != document.location.href)
	{				
	  size = (size == "1" ? "Large" : size);
		eval("use" + size.charAt(0).toUpperCase() + size.substr(1,size.length-1) + "Popup(temp);");
	}
}
// 2008-05-06 txe end //

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

// overrides -- move to shared.js when trusted //

function getPopupVersionOfUrl(wrappedElementOrUrl)
{
  var wrappedUrl    = (wrappedElementOrUrl.href ? wrappedElementOrUrl.href : wrappedElementOrUrl);
  var bookmarkIndex = wrappedUrl.indexOf("#");
	var before        = (bookmarkIndex >= 0 ? wrappedUrl.substring(0, bookmarkIndex) : wrappedUrl);
	var after         = (bookmarkIndex >= 0 ? wrappedUrl.substring(bookmarkIndex)    : "");
  return before + (wrappedUrl.indexOf("?") >= 0 ? "&" : "?") + "popup=" + getPopupDate() + after;
}

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

function useSmallPopup(linkElement,  name, w, h, settings) { return useSmallWindow(getPopupVersionOfUrl(linkElement),  name, w, h, settings); }
function useMediumPopup(linkElement, name, w, h, settings) { return useMediumWindow(getPopupVersionOfUrl(linkElement), name, w, h, settings); }
function useLargePopup(linkElement,  name, w, h, settings) { return useLargeWindow(getPopupVersionOfUrl(linkElement),  name, w, h, settings); }
function useHugePopup(linkElement,   name, w, h, settings) { return useHugeWindow(getPopupVersionOfUrl(linkElement),   name, w, h, settings); }
function useFullPopup(linkElement,   name, w, h, settings) { return useFullWindow(getPopupVersionOfUrl(linkElement),   name, w, h, settings); }

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

function request(name,defaultValue)
{
	var value = "";
	var queryParts = location.search.replace("?","").split("&");
	for (var i = 0; i < queryParts.length; i++)
	{
		var subparts = queryParts[i].split("=");
		value        = (subparts[0] == name ? subparts[1] : value);
	}
	return (value ? value : defaultValue);
}

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

function getHost()          { return request("host", location.hostname); }
function getHostParts()     { return getHost().split("."); }
function getHostPart(index) { return getHostParts()[index-1]; }
function getNumHostParts()  { return getHostParts().length; }
function isHostPrefixed()   { return (getNumHostParts() >= 4); }
function getHostPrefix()    { return (isHostPrefixed() ? getHostPart(1) + "." : ""); }

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

function redirectToMaster()
{
  var masterHost = getHostPrefix() + "www.careerperfect.com";
  var masterUrl  = location.href.replace(location.hostname, masterHost);
  if (masterUrl != location.href)
  {
    document.location = masterUrl;
  }
}

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

function appendCurrentRequestParametersToAllLinks()
{
  for (i = 0; i < document.links.length; i++)
  {
    var linkURL   = document.links[i].href;
    var linkQuery = document.links[i].search;
    var thisQuery = location.search;
    if (linkURL != null)
    {
      var linkHasParams = (linkQuery != "") && (linkQuery.indexOf("http://") == -1);  // why the 2nd test? //
      var linkHasParens = (linkURL   != "") && (linkURL.indexOf(")")   != -1);
      var suffix        = (linkHasParams ? "&" : "?") + thisQuery.substring(1, thisQuery.length);
      suffix            = (suffix == "?" ? "" : suffix);
      if (linkHasParens)
      {
        linkURL = linkURL.replace(/\'\)/, suffix + "')");   // note: this should be expanded later to handle //
        linkURL = linkURL.replace(/\"\)/, suffix + "\")");  //       javascript calls with multiple parameters //
      }
      else
      {
        linkURL += suffix;
      }
      document.links[i].href = linkURL;
    }
  }
}

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

function openResults()   { var surveypopup = window.open('',"surveyResults","width=350,height=300,resizable=yes"); }
function openPollsPage() { var pollWindow  = window.open('/CareerPerfect/polls.htm','pollWin','resizable=yes,toolbar=yes,location=yes,scrollbars=yes,personalbar=yes,statusbar=yes,menubar=yes'); }
function openWindow(url) { var popup       = window.open(url,'newWindow','width=520,height=450,resizable=yes,scrollbars=yes'); }

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

function forgotYourPin()
{
  alert("Forgot your PIN? \n\nEnter your e-mail address and click the Go button. \nWe will e-mail your PIN to you.");
  return false;
}


////////////////////////////////////////////////////////////////////////////////
// NUDGE
////////////////////////////////////////////////////////////////////////////////

function getNudgeDelaySeconds() { return 60; }
function showNudge()            { document.getElementById("cpNudge").className = "cpVisible"; }
function hideNudge()            { document.getElementById("cpNudge").className = "cpHidden"; }
function startNudgeTimer() 
{ 
  var numSeconds = getNudgeDelaySeconds();
  if (numSeconds >= 0)
  {
    setTimeout("showNudge()", numSeconds * 1000);
  }
}




////////////////////////////////////////////////////////////////////////////////
// MACROMEDIA FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ, selObj, restore, skipAppend)
{
  var jumpUrl            = selObj.options[selObj.selectedIndex].value;
  var appendCurrentQuery = !skipAppend;
  if (appendCurrentQuery)
  {
    var query  = location.search;
    var suffix = (jumpUrl.indexOf("?") == -1 ? "?" : "&") + query.substring(1, query.length);
    jumpUrl   += (suffix == "?" ? "" : suffix);
    jumpUrl    = jumpUrl.replace("&&", "&");
    jumpUrl    = jumpUrl.replace("?&", "?");
  }
  eval(targ + ".location='" + jumpUrl + "'");
  if (restore)
  {
    selObj.selectedIndex = 0;
  }
}

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