#!/usr/bin/perl -w
use strict;

# Use the mech-dump utiltily to get field and form names
use WWW::Mechanize;

sub getcall {
    my $bankno = shift;
    my $year = shift;
    my $quarter = shift;

    my @qdates = ('3/31/', '6/30/', '9/30/', '12/31/');
    my $qdate .= $qdates[$quarter-1];
    $qdate .= $year;
    my $mech = WWW::Mechanize->new();
    $mech->agent_alias('Windows IE 6');
    $mech->get('http://www2.fdic.gov/ubpr/UbprReport/SearchEngine/Default.asp');
    $mech->form_number(3);
    $mech->set_fields('txtCert' => $bankno);
    $mech->click('btnSubmit');
    $mech->form_name('myform');
    $mech->set_fields('UBPRTYPE' => 'Stan', 'CustQtrOne' => $qdate);
    $mech->click_button(value => 'Generate Report');
    # Report Generated
    $mech->follow_link(name => 'UpperFrame');
    $mech->follow_link(text => 'Export');
    print STDERR $mech->content();
    $mech->form_name('pageform');
    $mech->select('TARGET', 'Tab-Delimited');
    $mech->tick('AllPages', "ALL", 1);
    #$mech->submit(); # Empty content returned
    #$mech->click_button(number => 1); # Can't call method "click" on undefined value
    $mech->click_button(value => 'Export'); # Can't call method "header" on undefined value
    print STDERR $mech->content();

    return 1;
}

getcall(4619, 2002, 3);
