#!/usr/bin/python

import re
import cgi
from time import localtime as ltime

# Specify the location of the files
folder = "C:\\Programmer\\python\\cgi-bin\\"

# specify the filename of the template file
TemplateFile = "template.html"

# specify the filename of the form to show the user
FormFile = "oem.html"

# Display  takes one parameter - a string to Display
def Display(Content, title):
    TemplateHandle = open(folder+TemplateFile, "r")  # open in read only mode
    # read the entire file as a string
    TemplateInput = TemplateHandle.read() 
    TemplateHandle.close()                    # close the file

    # this defines an exception string in case our
    # template file is messed up
    BadTemplateException = "There was a problem with the HTML template."


    try:
        finito = re.sub("REPLACE_TITLE", title, TemplateInput)
        finito = re.sub("<!--REPLACE_CONTENT-->", Content, finito)
    except:
        print "ERROR"

        
    print "Content-Type: text/html\n\n"
    print finito


### what follows are our two main 'action' functions, one to show the
### form, and another to process it

# this is a really simple function
def DisplayForm():
    FormHandle = open(folder+FormFile, "r")
    FormInput = FormHandle.read()
    FormHandle.close()

    Display(FormInput, "OEM FORM INPUT")

def ProcessForm(form):    
    # extract the information from the form in easily digestible format

    # get customer name
    try:
        kdnavn = form["kdnavn"].value
    except:
        # name is required, so output an error if 
        # not given and exit script
        Display("You need to supply the customers name. Please go back.", "OEM FORM ERROR")
        raise SystemExit

    # Date of purchase (DOP)
    try:
        dop = form["dop"].value
    except:
        Display("You need to state the date of purchase. Please go back.", "OEM FORM ERROR")
        raise SystemExit

    # invoice number
    try:
        fak = form["fak"].value
    except:
        Display("You need to state invoice number (faktura). Please go back.", "OEM FORM ERROR")
        raise SystemExit

    # modeltype or number
    try:
        model = form["model"].value
    except:
        Display("You need to state the modelnumber. Please go back.", "OEM FORM ERROR")
        raise SystemExit

    # product description
    try:
        beskr = form["beskr"].value
    except:
        Display("You need to state some product description. Please go back.", "OEM FORM ERROR")
        raise SystemExit

    # Serial Number S/N
    try:
        SN = form["SN"].value
    except:
        Display("You need to state serial number. Please go back.", "OEM FORM ERROR")
        raise SystemExit

    # Error Description
    try:
        error = form["fejl"].value
    except:
        Display("You need to supply a detailed description of the problem. Please go back.", "OEM FORM ERROR")
        raise SystemExit

    # Date
    dato = str(ltime()[2]) + "/" + str(ltime()[1]) + "-" + str(ltime()[0],)
##    try:
##        dato = form["dato"].value
##    except:
##        Display("You need to state the date of purchase. Please go back.")
##        raise SystemExit

    # Radio buttons
    try:
        reason = form["reason"].value
    except:
        Display("You need to click specify the reason (DOA, Return or Repair). Please go back.", "OEM FORM ERROR")
        raise SystemExit
    
    if "_" in reason:
        reason = re.sub("_", " ", reason)

    Output = ""  # our output buffer, empty at first

    # Adding the above variables to an output variable
    Output = Output + "Dato:\t" + dato + "<br>"
    
    Output = Output + "Dette er en\n:" + reason + "<br><br><br>"

    Output = Output + "Kundens navn:\t" + kdnavn + "<br><br>"

    Output = Output + "K&oslash;bs dato:\t" + dop + "<br><br>"

    Output = Output + "Faktura nr.:\t" + fak + "<br><br>"

    Output = Output + "Varekode:\t" + model + "<br><br>"

    Output = Output + "Varebeskrivelse:\t" + beskr + "<br><br>"

    Output = Output + "Serienummer:\t" + SN + "<br><br>"

    Output = Output + "Fejlbeskrivelse:\t" + error + "<br><br>"

   
    Display(Output, "OEM FORM OUTPUT")


###
### Begin actual script
###

### evaluate CGI request
form = cgi.FieldStorage()

### "key" is a hidden form element with an 
### action command such as "process"
try:
    key = form["key"].value
except:
    key = None

if key == "process":
    ProcessForm(form)
else:
    DisplayForm()

