// shared cookie functions
var mycookie = document.cookie;

// read cookie data
function getCookieData (name) {
   var label = name + "=",
       labelLen = label.length,
       cLen = mycookie.length,
       i = 0,
       j,
       cEnd;

   while (i < cLen) {
      j = i + labelLen;
 
      if (document.cookie.substring(i, j) == label) {
         cEnd = document.cookie.indexOf(";", j);
         if (cEnd == -1) {
            cEnd = document.cookie.length;
         }
         return unescape(document.cookie.substring(j, cEnd));
      }
      i++;
   }
   return "";
}

// write cookie data
function setCookieData (name, data, expires) {
   mycookie = document.cookie = name + "=" + data + "; expires=" + expires;
}


// Reads the stored BGR data back.
//
// If the cookie exists then return the user preference for the direction,
// otherwise return clockwise ( zero ).

function getBgrDirection () {
  var data, d, cd;
  
  if (typeof(localStorage) != 'undefined') {
    data = localStorage.getItem("bgrFormData");
    if (data != null) {
      d = JSON.parse(data);
      if (d != null) {
        return d.direction;
      }
    }
    return 0;
  }

  data = getCookieData("bgrData");
  if (data) {
    cd = data.split('-');
    return cd[4];
  } else {
    return 0;
  }
}

function getBgrSched () {
  var data, d,  cd;
  
  if (typeof(localStorage) != 'undefined') {
    data = localStorage.getItem("bgrFormData");
    if(data != null) {
      d = JSON.parse(data);
      if (d != null) {
        return d.schedule/4;
      }
    }
    return 23.5;
  }

  data = getCookieData("bgrData");
  if (data) {
    cd = data.split('-');
    return cd[7]/4;
  } else {
    return 23.5;
  }
}

function getBgrStart () {
  var data, d, cd;
  
  if (typeof(localStorage) != 'undefined') {
    data = localStorage.getItem("bgrFormData");
    if (data != null) {
      d = JSON.parse(data);
      if (d != null) {
        return d.startTime;
      }
    }
    return 0;
  }

  data = getCookieData ("bgrData");
  if (data) {
    cd = data.split('-');
    return cd[3];
  } else {
    return 0;
  }
}

function getDateOfAttempt () {
   var data = getCookieData("bgrData"),
       y, m, d,
       now = new Date(),
       cd, tDate, td;
      
   y = now.getFullYear();
   if (data) {
      cd = data.split('-');
      
      m = cd[1];
      d   = parseInt(cd[2], 10) + 1;
      if (now.getMonth() > 5) {
         y += 1;
      }
   } else {
      m = 5;
      d = 20;
      
      if (now.getMonth() > 5) {
         y += 1;
      }
      // Find the closest Saturday to 20th June next year. getDay() returns 0 for 
      // Sunday, ..., 6 for Saturday.
      tDate = new Date(y, m, d);
      td = tDate.getDay();
      
      if (td != 6) {
         if (td < 3) {
            d -= (td + 1);
         } else {
            d += (6 - td);
         }
      }
   }   
   return new Date(y, m, d);
}

function getBgrYear()
{
   var data = getCookieData("bgrData");
   if(data)
   {
      var bgrCookieData = data.split('-');
      return parseInt(bgrCookieData[0]);
   }
   else
   {
      return 0;
   }
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Creates a date object that is the date at which serious training should begin.
//
///////////////////////////////////////////////////////////////////////////////////////////////////
function getTrainStart () {
  var w = 7 * 24 * 60 * 60 * 1000,
      ts = new Date(),
      as = getDateOfAttempt(),
      ms = as.getTime();
   
  ms -= (w * 32);
   
  ts.setTime(ms);
  return ts;
}

/////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////
function storeNameData (index) {
   // six month expiry date
   var d = new Date();
       
   d.setTime( d.getTime() + ( 180 * 24 * 60 * 60 * 1000 ) );
   d.toGMTString();   

   document.cookie = "nameData" + "=" + index + "; expires=" + d.toGMTString();   
}

function getNameData () {
   var data = getCookieData("nameData"),
       cd;
       
   if (data) {
      cd = data.split('-');
      return cd[3];
   } else {
      return 0;
   }
}

function setBgrMenu (opt) {
  var data = getCookieData("bgrMenu"),
      d = new Date(),
      val = 1,
      cd; 

  // one year expiry date
  d.setTime( d.getTime() + ( 365 * 24 * 60 * 60 * 1000 ) );
  d.toGMTString();   
  if (data) {
    val = parseInt(data, 10);
  } 

  cd = (val === 0) ? 1 : 0 ;

  document.cookie = "bgrMenu" + "=" + cd + "; expires=" + d.toGMTString();   
}


