/***********************************************************************
 * This API has been established to facilitate the "iFrame" solution for
 * Google Paid Placement advertising.
 ***********************************************************************
 * utils.js
 * $Revision: 2 $
 * $Date: 09/16/09 10:30p $
 ***********************************************************************/

// Define local constants for use in this javascript program
var COOKIE_EXPIRATION    = 60*1; // 60 Seconds
var COOKIE_PATH          = '/';
var HTTP_PROTOCOL        = "http:";
var HTTPS_PROTOCOL       = "https:";
var SECURE_SEM_URL       = "https://www.aaa.com/AAA/sem/sem.htm?redirectto=";
var SECURED_COOKIE_NAME  = 'iframeSecured';
var SECURED_QUERY_NAME   = 'iframeSecured';
var TARGET_TOP           = "_top";

// This function is used to ensure that the iframe is loaded using the
// same protocol as the current document. This is required if the iframed
// document is loaded using HTTPS so that the browser does not present
// the user with a security warning.
function controlIFrameProtocol()
{
    // Check if the document is being iframed
    if (top != self)
    {
        // Check the protocol of the document
        if (document.location.protocol == HTTPS_PROTOCOL)
        {
            
            // Check if the iframe is loaded using HTTPS
            if (readQuery(SECURED_QUERY_NAME) != 'true')
            {
                // Write a cookie indicating that the iframe will be SSL enabled
                // NOTE: Since there have been some issues writing cookies due to
                // P3P, this has been replaced with utilizing the URL query string
                // for determining if the iframe has been reloaded using HTTPS.
                //writeCookie(SECURED_COOKIE_NAME, 'true', COOKIE_EXPIRATION);

                // Secure the iframe using HTTPS
                secureIFrame();
            }
        }
    }
}

// This function is used to retrieve cookie data for a given key
function readCookie(CookieName) {
  var search = CookieName + "=";
  if(document.cookie.length > 0) 
  {
    offset = document.cookie.indexOf(search);
    if(offset != -1) 
    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);

      if(end == -1) 
      {
        end = document.cookie.length;
      }
      return document.cookie.substring(offset, end);
    }
  }
}


// This function is used to retrieve URL query data for a given key
function readQuery(key)
{
    var queryPieces;
    var queryString = document.location.search;
    var querySet    = queryString.split('&');

    for (var index=0; index<querySet.length; index++) 
    {
        queryPieces = querySet[index].split('=');

        if (queryPieces[0].substring (0,1) == ' ' || queryPieces[0].substring (0,1) == '?') 
        {
            queryPieces[0] = queryPieces[0].substring (1, queryPieces[0].length);
        }

        if (queryPieces[0] == key) 
        {
            if (queryPieces[1] != null)
            {
                return queryPieces[1];
            }
            break;
        }
    }
    return '';
}

// This function is used to write the cookie data for a given key/value pair
function writeCookie(name, value, expires, path, domain, secure)
{
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime(today.getTime());

  /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
  */
  if (expires) 
  {
    expires = expires * 1000 * 60 * 60 * 24;
  }
    
  var expires_date = new Date(today.getTime() + (expires));

  document.cookie = name + "=" + value +
    ((expires) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ((path) ? ";path=" + path : "" ) +
    ((domain) ? ";domain=" + domain : "" ) +
    ((secure) ? ";secure" : "" );
}

// This function is used to secure the specified frameset using the HTTPS 
// protocol as well as pass the URL of the current page in the query string
function secureIFrame()
{
    var reloadURL = SECURE_SEM_URL + document.location;
    
    // If the frameset URL is valid (i.e. not empty), then reload frameset;
    // Otherwise, do nothing.  Services frame will be loaded using correct
    // protocol, but browser may not indicate so.
    if (document.location.search != '')
    {
        reloadURL = reloadURL + "&" + SECURED_QUERY_NAME + "=true";
    }
    else
    {
        reloadURL = reloadURL + "?" + SECURED_QUERY_NAME + "=true";
    }
    
    top.location = reloadURL;
}

// This function is used to set the 'target' attribute
// associated with all of the links contained within
// an HTML document.
function setTarget(target)
{
    for(a = 0; a < document.links.length; a++)
    {
      document.links[a].target = target;
    }
}

// This function is used to update the 'target' attribute
// associated with all of the links contained within a
// document. This can be used so that an iframed page
// can 'break-out' of the iframe once a user clicks on
// a link.
function rewriteURLTargets()
{
    setTarget(TARGET_TOP);
}
