#!/usr/local/bin/perl

use CGI;

use strict;

#use Tire;

foreach my $q (new CGI)
{

   print $q->header, $q->start_html, "<center>\n";

   if ($ENV{'REQUEST_METHOD'} eq 'POST')
   {

      print "<h2>Calculations:</h2>\n";

      print "<table><tr>\n";

      if (
	$q->param('tire1_val1')
	&& $q->param('tire1_val2')
	&& $q->param('tire1_rad')
	&& $q->param('tire1_system')
	)
      {
         print "<td valign=top><h3>Original Tire:</h3>\n";

         my $tire1;

         if ($q->param('tire1_system') eq 'english')
         {
            $tire1 = new_english Tire($q->param('tire1_val1'),
		$q->param('tire1_val2'), $q->param('tire1_rad') );
         }
         elsif ($q->param('tire1_system') eq 'metric')
         {
            $tire1 = new_metric Tire($q->param('tire1_val1'),
		$q->param('tire1_val2'), $q->param('tire1_rad') );
         }

         printf "%s (%s)<br>", $tire1->size_english, $tire1->size_metric;
         printf "Width: %.1f\" (%.0fmm)<br>\n", mm2inches($tire1->width), $tire1->width;
         printf "Sidewall Height: %.1f\" (%.0fmm)<br>\n", mm2inches($tire1->height), $tire1->height;
         printf "Overall Height: %.1f\" (%.0fmm)<br>\n", mm2inches($tire1->diameter), $tire1->diameter;
         printf "Circumference: %.1f\" (%.0fmm)<br>\n", mm2inches($tire1->circumference), $tire1->circumference;

         print "</td>\n";

         my $tire2;

         if (
		$q->param('tire2_val1')
		&& $q->param('tire2_val2')
		&& $q->param('tire2_rad')
		&& $q->param('tire2_system')
		)
         {

            print "<td valign=top><h3>New Tire:</h3>\n";


            if ($q->param('tire2_system') eq 'english')
            {
               $tire2 = new_english Tire($q->param('tire2_val1'),
		$q->param('tire2_val2'), $q->param('tire2_rad') );
            }
            elsif ($q->param('tire2_system') eq 'metric')
            {
               $tire2 = new_metric Tire($q->param('tire2_val1'),
		$q->param('tire2_val2'), $q->param('tire2_rad') );
            }

            printf "%s (%s)<br>", $tire2->size_english, $tire2->size_metric;
            printf "Width: %.1f\" (%.0fmm)<br>\n", mm2inches($tire2->width), $tire2->width;
            printf "Sidewall Height: %.1f\" (%.0fmm)<br>\n", mm2inches($tire2->height), $tire2->height;
            printf "Overall Height: %.1f\" (%.0fmm)<br>\n", mm2inches($tire2->diameter), $tire2->diameter;
            printf "Circumference: %.1f\" (%.0fmm)<br>\n", mm2inches($tire2->circumference), $tire2->circumference;


            print "</td>\n";

         }

         if ($tire1 && $tire2)
         {
            print "<td valign=top><h3>Difference:</h3>\n";

            my $ratio = $tire1->diameter / $tire2->diameter;

            printf "Effective gearing change: %.2f.<p>\n", $ratio;

            print "<b>IF</b> speedometer was correct with old tire:<br>\n";
            printf "At 60mph, speedo will read %.1f.<br>\n", 60 * $ratio;
            printf "When speedo reads 60mph, actual speed is %.1f.<br>\n", 60 / $ratio;


            print "</td>\n";
         }

      }
      else
      {
         print "<td><h3>Original Tire:</h3>Incomplete measurements provided</td>\n";
      }

      print "</tr></table><hr>\n";

   }

   printf q{
<h1>Tire Measurements Calculator</h1>

To calculate tire parameters based on its size, just enter the 'Old Tire' size.
To calculate parameters for two tires, including the effective gearing change
and the affect on the speedometer, enter two tire sizes.
<p>

You must also indicate if the tire size is English or metric.  For a
metric tire sized at 225/75R15, you would enter 225, 75, and 15, and
check the 'Metric' button.  For an English tire sized at 33x12.50R15,
enter 33, 12.5, and 15, and check the 'English' button.
<p>

Let me know if there are any additional calculations that would be helpful:
<a href="mailto:robf@geekthing.com">robf@geekthing.com</a>
<p>

<form method=post>
<table width=90%>
<tr>
<td>
   <center>
   <h3>Original Tire Size</h3>
   <input type=text size=5 maxlength=5 name=tire1_val1> X 
   <input type=text size=5 maxlength=5 name=tire1_val2>
   R<input type=text size=5 maxlength=5 name=tire1_rad>
   <p>
   <input type=radio name=tire1_system value=english> English
   <input type=radio name=tire1_system value=metric> Metric
   </center>
</td>
<td>
   <center>
   <h3>New Tire Size (Optional)</h3>
   <input type=text size=5 maxlength=5 name=tire2_val1> X 
   <input type=text size=5 maxlength=5 name=tire2_val2>
   R<input type=text size=5 maxlength=5 name=tire2_rad>
   <p>
   <input type=radio name=tire2_system value=english> English
   <input type=radio name=tire2_system value=metric> Metric
   </center>
</td>
</tr>
</table>


<input type=submit value=" Calculate ">
</form>
};


   print "
<hr>
</center>
<a href=/~robf/jeep/>Rob's Jeep Pages</a><br>
<a href=/~robf/>Rob's Home Page</a><br>
", $q->end_html;

}

#####

sub mm2inches
{
   my $mm = shift || 1;
   return $mm / 25.4;
}

package Tire;
use vars qw(@EXPORT @ISA);

@ISA = qw(Exporter);
@EXPORT = qw(inches2mm mm2inches);

use Exporter;

use strict;

sub new_metric
{
   my $self = shift;
   my $type = ref $self || $self;
   my ($width, $profile, $wheel) = @_;

   return bless {
	width => $width,
	height => $width * $profile / 100,
	profile => $profile / 100,
	wheel => inches2mm($wheel),
	diameter => 2 * $width * $profile / 100 + inches2mm($wheel),
	}, $type;
}

sub new_english
{
   my $self = shift;
   my $type = ref $self || $self;
   my ($diameter, $width, $wheel) = @_;

   return bless {
	width => inches2mm($width),
	height => inches2mm(($diameter - $wheel) / 2),
	profile => ($diameter - $wheel) / (2 * $width),
	wheel => inches2mm($wheel),
	diameter => inches2mm($diameter),
	}, $type;
}

sub size_metric
{
   my $self = shift;

   return sprintf("%.0f/%.0fR%.1f", $self->{width}, $self->{height}/$self->{width}*100,
	mm2inches($self->{wheel}) );
}

sub size_english
{
   my $self = shift;

   return sprintf("%.1fX%.1fR%.1f", mm2inches($self->{diameter}),
	mm2inches($self->{width}), mm2inches($self->{wheel}) );
}

sub height
{
   my $self = shift;
   return $self->{height};
}

sub width
{
   my $self = shift;
   return $self->{width};
}

sub diameter
{
   my $self = shift;
   return $self->{diameter};
}

sub circumference
{
   my $self = shift;
   return 3.14159 * $self->diameter;
}


##### not methods

sub inches2mm
{
   my $inches = shift || 1;
   return $inches * 25.4;
}

sub mm2inches
{
   my $mm = shift || 1;
   return $mm / 25.4;
}


1;




