"""
	Jeff Johnson and Crista Dagati,	2001

	A simple join form written to demonstrate using FunFormKit with Cheetah
	and Webware.
	
	FunFormKit can be found at http://www.colorstudy.com.
	Cheetah can be found at http://www.cheetahtemplate.org.
	Webware can be found at http://webware.sourceforge.net.

	I haven't gotten around to asking Ian to add support for a 
	"Form Section" field yet and I had trouble doing one cleanly so I 
	added the "isFormSection" hack.
	
	Ian, can we get something like visibleFields() that doesn't include 
	submit fields?  The code below should illustrate why.
	
"""

templateDef = '''
<div align="center">
	$rf.start
	$rf.hidden
	<table border="0" cellspacing="1" cellpadding="3">
	#set $x = 0
	#set $bgcolors = ['#cccccc','#e6e6e6']
	#for $f in $rf.visibleFields
	#if (not $f.isSubmit)
		#if $f._field._options.isFormSection == 1
			#set $x = 0
			<tr>
				<td colspan="2" bgcolor="#ffcc00" align="center"><font size="2"><b>$f</b></font></td>
			</tr>
		#else
			#set $x = $x + 1
			<tr bgcolor="$bgcolors[$x % 2]">
				<td align="right"><font size="2">$f.description:</font></td>
				<td><font size="2">$f $f.error</font></td>
			</tr>
		#end if
	#end if
	#end for
		<tr>
			<td align="center" colspan="2">
			#for $f in $rf.submitFields
				$f $f.error
			#end for
			</td>
		</tr>
	</table>
	$rf.end
</div>
'''

from SitePage import SitePage
from Cheetah.Template import Template
from FunFormKit.Form import *
from FunFormKit.Field import *
from FunFormKit.Validator import *
#from mx.DateTime import *

cardMonths = [['01','01'],['02','02'],['03','03'],['04','04'],['05','05'],['06','06'],['07','07'],['08','08'],['09','09'],['10','10'],['11','11'],['12','12'],]
cardYears =  [['01','2001'],['02','2002'],['03','2003'],['04','2004'],['05','2005'],['06','2006'],['07','2007'],['08','2008'],['09','2009'],['10','2010'],]


class ValidateForm(FormValidator):
	def validate(self, fields):
		errors = {}

		if fields['email'] != fields['email2']:
			errors['email'] = "The email addresses do not match."
	
	
		return errors
			


fields = [
	HiddenField('amount', options={'isFormSection':0}),
	StaticText('personalInfo',text='PERSONAL INFORMATION', options={'isFormSection':1}),
	TextField('fname', size=25, maxLength=25,validators = [NotEmpty()], description="First Name", options={'isFormSection':0}),
	TextField('lname',  size=25, maxLength=25,validators = [NotEmpty()], description="Last Name", options={'isFormSection':0}),
	TextField('address',  size=25, maxLength=50,validators = [NotEmpty()], description="Address", options={'isFormSection':0}),
	TextField('zipCode', size=5, maxLength=10,validators = [NotEmpty()], description="Zip Code", options={'isFormSection':0}),
	TextField('email', size=25, maxLength=50,validators = [NotEmpty()], description="E-Mail Address", options={'isFormSection':0}),
	TextField('email2', size=25, maxLength=50,validators = [NotEmpty()], description="Confirm E-Mail", options={'isFormSection':0}),
	TextField('homePhone', size=25, maxLength=50,validators = [NotEmpty()], description="Home Phone", options={'isFormSection':0}),
	StaticText('paymentInfo',text='PAYMENT INFORMATION', options={'isFormSection':1}),
	TextField('cardNumber', size=30, maxLength=30,validators = [NotEmpty()], description="Credit Card Number", options={'isFormSection':0}),
	SelectField('cardExpMonth',selections = cardMonths,validators = [NotEmpty()], description="Credit Card Exp. Month", options={'isFormSection':0}),
	SelectField('cardExpYear',selections = cardYears,validators = [NotEmpty()], description="Credit Card Exp. Year", options={'isFormSection':0}),
	SubmitButton('submit', description='Submit', methodToInvoke='submit', options={'isFormSection':0}),
]

formDefs = [
	FormDefinition('SampleForm','writeForm',fields,formValidators = [ValidateForm()]),
	]


# @@ JCJ - I don't know if this is the best or only way to change the formatting of error messages but it works :)
def myFormatError(value):
	if type(value) is type([]):
		value = string.join(value, " <br>\n")
	return '<span style="color: #aa0000"><b>%s</b></span><br>' % value
import FunFormKit
FunFormKit.Form.formatError = myFormatError
	

class SampleForm(SitePage, FormServlet):
	def __init__(self):
		FormServlet.__init__(self, 'writeContent', formDefs)
		SitePage.__init__(self)
		self._joinFormTemplateValues = {}
		self._joinFormTemplate = Template(templateDef,self._joinFormTemplateValues)
		
	def writeForm(self):
		defaults = {}
		options  = {}
		req = self.request()

		defaults['amount'] = '1.00'
			
		rf = self.renderableForm(defaults=defaults, optionSet=options)
		self._joinFormTemplateValues['rf'] = rf
		self.writeln(self._joinFormTemplate)
		
	
	def submit(self, fields):
		self.writeln(fields)
		
		
