C Programming Column
by Al Stevens



Listing One
<SCRIPT LANGUAGE="JavaScript">
<!---------------------------------------------------------------
function isOK(field)
{
    if (field.value == null || field.value == "")   {
        alert("Please enter " + field.name);
        field.focus();
        field.select();
        return false;
    }
    return true;
}
function validateData(orderform)
{
    accumPrice(orderform);
    if (isOK(orderform.name))
        if (isOK(orderform.address))
            if (isOK(orderform.account_number))
                if (isOK(orderform.account_name))
                    if (isOK(orderform.expiration_date))
                        return true;
    return false;
}
function testDestination(orderform)
{

    var isUSA =     (orderform.country.value == "USA" || 
                     orderform.country.value == "US" || 
                     orderform.country.value == "usa" || 
                     orderform.country.value == "us");
    orderform.flres.checked = ( isUSA   &&
                        (orderform.state.value == "FL" || 
                         orderform.state.value == "Florida" ||
                         orderform.state.value == "florida" ||
                         orderform.state.value == "fl")
                      );
    if (isUSA)  {
        if (orderform.shipping[2].checked)
            orderform.shipping[0].checked = true;
    }
    else
        orderform.shipping[2].checked = true;
   accumPrice(orderform);
}
function padString(num)
{
    var str = num + "";
    var ndx = str.indexOf(".");
    if (ndx != -1)  {
        ndx += 3;
        if (ndx == str.length+1)
            str += "0";
        else
            str = str.substring(0, ndx);
    }
    else
        str += ".00";
    return str;
}
function accumPrice(orderform)
{
    var quantity = parseInt(orderform.quantity.value);
    if (quantity < 1)
        quantity = 1;
    orderform.quantity.value = quantity;

    var price = 79.95 * quantity;
    orderform.price.value = padString(price);

    var shippingcost = 15;
    if (orderform.shipping[0].checked == true)
        shippingcost = 3;
    orderform.shippingcost.value = padString(shippingcost);

    var salestax = 0;
    if (orderform.flres.checked == true)    {
        salestax = price + shippingcost;
        salestax *= 60;
        salestax += 5;
        salestax -= salestax % 10;
        salestax /= 1000;

    }
    orderform.salestax.value = padString(salestax);

    var total = price + shippingcost + salestax;
    orderform.total.value = padString(total);
}
// -------------------------------------------------------------->
</SCRIPT>


Listing Two
/* order_proc.c -- An order-entry processing cgi script. It accepts buyer 
and order data from a Web page and mails an order to a seller
*/
#include <stdio.h>

#define PRODUCT          "MidiFitz"
#define mail_program     "/usr/lib/sendmail -t"
#define order_processor  "midifitz@midifitz.com"
#define order_taker      "midifitz@midifitz.com"
#define link_to          "http://www.midifitz.com/index.html"

void PageHeader(const char* title);
void PageTrailer(void);
void PageHead(int level, const char* hed);
void PageLine(const char* line);
void PageParagraph(const char* text);
void PageLink(const char* link, const char* msg);

int main()
{
    char line[601];
    FILE* fp;
    if (fgets(line, 600, stdin) != 0)   {
        char* cp = line;
        while (*cp) {
            if (*cp == '&')
                *cp = '\n';
            else if (*cp == '+')
                *cp = ' ';
            cp++;
        }
    }
    PageHeader(PRODUCT " On-line Order System");
    if ((fp = popen(mail_program, "w")) != NULL)    {
        fprintf(fp,
            "To: " order_processor "\n"
            "From: " order_taker "\n"
            "Subject: " PRODUCT " on-line order\n"
            "%s\n", line);
        pclose(fp);
        PageParagraph("Your " PRODUCT " order has been placed.");
    }
    PageLink(link_to, "Click to return");
    PageTrailer();

    return 0;
}
void PageHeader(const char* title)
{
    puts("Content-type: text/html\n");
    puts("<HTML>");
    puts("<HEAD>");
    if (title != 0)
        printf("<TITLE>%s</TITLE>\n", title);
    puts("</HEAD>");
    puts("<BODY>");
}
void PageTrailer(void)
{
    puts("</BODY>");
    puts("</HTML>");
}
void PageHead(int level, const char* hed)
{
    printf("<h%d>%s</h%d>\n", level, hed, level);
}
void PageLine(const char* line)
{
    printf("%s<BR>\n", line);
}
void PageParagraph(const char* text)
{
    puts("<P>");
    PageLine(text);
}
void PageLink(const char* link, const char* msg)
{
    printf("<CENTER><A HREF=%s>%s</A></CENTER>\n", link, msg);
}

6


