/*
 * cookiescripts.js
 *
 */

var appletCookieMgr;

/* Ask for access cookies */
function jsCheckForCookies(CookieManager) {
  if(CookieManager != null) {
    appletCookieMgr = CookieManager;
    var cookiesOK = false;
    if(document.cookie != "") {
      cookiesOK = true;
    }
    else {
      jsSetCookie("OzlipApplet", "dummy cookie");
      if(document.cookie == "") {
        cookiesOK = false;
      }
      else {
        cookiesOK = true;
        jsDeleteCookie("OzlipApplet");
      }
    }
    /* Callback to the Java Applet to enable cookie access */
    CookieManager.setCookieStatus(cookiesOK);
  }
}


/* Set a cookie with an id and a value */
function jsSetCookie(strId, strValue) {
  document.cookie = strId + "=" + escape(strValue) + ";expires=" + new Date(2036, 12, 31).toGMTString();
}


/* Get a cookie identified by an id and pass data to Applet */
function jsGetCookie(CookieManager, strId) {
	strId = String(strId);
	var cookieValue = getCookie(strId);
	if (cookieValue) {
    	CookieManager.receiveCookieData(strId, cookieValue);
	}
}


// remove the cookie by setting ancient expiration date
function jsDeleteCookie(name) {
	name = String(name);
    if (getCookie(name)) {
        document.cookie = name + "=" +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

// remove all cookies with a common beginning to their IDs
function jsDeleteCookiesStarting(prefix) {
	prefix = String(prefix);
	var IdArray = getCookiesStarting(prefix);
	for (var i = 0; i < IdArray.length; i++) {
		jsDeleteCookie(IdArray[i]);
	}
}

/* Get a cookie identified by an id and return to caller */
function getCookie(strId) {
  if(document.cookie != "") {
    var arrCookies = document.cookie.split(";");
    for(var i = 0; i < arrCookies.length; i++) {
      var arrCookie = arrCookies[i].split("=");
      if(arrCookie.length == 2) {
	    if(strTrim(arrCookie[0]) == strTrim(strId)) {
           	return unescape(arrCookie[1]);
        }
      }
    }
    return null;
  }
  else {
    return null;
  }
}

/* Return an array of IDs of cookies where the IDs begin with a certain string */
function getCookiesStarting(IdPrefix) {
  	var IdArray = new Array();
  if(document.cookie != "") {
    var arrCookies = document.cookie.split(";");
	var ind = 0;
    for(var i = 0; i < arrCookies.length; i++) {
      var arrCookie = arrCookies[i].split("=");
      if(arrCookie.length == 2) {
	    if(strTrim(arrCookie[0]).indexOf(IdPrefix) == 0) {
           	IdArray[ind++] = unescape(arrCookie[0]);
        }
      }
    }
  }
  return IdArray;
}

function closeGame() {
  if(appletCookieMgr != null) {
    var strState = appletCookieMgr.getGameState();
    jsSetCookie("state", strState);
  }
}
 
/* Remove all white spaces */
function strTrim(str) {
  var strReturn = "";
  for(var i = 0; i < str.length; i++) {
    if(str.charAt(i) != " ") {
      strReturn += str.charAt(i);
    }
  }
  return strReturn;
}

