///////////////////////////////////
//         ajax im 3.41          //
//    AJAX Instant Messenger     //
//   Copyright (c) 2006-2008     //
//    http://www.ajaxim.com/     //
//   Do not remove this notice   //
///////////////////////////////////
/**
* Object to hold all Admin Windows as variables
*
* @author Joshua Gross
**/
var SearchWindow = {
/**
* Display window to allow admin to search for users
*
* @author Joshua Gross
**/
userSearch: function() {
var userSearchWin;
if($('userSearch')) {
Windows.getWindow('userSearch').toFront();
setTimeout("try {$('search').focus();} catch(e){}", 250);
return;
}
userSearchWin = new Window({id: 'userSearch', className: "dialog", width: 250, height: 120, resizable: false,
title: Languages.get('admin-userSearch'), draggable: true, closable: true, maximizable: false, minimizable: true, detachable: false,
minWidth: 250, minHeight: 110, showEffectOptions: {duration: 0}, hideEffectOptions: {duration: 0}});
userSearchWin.setConstraint(true, {left: 0, right: 0, top: 0, bottom: 0});
userSearchWin.getContent().innerHTML = '<div class="dialog_info" style="padding:3px;">' + Languages.get('searchInfo') + '</div> \
<div id="userSearchBox"> \
<div style="display:block;float:left;margin-right:46px;padding:4px 0 0 5px;">' + Languages.get('search') + ':</div> \
<input type="text" id="search" name="search" maxlength="32" style="width:110px;" onkeypress="handleInput(event, function() { Search.findUser($(\'search\').value, $(\'searchNick\').checked, $(\'searchFN\').checked, $(\'searchLN\').checked); })" /> \
<div id="searchFilter">\
<input type="checkbox" id="searchNick" checked="checked"><label for="searchNick">'+Languages.get('nickname')+'</label>\
<input type="checkbox" id="searchFN"><label for="searchFN">'+Languages.get('firstName')+'</label>\
<input type="checkbox" id="searchLN"><label for="searchLN">'+Languages.get('lastName')+'</label>\
</div> \
<div id="searchButtons">' +
ButtonCtl.create(Languages.get('search'), 'Search.findUser($(\'search\').value, $(\'searchNick\').checked, $(\'searchFN\').checked, $(\'searchLN\').checked);') +
ButtonCtl.create(Languages.get('cancel'), 'Windows.close(\'userSearch\');') +
'</div>';
$('searchButtons').setStyle({position: 'absolute',
top:      '115px',
left:     '32px'});
userSearchWin.setDestroyOnClose();
userSearchWin.showCenter();
try{ this.windows["userSearch"].toFront(); } catch (e) {};
Windows.focusedWindow = userSearchWin;
Windows.addObserver({ onFocus: SearchWindow.handleFocus });
setTimeout("try {$('search').focus();} catch(e){}", 250);
},
handleFocus: function(eventName, win) {
if (win.getId()!="userSearch") return;
try { $('search').focus(); } catch(e) {}
},
/**
* On admin window resize, fix elements within window
*
* @arguments
*   win - window to be resized
*
* @author Joshua Gross
**/
handleResize: function(win) {
if (win.getId() == 'userSearch') {
if($('userSearchResults')) {
$('userSearchResults').setStyle({'width': win.getSize()['width'] + 'px'});
$('userSearchResults').parentNode.setStyle({'width': win.getSize()['width'] + 'px'});
$('userExecFunctions').setStyle({'left': ((win.getSize()['width'] - $('userExecFunctions').getWidth()) / 2) + 'px'});
}
$('searchButtons').setStyle({'left': ((win.getSize()['width'] - $('searchButtons').getWidth()) / 2) + 'px'});
}
}
}
/**
* Handle all Admin requests
*
* @author Joshua Gross
**/
var Search = {
// current selected user from the user-search-results list
selectedUser: null,
/**
* Finds and displays users searched for by the admin
*
* @author Joshua Gross
* @update Benjamin Hutchins
*    - User's email be displayed.
**/
findUser: function(search, searchByNick, searchByFN, searchByLN) {
if (!searchByNick && !searchByFN && !searchByLN) {
searchByNick = true;
}
IMService.search(search, searchByNick, searchByFN, searchByLN, function(response) {
if($('userSearchResults'))
$('userSearchResults').parentNode.parentNode.removeChild($('userSearchResults').parentNode);
var results = response.data;
var resultsTable = '<table id="userSearchResults" style="text-align:center; width:100%;" class="listNotSelected">';
resultsTable += '<thead><tr style="cursor:pointer;"><th>' + Languages.get('nickname') + '</th><th>' + Languages.get('firstName') + '</th><th>' + Languages.get('lastName') + '</th></tr></thead><tbody>';
for(var i=0; i<results.length; i++) {
resultsTable += '<tr style="cursor:pointer;" onmouseover="Search.findUserListHover(this);" onmouseout="Search.findUserListDefault(this);" onclick="Search.findUserListSelect(this);" ondblclick="Search.imUser(\''+ results[i].nickName +'\')">' +
'<td>' + results[i].nickName + '</td><td>' + results[i].firstName + '</td><td>' + results[i].lastName + '</td></tr>';
}
resultsTable += '</tbody></table>';
var userSearch = Windows.getWindow('userSearch')
userSearch.setSize(500, 332);
userSearch.options.minWidth = 500;
userSearch.options.minHeight = 332;
userSearch.centered = false;
userSearch.show();
userSearch.getContent().innerHTML += "<div style='width: 500px; height: 220px; overflow-y: auto;'>"+resultsTable+"</div>";
if(!$('userExecFunctions')) {
userSearch.getContent().innerHTML += '<div id="userExecFunctions">' +
ButtonCtl.create(Languages.get('add'), 'Search.addContact();') +
ButtonCtl.create(Languages.get('imBuddy'), 'Search.imUser();') +
ButtonCtl.create(Languages.get('callBuddy'), 'Search.call();') +
'</div>';
$('userExecFunctions').setStyle({position: 'absolute',
top:      '310px',
left:     '108px'});
}
$('searchButtons').innerHTML = ButtonCtl.create(Languages.get('searchAgain'), 'Search.findUser($(\'search\').value, $(\'searchNick\').checked, $(\'searchFN\').checked, $(\'searchLN\').checked);') +
ButtonCtl.create(Languages.get('cancel'), 'Windows.close(\'userSearch\');');
$('searchButtons').setStyle({position: 'absolute',
top:      '340px',
left:     '155px'});
var t = new SortableTable($('userSearchResults'));
Search.selectedUser = null;
$('searchNick').checked = searchByNick;
$('searchFN').checked = searchByFN;
$('searchLN').checked = searchByLN;
$('search').value = search;
setTimeout("try {$('search').focus();} catch(e){}", 250);
});
},
addContact: function() {
if (!Search.selectedUser) {
return;
}
Dialogs.newBuddy(Search.selectedUser.getElementsByTagName('td')[0].innerHTML);
},
imUser: function() {
if (!Search.selectedUser) {
return;
}
var name = Search.selectedUser.getElementsByTagName('td')[0].innerHTML;
var winId = null;
try { winId = window['IM'].windows[name].getId(); } catch(e) { };
if(!$(winId)) {
try { window['IM'].create(name, name); } catch(e) { };
} else {
if(!window['IM'].windows[name].detached && !window['IM'].windows[name].isVisible()) {
window['IM'].windows[name].show();
setTimeout("try{ scrollToBottom('" + window['IM'].windows[who].getId() + "_rcvd'); } catch(e){}", 1000);
}
}
},
call: function() {
if (!Search.selectedUser) {
return;
}
BlazeDS.initCall(Search.selectedUser.getElementsByTagName('td')[0].innerHTML);
},
/**
* Add hover effect to an element
*
* @arguments
*   el - element to have effect added
*
* @author Joshua Gross
**/
findUserListHover: function(el) {
Element.addClassName(el, 'listHover').removeClassName('listSelected').removeClassName('listNotSelected');
},
/**
* Remove hover effect added by Admin.findUserListHover
*
* @arguments
*   el - element to have effect removed
*
* @author Joshua Gross
**/
findUserListDefault: function(el) {
if(el != Search.selectedUser) Element.addClassName(el, 'listNotSelected').removeClassName('listSelected').removeClassName('listHover');
else Element.addClassName(el, 'listSelected').removeClassName('listNotSelected').removeClassName('listHover');
},
/**
* Change Admin.selectedUser to the user clicked by the admin
*
* @arguments
*   el - list element to turn into selected
*
* @author Joshua Gross
**/
findUserListSelect: function(el) {
if(Search.selectedUser) Element.addClassName(Search.selectedUser, 'listNotSelected').removeClassName('listSelected').removeClassName('listHover');
Element.addClassName(el, 'listSelected').removeClassName('listNotSelected').removeClassName('listHover');
Search.selectedUser = el;
}
};
/**
*
* Sortable HTML table
* http://www.webtoolkit.info/
*
**/
function SortableTable (tableEl) {
this.tbody = tableEl.getElementsByTagName('tbody');
this.thead = tableEl.getElementsByTagName('thead');
this.tfoot = tableEl.getElementsByTagName('tfoot');
this.getInnerText = function (el) {
if (typeof(el.textContent) != 'undefined') return el.textContent;
if (typeof(el.innerText) != 'undefined') return el.innerText;
if (typeof(el.innerHTML) == 'string') return el.innerHTML.replace(/<[^<>]+>/g,'');
}
this.getParent = function (el, pTagName) {
if (el == null) return null;
else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
return el;
else
return this.getParent(el.parentNode, pTagName);
}
this.sort = function (cell) {
var column = cell.cellIndex;
var itm = this.getInnerText(this.tbody[0].rows[1].cells[column]);
var sortfn = this.sortCaseInsensitive;
if (itm.match(/\d\d[-]+\d\d[-]+\d\d\d\d/)) sortfn = this.sortDate; // date format mm-dd-yyyy
if (itm.replace(/^\s+|\s+$/g,"").match(/^[\d\.]+$/)) sortfn = this.sortNumeric;
this.sortColumnIndex = column;
var newRows = new Array();
for (j = 0; j < this.tbody[0].rows.length; j++) {
newRows[j] = this.tbody[0].rows[j];
}
newRows.sort(sortfn);
if (cell.getAttribute("sortdir") == 'down') {
newRows.reverse();
cell.setAttribute('sortdir','up');
} else {
cell.setAttribute('sortdir','down');
}
for (i=0;i<newRows.length;i++) {
this.tbody[0].appendChild(newRows[i]);
}
}
this.sortCaseInsensitive = function(a,b) {
aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]).toLowerCase();
bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]).toLowerCase();
if (aa==bb) return 0;
if (aa<bb) return -1;
return 1;
}
this.sortDate = function(a,b) {
aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]);
bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]);
date1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
date2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
if (date1==date2) return 0;
if (date1<date2) return -1;
return 1;
}
this.sortNumeric = function(a,b) {
aa = parseFloat(thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]));
if (isNaN(aa)) aa = 0;
bb = parseFloat(thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]));
if (isNaN(bb)) bb = 0;
return aa-bb;
}
// define variables
var thisObject = this;
var sortSection = this.thead;
// constructor actions
if (!(this.tbody && this.tbody[0].rows && this.tbody[0].rows.length > 0)) return;
if (sortSection && sortSection[0].rows && sortSection[0].rows.length > 0) {
var sortRow = sortSection[0].rows[0];
} else {
return;
}
for (var i=0; i<sortRow.cells.length; i++) {
sortRow.cells[i].sTable = this;
sortRow.cells[i].onclick = function () {
this.sTable.sort(this);
return false;
}
}
}
