var CookieObject = {};
CookieObject.cookieInfo = {};

CookieObject.createCookie = function(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
};

CookieObject.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
};

// Reads cookie set by the backend and writes available location data to global Vars.CurrentLocation object
CookieObject.getCookieLocation = function() { 
	this.cookieInfo.latitude = this.readCookie('latitude');
	this.cookieInfo.longitude = this.readCookie('longitude');
	return this.cookieInfo;
};

// Writes to cookie with data that is passed
CookieObject.writeCookieLocation = function(latitude, longitude, redirect) { 
	this.createCookie('latitude', latitude);
	this.createCookie('longitude', longitude);
	if(redirect != false)
	  window.location = SITE_ROOT + '/graduate-school-campus-locations#map';
};

CookieObject.ipCheck = function(latitude, longitude) {
  var checkCookie = CookieObject.getCookieLocation();
  if((checkCookie.latitude == null) && (checkCookie.longitude == null)){
      var latitude = latitude || '0';
      var longitude = longitude || '0';
      CookieObject.writeCookieLocation(latitude, longitude, false);
  }
}
