import cgi
import sha

NAVBAR_HIGHLIGHT = '<FONT COLOR="#FFFF00">'
NAVBAR_FOREGROUND = '<FONT COLOR="#BBBB77">'

BACKGROUND_COLOR = '#DDDDDD'

HEADING_FOREGROUND = '<FONT COLOR="#FFDD00">'

ERROR_COLOR = '<FONT COLOR="#FF0000">'

ISSUE_FILE = 'issues.dat'
NATION_FILE = 'nations.dat'
ACTION_COLOR = '<FONT COLOR="#DDFF00">'

BIG = '<FONT SIZE=+1>'
LARGE = '<FONT SIZE=+2>'
HUGE = '<FONT SIZE=+3>'

SPACES ='&nbsp;&nbsp;&nbsp;'
ENDFONT = '</FONT>'
CENTER = '<CENTER>'
UNCENTER = '</CENTER>'

BASE = 'http://rmmarm.freeshell.org/NationGame/'

names = (["Home"])
links = (["index.cgi"])

def navbar(nameToHighlight):
   global names
   global links
   print CENTER
   for i in range (0,len(names)):
      if names[i] == nameToHighlight:
         print NAVBAR_HIGHLIGHT + LARGE + names[i] + ENDFONT + ENDFONT + SPACES
      else:
         print '<A HREF=' + BASE + links[i] + '">' + BIG + names[i] + ENDFONT + '</A>' + SPACES

def html_header():
   print 'Content-type: text/html'
   print 
   print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
   print '<HTML>'
   print '<HEAD>'
   print '<TITLE>' + 'Campus Crusade for Christ' + '</TITLE>'
   print '<STYLE>'
   print '<!-- a{text-decoration: none;} a:hover{text-decoration: none; color: rgb(255,255,0);} --!>'
   print '</STYLE>'
   print '<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">\n'
   print '<META name="Description" content="William and Mary Chapter of Campus\n'
   print 'Crusade for Christ, a non-denominational student fellowship group.">\n'
   print '<META name="Keywords" content="Campus Crusade for Christ William and Mary'
   print ' Jesus is Lord He is alive">\n'
   print '<LINK REL="Author" HREF="mailto:rmmarm@freeshell.org" '
   print 'TITLE="Robert Mathias Marmorstein">'
   print '</HEAD>'
   print '<BODY BGCOLOR="#004477" TEXT = "#4488BB" LINK="#00EEEE" VLINK="55EEEE">'

def html_footer():
   print '</BODY>'
   print '</HTML>'

def heading(text):
   print CENTER
   print HUGE + HEADING_FOREGROUND + text + ENDFONT + ENDFONT
   print UNCENTER

#Wrap text at 80 characters
def format(text):
   retString = ""
   count = 0
   for word in text.split():
      retString += word + " "
      count += len(word)+1
      if word.find('<br>')>=0:
         count = 0
      if count>80:
         retString += "<br>"
         count = 0
   return retString

#Replace endls with <br>s
def replace_endl(str):
   newstr = ""
   for a in str:
      if a != '\n':
         newstr += a
      else:
         newstr += '<br>'
   return newstr      

#Remove endls from strings
def remove_endl(text):
   newtext = ""
   for ch in text:
      if ch != '\n':
         newtext += ch 
      else:
         newtext += " "
   return newtext      

def chomp(text):
   newtext = ""
   for ch in text:
      if ch != '\n':
         newtext += ch 
      else:
         pass
   return newtext      

def remove_whitespace(text):
   newtext = ""
   for ch in text:
      if ch != '\n' and ch != ' ' and ch != '\t':
         newtext += ch
   return newtext

#Wrap each line at 80 chars.
def wrap(str):
   count = 0
   msg = ""
   for char in str:
      count = count + 1
      msg = msg + char
      if count>80:
         msg = msg + "\n"
         count = 0
   return msg      

def errormsg(str):
   print 'Content-type: text/html'
   print
   print '<HTML><HEAD><TITLE>An error has occured</TITLE></HEAD>'
   print '<BODY><br><br><br><br>'
   print SPACES + LARGE + 'An error has occured.<br><br><br>' + ENDFONT
   print ERROR_COLOR + BIG + CENTER + format(str) + UNCENTER + ENDFONT + ENDFONT
   print '</BODY></HTML>'
   return

def getvalue(form, name):
   if form.has_key(name):
      return form[name].value
   else:
      return ""

def pass_fix(storedPass):
   fixedPass = ""
   for a in storedPass:
      if a =='\n':
         fixedPass +='+'
      else:
         fixedPass += a
   return fixedPass

def check_password(typedPass, storedPass):
   hashobj = sha.new()
   hashobj.update(typedPass)
   if pass_fix(hashobj.digest()) != storedPass:
      return 0
   else:
      return 1
      
def hash_pwd(typedPass):
   hashobj = sha.new()
   hashobj.update(typedPass)
   storedPass = hashobj.digest()
   return pass_fix(storedPass)

