#!/usr/bin/perl
# ----------------------------------------------------------------------------
#
#  (c) Copyright 2006 TradeExtender
# 
#  This software is proprietary to and embodies the confidential technology 
#  of TradeExtender. Possession, use, duplication or dissemination of the 
#  software and media is authorized only pursuant to a valid written license 
#  from TradeExtender.
#

use strict;
use lib "/var/www/vhosts/kantelpunten.com/httpdocs/pl/lib";
use lib "lib";
BEGIN {
    delete $INC{"Kantelpunten/Helper.pm"};
#    delete $INC{"Kantelpunten/MyRobots.pm"};
}
use Kantelpunten::Helper;
use Kantelpunten::MyRobots;
use CGI qw(:standard);
#use Data::Dumper;

local our $param  = undef;

show_buttons();
exit(0);

sub show_buttons()
{
	my $q = CGI->new;

	$param->{cc}		= defined($q->param('cc')) ? 1 : undef;
	$param->{status} 	= $q->param('status');
	$param->{hpage} 	= $q->param('hpage');
	$param->{rb} 		= $q->param('rb');

	my $remote_ip = init_helper();
	$param->{rb} = get_robot() if (!defined($param->{rb}) || $param->{rb});
	set_robot($param->{rb});

	my $sesid = get_session_cookie();   # try to get the session cookie
	my $userid = get_userid_cookie();   # or the userid cookie

	my $cook = 1;
	if (!defined($sesid) || !defined($userid))
	{
    	# probably no cookie supported. To be sure reload with cookie check
    	# did we already reload ?
    	if (defined($param->{cc}))
    	{
    		$cook = 0;
    	}
    	else
    	{
    		# wait and reload, cookies should be set by register
    		msleep(0.9);
	    	print redirect(-location => "/pl/ShowButtons.pl?cc=1");
    		exit(0);
    	}
	}

	# status defintions
	# 0 no status
	# 1 voorkeuze ingesteld
	# 2 voorkeuze gewist
	# 3 voorkeuze instelling mislukt

	my $status;
	my $hpage;
	my $mail = 0;
	if (defined($userid))
	{
    	get_user_rec();
    	$hpage = set_homepage();
		if (defined($param->{hpage}))
		{
		    my $cpage = ($param->{hpage} == 2) ? 0 : get_last_page_cookie();
		    if (defined($cpage))
	    	{
		        # filter out some not allowed home pages
		        $cpage = 0 if ($cpage == 10024);
	    	    $cpage = 0 if ($cpage == 10027);
	        	$cpage = 0 if ($cpage == 10028);
	    	}
    		$status = 0;
    		if (defined($hpage) && defined($cpage) && ($hpage != $cpage))
    		{
        		$status = ($cpage == 0) ? 2 : 3;
	        	my $rv = set_homepage($cpage);
    	    	$rv = write_user_rec() if defined($rv);
        		if (defined($rv))
        		{
            		$status = ($cpage == 0) ? 2 : 1;
        		}
    		}
    		print redirect(-location => "/pl/ShowButtons.pl?status=$status");
  			exit(0);
		}
	}

	$status = $param->{status};
	$status = 0 unless defined($status);
	$hpage = 0 unless defined($hpage);
	# currently i don't do anything with the status, make an icon with additonal info       

	my $script = make_script();
	
	print_header("+1dy");
	print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
	print "<html>\n";
	print "<head>\n";
	#print " <meta http-equiv=\"Cache-Control\" content=\"private\">\n";
	print " <base target=\"hoofd\">\n";
	print " <style>a{text-decoration: none}</style>\n";
	print " <style fprolloverstyle=\"\">A:hover {color: #ffff33; text-decoration: underline; font-family: comic sans ms; font-size: 10pt}</style>\n";
	print <<EndOfText;
 <style type="text/css">
  body {
    background-image: url('http://www.kantelpunten.com/pics/logo/beleggen-aandelen-logo.jpg');
    background-repeat: no-repeat;
    background-position: left top; 
    background-attachment: fixed; }
 </style>
EndOfText
    print $script;
	print "</head>\n";
	if ($cook) 
	{
    	print "<body style=\"background-color: rgb(200, 225, 250);\" alink=\"#000099\" link=\"#000099\" vlink=\"#000099\" leftmargin=\"0\" topmargin=\"0\" marginheight=\"0\" marginwidth=\"0\">\n";
	    print "<div style=\"cursor:hand; text-align: right; color: rgb(0, 0, 153); font-family: comic sans ms;\" onclick=\"JumpTo(event)\" target=\"hoofd\" url=\"http://www.kantelpunten.com/pl/ShowMain.pl?page=9986\">\n";
    	print "   <small style=\"font-family: comic sans ms;\"><br>\n";
#    	print_buttons(undef, undef, (is_logged_in() ? "info" : "registreer"));
#    	print_buttons(undef, undef, "nieuws");
#    	print_buttons(undef, undef, "functies");
#    	print_buttons(undef, undef, "mini_startwebsite");    
#    	print_buttons(undef, undef, "mail_toppy") if ($mail);
	} 
	else 
	{
    	print "<body style=\"background-color: rgb(200, 225, 250);\" alink=\"#FFFFFF\" link=\"#FFFFFF\" vlink=\"#FFFFFF\" leftmargin=\"0\" topmargin=\"0\" marginheight=\"0\" marginwidth=\"0\">\n";
	    print "<div style=\"text-align: center; color: rgb(0, 0, 153); font-family: comic sans ms;\">\n";
    	print " <small style=\"rgb(0, 0, 153) font-family: comic sans ms;\">\n";
	    print "  <a href=\"/pl/ShowMain.pl?page=10024\">geen cookies!</a>\n";
    	print " </small>\n";
	}
	print "</div>\n";
	print "</body>\n";
	print "</html>\n";                    
}

sub make_script()
{
	my $script = <<EndOfText;
<script language="JavaScript1.2">
	var ie5=document.all&&document.getElementById;
	var ns6=document.getElementById&&!document.all;

	function JumpTo(e){
		var firingobj=ie5? event.srcElement : e.target;
    	if(firingobj.getAttribute("url").length > 0)
			if (firingobj.getAttribute("target"))
				window.open(firingobj.getAttribute("url"),firingobj.getAttribute("target"));
			else
				window.location=firingobj.getAttribute("url");
	}
</script>
EndOfText
}
