// Protect a page with a required login.
// Also change version in /member/touch.php

function Member() {
	this.version = 1.03;
	this.setUid(getCookie('uid'));
	this.doo_page = '/member/member.php';
	this.profile_page = '/member/profile.php';
	this.login_page = '/member/login.php';
	this.loaded = true;
	this.callcnt = 0;
}

Member.prototype.setMember = function(uid,jasmin,allow,ob) {
	this.setUid(uid);
	if (jasmin) setCookie('jasmin',jasmin,365);
	if (allow) setCookie('basil',allow,365);
	this.loadMemberFromObject(ob);
}

Member.prototype.setUid = function(uid) {
	if (uid) {
		this.uid = uid;
		setCookie('uid',uid,365);
	}
}

Member.prototype.primary = function(doc) {
	this.over_page = doc.location.href;
}

Member.prototype.log = function(doc,event,note) {
	doc.write('<script src="/member/touch.php?event=' + event + '&note=' + escape(note) + '" language="JavaScript"></script>');
}

Member.prototype.call = function(doc,url) {
	return;
	this.callcnt++;
	if (!url) {
		alert('no call url!');
		this.noCallUrl = true;
		return;
	}
	saveCookie();
	doc.location = url;
}

Member.prototype.isValid = function() {return this.verifyUid(this.uid);}

Member.prototype.verifyUid = function(uid) {
	return true;
	// SYYYYMMDDHHMMSSNNNNNNA
	if (!uid) return false;
	if (uid.substr(0,1) != 'S') return false;
	if (uid.length < 22) return false;
	return true;
}

Member.prototype.wipe = function() {
	if (this.uid) delete this.uid;
	clearCookie('uid');
	nvpWipe(site,'uid');
	saveCookie();
}

Member.prototype.logout = function(url) {
	this.wipe();
	if (url) this.call(top.document,url);
}

Member.prototype.edit = function(doc) {
	if (!doc) alert('ERROR: top.my.edit() missing document argument!');
	if (this.isValid()) {
		this.call(doc,this.doo_page + '?doo=edit&uid=' + this.uid);
		return;
	}
	alert('You must be a member to do that!');
}

Member.prototype.protect = function(doc) {
	return;
	if (!doc) alert('ERROR: top.my.protect() missing document argument!');
	if (!this.isValid()) {
		this.call(doc,this.profile_page + '?doo=new');
		return;
	}
	var mbr = this;
	mbr.last = new Date();
	if (!mbr.email) {
		this.call(doc,this.doo_page + '?doo=load&uid=' + this.uid);
		return;
	}
}

Member.prototype.loadMemberFromObject = function(ob,uid) {
	var today = new Date();
	if (uid) this.setUid(uid);
	var mbr = this;
	if (ob.email) {
		mbr.email = ob.email;
		mbr.name = ob.firstname;
		mbr.lastname = ob.lastname;
		mbr.gender = ob.gender;
		mbr.birthname = ob.birthname;
		mbr.age = today.yyyy() - ob.birthyear;
		mbr.birthyear = ob.birthyear;
		mbr.birthmonth = ob.birthmonth;
		mbr.birthday = ob.birthday;
	}
}

//if (top == self) top.my = new Member();
//else alert('WARNING: Member not in the top frame!');

top.my = new Member();

