// $Id: jabber.js 1103 2008-05-09 10:06:09Z dmf $

function isInRoster () {
	return (inRoster);
}
	
function isBuddyPending () {
	return (isInRoster() && rosterStatus == 'pending');
}

function hasBuddyAccepted () {
	return (isInRoster() && rosterStatus == 'accepted');
}

function hasBuddyDeclined () {
	return (isInRoster() && rosterStatus == 'declined');
}

function isBuddyOnline () {
	return (hasBuddyAccepted() && chatLoggedIn);
}

// opens the main chat window
function startChatting (href, skipChecks) {
	var openWindow = true;
	
	if (skipChecks == null || !skipChecks) {
		if (!isInRoster()) {
			openWindow = confirm(copyItems["err_chatBuddyNotInRoster"]);
		} else if (isBuddyPending()) {
			openWindow = confirm(copyItems["err_chatBuddyPending"]);
		} else if (hasBuddyDeclined()) {
			openWindow = confirm(copyItems["err_chatBuddyDeclined"]);
		} else if (!isBuddyOnline()) {
			openWindow = confirm(copyItems["err_chatBuddyNotOnline"]);
		}
	}

	if (openWindow) {
		window.open(href, 'chatwin', 'width=350,height=560,resizable=yes,status=no');
	}
	
	return false;  // prevent anchor being followed
}

// --- templateVersion 1 stuff below onwards (some no longer used)

function BrowserCheck () {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else if (b=="Opera") this.b = "op"
	else this.b = b
	this.v = parseInt(navigator.appVersion)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.op = (this.b=="op" && this.v>=7)
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0)
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0)
	this.ie6 = (navigator.userAgent.indexOf('MSIE 6')>0)
	this.ie55 = (navigator.userAgent.indexOf('MSIE 5.5')>0)
	if (this.ie55) {
		this.v = 5.5;
		this.ie5 = false;
	}
	this.mac = (navigator.userAgent.indexOf('Mac')>0);
	if (this.ie5) this.v = 5
	this.min = (this.ns||this.ie)
}

// automatically create the "is" object
is = new BrowserCheck ()

function rosterAddUser (who, me, languageID, siteID) {
document.getElementById("rosterAddBox").innerHTML = "<B style=\"color: red\">" + sendingRequest + "</B>";
	
var xmlhttp=false;
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}

xmlhttp.open("GET", "/s/chat/rosterAdd.php?l="+languageID+"&s="+siteID+"&uid="+who+"&mid="+me,true);
xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.responseText != "invalid") {
			document.getElementById("rosterAddBox").innerHTML = "<span style=\"color: green\">" + xmlhttp.responseText + "</span>"
		} else {
			document.getElementById("rosterAddBox").innerHTML = "error: invalid"
		}
	}
}
xmlhttp.send(null);
}

function removeBuddy (which, layer, row) {
confText = deleted;

if (layer == 'myroster') {
	document.getElementById("boxdel"+which).innerHTML = "Deleted";
	restore = document.getElementById(layer).innerHTML;
	if (is.ns) {
	document.getElementById("delrow"+which).innerHTML = "<td align=\"center\" colspan=\"5\" style=\"background-color: #efefef; color: red\">" + briefMoment + "</td>";
	}
	ctable = document.getElementById("myroster").innerHTML;
	ctable = ctable.replace(new RegExp(/<a/g),"<!a");
	document.getElementById("myroster").innerHTML = ctable;
} else {
	document.getElementById(layer).innerHTML = "<B style=\"color: red\">" + sendingRequest + "</B>";
}

var xmlhttp=false;
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}
 
xmlhttp.open("GET", "/s/chat/rosterDelete.php?w="+which,true);
xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4) {
		if (layer == 'myroster') {
		document.getElementById(layer).innerHTML = restore;
		} else {
		document.getElementById(layer).innerHTML = xmlhttp.responseText + removeBuddyTxt + "&raquo;</a>";;
		}
	}
}
xmlhttp.send(null);
}


var confText = "";

function declineRequest (which, mid) {
	confText = declined;
	loadHTML("/s/chat/rosterDecline.php?rid="+which, "box"+mid);
	requestsHTML = document.getElementById("requests").innerHTML;
}

var requestsHTML;

/* ************************** */
function tableInnerHTML (target, table, rowHTML) {
	var tempDiv = document.createElement("div");
	document.body.appendChild(tempDiv);
	tempDiv.innerHTML = '' + rowHTML + '';
	var tt = document.getElementById(table);

/* Copy temporary table’s rows to target */
	for (var i = 0; i < tt.rows.length; i++) {
	target.appendChild(tt.rows[i].cloneNode(true));
	}
	//tt.parentNode.removeChild(tt);
}

function acceptRequest (which, mid, row) {
	confText = accepted;
	
	destinationDiv = document.getElementById("box"+mid);
	destinationDiv.innerHTML = confText;
//	accRoster = document.getElementById("requests");
	originalTable = document.getElementById("requests").innerHTML;
	if (is.ns) {
	document.getElementById("r"+row).innerHTML="<td align=\"center\" colspan=\"4\" style=\"background-color: #efefef; color: red\">" + waitAccepting + "</td>";
	}
	
//	tableInnerHTML(document.getElementById("r"+row), "requestsTable", "<td align=\"center\" colspan=\"4\" style=\"background-color: #efefef; color: red\">" + waitAccepting + "</td>");
	
	table = document.getElementById("requests").innerHTML;
	table = table.replace(new RegExp(/<a/g),"<!a");
	table = table.replace(new RegExp(/<a/g),"<!a");
	table = table.replace(new RegExp(/<tr/g),"<tr style=\"color:#dedede\"");
	document.getElementById("requests").innerHTML = table;
	
	if (document.getElementById("myroster")) {
		originalRoster = document.getElementById("myroster").innerHTML;
		roster = originalRoster;
		roster = roster.replace(new RegExp(/onClick\=\"removeBuddy/g),"onClick=\"alert('almost done...')\" xonClick=\"removeBuddy\"");
		document.getElementById("myroster").innerHTML = roster;
	}
	
	loadHTML("/s/chat/rosterAllow.php?rid="+which, "box"+mid);
}

var rosterHTML;
function deleteUser (which) {
	//loadHTML("/s/chat/rosterDelete.php?rid="+which, "box"+mid);
	requestsHTML = document.getElementById("myroster").innerHTML;
	document.getElementById("myroster").innerHTML = '<table width="80%" cellpadding="0" cellspacing="0" border="0" class="boxOuterTbl"><tr><td class="boxOuter"><table width="100%" cellpadding="20" cellspacing="1" border="0"><tr class="subsRow"><td align="center" colspan="4"><br><strong>' + rosterWait + '</strong><br>&nbsp;</td></tr></table>';
}

//function addUser(mid) {
//	loadHTML("/s/chat/reg.php?mid="+mid,'na');
//}

var dest;

function loadHTML (url, destination) {
	dest = destination;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		if (destination != 'na') {
//			destinationDiv = document.getElementById(destination);
//			destinationDiv.innerHTML = confText;
		}
		req.open("GET", url, true);
		req.send(null);
		
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			if (destination != 'na') {
//				destinationDiv = document.getElementById(destination);
//				destinationDiv.innerHTML = confText;
			}
			req.open("GET", url, true);
			req.send();
			}
		}
		
		req.onreadystatechange=function() {
		if (req.readyState==4) {
		document.getElementById("requests").innerHTML = originalTable;
		//document.getElementById("myroster").innerHTML = accRoster;
		//destinationDiv = document.getElementById(destination);
		//destinationDiv.innerHTML = confText;
		}
	}
}
