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

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// 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;
	}

}

var CHECK_LOGIN_TIMER=0;
var poflag = false;

function call_login_box(url)
{
   if(CHECK_LOGIN_TIMER > 0)
   {
	tb_show(LN['login'], SITEROOT+'/modules/login/login_light_box.php?url='+url+'&sendValuesBeforeTB_=savedValues&TB_iframe=true&height=180&width=600&modal=false', tb_pathToImage);
   }
   else
   {
      CHECK_LOGIN_TIMER = 1;
      window.setTimeout("$('#h2wait').html(''); call_login_box('"+url+"');",100);
   }
}

function PageOpener(back_url, check_login)
{
	if(poflag==true)
		return;
	poflag = true;
   if(back_url=='')
      back_url = window.location.href;

   if(USERID_SESSION=='' && check_login=='true')
   {
      //alert(back_url);
      encoded_back_url = Url.encode(back_url);
      //alert(encoded_back_url);
      call_login_box(encoded_back_url);
   }
   else
   {
      window.location = back_url;
   }
	poflag = false;
}

function openSendToFriend(moduleid, itemid)
{
   tb_show('Send To Friends', SITEROOT+'/modules/common/send_link_mail.php?moduleid='+moduleid+'&amp;itemid='+itemid+'&sendValuesBeforeTB_=savedValues&TB_iframe=true&height=400&width=500&modal=false', tb_pathToImage);
}