/* cookieHandler.js */

// set the index value for the disclaimer page in the map application introductions
var serverName = 'maps';
var mapIntroCTagLoc = 'mapintro_new';
var disclaimerNoCheckIndex_js = 4;
var disclaimerNoCheckTitle_js = 'Disclaimer';
var disclaimerNoCheckIndex_myfv10_js = 43;
var disclaimerNoCheckIndex_myfv20_js = 46;
var mapApplicationLoc = window.location.href;
var mapApplicationLocArray = mapApplicationLoc.split("/");
//alert(mapApplicationLocArray[mapApplicationLocArray.length-2]);
var applicationDirectory_js = mapApplicationLocArray[mapApplicationLocArray.length-2];
var applicationIdentifier_js = mapApplicationLocArray[mapApplicationLocArray.length-1];

var applicationAcronym_js = applicationDirectory_js;
var applicationCookieName_js = applicationAcronym_js;

/* flagFocusOnMapWindow is set to 1 when focus is changed from the map
   application to the map introduction window using focusOnMapIntro()
   (thus indicating that the map window exists and focus has changed);
   when a user changes focus from the map introduction window back to
   the map application using the focusOnMapWindow function, this flag is
   reset to zero
*/
var flagFocusOnMapWindow = 0;

/*
var check1_js = mapApplicationLocArray[mapApplicationLocArray.length-1];
var check3_js = mapApplicationLocArray[mapApplicationLocArray.length-3];
var check4_js = mapApplicationLocArray[mapApplicationLocArray.length-4];
var check5_js = mapApplicationLocArray[mapApplicationLocArray.length-5];
alert('length: ' + mapApplicationLocArray.length);
alert('unadjusted: ' + applicationAcronym_js);
alert('check1: ' + check1_js);
alert('check3: ' + check3_js);
alert('check4: ' + check4_js);
alert('check5: ' + check5_js);
*/



/*
if (applicationAcronym_js == mapIntroCTagLoc) {
   applicationAcronym_js = applicationAcronym_ct;
//alert('adjusted: ' + applicationAcronym_js);
}
*/

if (applicationAcronym_js == 'MapYourFarm') {
/* right now, applicationAcronym_js is used to specify location of introduction */
   applicationAcronym_js = 'mapyourfarm';
   applicationCookieName_js = 'MapYourFarm';
   disclaimerNoCheckIndex_js = disclaimerNoCheckIndex_myfv20_js;
   disclaimerNoCheckTitle_js = 'Disclaimer%20v2';
   //alert('adjusted: ' + applicationAcronym_js);
}
else if (applicationAcronym_js == 'mapyourfarm') {
   applicationAcronym_js = 'mapyourfarm';
   applicationCookieName_js = 'mapyourfarm';
   disclaimerNoCheckIndex_js = disclaimerNoCheckIndex_myfv10_js;
   disclaimerNoCheckTitle_js = 'Disclaimer%20v1';
}

if (applicationIdentifier_js == 'index.cfm?MenuItemID=46') {
   applicationAcronym_js = 'mapyourfarm';
   applicationCookieName_js = 'MapYourFarm';
   disclaimerNoCheckIndex_js = disclaimerNoCheckIndex_myfv20_js;
   disclaimerNoCheckTitle_js = 'Disclaimer%20v2';
   //alert('adjusted by identifier: ' + applicationAcronym_js);
}
else if (applicationIdentifier_js == 'index.cfm?MenuItemID=43') {
   applicationAcronym_js = 'mapyourfarm';
   applicationCookieName_js = 'mapyourfarm';
   disclaimerNoCheckIndex_js = disclaimerNoCheckIndex_myfv10_js;
   disclaimerNoCheckTitle_js = 'Disclaimer%20v1';
   //alert('adjusted by identifier 2: ' + applicationAcronym_js);
}

