var site = {};

site.user_info = null;

site.utils = {};

site.utils.go = function ( dest, new_w, op )
{
	if ( ! dest ) dest = '/';

	if ( new_w ) window.open ( dest );
	else window.location = dest;
};

site.utils.check_logged = function ()
{
	liwe.AJAX.request ( "/cgi-bin/AjaxCmd", { "command": "get_opere" }, 
			function ( v )
			{
				liwe.AJAX.request ( "/cgi-bin/AjaxCmd", { "command": "list_opere" }, function ( v1 ) {
					site._opere_list = v1;

					if ( v [ 'login' ] )
						Login.render_logged ( v, 'cnt_login' );
					else
						kernel.on_login ( v );
				}, true);
			},
			true );
};


site.utils.strip_html_body = function ( s )
{
	var p, p2;
	p = s.indexOf ( "<body" );
	if ( p >= 0 )
	{
		p2 = s.indexOf ( ">", p + 5 );
		if ( p2 >= 0 )
			s = s.substr ( p2 + 1 );
	}

	p = s.indexOf ( "</body>" );
	if ( p >= 0 )
		s = s.substr(0, p);

	return s;
};

site.utils.create_iframe = function ( url )
{
	url = url.replace ( /[[]/g, "?" );
	$ ( "static_cnt" ).style.padding = "0";
	$ ( "static_cnt" ).innerHTML = '<iframe id="static_frame" frameBorder="no" width="98%" height="850px" style="padding: 5px;" src="' + url + '"><\/iframe>';
};

site.utils.parseInt = function ( s )
{
	s = '' + s;
	while ( s && ( s.charAt ( 0 ) == '0' ) ) s = s.substr ( 1 );
	return parseInt ( s );
};


site.utils.order_date = function ( date, sep, order, new_sep )
{
	var s = '';

	if ( date )
	{
		if ( order == "IT" && sep )
		{
			s = date.split ( sep );

			if ( s.length < 2 ) return s;

			if ( new_sep ) sep = new_sep;

			s = s [ 2 ] + sep + s [ 1 ] + sep + s [ 0 ];
		}
	}

	return s;
};


site.utils.show_static = function ( page )
{
	liwe.AJAX.request ( "/cgi-bin/AjaxCmd", { command: "dump_html", fname: page }, function ( v ) {
		$ ( "static_cnt" ).style.padding = "1em 2em";
		$ ( "static_cnt" ).innerHTML = LinkReplacer.lnk2span ( site.utils.strip_html_body ( v [ "content" ] ) );
		LinkReplacer.replace ();
	}, true );
};

site.utils.make_doc_link = function ( row, ds, skip_history, highlight )
{
	var today = new Date ();
	/*var data = row [ "data" ];
	data = data.split ( "-" );
	var d = new Date ( data [ 0 ], ( site.utils.parseInt ( data [ 1 ] ) - 1 ), data [ 2 ] );
	*/

	if ( skip_history ) skip_history = 1;
	else skip_history = 0;

	if ( ! highlight ) highlight = "";
	highlight = escape ( highlight.replace ( /\'/g, "\\'" ) );


	if (
		( ( site.user_info.attive.indexOf ( "AP" ) >= 0 ) && ( site.user_info [ "login" ] != "" ) )
		/*||
		(
			d.getFullYear() == today.getFullYear() &&
			d.getMonth() == today.getMonth() &&
			d.getDate() == today.getDate()
		)*/
	   )
	{
		// sono loggato
		if ( ds )
		{
			row [ "link" ] = String.formatDict ( "javascript:kernel.go({mask:\'doc\',ds_name:\'%(ds_name)s\'," + 
							     "pos:%(_pos)d,opera:\'PA\',id:\'%(id)s\',highlight:\'%(highlight)s\'})", 
					{
						ds_name: ds.name,
						_pos: row._pos,
						id: row.get ( 'id' ),
						highlight: highlight
					} );
		}
		else
		{
			row [ "link" ] = String.formatDict ( "javascript:kernel.go({'__nh': %(skip_history)s, mask:\'doc\',id:\'%(id)s\'})",
				{ id: row.get ( "ID" ), skip_history: skip_history } );
		}
	}
	else
	{
		// NON sono loggato
		row [ "link" ] = "javascript:kernel.go({mask:'frame',page:'http://el.leggiditalia.it/studiolegale/coupon/'})";
	}
};

site.utils.show_hide = function ( id_name )
{
	var h = $( id_name );

	if ( h.style.display == 'none' )
		h.style.display = 'block';
	else
		h.style.display = 'none';
};
