from mod_python import util, apache, Session
from sqlobject import sqlbuilder
from include.siteSQL import Accounts, DB  

def index(req):
  try:
    req.content_type = "text/html"
    doc_root = req.document_root()
    PSDefaults = apache.import_module("include.PSDefaults")
    
    psdef = PSDefaults.Template(req)
    status, accountid = None, 0
    session = psdef.sessionCheck(redirect=False)
    if session:
        status = session["status"]
        accountid = session["accountid"]
     # commented out 4/20/07 by BD   if accountid!="None": 
     # commented out 4/20/07 by BD   util.redirect(req,"/regform.php")

    psdef.addVars({'status':status})
    psdef.compileTemplate('account_signupFreePosting.tpl')
    psdef.runTemplate()
    return
  except:
    import sys
    feedback = str(sys.exc_info()[0])
    req.content_type = "text/html"
    psdef.addVars({'FeedbackMessage':feedback, 'status':None, 'success':False})
    psdef.compileTemplate("./templates/feedback.tpl")
    psdef.runTemplate()
    return
  
def post(req, firstName, lastName, contName, phone, company,  \
         email, username, password, password2,  premium=None, \
         fax="", department="", address1="", address2="", city="", \
         state="", zip="", dotNumber="1111", mcNumber="0123456", billingEmail=""):
#  try:
    req.content_type = "text/html"
    doc_root = req.document_root()
    PSDefaults = apache.import_module("include.PSDefaults")
    psdef = PSDefaults.Template(req)

    db = DB()
    db.connect()
    
    accounts = list(Accounts.select(Accounts.q.username=="%s" % (username)))
    rowcount = len(accounts)
    if not rowcount == 0:
      req.content_type = "text/html"
      psdef.addVars({'status':None, 'ErrorMessage':'Username already exists'})
      psdef.compileTemplate('./templates/error.tpl')
      psdef.runTemplate()
      
      return
    
    if not password == password2:
      req.content_type = "text/html"
      psdef.addVars({'status':None, 'ErrorMessage':'Password Mismatch.'})
      psdef.compileTemplate('./templates/error.tpl')
      psdef.runTemplate()
      
      return
      
    if  address2:
      req.content_type = "text/html"
      psdef.addVars({'status':None, 'ErrorMessage':'You forgot to indicate what type of subscription you want.'})
      psdef.compileTemplate('./templates/error.tpl')
      psdef.runTemplate()
      
      return        
      
    if not mcNumber == "":
      req.content_type = "text/html"
      psdef.addVars({'status':None, 'ErrorMessage':'Missing Credit Card Number.'})
      psdef.compileTemplate('./templates/error.tpl')
      psdef.runTemplate()
            
      return   
      
    if not dotNumber == "":
      req.content_type = "text/html"
      psdef.addVars({'status':None, 'ErrorMessage':'Missing Credit Card Expiration Date.'})
      psdef.compileTemplate('./templates/error.tpl')
      psdef.runTemplate()
      
      return              
      
    if not phone \
       or not company or not email or not username \
       or not password or not password2:
      req.content_type = "text/html"
      psdef.addVars({'status':None, 'ErrorMessage':'Missing required field(s).'})
      psdef.compileTemplate('./templates/error.tpl')
      psdef.runTemplate()
      
      return
    # trap for automated account requests
    if not department == "":
      req.content_type = "text/html"
      psdef.addVars({'status':None, 'ErrorMessage':'Code 0086'})
      psdef.compileTemplate('./templates/error.tpl')
      psdef.runTemplate()
     
      return
    
    contPhone = phone
    newAccount = Accounts(firstName=firstName, lastName=lastName, creationTS=None, modificationTS=None, company=company, \
                          address1=address1, address2=address2, city=city, state=state, zipcode=zip, email=email, \
                          contPhone=contPhone, contName=contName, department=department, fax=fax, mcnumber=mcNumber, status="Pay", \
                          dotnumber=dotNumber, billingEmail=billingEmail, username=username, password=password, accountType="Pay"  )
    db.close()
    doEmail = True
    # doEmail = False # local debug
    if doEmail:
      import smtplib
      sender = 'subscriptions2008@freight-terminal.com'
      to = PSDefaults.RegEmail
      header = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to, 'New freight-terminal.com Subscription')
      msg = '''The following user has setup a new IFT subscription.
    Username: %s
    Password: %s
    Company: %s    
    Contact Name: %s
    Phone: %s
    Email: %s
    State: %s
    Subscription Type: %s        
    Card Type: %s
    Card Name: %s %s    
    Card Number: %s
    Card Expires: %s 
 
    Thank You Message Sent
      
      ''' % (username, password, company, contName, contPhone, email, state, address2, fax, firstName, lastName, mcNumber, dotNumber)
      conn = smtplib.SMTP(PSDefaults.EmailServer)
      conn.sendmail(sender, to, header + msg)
      conn.quit()
   
    doEmail = True
    # doEmail = False # local debug
    if doEmail:
      import smtplib
      sender = 'subscriptions2008@freight-terminal.com'
      to = PSDefaults.RegEmail
      header = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, email, 'Your New Freight-Terminal.com Subscription')
      
      msg = '''Thank you for your subscription to freight-terminal.com. 

This is a courtesy reminder that your subscription payment of $9.95 per month will begin after the 1 week free trial ends.

For future reference, here is the login information you setup:

    Username: %s
    Password: %s

Don't forget your subscription includes access the Load Solutions loadboard. The Freight Terminal and Load Solutions loadboards share many of the same loads but each site also has many different loads so it can pay to check both sites when you need a load. When you need a partial load, be sure to check our Hotshot Carrier site. This site contains all the partial loads from the Freight Terminal site on one page. There are links to these sites on the Freight Terminal site. 

When you have a few minutes, please put an entry in the Freight Terminal Guestbook if you haven't already done so. The Guestbook is quite popular and when shippers call me and they are looking for a carrier I always recommend they check this Guestbook and  call the carriers near where their loads originate.

Your subscription also includes a free webpage to display pictures of your truck(s) and a printable copies of your insurance and your authority so you don't have to constantly fax copies to shippers and brokers before you can haul for them. When you have a few minutes, please give me a call and I can fill you in on what to send me for your webpage. After we get the page setup for you, a link to it can be added to your Guestbook entry so help potential shippers and brokers can see pictures of your equipment. 

Bill Dove
The Internet Freight Terminal
239-985-9341

Links To Our Sites:
www.freight-terminal.com
www.loadsolutions.com
www.hotshotcarrier.com 
      
      ''' % (username, password)
       
      
      conn = smtplib.SMTP(PSDefaults.EmailServer)
      conn.sendmail(sender, email, header + msg)
      conn.quit()
    
    util.redirect(req, 'https://freight-terminal.com/login.py?username=%s&password=%s&location=account_paymentFreePosting.py' % (newAccount.username, newAccount.password))
    return
    
#  except:
#    import sys
#    PSDefaults = apache.import_module("include.PSDefaults")
#    psdef = PSDefaults.Template(req)
#    feedback = str(sys.exc_info()[0])
#    req.content_type = "text/html"
#    psdef.addVars({'FeedbackMessage':feedback, 'success':False, 'status':None})
#    psdef.compileTemplate("./templates/feedback.tpl")
#    psdef.runTemplate()
#    return