// function to value of boolean cookie
function checkBooleanCookie(cookieName) {
   var flagCookieValue = 0;
   var flagCookieExists = 0;
   var cookieValue = getCookie(cookieName);
   //alert('getCookie called');

   if (!cookieValue) {
      flagCookieValue = 0;
      flagCookieExits = 0;
      //alert('boolean cookie does not exist');
   }
   else if (cookieValue == 1) {
      flagCookieValue = cookieValue;
      flagCookieExits = 1;
      //alert('boolean cookie value is: ' + cookieValue);
   }
   else if (cookieValue == 0) {
      flagCookieValue = cookieValue;
      flagCookieExits = 1;
      //alert('boolean cookie value is: ' + cookieValue);
   }
   else {
      //alert('boolean cookie has value other than expected: ' + cookieValue);
   }
   /*
   // create an instance of the Date object
   var now = new Date();
   // fix the bug in Navigator 2.0, Macintosh
   fixDate(now);

   now.setTime(now.getTime());
   */

   return flagCookieValue;
}

// function to determine if a map should be made based on value of boolean cookie
function mapButton(cookieName, theMenuItemID, theURL, theTitle, showMenuBar, showToolBar, showLocationBar, showDirectoriesBar, showStatusBar, allowResize, useScrollbars, deltaWidth, deltaHeight) {

  //alert('theMenuItemID ' + theMenuItemID);
  var tmp1 = checkBooleanCookie(cookieName);
  var tmp2 = 0;
  var mapWindow = '';
  //alert('cookieName ' + cookieName + ' cookie value tmp1 ' + tmp1);
  if (tmp1 == 1) {
    //document.location.href = "index.cfm?MenuItemID=" + disclaimerNoCheckIndex_js;
    tmp2 = setCookie(cookieName, tmp1, '', '/');
    mapWindow = openNewWindow(theURL, theTitle, showMenuBar, showToolBar, showLocationBar, showDirectoriesBar, showStatusBar, allowResize, useScrollbars, deltaWidth, deltaHeight);
    flagFocusOnMapWindow = 0;
    document.location.href = "index.cfm?MenuItemID=" + theMenuItemID;
    if (mapWindow.focus) mapWindow.focus();
  }
  else {
    // load the disclaimer (that doesn't check if cookie has been set)
    if (cookieName == 'MapYourFarm') {
       document.location.href = "index.cfm?MenuItemID="+ disclaimerNoCheckIndex_myfv20_js;
    }
    else if (cookieName == 'mapyourfarm') {
       document.location.href = "index.cfm?MenuItemID="+ disclaimerNoCheckIndex_myfv10_js;
    }
    else {
       document.location.href = "index.cfm?MenuItemID="+ disclaimerNoCheckIndex_js;
    }
  }
  return mapWindow;
}

// function to "make a map"
function makeAMap(cookieName, theMenuItemID, theURL, theTitle, showMenuBar, showToolBar, showLocationBar, showDirectoriesBar, showStatusBar, allowResize, useScrollbars, deltaWidth, deltaHeight) {

   //alert('makeAMap ' + cookieName);
   //alert('makeAMap: theMenuItemID ' + theMenuItemID);
   //alert('makeAMap: theURL ' + theURL);
   var tmp1 = setCookie(cookieName, 1, '', '/');
   var tmp2 = 0;
   var mapWindow = '';

   mapWindow = openNewWindow(theURL, theTitle, showMenuBar, showToolBar, showLocationBar, showDirectoriesBar, showStatusBar, allowResize, useScrollbars, deltaWidth, deltaHeight);
   //alert('new window name ' + mapWindow.name);
   //alert('makeAMap ' + mapWindow.name);
   flagFocusOnMapWindow = 0;
}

// function to reset boolean cookie to 0 ("don't make a map")
function dontMakeAMap(cookieName) {
   //alert('dontMakeAMap ' + cookieName);
   //alert('self.name ' + self.name);
   var tmp1 = setCookie(cookieName, 0, '', '/');
   var mapWindow = self;
   // return to introductory page
   document.location.href = "index.cfm";
}

// function to return focus to map window
function focusOnMapWindow() {
   flagFocusOnMapWindow = 0;
   if (self.blur) self.blur();
}

// function to return focus to map window
function focusOnMapWindow2(mapWindow) {
   flagFocusOnMapWindow = 0;
   if (mapWindow.focus) {
     mapWindow.focus();
   } else if (self.blur) {
     self.blur();
   }
}

// function to return focus to introduction window
function focusOnMapIntro() {
   flagFocusOnMapWindow = 1;
   if (document.opener.focus) document.opener.blur();
}
