
Array.prototype.indexOf = function(obj)
{
  for (var i = 0; i < this.length; i++)
	{
    if (this[i] == obj)
      return i;
  }
  return -1;
}

//-------------------------------------------------------------------------------------------
// Prepare gettext translation
//-------------------------------------------------------------------------------------------
if (!(json_locale_data == null )) {
//var gt = new Gettext({ domain : 'myDomain', locale_data : json_locale_data});
  var gt = new Gettext({locale_data : json_locale_data});
}	

function _(str) {
  if (gt != null) {
    return gt.gettext(str);
	} else {
    return str;
	}
}	

//-------------------------------------------------------------------------------------------

function trim(str) {
  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}	

//-------------------------------------------------------------------------------------------

function GetDomainFromAddress(address) {
  var s = address.split('@');
	var domain = s[s.length - 1];
  return domain;
}

//-------------------------------------------------------------------------------------------

function ShowPanelContent(Panel)
{
	Panel.children('.PanelContent').show();
}	

//-------------------------------------------------------------------------------------------

function HidePanelContent(Panel)
{
	Panel.children('.PanelContent').hide();
}		

//-------------------------------------------------------------------------------------------

function DownloadFile(url)
{	
  if (jQuery('#dlcontainer').length == 0) {
    jQuery('body').append('<div id="dlcontainer"></div>"');
		jQuery('#dlcontainer').hide();
    jQuery('#dlcontainer').append('<iframe id="dliframe"/>');
	}
  jQuery('#dliframe').attr('src', url); 
}

//-------------------------------------------------------------------------------------------

function SetWindowKeyPressHandler(fn)
{
	jQuery(window).unbind('keydown');
	jQuery(window).keydown(fn);
}

//-------------------------------------------------------------------------------------------

function bytesToSize(bytes, precision)
{	
	var kilobyte = 1024;
	var megabyte = kilobyte * 1024;
	var gigabyte = megabyte * 1024;
	var terabyte = gigabyte * 1024;
	
	if ((bytes >= 0) && (bytes < kilobyte)) {
		return bytes + ' B';

	} else if ((bytes >= kilobyte) && (bytes < megabyte)) {
		return (bytes / kilobyte).toFixed(precision) + ' KB';

	} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
		return (bytes / megabyte).toFixed(precision) + ' MB';

	} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
		return (bytes / gigabyte).toFixed(precision) + ' GB';

	} else if (bytes >= terabyte) {
		return (bytes / terabyte).toFixed(precision) + ' TB';

	} else {
		return bytes + ' B';
	}
}

//-------------------------------------------------------------------------------------------

function byteFormater(cellvalue, options, rowObject)
{
   return bytesToSize(cellvalue, 2);
}

//-------------------------------------------------------------------------------------------

function selectOptionByValue(selObj, val){
  var A = selObj.options
	var L= A.length;

  while(L){
    if (A[--L].value== val){
      selObj.selectedIndex= L;
      L= 0;
    }
	}
}

//-------------------------------------------------------------------------------------------

function GetProfileByID(Profiles, ID)
{
  for (var i = 0; i < Profiles.length; i++) {
		if (Profiles[i].ID == ID) {
  		return Profiles[i];
  		break;
		}
	}
	return null;
}

//-------------------------------------------------------------------------------------------

function GetUserByID(Users, ID)
{
  for (var i = 0; i < Users.length; i++) {
		if (Users[i].ID == ID) {
  		return Users[i];
  		break;
		}
	}
	return null;
}

//-------------------------------------------------------------------------------------------

function GetLocalRecipientByID(LocalRecipients, ID)
{
  for (var i = 0; i < LocalRecipients.length; i++) {
		if (LocalRecipients[i].ID == ID) {
  		return LocalRecipients[i];
  		break;
		}
	}
	return null;
}

//-------------------------------------------------------------------------------------------

function GetSeperatedText(theArray, sep)
{
  var s = '';

	for (var i=0 ; i < theArray.length ; i++) {
		if (s == '') {
			s = theArray[i]; 
		} else {
			s = s + sep + theArray[i]; 
		}
	}
	
	return s;
}

//-------------------------------------------------------------------------------------------

function SortUserListByUserName(a, b) {
    var x = a.Username.toLowerCase();
    var y = b.Username.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

//-------------------------------------------------------------------------------------------

function BuildDeputyDropDown(dropDownControl)
{
	// Remove all options
	while (dropDownControl.hasChildNodes()) {
		dropDownControl.removeChild(dropDownControl.firstChild);
	}

	var dummy = document.createElement("option");
	dummy.value = 'none';
	dummy.appendChild(document.createTextNode(_('None')));
	dummy.selected = true;
	dropDownControl.appendChild(dummy);

	// User based deputies
	if (Session.UserDeputies != null) {
		var deputies = document.createElement("optgroup");
		deputies.label = _('Deputies');

		for (i = 0; i < Session.UserDeputies.length; i++) {
			var deputy = document.createElement("option"); 
			deputy.value = Session.UserDeputies[i];
			deputy.appendChild(document.createTextNode(Session.UserDeputies[i]));
			deputies.appendChild(deputy);
		}
  	dropDownControl.appendChild(deputies);
	}

	// Policy based deputies
	if (Session.GroupPolicy != null) {
		for (i = 0; i < Session.GroupPolicy.DeputyGroups.length; i++) {
			var deputyGroup = document.createElement("optgroup");
			deputyGroup.label = Session.GroupPolicy.DeputyGroups[i].Name;
			
			for (j = 0; j < Session.GroupPolicy.DeputyGroups[i].Deputies.length; j++) {
				var deputy = document.createElement("option"); 
				deputy.value = Session.GroupPolicy.DeputyGroups[i].Deputies[j];
				deputy.appendChild(document.createTextNode(Session.GroupPolicy.DeputyGroups[i].Deputies[j]));
				deputyGroup.appendChild(deputy);
			}

			dropDownControl.appendChild(deputyGroup);
		} 
	}
}