#!/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],)

    # 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

    # the underscore in garanti_rep should be replaced by a space
    if "_" in reason:
        reason = re.sub("_", " ", reason)

    # a tuple that holds all the variables so they can be inserted
    # into the HTML page usine the modulo operator (%)
    all = (dato, reason, kdnavn, fak, dop, SN, model, beskr, error)

    # import the HTML page to output results in.
    page = open(folder+"OEM_Preview.html", "r")
    page = page.read()

    # The page have the correct number modulo strings in it, (%s)
    # so lets add our variables into the file
    page = page % all

    # Now show it:
    Display(page, "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()

