#!/usr/bin/python
#=====================================================================================
# Generic responder for any html form 
#=====================================================================================
import HTTPparam                                      

print("Content-Type: text/html \n")                     #required HTTP header

firstname  = HTTPparam.getValue('firstname')            #get HTML form field value or None if nothing 
lastname   = HTTPparam.getValue('lastname')                
address    = HTTPparam.getValue('address')
brand      = HTTPparam.getValue('brand')         
flavor     = HTTPparam.getValue('flavor')               #flavor is a list
topping1   = HTTPparam.getValue('topping1')   or ""                
topping2   = HTTPparam.getValue('topping2')   or ""     #if None is returned set it to ""                
topping3   = HTTPparam.getValue('topping3')   or ""                
topping4   = HTTPparam.getValue('topping4')   or ""                
creditCard = HTTPparam.getValue('creditCard') or ""    

if firstname  == None : firstname  = ""                 #another way to clean None         
if lastname   == None : lastname   = ""                 #if nothing is entered you get None
if address    == None : address    = ""                 #if None clear the field         
if brand      == None : brand      = ""         
if flavor     == None : flavor     = ""         
            
flavor2 = ""
if type(flavor) is list :                               #if it is a list
    flavor2 = ','.join(flavor)                          #join it into a string , separated

msg = ''    

"""===================================================================================
 function validate():
      validates the content of the html form input fields (all fields are required)    
==================================================================================="""
def validate():                                     #function validate
        
    global msg

    if firstname == "" :
        print("<font color=red>Please enter First Name</font>")
        msg = 'error'
        return 
    if lastname == "" :
        print("<font color=red>Please enter Last Name</font>")
        msg = 'error'
        return 
    if address == "" :
        print("<font color=red>Please enter your Address</font>")
        msg = 'error'
        return 
    if flavor == "" :
        print("<font color=red>Please choose ice cream flavor(s)</font>")
        msg = 'error'
        return 
    if (topping1+topping2+topping3+topping4) == "" :
        print("<font color=red>Please select topping(s)</font>")
        msg = 'error'
        return 
    if creditCard == "" :
        print("<font color=red>Please select Credit Card</font>")
        msg = 'error'
        return 

"""===================================================================================
 function print_response():
      print data received from client    
==================================================================================="""
def print_response():
        
    print('''     
        <br>
        <table border=2>
        <tr bgcolor=tan>
        <th>Element Name</th><th>Element Value</th>
    ''') 
    print("<tr><th>Firstname</th>   <td>", firstname,  "</td>") 
    print("<tr><th>Lastname</th>    <td>", lastname,   "</td>") 
    print("<tr><th>Address</th>     <td>", address,    "</td>") 
    print("<tr><th>Brand</th>       <td>", brand,      "</td>") 
    print("<tr><th>Flavor</th>      <td>", flavor2,    "</td>") 
    print("<tr><th>Topping</th>     <td>", topping1,topping2,topping3,topping4, "</td>") 
    print("<tr><th>Credit Card</th> <td>", creditCard, "</td>") 
    print('''
        </table>
        </body>
        </html>
    ''')
#==========================================================================================

validate()                      #call validate function

if msg=='': 
    print_response()            #if no errors, save in database




#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/staff/sultan/public_html/cgi-bin/python')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================