// Copyright (c) 2001-2009 Ed Perrone. All rights reserved.
// This script may not be copied, used, modified, or
// distributed without the consent of the copyright holder.
// http://www.edperrone.com

// Script name:  ep.js

// Javascript routines for edperrone.com
// Modified (greatly) from astrocollege.com

// Most recent update: 02/22/09


// Set some variables and grab some info
function initialize() {


}





/////////////////////////////////////////////////////
// Cookie routines
/////////////////////////////////////////////////////

/////////////////////////////////////////////////////
// Creates a cookie.
// Pass it the cookie name, the cookie value, and number of days until expiration.
//  To set a session cookie, pass 0 as the number of days.  To expire a cookie,
//  pass -1 as the number of days.
// 11/26/01: Also passing cookieDomain and cookiePath here, so we can use this
//  script on multiple domains.
/////////////////////////////////////////////////////
function createCookie(cookieDomain, cookiePath, cookieName, cookieValue, days) {

  // See if the data was entered
  if (cookieValue != null) {

    // Calculate the expiration date, if not a session cookie, and set the cookie.
    if (days == 0) {                     // session cookie - don't set any date
        var string = cookieName + '=' + escape(cookieValue) + '; domain=' + cookieDomain + '; path=' + cookiePath;
        document.cookie = string;
    }
    else {                              // If there is a number of days, set the expiration to that date.  If -1, expire cookie (set expiration to yesterday)
        var expires = new Date ();
        expires.setTime(expires.getTime() + days * (24 * 60 * 60 * 1000));
        var string = cookieName + '=' + escape(cookieValue) + '; domain=' + cookieDomain + '; path=' + cookiePath + '; expires=' + expires.toGMTString();
        document.cookie = string;
    }
  }
}
/////////////////////////////////////////////////////


/////////////////////////////////////////////////////
// getCookies - gets any set cookies and puts them into an array of name=value pairs
/////////////////////////////////////////////////////
function getCookies() {
  cookiePairs = new Array();

  if (document.cookie.length > 0) {
    cookiePairs = document.cookie.split('; ');

    for (var i = 0 ; i < cookiePairs.length; i++) {
      cookiePairs[i] = unescape(cookiePairs[i]);
    }

    return(cookiePairs);
  }
  else {
    return('');
  }

}
/////////////////////////////////////////////////////



/////////////////////////////////////////////////////
// Miscellaneous routines
/////////////////////////////////////////////////////




/////////////////////////////////////////////////////
// getReferrer
//
// New, 07/07/02: Replaces updateReferrer()
//
// For OCA, we only want to store the referrer cookie the FIRST
//  time someone comes in.  After that, they belong to the original
//  referrer forever (or until the referrer no longer qualifies).
// So we only update the rid cookie if it is empty.  If we already
//  have one, we let it go.  BUT ... Since we will not know until
//  the end if the existing referrer is still qualified, and by that
//  time we will have lost any new referrer, we WILL store any
//  different referrer in a different cookie, rid_back. Then, if we
//  get to checkout and find the original referrer is no longer
//  there, we can change the main rid cookie to the new person.
//  We WILL overwrite the backup cookie every time through, however,
//  if there is a new query string.
//
// 07/20/02: Note: We are going to begin saving the referrer right
//  in the client file. So the first time they buy something, sign
//  up for a class, etc., their referrer will be recorded and we
//  can STOP looking at the cookies at checkout -- we simply look
//  at $client_data{'RID'}.  BUT ... If we discover that
//  $client_data{'RID'} is no longer a qualified affiliate, then
//  we will have to go back to the cookies again to see if there
//  is anyone else we can credit.
// The checkout routines will handle this. However, the note is
//  here, too, just so we remember... This routine can just keep
//  doing what it does, which is checking the query string, and
//  adding the backup when necessary.
/////////////////////////////////////////////////////
function getReferrer(qsPairs) {

  var referCookieName = 'rid';                                      // set name of both parameter and cookie
  var backupCookieName = 'rid_back';                                // set name of backup cookie
  var numDays = 3650;                                               // we will set the cookie for 10 years (arbitrary, but pretty long)
  var currentReferrer = '';                                         // this is the value we will return to the calling routine

  var queryStringReferrer = findNVValue(qsPairs, referCookieName);  // get query string value of this name
  var currentCookie = findNVValue(cookies, referCookieName);        // get cookie value of this name

  if (queryStringReferrer != '') {                                  // if we have a query string value, see where we want to put it
    if (currentCookie == '') {                                      // nothing in current cookie - put it there
      createCookie(cDomain, cPath, referCookieName, queryStringReferrer, numDays);
      currentReferrer = queryStringReferrer;
    }
    else {                                                          // current cookie exists, put new referrer into rid_back
      createCookie(cDomain, cPath, backupCookieName, queryStringReferrer, numDays);
      currentReferrer = currentCookie;
    }
  }
  else {                                                            // we don't have a query string - use the cookie value, even if it is empty
    currentReferrer = currentCookie;
  }

  return(currentReferrer);

}
/////////////////////////////////////////////////////



