function useStyledLoadingMessage(message) {
  var loadingMessage;
  if (message) loadingMessage = message;
  else loadingMessage = "Loading";
  
  if ( $('messageZone') ) {
  	$('messageZone').style.top = f_scrollTop()+'px';
  	//possibly unnecessary, an imprecise attempt at horizontal centering:
  	//$('messageZone').style.left = ( parseInt((f_clientWidth() / 2), 10) + f_scrollLeft())+'px';
  }

  DWREngine.setPreHook(function() {
    var disabledZone = $('disabledZone');
    if (!disabledZone) {
      disabledZone = document.createElement('div');
      disabledZone.setAttribute('id', 'disabledZone');
      disabledZone.style.position = "absolute";
      disabledZone.style.zIndex = "1000";
      disabledZone.style.left = "0px";
      disabledZone.style.top = "0px";
      disabledZone.style.width = "100%";
      disabledZone.style.height = "100%";
      //disabledZone.style.cursor = "wait";
      document.body.appendChild(disabledZone);
      var messageZone = document.createElement('div');
      messageZone.setAttribute('id', 'messageZone');
      messageZone.style.position = "absolute";
      messageZone.style.top = "3px";
      messageZone.style.right = "3px";
      messageZone.style.background = "black";
      messageZone.style.color = "white";
      messageZone.style.fontFamily = "Tahoma,Arial,Helvetica,sans-serif";
      messageZone.style.padding = "3px";
      disabledZone.appendChild(messageZone);
      var text = document.createTextNode(loadingMessage);
      messageZone.appendChild(text);
    }
    else {
      $('messageZone').innerHTML = loadingMessage;
      disabledZone.style.visibility = 'visible';
    }
  });

  DWREngine.setPostHook(function() {
    $('disabledZone').style.visibility = 'hidden';
  });
}

function backButtonPageNavigation(){
	var currentPage = location.hash;
   	if(currentPage != "" && currentPage.indexOf("currentPage") != -1){
   		currentPage = currentPage.slice(currentPage.indexOf("currentPage")+11); 
   		for(i=0;i<currentPage.length;i++){
   			if(currentPage.charAt(i)>9||currentPage.charAt(i)<0){
   				currentPage = currentPage.slice(0,i); 
   				break;
   			}
   		}
   		if(currentPage != ""){
   			paging.pageToNavigate = currentPage;
   		}
   	}
}

function updateCurrentPageHash(){
  	location.hash = "currentPage"+paging.pageToNavigate;
}

function DWRErrorHandler(msg) {
  //alert('DWR Error: ' + msg);
}

function DWRWarningHandler(msg) {
  //alert('DWR Warning: ' + msg);
}


function init_paging_base()
{
    
   	useStyledLoadingMessage("Loading... (press Esc to abort)");
   	backButtonPageNavigation();
}

  
function put_data_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_new_contacts_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_visitors_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_favourites_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data), { escapeHtml:false };
  }catch(err){
  }
}

function put_recent_interactions_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_interesting_people_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_dummy_people_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_most_visited_profiles_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_neglected_persons_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_group_banned_contacts_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_my_contacts_in_group_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_group_members_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_second_level_contacts_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_third_level_contacts_in_view(data) {
  try {
  DWRUtil.setValue("inc_dwr", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_data_in_view_online(data) {
  try {
  DWRUtil.setValue("inc_dwr_online", data, { escapeHtml:false });
  }catch(err){
  }
}
function put_data_in_view_recent_files(data) {
  try {
  DWRUtil.setValue("inc_dwr_recent_files", data, { escapeHtml:false });
  }catch(err){
  }
}

function put_data_in_view_blogs(data) {
  try {
  	DWRUtil.setValue("inc_dwr_blogs", data, { escapeHtml:false });
  } catch(err) {
  }
}

function put_data_in_view_new_msg(data) {
	try{
	  DWRUtil.setValue("inc_dwr_new_msg", data, { escapeHtml:false });
	}catch(err){
	}
}

function put_data_in_view_new_msg_direct_cont(data) {
	try{
	  DWRUtil.setValue("inc_dwr_new_msg_direct_cont", data, { escapeHtml:false });
	}catch(err){
	}
}

function put_data_in_view_tag(data) {
  try {
  DWRUtil.setValue("inc_dwr_tag", data, { escapeHtml:false });
  }catch(err){
  }
}

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

/**
*
*  'reliable' window size and scrollbar position
*  http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
*
**/
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
