// +----------------------------------------------------------------------+
// | Cookie                                                               |
// +----------------------------------------------------------------------+
// | Author: Stuart Lowes                                                 |
// | Version: 2.0                                                         |
// | Date: 11/08/2002                                                     |
// | Modified: 20/08/2003                                                 |
// +----------------------------------------------------------------------+

// +----------------------------------------------------------------------+
// | COOKIE FUNCTIONS                                                     |
// +----------------------------------------------------------------------+
// | Functions:                                                           |
// |  - setCookie(string name,string value [,string hours,string path,    |
// |    string domain,string secure]                                      |
// |  - getCookie(string name)                                            |
// |  - delCookie(string name[, path, domain])                            |
// |  - getCookieKeys(void)                                               |
// +----------------------------------------------------------------------+

  // BEGIN setCookie
  function setCookie(name, value, hours, path, domain, secure) {

	 if(value == "") {
	 	delCookie(name)
	 }
    var not_NN2 = (navigator && navigator.appName
		  && (navigator.appName == 'Netscape')
		  && navigator.appVersion
		  && (parseInt(navigator.appVersion) == 2))?false:true;

    // NN2 cannot handle Dates, so skip this part
    if(hours && not_NN2) {
     // already a Date string
	   if((typeof(hours) == 'string') && Date.parse(hours) ) {
	     var numHours = hours;

	   // calculate Date from number of hours
	   } else if (typeof(hours) == 'number') {
	     var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	   }
    }

    // Set the cookie, adding any parameters that were specified.
  	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':'');
  // END setCookie
  }


  function getCookie(name) {

    // there's no cookie, so go no further
    if(document.cookie == '') {
	    return false;
	  // there is a cookie
    } else {

	   	// match var=val pair

			//var re = new RegExp("\s?(" + name + ")=([^;]*)","i")
			//var matches = document.cookie.match(re)
			var pairs = document.cookie.split(";");
			for(var i=0;i < pairs.length;i++) {
				var temp = new Array()

				var temp = pairs[i].split("=");
				// CLEAN UP SPACES
				var re = /^\s/;
				temp[0] = temp[0].replace(re,"");
				if(temp[0] == name) {
					if(temp.length > 0) {
						return unescape(temp[1]);
					} else {
						return "";
					}
				}
			}

		// If there was no cookie of that name, return false.
   	return false;
    }
  }
  // END readCookie

  // BEGIN delCookie
  function delCookie(name, path, domain) {
    var theValue = getCookie(name); // We need the value to kill the cookie
    if(theValue) {
        // set an already-expired cookie
        document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');
    }
  }
  // END delCookie

  // BEGIN getCookieKeys
  function getCookieKeys() {

	 // No cookie so return
	 var keys = new Array();
	 var matches = new Array();
	 if(document.cookie == '') {
	    return false;
	  // there is a cookie
    } else {
    	var re = /([^\=\;]+)\=[^\;]*/g; // Global
    	var re2 = /([^\=\;]+)\=[^\;]*/; // Not global
    	var re3 = /^\s|\s$/;
    	var matches = document.cookie.match(re)
    	if(matches != null) {
				for(var i=0; i < matches.length;i++) {
					 keys[i] = matches[i].match(re2)[1].replace(re3,"");
				}
			}
    	return keys;
    }

  }
  // END getCookieKeys