/////////////////////////////////////////////////////
// Update the session id, if necessary.
// Pass it the cookies array (name=value pairs)
// Returns the session ID (whether current or newly created)
/////////////////////////////////////////////////////
function updateSessionID(cookiePairs) {

  var sessionIDCookieName = 'sid';
  var currentID = findNVValue(cookiePairs, sessionIDCookieName);
  var newSID = '';

  if (currentID != '') {    // current sid cookie already exists
    newSID = currentID;
  }
  else {                            // cookie does not exist - create it
    newSID = createSessionID();
  }

  return(newSID);

}
/////////////////////////////////////////////////////


/////////////////////////////////////////////////////
// Create a session file id
// We will set up an initial session file for them as well, and issue a session
//  cookie called sid.  This file will store any picks they make until they go
//  through the login routines and/or new member signup, so we can give it back
//  to them on the other side.
// If the cookie  sid   exists, we will continue to use it.  If it does not, we
//  will set a new one.
/////////////////////////////////////////////////////
function createSessionID() {
  now = new Date();                       // date object, to get date
  var millis = now.getTime();             // milliseconds since 1/1/70
  var rand = randomNumber(1000000, 1);    // random number between 1 and 1,000,000
  var id = (millis * rand) + millis;      // multiply, to make it bigger and a little more random, then add back millis for more uniqueness

  return(id);
}
/////////////////////////////////////////////////////



/////////////////////////////////////////////////////
// Get query string parameters
//  Gets the name=value pairs of a query string and puts them into an array as name=value.
//  Pass it the query string you want analyzed.
/////////////////////////////////////////////////////
function getQueryStringParameters (qString) {
  qsPairs = new Array();

  // lose the leading ?
  qString = qString.substring(1);          // go from 1 to the end (skip 0)

  // Now split on & to get the name/value pairs.  The returned array has elements in
  //  the format  name=value
  qsPairs = qString.split("&");

  return(qsPairs);
}
/////////////////////////////////////////////////////


/////////////////////////////////////////////////////
// Find the value of a particular parameter within any name=value pair.
//  Pass it the array of name=value pairs that you get by calling
//  getQueryStringParameters() or  getCookies() (or any other set of name=value
//  pairs you might have), and the name of the parameter whose value you are looking for.
/////////////////////////////////////////////////////
function findNVValue(nvPairs, findValue) {
  temp = new Array();
  names = new Array();
  values = new Array();
  returnValue = '';

  if (nvPairs.length > 0) {                       // don't do anything if there's nothing there
    for (var i = 0; i < nvPairs.length; i++) {    // loop through pairs
      temp = nvPairs[i].split("=");               // split name=value into temp - name is temp[0] value is temp[1]
      if (temp[0] == findValue) {                 // temp[0] is name
        returnValue = (unescape(temp[1]));        // temp[1] is value
      }
    }
  }
  return(returnValue);
}
/////////////////////////////////////////////////////



/////////////////////////////////////////////////////
// Pick random number
//
// Pass it the high limit and the low limit (between
//  which you want the number selected).
/////////////////////////////////////////////////////
function randomNumber(high, low) {

  var newPick = (Math.floor(Math.random()*(high-low)))+low;   // get random number between low and high

  return(newPick);
}
/////////////////////////////////////////////////////


/////////////////////////////////////////////////////
// Show popup
//
// Show popup window with certain standard display
//  defaults.
//
// 02/22/09: Modified to set defaults and include
//  resizing.
//
// Pass it the window name, window URL, height, width,
//  and "yes" or "no" for scrollbars and resizing.
/////////////////////////////////////////////////////
function showPopup(name, url, h, w, scroll, resize) {

if (! h) {
  h = 500;
}

if (! w) {
 w = 600;
}

if (! scroll) {
  scroll = 'yes';
}

if (! resize) {
  resize = 'no';
}

PopupWindow = window.open(url,name,'height=' + h + ',width=' + w + ',directories=no,location=no,menubar=no,resizable=' + resize + ',status=no,toolbar=no,scrollbars=' + scroll + ',screenX=10,screenY=10');

return (PopupWindow);
}

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





