///////////////////////////////////////////////////////////////////////////
// Making changes to the JavaScript below violates your CfMC support
// agreement.  DO NOT MAKE CHANGES TO THIS SCRIPT!
///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//
// initial.js - javascript for index.html
//   written by   - ??????? cbova
//   last updated - 30nov01 cbova - for frames
//
////////////////////////////////////////////////////////////
// Controls popup window that survey runs in.
// Handles filling out of form from hotlinks
//   http://server.where.com/dir/index.html?name=ABC&password=XYZ
//   http://server.where.com/dir/index.html?ID=ABC123
// Also can remember name and password in a cookie
// 
// Variables set externally are:
//   use_popwindows = true/false;
//   xsize = pixels; // size of the pop-up window
//   ysize = pixels;
//   xcorner = pixels; // position of the pop-up window
//   ycorner = pixels;
//   use_autostart = true/false; // submit index page if name/passwd are known
//   use_cookies = true/false;
//   cookie_lifetime = days;     // how many day till cookie expires
////////////////////////////////////////////////////////////
// all the controling variables
////////////////////////////////////////////////////////////

var use_cookies    = false;      // true or false
var has_priority   = "cookies";  // "hot-links" or "cookies"
var use_autostart  = false;      // true or false

// cookie controls
var cookie_lifetime = 30;       // in days
var use_popwindows = true;      // true or false

// pop-up controls
var expand = true;  //set this to true if you want the popup to expand,
                    //false if you want the popup to be fixed size

if (expand == true) {
   var adjwidth = 10;
    //adjust width by this number in pixels, width will be # pixels less than
    //full screen width
   var adjheight = (screen.height - screen.availHeight) + 20;
    //adjust height by this # in pixels. height will be # pixels less than
    //full screen height (including the height of the toolbar

   var xcorner =  0; // in pixels
   var ycorner =  0;
   var xsize   = screen.width - adjwidth;
   var ysize   = screen.availHeight - adjheight;
}  else {

   var xcorner =  30; // in pixels
   var ycorner = 100;
   var xsize   = 620;
   var ysize   = 420;
}


////////////////////////////////////////////////////////////
var undefined;
var oreos = false, where = "!!!";
var popup = false;
var start = false;
var first = "cookies";
var life  = 30;
var FORM;

////////////////////////////////////////////////////////////
// this is the actual thing
////////////////////////////////////////////////////////////
if (document.forms[0] != undefined &&
    document.forms[0].name == "cfmclogin") {
   FORM = document.cfmclogin;
   runit();
   if (document.forms.CookieMgr != undefined) cookie_manager();
}

////////////////////////////////////////////////////////////
// the heart of the matter
//  0) inititalize all my local variables
//  1) try to fill out form automatically
//  2) if form filled out, maybe pop open a window, set a cookie and start
//  3) else, maybe pop open a window and set a cookie, when start is clicked
////////////////////////////////////////////////////////////
function runit () {
   var ok = false;
   var have_cookie = false;

   if (use_autostart   != undefined) start = (use_autostart  == true);
   if (use_popwindows  != undefined) popup = (use_popwindows == true);
   if (use_cookies     != undefined) oreos = (use_cookies    == true);
   if (has_priority    != undefined) first = has_priority;
   if (cookie_lifetime != undefined) life  = cookie_lifetime;

   if (first == "hot-links") { // try hot-link data first
     ok = fillout_from_url();
     if (!ok && oreos) {
        ok = fillout_from_cookie();
        if (ok) have_cookie = true;
     }
   }
   else {                      // try cookie data first
     if (oreos) {
        ok = fillout_from_cookie();
        if (ok) have_cookie = true;
     }
     if (!ok) ok = fillout_from_url();
   }

   if (ok && start) {
      if (oreos && !have_cookie) set_cfmc_cookie(life);
      if (popup) popopen();
      FORM.submit();
   }

   FORM.onsubmit = function() {
      if (oreos && !have_cookie) set_cfmc_cookie(life);
      if (popup) popopen();
   }

}

////////////////////////////////////////////////////////////
// fill out name/password from the url?query
//   query = (name=xxx&password=yyy) or (id=abc)
////////////////////////////////////////////////////////////
function fillout_from_url () {

   var w = window;
   if (window.parent != undefined) w = window.parent;


   var query  = w.location.search.substring(1) + "&";
   var name   = getvalue(query, "name");
   var passwd = getvalue(query, "password");
   var id     = getvalue(query, "id");

   if (id.length > 0) {
      name = "respondent";
      passwd = id;
   }
   if (name.length == 0 && passwd.length == 0) return false;

   fillout_form(name, passwd);
   return true;
}

////////////////////////////////////////////////////////////
// fill out the login form from a cookie
////////////////////////////////////////////////////////////
function fillout_from_cookie() {
   var oreo = get_cfmc_cookie();
   if (oreo[0] == "!!!") return false;

   fillout_form(oreo[1], oreo[2]);
   return true;
}

