/* --------------------------------------------------------------------
	General Cookie Functions
	(c) copyright eSolutions Ltd., contact: info@esol-group.com
---------------------------------------------------------------------*/
function getCookie(name){
	var result='', myCookie=' '+document.cookie+';', searchName=' '+name+'='
	var startOfCookie=myCookie.indexOf(searchName), endOfCookie
	if (startOfCookie!=-1) {
		startOfCookie+=searchName.length
		endOfCookie=myCookie.indexOf(';',startOfCookie)
		result=unescape(myCookie.substring(startOfCookie,endOfCookie))
	}
	return result
}
function setCookie(name,value,exp,path){ // expires in exp minutes or session if exp='session'
	var expDate=new Date()
	expDate.setTime(expDate.getTime()+exp*60*1000)
	document.cookie = name + '=' + escape(value) + (exp=='session' ? '' : '; expires=' + expDate.toGMTString())+ (path?'; path='+path:'')+';'
}
function clearCookie(name, path){ setCookie(name,'ByeBye',-3*24*60, path) }

/*******************************************************************
  xCookie
  - combines cookies in a cookie jar to avoid 20 cookie per site limit.
  - all cookie limit is 80k per site.
  - single cookie limit is 4096 bytes, escaped & incl. session
*******************************************************************/
function xCookie(cookie,exp,d,path){ // cookie=cookieName, exp=expiration, d=delim, n=number of cookies to combine into jar (not implemented yet)
  this.exp=exp // exp = numeric or '' for session cookie
  this.d=d?d:'|'
  this.path=path?path:''
  this.load=function(){ this.jar = getCookie(cookie) }
  this.save=function(){ setCookie(cookie,this.jar,this.exp,this.path) }
  this.get=function(name){ this.load(); return getVField(this.jar,name,this.d).toString() }
  this.set=function(name,value){ this.load(); this.jar=setVField(this.jar,name,value,this.d); this.save() }
  this.clear=function(){ clearCookie(cookie,this.path) }
  // to clear a single cookie, xcookie.set('myCookie','')
}
var xcookie=new xCookie('xcookie',48*60)

/*******************************************************************
  xClip - adapted for xCookie
*******************************************************************/
function xClip(fm,pfx,exp,path){ // e.g. fm='fmCmDBX', pfx='chk', exp=1440|'session', path=''|'/path'
  this.cookie=new xCookie('xclip_'+pfx,(exp?exp:1440),'|',path)
  this.init=function(){
    var fm1=eval('document.'+fm)
    for(var i=0;i<fm1.elements.length;i++)
      if(fm1[i]) if(fm1[i].type=='checkbox'&&fm1[i].name.indexOf(pfx)>=0) fm1[i].checked=(this.cookie.get(fm1[i].name)=='on')
  }
	this.clip=function(el,k,v){if(el.tagName=='SELECT')this.cookie.set(k,v);else this.cookie.set(k,(el.checked?v:''))}
	this.show=function(s,sel){ // s contains tags: KEY, VALUE, sel=select_listbox_object | ''
    this.cookie.load()
		var pan = unescape(' '+this.cookie.jar).split('|').sort(function(a1,a2){var i1=parseInt(a1.replace(/^(\D*)/,'')),i2=parseInt(a2.replace(/^(\D*)/,'')); if(isNaN(i1)||isNaN(i2))return 0; else return i1-i2});
    if(sel)while(sel.length>0)sel.options[0]=null
		for(var t='',i=0;i<pan.length;i++)
			if(pan[i].indexOf(pfx)>=0){
				var key=getTokenValue(pan[i],pfx,'='), txt=s.replace(/KEY/g,key).replace(/VALUE/g,getMField(1,pan[i],'='))
				t+=txt; if(sel)sel.options[sel.length]=new Option(txt,pfx+key)
			}
		return t
	}
  // this.cookie.clear() // to clear all clip cookies
}
