//<% 
//remove '//' from '//<%' above, for server side ASP, see also bottom of page to remove other '//'. When removed rename to database.inc and use include file on ASP to pull page.
/*
CookieMethods(v2.1) for Access-Object JavaScript Database
Latest Version and help file available at http://www.javascriptdatabase.com

Copyright (C) 2001 Kevin Gibney
Distributed under the terms of the GNU Library General Public License
*/

function CookieProperties() {
	this.AOJDCookieVersion = '2.1'
	
	//these properties are fields in DBaseCookieRecordset.
	this.Domain = ''		//rw	string
	this.Expires = -1		//r		string
	this.ExpireHours = 0 	//rw 	integer 	1 = 1 hour
	this.ExpireMinutes = 0  //rw	integer 	1 = 1 min
	this.Path = ''			//rw	string
	this.Secure = false		//rw	boolean
	
	//other
	this.Document = ''		//r		string		set to current 'document'(object) client side and string server side ASP
	
	//extra properties that can be viewed after saving cookie
	this.CookieString = ''	//r		string		unescaped
	this.CookieLength = 0	//r		integer		
	this.Loaded = false		//r		boolean

	//all rw strings
	this.SeparatorDBName = '=' 				
	this.SeparatorFieldNames = ','
	this.SeparatorFielddbIndex = ','
	this.SeparatorFieldType = ','
	this.SeparatorFieldValues = '#'			//field value separator
	this.SeparatorRecords = '<*'				//records separator
	this.SeparatorRsets = '*RSETS*'			//RecordSETs Separator
	this.SeparatorRsetDefinition = '*RD*' 	//Recordset Definition Separatot
	this.SeparatorEnd = ';'
}

function _cookie_save(){
	var pCRset = this.DBaseCookieRecordset, 
		pCProp = this.DBaseCookieProperties,
		pDBProp = this.DBaseProperties;
	
	if (this.Recordsets.Count > 0) {
		//create and load cookie recordset
		if (typeof(pCRset) != "undefined") {
			this.LoadCookieRecordset()
		}
		
		//setup expires
		if (typeof(document)=="undefined") {
			//***ASP - SERVER SIDE
			//Note: date.toGMTString() function does not seem to work in my version of win2000 server side - hence the hack
			var expiretime = 0
			if (pCProp.ExpireHours != 0)  expiretime=pCProp.ExpireHours*(1000*60*60)
			if (pCProp.ExpireMinutes != 0)  expiretime+=pCProp.ExpireMinutes*(1000*60)
	
			if (expiretime != 0) pCProp.Expires = new Date((new Date()).getTime() + expiretime)

			var myyear = pCProp.Expires.getFullYear()
			var mymonth = pCProp.Expires.getMonth() + 1
			var myday = pCProp.Expires.getDate()
			
			var myHH = pCProp.Expires.getHours()
			var myMM = pCProp.Expires.getMinutes()
			var mySS = pCProp.Expires.getSeconds()
			var expireon = myday + "-" + mymonth + "-" + myyear + " " + myHH + ":" + myMM + ":" + mySS
			//.toGMTString()does not work
		} else {
			//***CLIENT SIDE			
			var expiretime = 0
			if (pCProp.ExpireHours != 0)  expiretime=pCProp.ExpireHours*(1000*60*60)
			if (pCProp.ExpireMinutes != 0)  expiretime+=pCProp.ExpireMinutes*(1000*60)
	
			if (expiretime != 0) pCProp.Expires = new Date((new Date()).getTime() + expiretime)
			var cookieExtras = ""
			
			//.toGMTString is required for client side NOT server side (it doesnt work - error)
			if (expiretime != 0) cookieExtras += '; expires=' + pCProp.Expires.toUTCString()
			if (pCProp.Path != '') cookieExtras += '; path=' + pCProp.Path
			if (pCProp.Domain != '') cookieExtras += '; domain=' + pCProp.Domain
			if (pCProp.Secure != false) cookieExtras += '; secure'
		}
		with (this.DBaseCookieRecordset) {
			MoveFirst()
			UpdateField('Expires',pCProp.Expires.toUTCString())
		}
		
		var s = '', startRsetLoop = true;
		for (var r=0;r<this.Recordsets.Count;r++) {
			var pRset = this.Recordsets(r)
			if (pRset.RsetProperties.CookieOn) {
				//recordsets separator
				if (!startRsetLoop) s+= pCProp.SeparatorRsets
				startRsetLoop = false
				
				//recordset name
				s += pRset.RsetProperties.Name + pCProp.SeparatorRsetDefinition
				
				//fieldproperties: Name,Indexed,Type
				var sFN="",sFI="",sFT=""
				if (pRset.Fields.Count>0) {	
					sFN = pRset.Fields(0).FieldProperties.Name
					sFI = pRset.Fields(0).FieldProperties.Indexed
					sFT = pRset.Fields(0).FieldProperties.Type
				}
				for (var f=1;f<this.Recordsets(r).Fields.Count;f++) {
					sFN += pCProp.SeparatorFieldNames + pRset.Fields(f).FieldProperties.Name
					sFI += pCProp.SeparatorFielddbIndex + pRset.Fields(f).FieldProperties.Indexed
					sFT += pCProp.SeparatorFieldType + pRset.Fields(f).FieldProperties.Type
				}
				s += sFN + pCProp.SeparatorRsetDefinition
				s += sFI + pCProp.SeparatorRsetDefinition
				s += sFT + pCProp.SeparatorRsetDefinition
				
				//save recordset records
				var sR = ""
				if (pRset.Count() > 0) {
					var startRecordLoop = true, currentrec
					pRset.MoveFirst()
					while (!pRset.RsetProperties.Eof) {
						currentrec=pRset.RsetProperties.CurrentRecord
						if (pRset.RsetRecs[currentrec].RecordProperties.Deleted==false) {
							if (!startRecordLoop) sR += pCProp.SeparatorRecords
							sR += pRset.RsetRecs[currentrec].join(pCProp.SeparatorFieldValues)
							startRecordLoop = false
						}
						pRset.MoveNext()
						
					}
					s += sR
				}
			}
		}

 		pCProp.CookieString = s
		if (typeof(document) == "undefined") {
			//***ASP - SERVER SIDE
			var cookie = s //no need of escape - automatically done
			var cookieName = this.DBaseProperties.Name
			eval("Response.Cookies(cookieName) = cookie") //statement 'hidden' cause Netscape doesnt like, even though it never executes this code 
			Response.Cookies(cookieName).Expires = expireon//"15-8-1981 21:31:00"
		} else {
			//***CLIENT SIDE
			var sEscape = escape(s)
			var cookie = pDBProp.Name + pCProp.SeparatorDBName + sEscape + cookieExtras
			pCProp.CookieLength = sEscape.length
			pCProp.Document.cookie = cookie
		}
		pCProp.Loaded = true
		return true
	} else {
		//no cookie
		return false
	}
}