////////////////////////////////////////////////////////////
// open a new window that becomes the FORM's target
////////////////////////////////////////////////////////////
var xsize, ysize, xcorner, ycorner;
function popopen() {
   var undefined;
   var XD = 700;   if (xsize   != undefined) XD = xsize;
   var YD = 400;   if (ysize   != undefined) YD = ysize;
   var XP =  30;   if (xcorner != undefined) XP = xcorner;
   var YP = 100;   if (ycorner != undefined) YP = ycorner;

   var pars = "resizable=1,scrollbars=1,status=1";
   var size = "width=" + XD + ",height=" + YD;
   var loca = "left=" + XP + ",top=" + YP;
   pars += "," + size + "," + loca;

   var popname = "cfmcpopup";
   var study = find_element("studycode");
   if (study != undefined) popname = "cfmc" + study.value;

   var pop = open("", popname, pars);
   FORM.target = popname;
   pop.focus();

   return true;
}

////////////////////////////////////////////////////////////
// set a cookie
//   the cookie is "cfmc[studycode]=name+password"
////////////////////////////////////////////////////////////
function set_cfmc_cookie (days) {
   var cookie;

   if (!oreos) return;

// cookie expiration
   var today = new Date();
   var mtoday = Date.parse(today) + days * 24*60*60*1000;
   var life = new Date( mtoday );
   var expry = "expires=" + life.toGMTString();

// cookie domain from 1) form's action 2) server's IP
   var ip = document.location.hostname;
   if (FORM.action != undefined && FORM.action.substr(0,7) == "http://") {
      var arr = FORM.action.substr(7).split("/");
      ip = arr[0];
   }
   var whence = "path=/; domain=" + ip;

// the cookie is "cfmc[studycode]=name+password"
   var study  = find_element("studycode");
   var name   = find_element("name");
   var passwd = find_element("password");
   if (study == undefined || name == undefined || passwd == undefined) return;

   cookie  = "cfmc" + study.value + "=";
   cookie += escape( name.value   ) + "+";
   cookie += escape( passwd.value ) + "+";
   cookie += expry  + "; ";
   cookie += whence + "; ";

   document.cookie = cookie;

   cookie  = "cfmc" + "wowza" + "=";
   cookie += escape( name.value   ) + "+";
   cookie += escape( passwd.value ) + "+";
   cookie += expry  + "; ";
   cookie += whence + "; ";

   document.cookie = cookie;
}

////////////////////////////////////////////////////////////
// get a cookie
////////////////////////////////////////////////////////////
function get_cfmc_cookie () {
   var undefined;
   var cookie = new Array("!!!", "!!!", "!!!", "!!!");
   var string = "";

   var study = find_element("studycode");
   if (study == undefined) return cookie;
   if (document.cookie.length == 0) return cookie; 

   // actually see a string of all cookies that we can see
   var cookie_name = "cfmc" + study.value;
   var cookie_array = document.cookie.split(";");
   for (var k=0 ; k < cookie_array.length ; k++) {
      if (cookie_array[k].substr(0, cookie_name.length) == cookie_name)
         string = cookie_array[k];
   }
   if (string == "") return cookie;

   // found the right cookie, look at the date
//   alert("Found Cookie = *" + string + "*");
   cookie_array = string.split("=");
   var expry = new Date(cookie_array[2]);
   var today = new Date();
   if (expry.valueOf() <= today.valueOf()) return cookie;

   // cookie is there, and date is okay, so send it back
   cookie_array = cookie_array[1].split('+');
   cookie[0] = study;
   cookie[1] = unescape(cookie_array[0]);
   cookie[2] = unescape(cookie_array[1]);

   return cookie;
}

////////////////////////////////////////////////////////////
// set up the cookie manager so it runs
////////////////////////////////////////////////////////////
function cookie_manager () {
   CM = document.forms.CookieMgr;

   CM.clearcookie.onclick = function () {
      var study  = find_element("studycode");
      var name   = find_element("name ");
      var passwd = find_element("password");

      study  = (study  != undefined) ? study.value  : "!!!";
      name   = (name   != undefined) ? name.value   : "!!!";
      passwd = (passwd != undefined) ? passwd.value : "!!!";

      set_cfmc_cookie(-30);
      fillout_form("", "");
   }

   CM.showcookie.onclick = function () {
      alert("*" + document.cookie + "*");
      get_cfmc_cookie();
   }

}

////////////////////////////////////////////////////////////
// pick off value part of a name=value& pair
////////////////////////////////////////////////////////////
function getvalue (string, name) {
   var arr = string.split("&");
   var blank = "";

   for (var i = 0 ; i < arr.length ; i++) {
      var pair = arr[i].split("=");
      if (name == pair[0]) return pair[1];
   }
   return blank;
}

////////////////////////////////////////////////////////////
// find an element in the cfmclogin form
////////////////////////////////////////////////////////////
function find_element (name) {
   var ucname = name.toUpperCase();
   var undefined;
   for (var i = 0 ; i < FORM.length ; i++) {
      var el_name = FORM.elements[i].name.toUpperCase();
      if (el_name == ucname) return FORM.elements[i];
   }
   return undefined;
}

////////////////////////////////////////////////////////////
// fill out the form
////////////////////////////////////////////////////////////
function fillout_form (name, passwd) {
   var N = find_element("name");
   var P = find_element("password");
   if (N != undefined) N.value = name;
   if (P != undefined) P.value = passwd;
}

