﻿/******************************************************************************
 qForm JSAPI: Cookie Library

 Author: Dan G. Switzer, II
 Build:  105
******************************************************************************/
// initialize workspace variables
var _c_dToday = new Date();
var _c_iExpiresIn = 90;
var _c_strName = self.location.pathname;

/******************************************************************************
 Required Functions
******************************************************************************/
// retrieve a cookie from the browser
function _getCookie(name){
	var iStart = document.cookie.indexOf(name + "=");
	var iLength = iStart + name.length + 1;
	if( (iStart == -1) || (!iStart && (name == document.cookie.substring(0)) ) ) return null;
	var iEnd = document.cookie.indexOf(";", iLength);
	if( iEnd == -1 ) iEnd = document.cookie.length;
	return unescape(document.cookie.substring(iLength, iEnd));
}

// set a cookie to the browser
/* Note: Only "name" and "value" are required parameters */
function _setCookie(name, value, expires, path, domain, secure) {
	//if (!expires) {expires = new Date(); expires = expires.setFullYear(2020, 01);}
	var d = new Date();
  d.setFullYear(d.getFullYear() + 1);
	//alert("d: " + d + "\n d.toUTCString: " + d.toUTCString());
	//alert("expires: " + expires + "\n expires.toUTCString: " + expires.toUTCString());
	
	document.cookie = name + "=" + escape(value) + 
	( (d) ? ";expires=" + d.toGMTString() : "") +
	( (path) ? ";path=" + path : "") + 
	( (domain) ? ";domain=" + domain : "") +
	( (secure) ? ";secure" : "");
	//alert("document.cookie: " + document.cookie + "\n====\n" + document.cookie.length);
}

function _deleteCookie(name, path, domain){
	if (_getCookie(name)) document.cookie = name + "=" +
		( (path) ? ";path=" + path : "") +
		( (domain) ? ";domain=" + domain : "") +
 		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function _createCookiePackage(struct){
	var cookie = "";
	for( key in struct ){
		if( cookie.length > 0 ) cookie += "&";
		cookie += key + ":" + escape(struct[key]); 
	}
	return cookie;
}

function _readCookiePackage(pkg){
	struct = new Object();
	// break the package into key/value pairs
	var a = pkg.split("&");
	// loop through the array and seperate the key/value pairs
	for( i=0; i < a.length; i++ ) a[i] = a[i].split(":");
	// convert the values into a structure
	for( i=0; i < a.length; i++ ) struct[a[i][0]] = unescape(a[i][1]);
	// return the structure
	return struct;
}
