function _cookie_load() {
	var pCRset = this.DBaseCookieRecordset, pCProp = this.DBaseCookieProperties, pDBProp = this.DBaseProperties,
		cookieStr = "",	mycookie = "";

	if (typeof(document)=="undefined") {
		//Server side - ASP
		cookieStr = Request.Cookies(pDBProp.Name).Item
		
		if (cookieStr == "") {
			//create and load cookie recordset
			if (typeof(this.DBaseCookieRecordset) == "undefined") {
				this.CreateCookieRecordset()
				this.LoadCookieRecordset()
			}
			return false
		}

		cookieStr = unescape(cookieStr)
		mycookie = cookieStr
	} else {
		//Client side
		if (this.DBaseCookieProperties.Document=="") this.DBaseCookieProperties.Document=document
		cookieStr = this.DBaseCookieProperties.Document.cookie
		if (cookieStr == "") {
			//create and load cookie recordset
			if (typeof(this.DBaseCookieRecordset) == "undefined") {
				this.CreateCookieRecordset()
				this.LoadCookieRecordset()
			}
			return false
		}	
 		//incase cookie was saved server side replace '+' with space
		var temp = cookieStr.replace(/\+/g,' ')
		cookieStr = temp 
		
		cookieStr = unescape(cookieStr)
		
		var startString = pDBProp.Name + pCProp.SeparatorDBName
		var startLength = (startString).length
		var start = cookieStr.indexOf(startString)

		if (start == -1) {
			pCProp.Loaded=false
			//create and load cookie recordset
			if (typeof(this.DBaseCookieRecordset) == "undefined") {
				this.CreateCookieRecordset()
				this.LoadCookieRecordset()
			}
			return false
		}
		start += startLength

		var end = cookieStr.indexOf(pCProp.SeparatorEnd,start)
		if (end == -1) end = cookieStr.length
		mycookie = cookieStr.substring(start,end)
	}

	//set CookieString property
	pCProp.CookieString = mycookie
	pCProp.CookieLength = escape(mycookie).length
	
	//start create recordsets
	var temp = new Array() //holds rset names
	temp = pCProp.CookieString.split(pCProp.SeparatorRsets)
	
	var nofRsets = temp.length
	for (var r=0;r<nofRsets;r++) {
		//split for rsetname
		var RsetFieldsRecs = new Array()
		RsetFieldsRecs = temp[r].split(pCProp.SeparatorRsetDefinition)
		var RsetName = RsetFieldsRecs[0]
		
		if (this.Recordsets(RsetName) == false) { //if rset does not exist then create, else reset rest and reload from cookie
			this.CreateRecordset(RsetName)
			this.Recordsets(RsetName).RsetProperties.CookieOn = true //switch on cookie create for this recordset
	
			//split for fieldnames/dbIndex/Type
			var FieldNames = new Array()
			var FielddbIndex = new Array()
			var FieldType = new Array()
			FieldNames = RsetFieldsRecs[1].split(pCProp.SeparatorFieldNames)
			FielddbIndex = RsetFieldsRecs[2].split(pCProp.SeparatorFielddbIndex)
			FieldType = RsetFieldsRecs[3].split(pCProp.SeparatorFieldType)
			for (var f=0;f<FieldNames.length;f++) {
				if (FielddbIndex[f] == "true") {
					this.Recordsets(RsetName).CreateField(FieldNames[f],dbIndex)
				} else {
					this.Recordsets(RsetName).CreateField(FieldNames[f])
				}
				this.Recordsets(RsetName).Fields(f).FieldProperties.Type = FieldType[f] 
			}
		} else {
			this.ResetRecordset(RsetName)
			this.Recordsets(RsetName).RsetProperties.CookieOn = true
		}
		
		//split for records
		var Recs = new Array()
		Recs = RsetFieldsRecs[4].split(pCProp.SeparatorRecords)
		var nofRecs = Recs.length
		for (var recs=0;recs<nofRecs;recs++) {
			var myRec = new Array()
			myRec = Recs[recs].split(pCProp.SeparatorFieldValues)
			if (myRec == "") break
			this.Recordsets(RsetName).A(myRec)
		}	
	}
	
	//create and load cookie recordset
	if (typeof(this.DBaseCookieRecordset) != "undefined") {
		this.LoadCookieRecordset()
	}
			
	pCProp.Loaded=true
	return true
}


