#!/usr/bin/python
"""
Asking for a matrix of any dimension.
"""
import cgi

def print_header(title):
    """
    writes title and header of page
    """
    print("""Content-type: text/html

<html>
<head>
<title>%s</title>
</head> 
<body>""" % title)

def ask_dimension():
    """
    form to enter the dimension
    """
    print("""<form method = "post"
                   action = "web_det.py">
    <p>
      Give dimension:
      <input type = "text" name = "dim" size = 4 value = 2>
      <input type = "submit">
    </p>
    </form>""")

def ask_matrix(dim):
    """
    form to enter a matrix of dimension dim
    """
    print("""<form method = "post"
                   action = "compute_det.py">""")
    print("""The dimension:
       <input type = "text" name = "dim"
              size = 4 value = %d>""" % dim)
    print("<p>Enter matrix :</p>")
    print("<table>")
    for row in range(0, dim):
        print("<tr>")
        for col in range(0, dim):
            print("""<td><input type = "text"
            name = %d,%d size = 4></td>""" % (row, col))
    print("</table>")
    print("""<input type = "submit">""")
    print("</form>")

def main():
    """
    web interface to compute a determinant
    """
    print_header("compute determinant")
    ask_dimension()
    form = cgi.FieldStorage()
    if "dim" in form:
        dim = int(form["dim"].value)
        ask_matrix(dim)
    print("</body></html>\n")

main()
