
// CrewScheduler: js for cookie-handling (also implemented in cs_shared)

var sJobCookieKey = "CrewSkedJobRunning"  // This key must be same as in class CS_Cookies

function IsJobRunning() {
	// return true if the job cookie shows a running job
	var ckVal = GetCookie(sJobCookieKey);			
	if (ckVal == null || ckVal == "0") {
		return false;
	} else {
		return true;
	}
}

function SetJobRunning() {
	SetCookie(sJobCookieKey, "1");			
}

function SetJobFinished() {
	SetCookie(sJobCookieKey, "0");	  // it's way easier to set a flag, than to delete a cookie		
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
return null;
}  

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

// example:  SetCookie ("username", "bob", "1/1/2007");
function SetCookie (name, value) {

	// debug:
	//if (name == sJobCookieKey) { alert('Setting jobRunning to ' + value); }

	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	
	// if no expire date passed, set for 3 months
	if (expires == null) {
		expires = new Date();
		expires.setTime(expires.getTime() + (3 * 31 * 24 * 60 * 60 * 1000));
	}
	
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}