function _cookie_remove() {
	var pCProp = this.DBaseCookieProperties, pDBProp = this.DBaseProperties;
	if (typeof(document)=="undefined") {
		//***ASP - SERVER SIDE
		//NOTE need to change below PATH DOMAIN ETC
		
		var cookieName = pDBProp.Name
		eval("Response.Cookies(cookieName) = cookie") //statement 'hidden' cause Netscape doesnt like, even though it never executes this code 
		if (pCProp.Path) Response.Cookies(cookieName).Path = pCProp.Path
		if (pCProp.Domain) Response.Cookies(cookieName).Domain = pCProp.Domain
		Response.Cookies('user').Expires = "01-10-1980 00:00:00"
		pCProp.Loaded = false
	} else {
		//***CLIENT SIDE
		var cookie = pDBProp.Name + pCProp.SeparatorDBName
		if (pCProp.Path) cookie += '; path=' + pCProp.Path
		if (pCProp.Domain) cookie += '; domain=' + pCProp.Domain
		cookie += ';expires=Fri, 02-Jan-1970 00:00:00 GMT'

		if (pCProp.Document == "") pCProp.Document=document

		pCProp.Document.cookie = cookie
		pCProp.Loaded = false
	}
	return true
}

function _create_cookie_recordset() {
	var pCProp = this.DBaseCookieProperties;
	this.CreateRecordset('DBaseCookieRecordset')
	with (this.DBaseCookieRecordset) {
		CreateField('Domain')
		CreateField('Expires')
		CreateField('ExpireHours')
		CreateField('ExpireMinutes')
		CreateField('Path')
		CreateField('Secure')
		
		RsetProperties.CookieOn = true
	}
	this.DBaseCookieRecordset.AddRec([pCProp.Domain,pCProp.Expires,pCProp.ExpireHours,pCProp.ExpireMinutes,pCProp.Path,pCProp.Secure])
}
 
function _load_cookie_recordset() {
	var pCRset = this.DBaseCookieRecordset, pCProp = this.DBaseCookieProperties;
	pCRset.MoveFirst()
	with (this.DBaseCookieRecordset) {
		pCProp.Domain = Domain
		pCProp.Expires = Expires
		pCProp.ExpireHours = ExpireHours
		pCProp.ExpireMinutes = ExpireMinutes
		pCProp.Path = Path
		pCProp.Secure = eval(Secure)
	} 
}

Database.prototype.CookieSave = _cookie_save
Database.prototype.CookieLoad = _cookie_load
Database.prototype.CookieRemove = _cookie_remove
Database.prototype.CreateCookieRecordset = _create_cookie_recordset
Database.prototype.LoadCookieRecordset = _load_cookie_recordset
//%>