// Instant Messenger
// im_preferences.js
function IM_Preferences()
{
this.save_vcard = "";
this.jid = false;
this.im_timer_xa = "";
}
/*
* Add
*/
IM_Preferences.prototype.Add_userLoad = function()
{
Templates.add_user();
this.SearchOrganizationsLdap();
var select_contact = document.getElementById('ImSelAddContact');
select_contact.innerHTML = "";
}
IM_Preferences.prototype.SearchOrganizationsLdap = function()
{
var handler_organizations = function(data)
{
var data = func.interface(data);
var select_org = document.getElementById('ImSelOrg');
if(select_org.options.length > 0)
select_org.options.splice(0,select_org.options.length);
var options = new Option("--Selecione -- ", -1, false, true);
select_org[select_org.length] = options;
for(var i in data){
var options = new Option(data[i], data[i], false, true);
select_org[select_org.length] = options;
}
select_org[0].selected = true;
}
XMLTools.request('$this.ldap_im.list_organizations_ldap','GET',handler_organizations);
}
IM_Preferences.prototype.SearchContacts = function()
{
var select_org = document.getElementById('ImSelOrg');
var organization = "";
var span_load = document.getElementById('_span_load_im');
span_load.style.display = "block";
for(var i=0; i < select_org.options.length; i++){
if(select_org.options[i].selected == true){
organization = select_org.options[i].value;
}
}
var handler_contacts = function(XMLdata)
{
var data = func.interface(XMLdata);
var select_contact = document.getElementById('ImSelAddContact');
var text0 = document.getElementById('text0');
var text1 = document.getElementById('text1');
var content = "";
select_contact.innerHTML = "";
text0.value = "";
text1.value = "";
if(data)
{
for(var i in data)
{
var options = new Option(data[i].cn + " ( "+ data[i].mail + " )", data[i].mail + ";" + data[i].uid, false, false);
select_contact[select_contact.length] = options;
}
}
span_load.style.display = "none";
}
XMLTools.request('$this.contacts_im.list_contacts','POST',handler_contacts, 'ou='+organization);
}
IM_Preferences.prototype.preenche = function()
{
var select = document.getElementById('ImSelAddContact');
if(select.options.length > 0){
for(var i=0; i < select.options.length; i++){
if(select.options[i].selected == true){
var email = select.options[i].value.substr(0,select.options[i].value.indexOf(';'));
var uid = select.options[i].value.substr((select.options[i].value.indexOf(';')+1),select.options[i].value.length);
document.getElementById('text0').value = email;
document.getElementById('text1').value = select.options[i].value.substr(0,select.options[i].value.indexOf('@'));
document.getElementById('text2').focus();
document.getElementById('text3').value = uid;
}
}
}
}
IM_Preferences.prototype.Add_user = function(pFunc)
{
var text0 = document.getElementById('text0');
var text1 = document.getElementById('text1');
var text2 = document.getElementById('text2');
var text3 = ( document.getElementById('text3') != null ) ? document.getElementById('text3') : false;
var _this = this;
var jid = text0.value;
var subscription = "";
if(func.trim(text0.value) != "" && func.trim(text1.value) != "" && func.trim(text2.value) != "")
{
var handler_add_contact = function(data)
{
IM.vcards = false;
IM.Vcard();
text0.value = "";
text1.value = "";
text2.value = "";
if(pFunc != 'divAddUser_im')
{
if(document.getElementById('im_cell_' + jid))
{
var div_jid = document.getElementById('im_cell_' + jid);
div_jid.parentNode.removeChild(div_jid);
for(var i in IM.UsersNot)
if( IM.UsersNot[i] === jid )
IM.UsersNot.splice(i,1);
im_win.windowClose(pFunc);
Subscription.subscribed(jid);
IM.setStatus(statusFlag);
setTimeout('Subscription.subscribe("'+jid+'")',2000);
}
}
if( IM.UsersNot.length == 0 )
im_win.windowClose('divFormAuthUser_im');
}
var name = text1.value;
var group = text2.value;
if(text3){
var email = text3.value + "@" + text0.value.substr((text0.value.indexOf('@')+1),text0.value.length);
}else{
var email = text0.value;
}
XMLTools.request('$this.Ujabber.addContacts','POST',handler_add_contact,"email="+email+"&name="+name+"&group="+group);
}else{
alert("Informe todos os campos!");
}
}
IM_Preferences.prototype.AcceptRequestUser = function(pJid)
{
Templates.AcceptRequestUser();
document.getElementById('text0').value = pJid;
document.getElementById('text1').value = pJid.substr(0,pJid.indexOf('@'));
document.getElementById('text1').focus();
document.getElementById('text1').select();
document.getElementById('span0').innerHTML = pJid.substr(0,pJid.indexOf('@'));
}
/*
* Remove
*/
IM_Preferences.prototype.RemoveContact = function(pJid)
{
var _this = this;
if(!pJid)
{
var sel_box = document.getElementById('sel_contact');
for(var i=0; i < sel_box.options.length; i++)
{
if(sel_box[i].selected && sel_box.options[i].value != 0)
pJid = sel_box.options[i].value;
}
}
if( pJid )
{
if( confirm('Deseja remover o contato : ' + pJid) )
{
IM.vcards = false;
IM.Vcard();
var div_child = document.getElementById(pJid);
div_child.parentNode.removeChild(div_child);
_this.ComboRemoveUser(_this.LoadJids());
var handler_remove_contact = function(data)
{
};
XMLTools.request('$this.Ujabber.removeContact','POST',handler_remove_contact,"jid="+pJid);
}
}else
alert('Opção inválida !!');
}
IM_Preferences.prototype.Remove_userLoad = function()
{
Templates.remove_user();
var div_allg = document.getElementById('all_groups');
var _this = this;
_this.ComboRemoveUser(_this.LoadJids());
}
IM_Preferences.prototype.ComboRemoveUser = function(pJids)
{
var sel_box = document.getElementById('sel_contact');
if( document.getElementById('sel_contact') != null )
{
if( sel_box.options.length > 0 )
{
for(var i=0; i < sel_box.options.length; i++)
{
sel_box.options[i] = null;
i--;
}
}
var opt = new Option('--- Selecione ---',0,false,true);
sel_box[sel_box.length] = opt;
for(var i in pJids)
{
var opt = new Option(pJids[i], pJids[i], false, true);
sel_box[sel_box.length] = opt;
}
sel_box[0].selected = true;
}
}
IM_Preferences.prototype.DenyRequestUser = function(pJid)
{
if(confirm('Deseja excluir ' + pJid + '?'))
{
var div_jid = document.getElementById('im_cell_' + pJid);
div_jid.parentNode.removeChild(div_jid);
for(var i in IM.UsersNot)
if(IM.UsersNot[i] === pJid)
IM.UsersNot.splice(i,1);
if( IM.UsersNot.length == 0 )
im_win.windowClose('divFormAuthUser_im');
var handler_denyrequest = function(data)
{
};
XMLTools.request('$this.Ujabber.unsubscribe','POST',handler_denyrequest,'jid='+pJid);
}
}
/*
* LoadJids
*/
IM_Preferences.prototype.LoadJids = function()
{
var div_allg = document.getElementById('all_groups');
var elements = div_allg.firstChild;
var jids = [];
while ( elements )
{
var childs = elements.firstChild;
while( childs )
{
if(childs.tagName == "DIV")
jids[jids.length] = childs.id;
childs = childs.nextSibling;
}
elements = elements.nextSibling;
}
jids.sort();
return jids;
}
/*
* Update
*/
IM_Preferences.prototype.Update_ = function(pJid,pType)
{
var div_child = document.getElementById(pJid);
var group = div_child.parentNode.id;
var name = div_child.firstChild;
name = name.nextSibling.nodeValue;
if(pType)
{
var action = prompt('Digite um novo apelido para : ' + pJid, " ");
name = (action == "Apelido") ? "" : action ;
}else{
var action = prompt('Digite um novo grupo para : ' + pJid , " ");
group = action ;
}
if( action != "" && action != null)
{
var handler_update_contact = function(XMLdata)
{
};
XMLTools.request('$this.Ujabber.updateContact','POST',handler_update_contact,"jid="+pJid+"&name="+name+"&group="+group);
}
}
IM_Preferences.prototype.Update_Group = function(pJid)
{
this.Update_(pJid,false);
}
IM_Preferences.prototype.Update_NickName = function(pJid)
{
this.Update_(pJid,true);
}
/*
* Vcard
*/
IM_Preferences.prototype.vCardInit = function()
{
var _this = this;
var nickname = "";
var message = "";
var vcards = "";
var handler_getId = function(_XMLdata)
{
var jid = _XMLdata.getElementsByTagName("retorno").item(0);
jid = jid.firstChild.nodeValue;
_this.jid = jid;
if((vcards = _this.vCardLoad(jid)))
{
nickname = vcards.substr(vcards.indexOf('NICKNAME'),vcards.length);
nickname = nickname.substr(0,nickname.indexOf('|'));
nickname = nickname.substr(nickname.indexOf(':') + 1 , nickname.length);
message = vcards.substr(vcards.indexOf('DESC'),vcards.length);
message = message.substr(0,message.indexOf('|'));
message = message.substr(message.indexOf(':') + 1 , message.length);
}
nickname = (func.trim(nickname) != "" ) ? nickname : _this.jid ;
message = (func.trim(message) != "" ) ? message : "Sua Mensagem";
var span_nick = document.getElementById('span_nickname');
span_nick.innerHTML = ( nickname ) ? " " + decodeURI(nickname.substring(0,15)) + "" : " " + IM.get_lang('Nickname') + "";
var span_mess = document.getElementById('span_message');
span_mess.innerHTML = ( message ) ? " " + decodeURI(message.substring(0,20)) + "" : " " + IM.get_lang('Status') + "";
}
XMLTools.request('$this.Ujabber.getJid','GET',handler_getId);
}
IM_Preferences.prototype.vCardLoad = function(Jid)
{
var div_jid = document.getElementById("vcard_" + Jid);
var strg = div_jid.innerHTML;
var elements = div_jid.firstChild;
var exp , tag = '';
while ( elements )
{
if(elements.tagName != undefined && elements.tagName.substr(0,1) != "/" )
{
tag = elements.tagName;
exp = eval('/<'+tag+'>/gi');
strg = strg.replace(exp, tag + " : " );
exp = eval('/<\\/'+tag+'>/gi');
strg = strg.replace(exp, " | ");
}
elements = elements.nextSibling;
}
return strg;
}
IM_Preferences.prototype.vCard = function()
{
if(!this.jid){
alert('Carregando !!');
}else{
var div_jid = document.getElementById("vcard_" + this.jid);
var strg = div_jid.innerHTML;
Templates.information_user(strg);
}
}
IM_Preferences.prototype.vCardLoad_Contact = function(pJid)
{
if(!this.jid){
alert('Carregando !!');
}else{
if(document.getElementById('vcard_' + pJid) != null)
{
var vcards = document.getElementById('vcard_' + pJid);
Templates.information_Contact(vcards.innerHTML);
}else
alert('Vcard não carregado!');
}
}
IM_Preferences.prototype.vCardAdd = function()
{
var Nvcard = new Array('FN','NICKNAME','ORGNAME','ORGUNIT','ROLE','BDAY','DESC');
var vcard = "";
var value = "";
for(var i in Nvcard)
{
if( Nvcard[i] === 'NICKNAME' )
{
var nn = ( func.trim(document.getElementById(Nvcard[i]).value) != "") ? document.getElementById(Nvcard[i]).value : this.jid;
document.getElementById('span_nickname').innerHTML = " " + nn.substring(0,15) + "";
}
if( Nvcard[i] === 'DESC' )
{
document.getElementById('span_message').innerHTML = " " + document.getElementById(Nvcard[i]).value.substring(0,20) + "...";
}
value = document.getElementById(Nvcard[i]).value;
value = (func.trim(value) != "") ? value : "";
vcard += value + "_vkrd_";
}
vcard = vcard.substr(0,vcard.length - 6 );
var handler_vcard_add = function(XMLdata)
{
};
XMLTools.request('$this.Ujabber.newVcard','POST',handler_vcard_add,"vcard="+escape(vcard));
alert('Informações Cadastradas / Adicionadas !!!');
}
/*
* IM_Preferences
*/
IM_Preferences.prototype.yourPreferences = function()
{
Templates.yourPreferences();
}
IM_Preferences.prototype.SetYourPreferences = function()
{
var preferences = "";
preferences = ( func.byId('ch_offline').checked ) ? 'ch_offline:true;' : 'ch_offline:false;';
if( func.byId('in_time').value )
{
var numbers = /^\d+$/;
if(numbers.test(func.byId('in_time').value) && func.byId('in_time').value != "0")
var minutes = func.byId('in_time').value;
else
{
alert('Informe valores válidos !');
func.byId('in_time').focus();
return false;
}
}
this.im_timer_xa = func.byId('in_time').value;
preferences += ( func.trim(func.byId('in_time').value )) ? 'in_time:' + func.byId('in_time').value + ";" : 'in_time:0;';
preferences += ( func.byId('rd_nm').checked ) ? 'rd_nm:true;' : 'rd_nm:false;';
preferences += ( func.byId('rd_ch').checked ) ? 'rd_ch:true' : 'rd_ch:false';
IM.userPrefe = preferences;
var handler_preferences = function(XMLdata)
{
var data = XMLdata.getElementsByTagName('retorno').item(0);
data = eval(data.firstChild.nodeValue);
if(data)
{
IM.Contacts_Offline();
alert('Preferências salvas !');
}
};
XMLTools.request('$this.db_im.set_preferences','POST',handler_preferences,'preferences='+preferences);
}
IM_Preferences.prototype.GetYourPreferences = function()
{
var _this = this;
if(!IM.userPrefe)
{
var handler_getPreferences = function(XMLdata)
{
var data = XMLdata.getElementsByTagName('retorno').item(0);
data = data.firstChild.nodeValue;
IM.userPrefe = data;
_this.TimerXa(_this.LoadPreferences('in_time'));
}
XMLTools.request('$this.db_im.get_preferences','GET',handler_getPreferences);
}
}
IM_Preferences.prototype.LoadPreferences = function(pElement)
{
var preferences_im = IM.userPrefe;
while( preferences_im !== "")
{
var values = "";
var value = "";
if(value = preferences_im.substr(0,preferences_im.indexOf(';')))
{
values = value + '\n';
preferences_im = preferences_im.replace(value,'');
}
if(preferences_im.substr(0,1) == ";"){
preferences_im = preferences_im.substr(1,preferences_im.length);
}else{
values = preferences_im + '\n';
preferences_im = '';
}
var ele = values.substr(0,values.indexOf(':'));
var val = values.substr((values.indexOf(':')+1),values.length);
if(ele == pElement)
return val;
if((document.getElementById(ele)!= null) && (document.getElementById(ele).type == 'text'))
document.getElementById(ele).value = val;
else
if(document.getElementById(ele) != null)
document.getElementById(ele).checked = eval(val);
}
}
/*
* Estado de Ausente Automático
*/
IM_Preferences.prototype.TimerXa = function(pTimer)
{
this.im_timer_xa = pTimer;
_awayTimer = setTimeout("IM.setAway()", parseInt(pTimer) * 60000 );
}
/*
* Detalhes do Contato para Autorização
*/
IM_Preferences.prototype.Info_Contact = function(pJid)
{
var element = "_span_not_" + pJid;
var uid = pJid.substr(0,pJid.indexOf('@'));
var handler_ldap = function(data)
{
var data = func.interface(data);
var XMLinfoLdap = "";
if(data)
{
XMLinfoLdap = '';
for(var i in data)
XMLinfoLdap += ' ';
XMLinfoLdap += '';
}
im_menu_action.menu(element, parse_XmlXsl(XMLinfoLdap,'info_contact_ldap.xsl'));
if(document.getElementById('im_span_avatar') != null)
{
var span = document.getElementById('im_span_avatar');
var img = func.newEl('img');
func.confEl(img, 'id', pJid + '_avatar');
func.confEl(img, 'src', img_photo.src);
func.confEl(img, 'style', 'height:65px;width:60px;align:right;');
span.appendChild(img);
// Foto Ldap;
im_win.get_photo_ldap(pJid);
}
}
XMLTools.request('$this.ldap_im.info_user','POST',handler_ldap,'uid='+uid);
}
// Build Object
var IM_Preferences = new IM_Preferences();