var jsdataversion = 10;

var debugdata = 0;
var lasticon = 0;
var totalonline = 0;
var refreshtype = 0;
var aindex = 1;
var whosonlinerefreshenabled = 1;
var theicon = new Array();
var savedicon = new Array();
var priorname = "";
var whosonlineexpire = "";
var whosonlineslowdown = 0;
var thetimeout = "";
var errcount = 0;
var downloadinfo = "";
var eucountrylist = ",Austria,Belgium,Bulgaria,Cyprus,Czech Republic,Denmark,Estonia,Finland,France,Germany,Greece,Hungary,Ireland,Italy,Latvia,Lithuania,Luxembourg,Malta,Netherlands,Poland,Portugal,Romania,Slovakia,Slovenia,Spain,Sweden,United Kingdom,";

function comparename(a,b) {
	return (a.nameorder < b.nameorder ? -1 : 1);
}
function compareonlinetime(a,b) {
	return (a.onlinetime < b.onlinetime ? -1 : 1);
}
function comparedist(a,b) {
	return (a.dist < b.dist ? -1 : 1);
}
function comparehotspotdist(a,b) {
	return (a.dist < b.dist ? -1 : 1);
}
function refreshwhosonline(rtype) {
	if (aindex < 10 && debugdata) {
			var midlist = rtype+" ";
			for (var k = 0; k < theicon.length; k++) {
				midlist += theicon[k].memberid+" ";
			}
			new Image().src = "setwhosonline.cfm?index="+aindex+"&midlist="+encodeURIComponent(midlist);
			aindex++;
	}
	var xtime = new Date().getTime();
	var nexticon = 0;
	if (sortorder == 0) theicon.sort(comparename);
	else if (sortorder == 1) theicon.sort(compareonlinetime);
	else if (sortorder == 2) {
		if (whosonlinelocationmask != 1024) theicon.sort(comparedist);
		else theicon.sort(comparehotspotdist);
	}
	var blankrow = "<tr><td height=10>";
	for (var j = 1; j < iconsacross; j++) blankrow += "<td>";
	blankrow += "<tr>";
	var thehtml = "";
	var showfr = whosonlinesubsetmask&(1+16);
	var showbm = whosonlinesubsetmask&(1+4+8+16);
	var showfof = whosonlinesubsetmask&0;
	var altwhosonlinesubsetmask = whosonlinesubsetmask;
	if (altwhosonlinesubsetmask == 4) altwhosonlinesubsetmask += 8;
	whosonlinesubset = document.theform.whosonlinesubset.value;
	var skiptest = !(whosonlinesubset == 0 || whosonlinesubset == 4);
	var thewidth = Math.floor(100/iconsacross)+"%";
	showhidelayer("thepart",!skiptest);
	var subicon = new Array();
	for (var i = 0; i < lasticon; i++) {
		thisicon = theicon[i];
		if ((skiptest ||
			((thisicon.gender == gender || gender == '')
			&& (thisicon.sexualorientation == sexualorientation || sexualorientation == 0 || sexualorientation == 4 && thisicon.sexualorientation >= 2)
			&& (relationshiptypemask&thisicon.relationshiptypemask)
			&& ((whosonlinelocationmask&thisicon.whosonlinelocationmask) || whosonlinelocationmask == 256 && state == thisicon.state)
			&& (thisicon.certified || !certified)
			&& (thisicon.showchat || !showchat)))
			&& (altwhosonlinesubsetmask&thisicon.whosonlinesubsetmask)
			&& (sortorder != 2 || thisicon.dist >= 0)
			) {
			subicon[nexticon++] = thisicon;
		}
	}
	var start = 0;
	var end = nexticon;
	var numpages = Math.floor((nexticon-1)/iconsperpage)+1;
	var controls = "";
	if (pagenum >= 0) {
		if (pagenum && pagenum > Math.floor((nexticon-1)/iconsperpage)) pagenum = Math.floor((nexticon-1)/iconsperpage);
		start = pagenum*iconsperpage;
		if (start+iconsperpage < nexticon) end = start+iconsperpage;
		if (numpages > 1) {
			controls = navbar("setpagenum",pagenum,numpages-1);
			thehtml += controls+"<hr>";
		}
	}
	thehtml += "<table class=ta width=100%>";
	nexticon = 0;
	for (var i = start; i < end; i++) {
		thisicon = subicon[i];
		if (!(nexticon%iconsacross)) {
			if (nexticon) thehtml += blankrow;
			else thehtml += "<tr>";
		}
		var theclass = "";
		if (showfr && (thisicon.whosonlinesubsetmask&2)) theclass = "tfriend";
		else if (showbm && (thisicon.whosonlinesubsetmask&4)) theclass = "tmybookmark";
		else if (showbm && (thisicon.whosonlinesubsetmask&8)) theclass = "totherbookmark";
		else if (showfof && (thisicon.whosonlinesubsetmask&16)) theclass = "tfof";
		else theclass = "tnorm";
		thehtml += "<td class="+theclass+">"+thisicon.thehtml.replace("====",whosonlinelocationmask == 1024 ? thisicon.hotspotdisttext : thisicon.disttext).replace("#page#",pagenum >= 0 ? pagenum : 0);
		nexticon++;
		if (!memberid && nexticon >= 50) break;
	}
	if (nexticon == totalonline) document.getElementById("onlinecount").innerHTML = totalonline;
	else document.getElementById("onlinecount").innerHTML = nexticon+' of '+totalonline;
	if (nexticon) {
		while (nexticon%iconsacross) {
			thehtml += "<td width="+thewidth+">&nbsp;";
			nexticon++;
		}
	}
	else thehtml += "<tr><td><i>No such members online.</i>";
	thehtml += "</table>";
	if (controls != "") thehtml +="<hr>"+controls;
	showhidelayer("statelist",whosonlinelocationmask == 256);
	showhidelayer("newguy",1);
	showhidelayer("legend",showfr+showbm+showfof != 0);
	showhidelayer("friends",showfr != 0);
	showhidelayer("fof",showfof != 0);
	showhidelayer("bookmarks",showbm != 0);
	xtime = (new Date().getTime()-xtime);
	var starttime = new Date().getTime();
	document.getElementById("onlinetable").innerHTML = thehtml;
	starttime = (new Date().getTime()-starttime);
	// new Image().src = "innerhtmltime.cfm?rtype="+rtype+"&type=11&t="+starttime;
	if (memberid == 1 || memberid == 13455) {
		document.getElementById("innerhtmltime").innerHTML = downloadinfo+"Calc HTML: "+xtime+"ms; Set innerHTML: "+starttime+"ms for "+thehtml.length+" bytes <br>";
	}
	showhidelayer("loadingicon",0);
}
function setpagenum(p) {
	pagenum = p;
	refreshwhosonline(1);
}
function getselecttext(str) {
	return (document.theform[str].options[document.theform[str].selectedIndex].text);
}
function getselecttextlc(str) {
	return (getselecttext(str).toLowerCase());
}
function editcriteria(show) {
	showhidelayer("criteriaform",show);
	showhidelayer("criteriatext",!show);
}
function getcriteria() {
	var res = getselecttext("tripid");
	if (res == "Home") res = "";
	else res = trim(res.split("(")[0])+". ";
	res += "Showing "+getselecttextlc("sexualorientation")+" "+getselecttextlc("gender")+" who are "+getselecttextlc("relationshiptype");
	if (icenabled && document.theform.showchat.checked) res += ", can chat";
	if (memberid && document.theform.certified.checked) res += ", are certified";
	res += " and live "+getselecttextlc("whosonlinelocation");
	res += " sorted by "+getselecttextlc("sortorder")+".";
	document.getElementById("criteria").innerHTML = res;
}
function popchange(thepopup) {
	if (typeof thepopup != "undefined") {
		showhidelayer("updateid",thepopup.options[thepopup.selectedIndex].value == 2);
		if (!haveaddress && thepopup.options[thepopup.selectedIndex].value == 2) {
			thepopup.selectedIndex = sortorder;
			myalert("You must set up your address to use this option.");
			return;
		}
	}
	gender = document.theform.gender.options[document.theform.gender.selectedIndex].value;
	sexualorientation = document.theform.sexualorientation.options[document.theform.sexualorientation.selectedIndex].value;
	relationshiptype = document.theform.relationshiptype.options[document.theform.relationshiptype.selectedIndex].value;
	// needreload = relationshiptypemask != (1<<relationshiptype);
	needreload = 0;
	relationshiptypemask = 1<<relationshiptype;
	whosonlinelocation = document.theform.whosonlinelocation.value;
	showhidelayer("statelist",whosonlinelocation == 8);
	needreload |= whosonlinelocationmask != (1<<whosonlinelocation);
	whosonlinelocationmask = 1<<whosonlinelocation;
	needreload |= whosonlinesubset != document.theform.whosonlinesubset.value;
	whosonlinesubset = document.theform.whosonlinesubset.value;
	if (whosonlinesubset >= 0) whosonlinesubsetmask = 1<<whosonlinesubset;
	else {
		whosonlinesubsetmask = 0;
		for (var i = 0; i < orgidlist.length; i++) {
			if (orgidlist[i] == -whosonlinesubset) {
				whosonlinesubsetmask = 1<<(5+i);
				break;
			}
		}
		if (!whosonlinesubsetmask) {
			whosonlinesubsetmask = 1;
			whosonlinesubset = 0;
		}
	}
	needreload |= state != document.theform.state.options[document.theform.state.selectedIndex].value;
	state = document.theform.state.options[document.theform.state.selectedIndex].value;
	if (typeof document.theform.certified != "undefined") certified = document.theform.certified.checked;
	else certified = 0;
	if (typeof document.theform.showchat != "undefined") showchat = document.theform.showchat.checked;
	else showchat = 0;
	sortorder = document.theform.sortorder.options[document.theform.sortorder.selectedIndex].value;
	refreshurl = "setwhosonline.cfm?gender="+gender+"&sexualorientation="+sexualorientation+"&relationshiptype="+relationshiptype
			+"&whosonlinelocation="+whosonlinelocation+"&state="+state+"&whosonlinesubset="+whosonlinesubset+"&certified="+Math.floor(certified)+"&showchat="+Math.floor(showchat)+"&sortorder="+sortorder;
	if (needreload) {
		clearTimeout(thetimeout);
		document.getElementById("onlinetable").innerHTML = "Loading...";
		geturl(refreshurl,refreshdata,0,0);
	}
	else {
		refreshwhosonline(1);
		geturl(refreshurl,null,0,0);
	}
	if (iphone) getcriteria();
}
function setrefresh(thepopup) {
	refreshinterval = thepopup.options[thepopup.selectedIndex].value;
	new Image().src = "setwhosonline.cfm?refreshinterval="+refreshinterval;
	clearTimeout(thetimeout);
	if (refreshinterval > 0) refreshdata();
}
function refreshpage() {
	if (whosonlinerefreshenabled) {
		refreshdata();
		if (refreshinterval > 0) thetimeout = setTimeout(refreshpage,refreshinterval*1000*(1+whosonlineslowdown));
	}
}
function setsubset(subset) {
	document.theform.whosonlinesubset.value = subset;
	popchange();
	showhidelayer("thepart",subset == 0 || subset == 4);
	return (true);
}
if (refreshinterval > 0) thetimeout = setTimeout(refreshpage,refreshinterval*1000*(1+whosonlineslowdown));
function t(x,t,slowdown,tot,state) {
	mystate = state;
	whosonlineexpire = x;
	whosonlineslowdown = slowdown;
	totalonline = tot;
	document.getElementById("onlinetime").innerHTML = t+(slowdown ? "!" : "");
	errcount = 0;
	lasticon = 0;
}
function noselfchat() {
	myalert("You can't chat with yourself!");
}
function i(onlinetime,id,strings,whosonlinesubsetmask,dist,hotspotdist) {
	var d = 2*3956;	// sb 2*3952??
	var theobj = new Object();
	var thumbhtml = "";
	var newguy = onlinetime <= newdur;
	var strs = strings.split("\t");
	var photoid = strs[0].charAt(0);
	var certchatgender = strs[0].charCodeAt(1)-48;
	var usejschat = (certchatgender>>5)&1;
	var mycountry = (certchatgender>>4)&1;
	var hidedist = ((certchatgender>>3)&1) || id == memberid || memberid == 0;
	var certified = (certchatgender>>2)&1;
	var showchat = (certchatgender>>1)&1;
	var gender = (certchatgender&1) ? "m" : "f";
	var fntypesexualorientation = strs[0].charCodeAt(2)-48;
	var sexualorientation = fntypesexualorientation&7;
	var fntype = fntypesexualorientation>>3;
	var relationshiptypemask = strs[0].charCodeAt(3)-48;
	var firstname = strs[0].substr(4);
	var title = strs[1];
	var state = strs[2];
	var thehtml = "";
	var whosonlinelocationmask = 1;
	if (fntype == 1) firstname = priorname+firstname;
	priorname = firstname;
	thumbhtml = getthumbnailimg(id,photoid);
	firstname = fixfirstname(firstname)+"<br>";
	if (canhover) title = " title=\""+title+"\"";
	else title = "";
	if (newguy) firstname = "<span class=warn>"+firstname+"</span>";
	if (memberid == 1 && 0) {
		thehtml = "<a href=viewprofile.cfm?sret=3,0,#page#&id="+id+title+">"+firstname+thumbhtml+"</a><br><a href=javascript:startic("+id+","+usejschat+")><img src="+imageserver+"/onlinetool2.gif title=Chat></a><br><span class=small>====</span>";
	} else if (showchat && id == memberid && imprefs) {
		thehtml = "<a href=viewprofile.cfm?sret=3,0,#page#&id="+id+title+">"+firstname+thumbhtml+"</a><br><a href=javascript:noselfchat()>Chat</a><br><span class=small>====</span>";
	} else if (showchat && id != memberid) {
		thehtml = "<a href=viewprofile.cfm?sret=3,0,#page#&id="+id+title+">"+firstname+thumbhtml+"</a><br><a href=javascript:startic("+id+","+usejschat+")>Chat</a><br><span class=small>====</span>";
	} else {
		thehtml = "<a href=viewprofile.cfm?sret=3,0,#page#&id="+id+title+">"+firstname+thumbhtml+"</a><br><span class=small>====</span>";
	}
	if (mycountry) whosonlinelocationmask += 2;
	if (state == mystate) whosonlinelocationmask += 4;
	if (1) whosonlinelocationmask += 512;
	if (dist >= 0) {
		if (dist) dist = d*Math.asin(dist/100./d);
		if (id != memberid) dist += .0001;
		if (dist <= 100) {
			if (dist <= 10) whosonlinelocationmask += 8+16+32+64+128;
			else if (dist <= 25) whosonlinelocationmask += 16+32+64+128;
			else if (dist <= 50) whosonlinelocationmask += 32+64+128;
			else if (dist <= 75) whosonlinelocationmask += 64+128;
			else whosonlinelocationmask += 128;
		}
	}
	theobj.memberid = id;
	theobj.firstname = priorname;
	theobj.nameorder = lasticon;
	theobj.thehtml = thehtml;
	theobj.hidedist = hidedist;
	theobj.certified = certified;
	theobj.showchat = showchat;
	theobj.gender = gender;
	theobj.newguy = newguy;
	theobj.sexualorientation = sexualorientation;
	theobj.relationshiptypemask = relationshiptypemask;
	theobj.whosonlinelocationmask = whosonlinelocationmask;
	theobj.whosonlinesubsetmask = whosonlinesubsetmask;
	theobj.state = state;
	theobj.dist = dist;
	if (dist < 0 || theobj.hidedist) theobj.disttext = "";
	else {
		theobj.dist /= conversion;
		if (theobj.dist < 1.5) theobj.disttext = "&lt;2";
		else theobj.disttext = "~"+Math.floor(theobj.dist+.5);
		theobj.disttext += conversion == 1 ? " mi" : "km";
	}
	if (typeof hotspotdist == "undefined") hotspotdist = -1;
	else {
		if (hotspotdist) hotspotdist = d*Math.asin(hotspotdist/100./d);
		if (hotspotdist <= 10)  whosonlinelocationmask += 1024;
	}
	theobj.hotspotdist = hotspotdist;
	if (hotspotdist < 0 || theobj.hidedist) theobj.hotspotdisttext = "";
	else {
		theobj.hotspotdist /= conversion;
		if (theobj.hotspotdist < 1.5) theobj.hotspotdisttext = "&lt;2";
		else theobj.hotspotdisttext = "~"+Math.floor(theobj.hotspotdist+.5);
		theobj.hotspotdisttext += conversion == 1 ? " mi" : "km";
	}
	theobj.onlinetime = onlinetime;
	theicon[lasticon++] = theobj;
}
function j(onlinetime,c,mid,param) {
	var theobj = savedicon[c];
	if (typeof mid != "undefined" && mid != theobj.memberid && errcount < 1) {
		if (debugdata) {
			if (errcount == 0) {
				var midlist = "*"+c+"."+mid+"."+theobj.memberid+" ";
				for (var k = 0; k < savedicon.length; k++) {
					midlist += savedicon[k].memberid+" ";
				}
				new Image().src = "setwhosonline.cfm?index="+aindex+"&midlist="+encodeURIComponent(midlist);
				aindex++;
				var midlist = "SoFar";
				for (var k = 0; k < theicon.length; k++) {
					midlist += theicon[k].memberid+" ";
				}
				new Image().src = "setwhosonline.cfm?index="+aindex+"&midlist="+encodeURIComponent(midlist);
				aindex++;
			}
			myalert("At "+c+" Looking for "+mid+" but found "+theobj.memberid+": "+param);
		}
		else {
			//location = "whosonlinephotos.cfm?resync=1&midlist="+midlist;
		}
		errcount++;
	}
	priorname = theobj.firstname;
	theobj.nameorder = lasticon;
	theobj.onlinetime = onlinetime;
	if (theobj.newguy && onlinetime > newdur) {
		var thehtml = theobj.thehtml.replace(/<span class=warn>/,"");
		theobj.thehtml = thehtml.replace(/<\/span>/,"");
		theobj.newguy = 0;
	}
	theicon[lasticon++] = theobj;
}
var downloadtime;
function refreshdatasub(err,param,str) {
	if (!err) {
		if (str.substring(0,6) == "// AOK") {
			jslen = str.length;
			downloadinfo = "Download: "+str.length+" bytes in "+(new Date().getTime()-downloadtime)+"ms; ";
			savedicon = new Array();
			for (var r = 0; r < theicon.length; r++) savedicon[r] = theicon[r];
			savedicon.sort(comparename);
			theicon = new Array();
			eval(str);
			savedicon = new Array();
			refreshwhosonline(refreshtype);
			if (refreshinterval > 0) {
				clearTimeout(thetimeout);
				thetimeout = setTimeout(refreshpage,refreshinterval*1000*(1+whosonlineslowdown));
			}
		}
		else location = "whosonlinephotos.cfm";
	}
}
function refreshdata() {
	showhidelayer("loadingicon",1);
	downloadtime = new Date().getTime();
	geturl("whosonlinerefresh.cfm?whosonlineexpire="+encodeURIComponent(whosonlineexpire)+"&jsdataversion="+jsdataversion,refreshdatasub,0,0)
}
