//
//  StuntPuppyViewController.m
//  StuntPuppy
//
//  Created by Victor Johnson on 7/12/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "StuntPuppyViewController.h"
#import "RadioBtn.h"
#import "AdminView.h"
#import "State.h"
#import "MapScroller.h"

@implementation StuntPuppyViewController

@synthesize admin,formView,formData,thankyouView,buttonsArray,submit,firstName,middleName, 
			lastName,address1,city,state,statesView,sArray,zip,address2,apt,phone,phoneNum,
			toolbar,email,answersTextArray,questionsTextArray,r1,r2,r3,r4,r5,r6,rt1,rt2,rt3,
			rt4,rt5,rt6,rt7,rX,rY,q7,q7_txt,qArray,btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,
			btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16;



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	[super viewDidLoad];
	[self initStuntPuppyViewController];
}

-(void)initStuntPuppyViewController {
    
	//if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)  {
	
	//}
	
	
	buttonsArray = [[NSMutableArray alloc] init];
	
	questionsTextArray = [[NSMutableArray alloc] init];
	[questionsTextArray addObject:@"Last Name,First Name, MI,Address,Apt #,City,State,Zip,Phone,Email,"];
	[questionsTextArray addObject:@"1)Would you like to be contacted by a Subaru dealer to test drive a Subaru?,"];
	[questionsTextArray addObject:@"2)Do you currently own a Subaru?,"];
	[questionsTextArray addObject:@"\"3)Before attending the event, would you say that Subaru was...?\","];
	[questionsTextArray addObject:@"\"4)Based on your experience at the event, how has your opinion of Subaru changed?\","];
	[questionsTextArray addObject:@"5)The next time you're shopping for a vehicle will Subaru be...?,"];
	[questionsTextArray addObject:@"6)Are you in the market for a new vehicle?,"];
	[questionsTextArray addObject:@"7)(If yes to #6)When do you intend to purchase?\r\n"];
	
	answersTextArray = [[NSMutableArray alloc] init];
	[answersTextArray addObject:@"Yes"];
	[answersTextArray addObject:@"No"];
	[answersTextArray addObject:@"Yes"];
	[answersTextArray addObject:@"No"];
	[answersTextArray addObject:@"The only brand you'd consider purchasing"];
	[answersTextArray addObject:@"One of the many brands you'd consider purchasing"];
	[answersTextArray addObject:@"One of the few brands you'd consider purchasing"];
	[answersTextArray addObject:@"A brand you probably would not consider purchasing"];
	[answersTextArray addObject:@"Increased your opinion"];
	[answersTextArray addObject:@"Did not affect your opinion"];
	[answersTextArray addObject:@"Decreased your opinion"];
	[answersTextArray addObject:@"The only brand you'd consider purchasing"];
	[answersTextArray addObject:@"One of the many brands you'd consider purchasing"];
	[answersTextArray addObject:@"One of the few brands you'd consider purchasing"];
	[answersTextArray addObject:@"A brand you probably would not consider purchasing"];
	[answersTextArray addObject:@"Yes"];
	[answersTextArray addObject:@"No"];
	
	self.view.backgroundColor = [UIColor blackColor];
	
	thankyouView = [[UIView alloc] initWithFrame:CGRectMake(0,0,768,1024)];
	thankyouView.center = CGPointMake(384, 492);
	thankyouView.backgroundColor = [UIColor blackColor];
	thankyouView.tag = 110;
	thankyouView.hidden = YES;
	thankyouView.userInteractionEnabled = YES;
	[self.view addSubview:thankyouView];
	
	UIImageView *bg_thankyouView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,768,1024)];
	bg_thankyouView.center = CGPointMake(384, 492);
	bg_thankyouView.image = [UIImage imageNamed:@"bg_thankyou.png"];
	bg_thankyouView.hidden = NO;
	bg_thankyouView.userInteractionEnabled = NO;
	bg_thankyouView.alpha = 1;
	//bg_formView.backgroundColor = [UIColor redColor];
	[thankyouView addSubview:bg_thankyouView];
	
	formView = [[UIView alloc] initWithFrame:CGRectMake(0,0,768,1024)];	
	formView.center = CGPointMake(384, 492);
	formView.hidden = NO;
	formView.tag = 10;
	formView.userInteractionEnabled = YES;
	formView.backgroundColor = [UIColor blackColor];
	[self.view addSubview:formView];
	
	UIImageView *bg_formView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,768,1024)];
	bg_formView.image = [UIImage imageNamed:@"bg_form.png"];
	bg_formView.hidden = NO;
	bg_formView.alpha = 1;
	bg_formView.userInteractionEnabled = NO;
	bg_formView.backgroundColor = [UIColor blackColor];
	[formView addSubview:bg_formView];
	
	submit = [[UIButton alloc] initWithFrame:CGRectMake(0,0,81,34)];
	submit.backgroundColor = [UIColor clearColor];
	submit.tag = 200;
	submit.center = CGPointMake(655,960);
	submit.userInteractionEnabled = YES;
	[submit setBackgroundImage:[UIImage imageNamed:@"btn_submit.png"] forState:UIControlStateNormal];
	//[btn setCenter:CGPointMake(230, 272)];
	submit.hidden = NO;
	[submit addTarget:self action:@selector(submitForm) forControlEvents:UIControlEventTouchUpInside];
	[formView addSubview:submit];
	
	currTextField = 400;
	
	firstName = [[UITextField alloc] initWithFrame:CGRectMake(0,0,280,30)];
	//firstName.autocapitalizationType = UITextAutocapitalizationTypeWords;
	firstName.autocorrectionType = UITextAutocorrectionTypeNo;
	firstName.alpha = 1;
	firstName.tag = 400;
	//firstName.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	firstName.textColor = [UIColor blackColor];
	firstName.font = [UIFont fontWithName:@"Helvetica" size:15];	
	firstName.backgroundColor = [UIColor clearColor];
	firstName.borderStyle = UITextBorderStyleRoundedRect;
	firstName.center = CGPointMake(202, 226);
	firstName.placeholder = @"First Name";
	firstName.textAlignment = UITextAlignmentLeft;
	firstName.delegate = self;
	[formView addSubview:firstName];
	
	middleName = [[UITextField alloc] initWithFrame:CGRectMake(0,0,62,30)];
	//middleName.autocapitalizationType = UITextAutocapitalizationTypeWords;
	middleName.autocorrectionType = UITextAutocorrectionTypeNo;
	middleName.alpha = 1;
	middleName.tag = 401;
	//middleName.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	middleName.textColor = [UIColor blackColor];
	middleName.font = [UIFont fontWithName:@"Helvetica" size:15];	
	middleName.backgroundColor = [UIColor clearColor];
	middleName.borderStyle = UITextBorderStyleRoundedRect;
	middleName.center = CGPointMake(382, 226);
	middleName.placeholder = @"MI";
	middleName.textAlignment = UITextAlignmentLeft;
	middleName.delegate = self;
	[formView addSubview:middleName];
	
	lastName = [[UITextField alloc] initWithFrame:CGRectMake(0,0,278,30)];
	lastName.alpha = 1;
	lastName.tag = 402;
	lastName.autocorrectionType = UITextAutocorrectionTypeNo;
	//lastName.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	lastName.textColor = [UIColor blackColor];
	lastName.font = [UIFont fontWithName:@"Helvetica" size:15];	
	lastName.backgroundColor = [UIColor clearColor];
	lastName.borderStyle = UITextBorderStyleRoundedRect;
	lastName.center = CGPointMake(562, 226);
	lastName.placeholder = @"Last Name";
	lastName.textAlignment = UITextAlignmentLeft;
	lastName.delegate = self;
	[formView addSubview:lastName];
	
	address1 = [[UITextField alloc] initWithFrame:CGRectMake(0,0,496,30)];
	address1.alpha = 1;
	address1.autocorrectionType = UITextAutocorrectionTypeNo;
	//address1.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	address1.textColor = [UIColor blackColor];
	address1.font = [UIFont fontWithName:@"Helvetica" size:15];	
	address1.backgroundColor = [UIColor clearColor];
	address1.borderStyle = UITextBorderStyleRoundedRect;
	address1.center = CGPointMake(310, 261);
	address1.placeholder = @"Address";
	address1.textAlignment = UITextAlignmentLeft;
	address1.tag = 403;
	address1.delegate = self;
	[formView addSubview:address1];
	
	city = [[UITextField alloc] initWithFrame:CGRectMake(0,0,278,30)];
	city.alpha = 1;
	city.tag = 404;
	city.autocorrectionType = UITextAutocorrectionTypeNo;
	//city.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	city.textColor = [UIColor blackColor];
	city.font = [UIFont fontWithName:@"Helvetica" size:15];	
	city.backgroundColor = [UIColor clearColor];
	city.borderStyle = UITextBorderStyleRoundedRect;
	//city.center = CGPointMake(420, 261);
	city.center = CGPointMake(202, 296);
	city.placeholder = @"City";
	city.textAlignment = UITextAlignmentLeft;
	city.delegate = self;
	[formView addSubview:city];
	
	state = [[UITextField alloc] initWithFrame:CGRectMake(0,0,138,30)];
	state.alpha = 1;
	state.tag = 405;
	state.autocorrectionType = UITextAutocorrectionTypeNo;
	//state.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	state.textColor = [UIColor blackColor];
	state.font = [UIFont fontWithName:@"Helvetica" size:15];	
	state.backgroundColor = [UIColor clearColor];
	state.borderStyle = UITextBorderStyleRoundedRect;
	state.center = CGPointMake(420, 296);
	state.placeholder = @"ST";
	state.textAlignment = UITextAlignmentLeft;
	state.delegate = self;
	[state addTarget:self action:@selector(formatState) forControlEvents:UIControlEventEditingDidEnd];
	[formView addSubview:state];
		
	statesView = [[UIView alloc] initWithFrame:CGRectMake(0,0,314,684)];
	statesView.hidden = YES;
	statesView.tag = -100;
	statesView.backgroundColor = [UIColor clearColor];
	statesView.center = CGPointMake (420, 647.5);
	statesView.userInteractionEnabled = YES;
	[formView addSubview:statesView];
	
	UIButton *st_hotspot = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 138, 30)];
	[st_hotspot setCenter:CGPointMake(420, 296)];
	st_hotspot.backgroundColor = [UIColor clearColor];
	st_hotspot.userInteractionEnabled = YES;
	st_hotspot.hidden = NO;
	st_hotspot.alpha = 1;
	[st_hotspot addTarget:self action:@selector(showStatesView) forControlEvents:UIControlEventTouchDown];
	[formView addSubview:st_hotspot];
	
	MapScroller *statesScrollView = [[MapScroller alloc] initWithFrame:CGRectMake(0,0,314,615)];
	statesScrollView.center = CGPointMake (157, 370.5);
	statesScrollView.hidden = NO;
	statesScrollView.alpha = 1;
	statesScrollView.tag = -101;
	statesScrollView.userInteractionEnabled=YES;
	statesScrollView.showsVerticalScrollIndicator = YES;
	statesScrollView.showsHorizontalScrollIndicator = NO;
	[statesScrollView setBackgroundColor:[UIColor whiteColor]];
	statesScrollView.clipsToBounds = YES;		// default is NO, we want to restrict drawing within our scrollview
	statesScrollView.scrollEnabled = YES;
	statesScrollView.pagingEnabled = NO;
	statesScrollView.maximumZoomScale = 1;
	statesScrollView.minimumZoomScale = 1;
	statesScrollView.contentSize = CGSizeMake(298,2246);
	[statesView addSubview:statesScrollView];
	sArray = [[NSMutableArray alloc] init];
	
	for (int c = 0; c < 51; c++){
		
		State *_s = [[State alloc] initWithFrame:CGRectMake(0,0,298,44)];
		[_s initText:[self getFullState:c]];
		float _y = c*44+20;
		_s.tag = 1000 + c;
		_s.center = CGPointMake (157, _y);
		_s.hidden = NO;
		_s.userInteractionEnabled = YES;
		[sArray addObject:_s];
		[statesScrollView addSubview:_s];
		//[_s release];
	}		
	
	UIImageView *bg_statesView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,314,684)];
	bg_statesView.image = [UIImage imageNamed:@"bg_states_picker.png"];
	bg_statesView.hidden = NO;
	bg_statesView.alpha = 1;
	bg_statesView.tag = -102;
	bg_statesView.center = CGPointMake (157,342);
	bg_statesView.userInteractionEnabled = NO;
	[statesView addSubview:bg_statesView];
	
	
	zip = [[UITextField alloc] initWithFrame:CGRectMake(0,0,204,30)];
	zip.alpha = 1;
	zip.tag = 406;
	zip.autocorrectionType = UITextAutocorrectionTypeNo;
	//zip.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	zip.textColor = [UIColor blackColor];
	zip.font = [UIFont fontWithName:@"Helvetica" size:15];	
	zip.backgroundColor = [UIColor clearColor];
	zip.borderStyle = UITextBorderStyleRoundedRect;
	zip.center = CGPointMake(600, 296);
	zip.placeholder = @"ZIP";
	zip.textAlignment = UITextAlignmentLeft;
	[zip setKeyboardType:UIKeyboardTypeNumberPad];
	zip.delegate = self;
	//[zip addTarget:self action:@selector(formatZip) forControlEvents:UIControlEventEditingChanged];
	[formView addSubview:zip];
	
	/*
	
	address2 = [[UITextField alloc] initWithFrame:CGRectMake(0,0,280,30)];
	//address2.autocapitalizationType = UITextAutocapitalizationTypeWords;
	address2.autocorrectionType = UITextAutocorrectionTypeNo;
	address2.alpha = 1;
	address2.tag = 407;
	//address2.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	address2.textColor = [UIColor blackColor];
	address2.font = [UIFont fontWithName:@"Helvetica" size:15];	
	address2.backgroundColor = [UIColor clearColor];
	address2.borderStyle = UITextBorderStyleRoundedRect;
	address2.center = CGPointMake(202, 296);
	address2.placeholder = @"Address 2";
	address2.textAlignment = UITextAlignmentLeft;
	address2.delegate = self;
	[formView addSubview:address2];
	
	*/
	
	apt = [[UITextField alloc] initWithFrame:CGRectMake(0,0,134,30)];
	apt.alpha = 1;
	apt.tag = 408;
	apt.autocorrectionType = UITextAutocorrectionTypeNo;
	address1.autocapitalizationType = UITextAutocapitalizationTypeNone;
	//apt.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	apt.textColor = [UIColor blackColor];
	apt.font = [UIFont fontWithName:@"Helvetica" size:15];	
	apt.backgroundColor = [UIColor clearColor];
	apt.borderStyle = UITextBorderStyleRoundedRect;
	apt.center = CGPointMake(634, 261);
	apt.placeholder = @"Apt";
	apt.textAlignment = UITextAlignmentLeft;
	apt.delegate = self;
	[formView addSubview:apt];
	
	phone = [[UITextField alloc] initWithFrame:CGRectMake(0,0,278,30)];
	phone.alpha = 1;
	phone.tag = 409;
	phone.autocorrectionType = UITextAutocorrectionTypeNo;
	//phone.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	phone.textColor = [UIColor blackColor];
	phone.font = [UIFont fontWithName:@"Helvetica" size:15];	
	phone.backgroundColor = [UIColor clearColor];
	phone.borderStyle = UITextBorderStyleRoundedRect;
	phone.center = CGPointMake(202, 331);
	phone.placeholder = @"Phone";
	phone.textAlignment = UITextAlignmentLeft;
	phone.delegate = self;
	[phone setKeyboardType:UIKeyboardTypeNumberPad];
	[phone addTarget:self action:@selector(clearFormatPhone) forControlEvents:UIControlEventEditingDidBegin];
	[phone addTarget:self action:@selector(formatPhone) forControlEvents:UIControlEventEditingDidEnd];
	[formView addSubview:phone];
	
	phoneNum = [[NSMutableString alloc] init];
	
	email = [[UITextField alloc] initWithFrame:CGRectMake(0,0,278,30)];
	email.alpha = 1;
	email.tag = 410;
	//email.textColor = [[UIColor alloc] initWithRed:.55 green:.55 blue:.55 alpha:1];
	email.textColor = [UIColor blackColor];
	email.font = [UIFont fontWithName:@"Helvetica" size:15];	
	email.backgroundColor = [UIColor clearColor];
	email.borderStyle = UITextBorderStyleRoundedRect;
	email.center = CGPointMake(490, 331);
	email.placeholder = @"Email";
	email.textAlignment = UITextAlignmentLeft;
	email.autocorrectionType = UITextAutocorrectionTypeNo;
	email.autocapitalizationType = UITextAutocapitalizationTypeNone;
	email.delegate = self;
	//[email addTarget:self action:@selector(formatEmail) forControlEvents:UIControlEventEditingChanged];
	[email setKeyboardType:UIKeyboardTypeEmailAddress];
	[formView addSubview:email];
	
	btn0 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn0 initBtn];
	btn0.tag = 501;
	btn0.center = CGPointMake(81,479);
	[buttonsArray addObject:btn0];
	[formView addSubview:btn0];
	
	btn1 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn1 initBtn];
	btn1.tag = 501;
	btn1.center = CGPointMake(153,479);
	[buttonsArray addObject:btn1];
	[formView addSubview:btn1];
	
	btn2 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn2 initBtn];
	btn2.tag = 502;
	btn2.center = CGPointMake(82,544);
	[buttonsArray addObject:btn2];
	[formView addSubview:btn2];
	
	btn3 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn3 initBtn];
	btn3.tag = 502;
	btn3.center = CGPointMake(154,544);
	[buttonsArray addObject:btn3];
	[formView addSubview:btn3];	
	
	btn4 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn4 initBtn];
	btn4.tag = 503;
	btn4.center = CGPointMake(103,631);
	[buttonsArray addObject:btn4];
	[formView addSubview:btn4];
	
	btn5 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn5 initBtn];
	btn5.tag = 503;
	btn5.center = CGPointMake(420,631);
	[buttonsArray addObject:btn5];
	[formView addSubview:btn5];
	
	btn6 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn6 initBtn];
	btn6.tag = 503;
	btn6.center = CGPointMake(103,669);
	[buttonsArray addObject:btn6];
	[formView addSubview:btn6];
	
	btn7 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn7 initBtn];
	btn7.tag = 503;
	btn7.center = CGPointMake(420,669);
	[buttonsArray addObject:btn7];
	[formView addSubview:btn7];
	
	btn8 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn8 initBtn];
	btn8.tag = 504;
	btn8.center = CGPointMake(103,750);
	[buttonsArray addObject:btn8];
	[formView addSubview:btn8];
	
	btn9 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn9 initBtn];
	btn9.tag = 504;
	btn9.center = CGPointMake(290,750);
	[buttonsArray addObject:btn9];
	[formView addSubview:btn9];
	
	btn10 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn10 initBtn];
	btn10.tag = 504;
	btn10.center = CGPointMake(502,750);
	[buttonsArray addObject:btn10];
	[formView addSubview:btn10];
	
	btn11 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn11 initBtn];
	btn11.tag = 505;
	btn11.center = CGPointMake(103,838);
	[buttonsArray addObject:btn11];
	[formView addSubview:btn11];
	
	btn12 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn12 initBtn];
	btn12.tag = 505;
	btn12.center = CGPointMake(420,838);
	[buttonsArray addObject:btn12];
	[formView addSubview:btn12];
	
	btn13 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn13 initBtn];
	btn13.tag = 505;
	btn13.center = CGPointMake(103,876);
	[buttonsArray addObject:btn13];
	[formView addSubview:btn13];
	
	btn14 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn14 initBtn];
	btn14.tag = 505;
	btn14.center = CGPointMake(420,876);
	[buttonsArray addObject:btn14];
	[formView addSubview:btn14];
	
	btn15 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn15 initBtn];
	btn15.tag = 506;
	btn15.center = CGPointMake(103,957);
	[buttonsArray addObject:btn15];
	[formView addSubview:btn15];
	
	btn16 = [[RadioBtn alloc] initWithFrame:CGRectMake(0,0,50,50)];
	[btn16 initBtn];
	btn16.tag = 506;
	btn16.center = CGPointMake(175,957);
	[buttonsArray addObject:btn16];
	[formView addSubview:btn16];
	
	for (int c = 0; c < [buttonsArray count]; c++){
		UIImageView *temp = [buttonsArray objectAtIndex:c];
		//NSLog(@"buttonsArray object at index %i:%@",c,temp);
		temp.hidden = NO;
		temp.alpha = 1;
		temp.userInteractionEnabled = YES;
		//[formView bringSubviewToFront:temp];
	}
	[self.view bringSubviewToFront:formView];
	
	float _x = 120;
	
	r1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,134,67)];
	r1.image = [UIImage imageNamed:@"required.png"];
	r1.hidden = NO;
	r1.alpha = 0;
	r1.tag = 0;
	r1.center = CGPointMake(_x, 375);
	//r1.center = CGPointMake(384, 375);
	[formView addSubview:r1];

	r2 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,134,67)];
	r2.image = [UIImage imageNamed:@"required.png"];
	r2.hidden = NO;
	r2.alpha = 0;
	r2.tag = 1;
	r2.center = CGPointMake(_x, 455);
	//r2.center = CGPointMake(284, 455);
	[formView addSubview:r2];
	
	r3 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,134,67)];
	r3.image = [UIImage imageNamed:@"required.png"];
	r3.hidden = NO;
	r3.alpha = 0;
	r3.tag = 2;
	r3.center = CGPointMake(_x, 522);
	//r3.center = CGPointMake(384, 522);
	[formView addSubview:r3];
	
	r4 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,134,67)];
	r4.image = [UIImage imageNamed:@"required.png"];
	r4.hidden = NO;
	r4.alpha = 0;
	r4.tag = 3;
	r4.center = CGPointMake(_x, 660);
	//r4.center = CGPointMake(384, 660);
	[formView addSubview:r4];

	r5 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,134,67)];
	r5.image = [UIImage imageNamed:@"required.png"];
	r5.hidden = NO;
	r5.alpha = 0;
	r5.tag = 4;
	r5.center = CGPointMake(_x, 746);
	//r5.center = CGPointMake(384, 746);
	[formView addSubview:r5];
	
	r6 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,134,67)];
	r6.image = [UIImage imageNamed:@"required.png"];
	r6.hidden = NO;
	r6.alpha = 0;
	r6.tag = 5;
	r6.center = CGPointMake(_x, 866);
	//r6.center = CGPointMake(304, 866);
	[formView addSubview:r6];

	q7_txt = [[NSString alloc] init];
	
	rX = [[NSMutableArray alloc] init];
	[rX addObject:@"384"];
	[rX addObject:@"284"];
	[rX addObject:@"384"];
	[rX addObject:@"384"];
	[rX addObject:@"384"];
	[rX addObject:@"304"];

	rY = [[NSMutableArray alloc] init];
	[rY addObject:@"375"];
	[rY addObject:@"455"];
	[rY addObject:@"522"];
	[rY addObject:@"660"];
	[rY addObject:@"746"];
	[rY addObject:@"866"];
	
	rt1 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,134,30)];
	rt1.alpha = 0;
	rt1.textColor = [UIColor redColor];
	rt1.font = [UIFont fontWithName:@"Helvetica" size:15];	
	rt1.backgroundColor = [UIColor clearColor];
	rt1.center = CGPointMake(260, 225);
	rt1.text = @"*required";
	rt1.textAlignment = UITextAlignmentRight;
	[formView addSubview:rt1];

	rt2 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,134,30)];
	rt2.alpha = 0;
	rt2.textColor = [UIColor redColor];
	rt2.font = [UIFont fontWithName:@"Helvetica" size:15];	
	rt2.backgroundColor = [UIColor clearColor];
	rt2.center = CGPointMake(620, 225);
	rt2.text = @"*required";
	rt2.textAlignment = UITextAlignmentRight;
	[formView addSubview:rt2];

	rt3 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,134,30)];
	rt3.alpha = 0;
	rt3.textColor = [UIColor redColor];
	rt3.font = [UIFont fontWithName:@"Helvetica" size:15];	
	rt3.backgroundColor = [UIColor clearColor];
	rt3.center = CGPointMake(260, 260);
	rt3.text = @"*required";
	rt3.textAlignment = UITextAlignmentRight;
	[formView addSubview:rt3];	

	rt4 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,134,30)];
	rt4.alpha = 0;
	rt4.textColor = [UIColor redColor];
	rt4.font = [UIFont fontWithName:@"Helvetica" size:15];	
	rt4.backgroundColor = [UIColor clearColor];
	rt4.center = CGPointMake(408, 260);
	rt4.text = @"*required";
	rt4.textAlignment = UITextAlignmentRight;
	[formView addSubview:rt4];	

	rt5 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,134,30)];
	rt5.alpha = 0;
	rt5.textColor = [UIColor redColor];
	rt5.font = [UIFont fontWithName:@"Helvetica" size:15];	
	rt5.backgroundColor = [UIColor clearColor];
	rt5.center = CGPointMake(620, 260);
	rt5.text = @"*required";
	rt5.textAlignment = UITextAlignmentRight;
	[formView addSubview:rt5];
	
	rt6 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,30)];
	rt6.alpha = 0;
	rt6.textColor = [UIColor redColor];
	rt6.font = [UIFont fontWithName:@"Helvetica" size:15];	
	rt6.backgroundColor = [UIColor clearColor];
	rt6.center = CGPointMake(587, 295);
	rt6.text = @"*phone or email required";
	rt6.textAlignment = UITextAlignmentRight;
	[formView addSubview:rt6];

	rt7 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,30)];
	rt7.alpha = 0;
	rt7.textColor = [UIColor redColor];
	rt7.font = [UIFont fontWithName:@"Helvetica" size:15];	
	rt7.backgroundColor = [UIColor clearColor];
	rt7.center = CGPointMake(227, 330);
	rt7.text = @"*email or phone required";
	rt7.textAlignment = UITextAlignmentRight;
	[formView addSubview:rt7];
	
	// question 7
	
	q7 = [[MapScroller alloc] initWithFrame:CGRectMake(0,0,314,247)];
	q7.center = CGPointMake (219, 824);
	q7.hidden = NO;
	q7.alpha = 0;
	q7.tag = 699;
	q7.userInteractionEnabled=YES;
	q7.showsVerticalScrollIndicator = YES;
	q7.showsHorizontalScrollIndicator = NO;
	[q7 setBackgroundColor:[UIColor clearColor]];
	q7.clipsToBounds = YES;		// default is NO, we want to restrict drawing within our scrollview
	q7.scrollEnabled = YES;
	q7.pagingEnabled = NO;
	q7.maximumZoomScale = 1;
	q7.minimumZoomScale = 1;
	q7.contentSize = CGSizeMake(298,176);
	[formView addSubview:q7];
	
	NSArray *q7Array = [NSArray arrayWithObjects:@"Less than 1 month",
						@"2-3 months",@"4-6 months",@"More than 6 months",nil];
	qArray = [[NSMutableArray alloc] init];
	for (int d = 0; d < 4; d++){
		State *_q = [[State alloc] initWithFrame:CGRectMake(0,0,298,44)];
		[_q initText:[q7Array objectAtIndex:d]];
		float _y = d*44+30;
		_q.tag = 700 + d;
		_q.center = CGPointMake (157, _y);
		_q.hidden = NO;
		_q.userInteractionEnabled = YES;
		[qArray addObject:_q];
		[q7 addSubview:_q];
	}		
	
	UIImageView *bg_q7 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,314,247)];
	bg_q7.image = [UIImage imageNamed:@"bg_q7.png"];
	bg_q7.hidden = NO;
	bg_q7.alpha = 1;
	bg_q7.tag = -102;
	bg_q7.center = CGPointMake (157,123.5);
	bg_q7.userInteractionEnabled = NO;
	[q7 addSubview:bg_q7];
		
	admin = [[AdminView alloc] initWithFrame:CGRectMake(0,0,768,1024)];
	admin.center = CGPointMake(384,512);
	admin.hidden = YES;
	admin.userInteractionEnabled  = YES;
	admin.backgroundColor = [UIColor blackColor];
	[admin initAdminView];
	[self.view addSubview:admin];
	
	UIView *adminHotSpot = [[UIView alloc] initWithFrame:CGRectMake(0,0,150,150)];
	adminHotSpot.backgroundColor = [UIColor clearColor];
	adminHotSpot.hidden = NO;
	adminHotSpot.tag = 300;
	adminHotSpot.center = CGPointMake(693, 75);
	adminHotSpot.userInteractionEnabled = YES;
	[formView addSubview:adminHotSpot];
	
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showEmailModalView) name:@"EmailData" object:admin];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showForm) name:@"ReturnToForm" object:admin];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteContents) name:@"ClearData" object:admin];
	
	
	formData = [[NSString alloc] initWithString:@""];
	
	NSFileManager *filemgr;
	filemgr = [NSFileManager defaultManager];
	NSData *dataBuffer = [[NSData alloc] init];
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
	NSString *documentsDirectory = [paths objectAtIndex:0]; 
	NSString *path = [NSString stringWithFormat:@"%@/SubaruData.txt",documentsDirectory];
	NSLog(@"%@",path);
	if ([filemgr fileExistsAtPath:path] == YES) {
        dataBuffer = [filemgr contentsAtPath: path];
		formData = [NSString stringWithUTF8String:[dataBuffer bytes]];
		NSLog (@"File exists");
		NSLog(@"formData to start file:\r\n%@",formData);
	}
	else {
        NSLog (@"File not found");
		NSError *error = noErr;
		NSString *introText = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",[questionsTextArray objectAtIndex:0],
							   [questionsTextArray objectAtIndex:1],[questionsTextArray objectAtIndex:3],
							   [questionsTextArray objectAtIndex:4],[questionsTextArray objectAtIndex:5],
							   [questionsTextArray objectAtIndex:2],[questionsTextArray objectAtIndex:6],
							   [questionsTextArray objectAtIndex:7]];
		
        BOOL success = [introText writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
        if (!success) {
            // handle the error
            NSLog(@"%@", error);
        }
		else {
			NSLog(@"new file created. header text: %@",introText);
		}
	}
	
	toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 768, 50)];
	toolbar.center = CGPointMake(384, 1050);
	toolbar.barStyle = UIBarStyleDefault;
	//toolbar.barStyle = UIBarStyleBlackTranslucent;
	toolbar.tintColor = [UIColor lightGrayColor];
	//toolbar.tintColor = [[UIColor alloc] initWithRed:.07 green:.11 blue:.17 alpha:1];
	UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:@"prev" style:UIBarButtonItemStyleBordered target:self action:@selector(prevClicked)];
	previous.tag = 0;
	UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStyleBordered target:self action:@selector(nextClicked)];
	next.tag = 1;
	UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStyleDone target:self action:@selector(doneClicked)];
	done.tag = 2;
	UIBarButtonItem *BtnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
	NSArray *items = [[NSArray alloc] initWithObjects: previous, next, BtnSpace, done, nil];
	[toolbar setItems:items];
	[items release];
	[formView addSubview:toolbar];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
   //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; 
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; 
	
	currQ7 = 0;
	currState = 0;
	
	
	[formView bringSubviewToFront:statesView];
}

- (void)keyboardWillShow:(NSNotification *)notification {
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:.4];
	toolbar.center = CGPointMake(384, 736);
	[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:.4];
	toolbar.center = CGPointMake(384, 1050);
	[UIView commitAnimations];
}
-(void)nextClicked {
	NSLog(@"textFieldShouldReturn");
	currTextField++;
	if (currTextField == 405){
		[self hideKeyboard];
		[self showStatesView];
	}
	else {
		NSInteger nextTag = currTextField;
		// Try to find next responder
		UIResponder* nextResponder = [self.view viewWithTag:nextTag];
		if (nextResponder) {
			// Found next responder, so set it.
			[nextResponder becomeFirstResponder];
		} else {
			// Not found, so remove keyboard.
			[self hideKeyboard];
		}
	}
}
-(void)prevClicked {
	 NSLog(@"textFieldShouldReturn");
	currTextField--;
	if (currTextField == 405){
		[self hideKeyboard];
		[self showStatesView];
	}
	else {
		NSInteger nextTag = currTextField;
		 // Try to find next responder
		 UIResponder* nextResponder = [self.view viewWithTag:nextTag];
		 if (nextResponder) {
			 // Found next responder, so set it.
			 [nextResponder becomeFirstResponder];
		 } else {
			 // Not found, so remove keyboard.
			 [self hideKeyboard];
		 }
	}
}
	 
-(void) doneClicked {
	//UIBarButtonItem *b = sender;
	//NSLog(@"button tag: %i",b.tag);
	//if (b.tag == 2){
		[self hideKeyboard];
	//}
}

-(void) formatState {
	state.text = [state.text uppercaseString];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
	//NSLog(@"textField function");
	BOOL results = FALSE;
	
	//NSLog(@"tf(): %i",textField.tag);
	/*
	 403 address 1
	 405 state
	 406 zip
	 409 phone
	 410 email
	*/
	 
	currTextField = textField.tag;
	if (textField.tag != 405){
		[self hideStatesView];
	}
	if (textField.tag == 405){ // state  
		
		results = FALSE;
		[self hideKeyboard];
		[self showStatesView];
		// code below limits to two characters and capitalizes string - with scrollview
		// we can just kill any editing here
		
		/*
		NSLog(@"zip code textfield!");
		NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", @"D",@"E",@"F",
							   @"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",
							   @"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
		if ([alphaArray containsObject:[string uppercaseString]]) {
			if ([state.text length] < 2){
				results = TRUE;
			}
		}
		else if ([string isEqualToString:@""]) {
			results = TRUE;
		}
		*/
		
	}
	else if (textField.tag == 406){ // zip code 
		NSLog(@"zip textfield!");
		NSArray* numericArray = [NSArray arrayWithObjects: @"0", @"1", @"2", @"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
		
		if ([numericArray containsObject:string]){
			if ([zip.text length] < 5){
				results = TRUE;
			}
		}
		else if ([string isEqualToString:@""]) {
			results = TRUE;
		}
	}
	else if (textField.tag == 409){ // phone
		//NSLog(@"phone textfield!");
		NSArray* numericArray = [NSArray arrayWithObjects: @"0", @"1", @"2", @"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
		
		if ([numericArray containsObject:string]){
			if ([phoneNum length] < 10){
				results = TRUE;
				[phoneNum appendFormat:string];
		//		NSLog(@"phoneNum: %@",phoneNum);
			}	
		}
		else if ([string isEqualToString:@""]) {
			results = TRUE;
			[phoneNum replaceCharactersInRange:NSMakeRange([phoneNum length]-1, 1) withString:@""];
		}
	}
	else {
		results = TRUE;
	}
	
	return results;
}

-(void) clearFormatPhone {
	phone.text = phoneNum;
}

-(void) formatPhone {
	
	if ([phoneNum length] > 3){
		NSString *tempStr = [[NSString alloc] initWithString:@""];
		NSString *areaCode = [phoneNum substringToIndex:3];
		NSString *phone1;
		NSString *phone2;
		NSRange range;
		
		if ([phoneNum length] < 7) {
			range.length = [phoneNum length] - 3;
			range.location = 3;
			
			phone1 = [phoneNum substringWithRange:range];
			tempStr	= [NSString stringWithFormat:@"(%@) %@", areaCode, phone1];	
		}
		else if ([phoneNum length] >= 7) {
			range.length = 3;
			range.location = 3;
		
			phone1 = [phoneNum substringWithRange:range];
			phone2 = [phoneNum substringFromIndex:6];
			tempStr	= [NSString stringWithFormat:@"(%@) %@-%@", areaCode, phone1,phone2];	
		}
		phone.text = tempStr;
	}
}

-(BOOL) emailIsValid {
	BOOL isAtSymbol = FALSE;
	BOOL isPeriod = FALSE;
	NSRange range;
	
	for (int i = 0; i < [email.text length]; i++){
		range.length = 1;
		range.location = i;
		
		NSString *temp = [email.text substringWithRange:range];
		if ([temp isEqualToString:@"@"]){
			isAtSymbol = TRUE;
		}
		else if ([temp isEqualToString:@"."]) {
			isPeriod = TRUE;
		}
	}
	if (isAtSymbol && isPeriod){
		return TRUE;
	}
	else {
		return FALSE;
	}
}

-(void)showAdmin{
	[admin showKeyboard];
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:1];
	//[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
	[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[self.view superview] cache:YES];
	admin.hidden = NO;
	formView.hidden = YES;
	
	[UIView commitAnimations];	
}
-(void)showForm{
	[self resetForm];
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:1];
	[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[self.view superview] cache:YES];
	admin.hidden = YES;
	formView.hidden = NO;
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(hideAdminKeyboard)];
	[UIView commitAnimations];	
}
-(void)showQ7 {
	q7.alpha = 1;
}
-(void)hideQ7 {
	q7.alpha = 0;
}
-(void)hideAdminKeyboard{
	[admin hideKeyboard];	
}

-(void) showReqText:(UILabel *)_l {
	[UIView beginAnimations:nil context:_l];
	//[UIView setAnimationDelay:_delay];
	[UIView setAnimationDuration:.25];		
	_l.alpha = 1;
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(hideReqText:finished:context:)];
	[UIView commitAnimations];		
}
-(void) hideReqText:(NSString *)animationID finished:(NSNumber *)finished context:(void *)_l {
	UILabel *tempLabel = (UILabel *)_l;
	[UIView beginAnimations:nil context:tempLabel];
	[UIView setAnimationDelay:2.25];
	[UIView setAnimationDuration:.25];		
	tempLabel.alpha = 0;
	[UIView commitAnimations];			
}

-(void) showReq:(UIImageView *)_v {
	//float _x = [[rX objectAtIndex:_v.tag] floatValue];
	float _y = [[rY objectAtIndex:_v.tag] floatValue] + 30;
	float _delay = _v.tag *.1;
	float _x = 120;
	[UIView beginAnimations:nil context:_v];
	[UIView setAnimationDelay:_delay];
	[UIView setAnimationDuration:.25];		
	_v.center = CGPointMake(_x,_y);
	_v.alpha = 1;
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(showReq2:finished:context:)];
	[UIView commitAnimations];	
}
-(void) showReq2:(NSString *)animationID finished:(NSNumber *)finished context:(void *)_c {
	UIImageView *_v = (UIImageView *)_c;
	//float _x = [[rX objectAtIndex:_v.tag] floatValue];
	float _y = [[rY objectAtIndex:_v.tag] floatValue]+15;
	float _x = 120;
	[UIView beginAnimations:nil context:_v];
	[UIView setAnimationDuration:.1];		
	_v.center = CGPointMake(_x,_y);
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(showReq3:finished:context:)];
	[UIView commitAnimations];	
}
-(void) showReq3:(NSString *)animationID finished:(NSNumber *)finished context:(void *)_c {
	UIImageView *_v = (UIImageView *)_c;
	//float _x = [[rX objectAtIndex:_v.tag] floatValue];
	float _y = [[rY objectAtIndex:_v.tag] floatValue]+30;
	//NSLog(@"ShowReq3 y: %f",_y);
	
	float _x = 120;
	[UIView beginAnimations:nil context:_v];
	[UIView setAnimationDuration:.1];		
	_v.center = CGPointMake(_x,_y);
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(hideReq:finished:context:)];
	[UIView commitAnimations];	
}

-(void) hideReq:(NSString *)animationID finished:(NSNumber *)finished context:(void *)_c {
	//NSLog(@"context: %@",_c);
	UIImageView *_v = (UIImageView *)_c;
	//float _x = [[rX objectAtIndex:_v.tag] floatValue];
	float _y = [[rY objectAtIndex:_v.tag] floatValue];
	
	float _x = 120;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDelay:1.5];
	[UIView setAnimationDuration:.25];
	_v.center = CGPointMake(_x, _y);
	_v.alpha = 0;
	[UIView commitAnimations];		
}
-(void)showStatesView {
	statesView.hidden = NO;
	[self hideKeyboard];
}
-(void)hideStatesView {
	statesView.hidden = YES;
}
-(void)clearQ7:(int)i {
	NSLog(@"clearQ7: %@",[qArray objectAtIndex:i]);
	State *temp = [qArray objectAtIndex:i];
	[temp upState];	
}
-(void)clearState:(int)i {
	NSLog(@"clearState: %@",[sArray objectAtIndex:i]);
	State *temp = [sArray objectAtIndex:i];
	[temp upState];
}
-(BOOL) contentIsGood {
	BOOL results = TRUE;
	if ([firstName.text isEqualToString: @""] || (firstName.text == NULL)) {
		results = FALSE;
		[self showReqText:rt1];
	}
	if ([lastName.text isEqualToString: @""] || (lastName.text == NULL)) {
		results = FALSE;
		[self showReqText:rt2];		
	}
	if ([address1.text isEqualToString: @""] || (address1.text == NULL)) {
		results = FALSE;
		[self showReqText:rt3];		
	}
	if ([city.text isEqualToString: @""] || (city.text == NULL)) {
		results = FALSE;
		[self showReqText:rt4];		
	}
	if ([zip.text isEqualToString: @""] || (zip.text == NULL)) {
		results = FALSE;
		[self showReqText:rt5];		
	}
	if ((([phone.text isEqualToString: @""]) && ([email.text isEqualToString: @""])) || ((phone.text == NULL) && (email.text == NULL))){
		results = FALSE;
		rt6.text = @"*phone or email required";
		[self showReqText:rt6];
		rt7.text = @"*email or phone required";
		[self showReqText:rt7];
	}
	if (([phoneNum length] > 0) && ([phoneNum length] < 10)){
		results = FALSE;
		rt6.text = @"*10 digits required";
		[self showReqText:rt6];		
	}
	if ([email.text length] > 0) {
		if (![self emailIsValid]){
			rt7.text = @"*invalid address";
			[self showReqText:rt7];
			results = FALSE;
		}
	}
	NSLog(@"btn0: %d",[btn0 btnTouched]);
	if (![btn0 btnTouched] && ![btn1 btnTouched]) {
		[self showReq:r1];	
		results = FALSE;
	}
	if (![btn2 btnTouched] && ![btn3 btnTouched]) {
		[self showReq:r2];	
		results = FALSE;
	}
	if (![btn4 btnTouched] && ![btn5 btnTouched] && ![btn6 btnTouched] && ![btn7 btnTouched]) {
		[self showReq:r3];		
		results = FALSE;
	}
	if (![btn8 btnTouched] && ![btn9 btnTouched] && ![btn10 btnTouched]) {
		[self showReq:r4];
		results = FALSE;
	}
	if (![btn11 btnTouched] && ![btn12 btnTouched] && ![btn13 btnTouched] && ![btn14 btnTouched]) {
		[self showReq:r5];
		results = FALSE;
	}
	if (![btn15 btnTouched] && ![btn16 btnTouched]) {
		[self showReq:r6];		
		results = FALSE;
	}
	if ([btn15 btnTouched]){
		if ((q7_txt == NULL) || [q7_txt isEqualToString:@""]) {
			results = FALSE;
		}
	}
	return results;
}

-(NSString *) appendFormData {
	NSString *tempAddress;
	//if ((address2.text != NULL) && ![address2.text isEqualToString:@""]){
	//	tempAddress = [NSString stringWithFormat:@"\"%@, %@\"",address1.text,address2.text];
	//}
	//else {
		tempAddress = [NSString stringWithFormat:@"%@",address1.text];
	//}
	NSString *results = [NSString stringWithFormat:@"\r\n%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,",
						 lastName.text,firstName.text,middleName.text,
						 tempAddress,apt.text,city.text,state.text,
						 zip.text,phone.text,email.text];
	
	
	for (int c = 0; c < [buttonsArray count]; c++){
		RadioBtn *b = [buttonsArray objectAtIndex:c];
		//NSLog(@"c: %i",c);
		if ([b btnTouched]){
			//NSLog(@"button touched: %i",c);
			NSString *tempString = [answersTextArray objectAtIndex:c];
			results = [results stringByAppendingFormat:@"%@,",tempString];
			//NSLog(@"answer text: %@",[answersTextArray objectAtIndex:c]);
		}
	}
	if ([btn15 btnTouched]){
		results = [results stringByAppendingFormat:@"%@",[[qArray objectAtIndex:currQ7] getTxt]];
	}
//	NSLog(@"results: %@",results);
//	NSLog(@"writeContents function - formData: %@\n",formData);
	
	return results;
}

-(void) submitForm {
	if ([self contentIsGood]){
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
		NSString *documentsDirectory = [paths objectAtIndex:0]; 
		NSString *path = [NSString stringWithFormat:@"%@/SubaruData.txt",documentsDirectory];
		NSString *results = [self appendFormData];
		//NSLog(@"submitForm results: %@",results);
		[self writeContents:results toFile:path];
		[UIView beginAnimations:nil context:nil];
		[UIView setAnimationDelay:.5];
		[UIView setAnimationDuration:1];
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[self.view superview] cache:YES];
		formView.hidden = YES;
		thankyouView.hidden = NO;
		//the selector only works if you set the delegate here
		//[UIView setAnimationDidStopSelector:@selector(resetForm)];
		//[UIView setAnimationDelegate:self];
		[UIView commitAnimations];		
	}
}
-(void)hideKeyboard {
	[firstName resignFirstResponder];
	[middleName resignFirstResponder];
	[lastName resignFirstResponder];
	[address1 resignFirstResponder];
	[city resignFirstResponder];
	[state resignFirstResponder];
	[zip resignFirstResponder];
	[address2 resignFirstResponder];
	[apt resignFirstResponder];
	[phone resignFirstResponder];
	[email resignFirstResponder];	
}
-(void) resetForm {
	
	for (int c = 0; c < [buttonsArray count]; c++){
		RadioBtn *temp = [buttonsArray objectAtIndex:c];
		[temp btnOff];
	}
	firstName.text = @"";
	middleName.text = @"";
	lastName.text = @"";
	address1.text = @"";
	city.text = @"";
	state.text = @"";
	zip.text = @"";
	address2.text = @"";
	apt.text = @"";
	phone.text = @"";
	email.text = @"";
	[phoneNum setString:@""];
	
	[UIView beginAnimations:nil context:nil];
	//[UIView setAnimationDelay:1.5];
	[UIView setAnimationDuration:1];
	[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[self.view superview] cache:YES];
	formView.hidden = NO;
	thankyouView.hidden = YES;
	[UIView commitAnimations];		
	
	[self clearState:currState];
	[self clearQ7:currQ7];
	currState = 0;
	currQ7 = 0;
}

-(void)updateButtons:(RadioBtn *)_btn {
	//NSLog(@"updateBUttons, received: %@",_btn);
	for (int c = 0; c < [buttonsArray count]; c++) {
		RadioBtn *temp = [buttonsArray objectAtIndex:c];
		if (temp.tag == _btn.tag){
			if (temp != _btn){
				[temp btnOff];
			}
			//		NSLog(@"tag match for: %i",c);
		}
		
	}
	
}
-(NSString *)getFullState:(int)_i {
	NSArray *statesFullArray = [NSArray arrayWithObjects:@"Alaska",@"Alabama",@"Arkansas",@"Arizona",
								@"California",@"Colorado",@"Connecticut",@"District of Columbia",
								@"Delaware",@"Florida",@"Georgia",@"Hawaii",@"Iowa",@"Idaho",
								@"Illinois",@"Indiana",@"Kansas",@"Kentucky",@"Louisiana",
								@"Massachusetts",@"Maryland",@"Maine",@"Michigan",@"Minnesota",
								@"Missouri",@"Mississippi",@"Montana",@"North Carolina",
								@"North Dakota",@"Nebraska",@"New Hampshire",@"New Jersey",
								@"New Mexico",@"Nevada",@"New York",@"Ohio",@"Oklahoma",
								@"Oregon",@"Pennsylvania",@"Rhode Island",@"South Carolina",
								@"South Dakota",@"Tennessee",@"Texas",@"Utah",@"Virginia",
								@"Vermont",@"Washington",@"Wisconsin", @"West Virginia",
								@"Wyoming",nil];
	
	return [statesFullArray objectAtIndex:_i];
}


-(NSString *)getState:(int)_i {
	NSArray *statesArray = [NSArray arrayWithObjects:@"AK",@"AL",@"AR",@"AZ",@"CA",@"CO",@"CT",
							@"DC",@"DE",@"FL",@"GA",@"HI",@"IA",@"ID",
							@"IL",@"IN",@"KS",@"KY",@"LA",@"MA",@"MD",
							@"ME",@"MI",@"MN",@"MO",@"MS",@"MT",@"NC",
							@"ND",@"NE",@"NH",@"NJ",@"NM",@"NV",@"NY",
							@"OH",@"OK",@"OR",@"PA",@"RI",@"SC",@"SD",
							@"TN",@"TX",@"UT",@"VA",@"VT",@"WA",@"WI",
							@"WV",@"WY",nil];	
	return [statesArray objectAtIndex:_i];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event	{
	UITouch *touch = [touches anyObject];
	//NSLog(@"touch: %@",touch.view);
	//UIImageView *_v = (UIImageView *)touch.view;
	NSLog(@"button touch: %i",touch.view.tag);
	
	// radio buttons
	if ((touch.view.tag > 500) && (touch.view.tag < 600)){
		RadioBtn *temp = (RadioBtn *) touch.view;

		if (temp == btn15){
			[self showQ7];
		}
		if (temp == btn16){
			[self hideQ7];
		}
		
		[self hideKeyboard];
		[self hideStatesView];
		[self updateButtons:temp];
	}
	else if ((touch.view.tag == 10) && ([touch tapCount] == 3)){
		[self deleteContents];
	}
	// quiz background
	else if (touch.view.tag == 10){
		[self hideStatesView];
		[self hideKeyboard];
	}
	//thank you page
	else if ((touch.view.tag == 110) && ([touch tapCount] == 2)){
		[self resetForm];
	}
	// admin hotspot in top right corner
	else if ((touch.view.tag == 300) && ([touch tapCount] == 3) && (!formView.hidden)){
		[self showAdmin];
	}
	// question 7 responses
	else if ((touch.view.tag >= 700) && (touch.view.tag < 800)) {
		State *temp = (State *)touch.view;
		[temp downState];
		[self hideStatesView];
		int _s = touch.view.tag - 700;
		if (currQ7 != _s){
			[self clearQ7:currQ7];
			currQ7 = _s;
		}
	} 	
	// states 
	else if (touch.view.tag >= 1000) {
		State *temp = (State *)touch.view;
		[temp downState];
		// if current touched item is not the previous one, clear previous
		int _t = touch.view.tag - 1000;
		if (currState != _t){
			NSLog(@"touches ended 1000, tag: %i",_t);
			[self clearState:currState];
			currState = _t;
		}
	}
	
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event	{
	UITouch *touch = [touches anyObject];
	//UIImageView *_v = (UIImageView *)touch.view;
	
	if ((touch.view.tag > 500) && (touch.view.tag < 600)){
		RadioBtn *temp = (RadioBtn *) touch.view;
		//[temp btnOff];
		[temp btnOn];
	}
	else if ((touch.view.tag >= 700) && (touch.view.tag < 800)) {
		State *temp = (State *)touch.view;
		//[temp upState];
		q7_txt = [temp getTxt];
		[self hideQ7];
	}
	else if (touch.view.tag >= 1000) {
		int _t = touch.view.tag - 1000;
		state.text = [self getState:_t];
		[self hideStatesView];
	}
}

// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{ 
	// Notifies users about errors associated with the interface
	switch (result)
	{
		case MFMailComposeResultCancelled:
			break;
		case MFMailComposeResultSaved:
			break;
		case MFMailComposeResultSent:
			break;
		case MFMailComposeResultFailed:
			break;
			
		default:
		{
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Sending Failed - Unknown Error :-("
														   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
			[alert show];
			[alert release];
		}
			
			break;
	}
	[self dismissModalViewControllerAnimated:YES];
}

///
-(void) showEmailModalView {
	NSFileManager *filemgr;
	filemgr = [NSFileManager defaultManager];
	NSData *dataBuffer = [[NSData alloc] init];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; 
	NSString *path = [NSString stringWithFormat:@"%@/SubaruData.txt",documentsDirectory];
	dataBuffer = [filemgr contentsAtPath:path];
	//NSString *formText = [NSString stringWithUTF8String:[dataBuffer bytes]];
	//NSLog(@"formText: %@",formText);
	
	if (dataBuffer != NULL){
		
		MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
		picker.mailComposeDelegate = self; // &lt;- very important step if you want feedbacks on what the user did with your email sheet
		[picker addAttachmentData:dataBuffer mimeType:@"text/csv" fileName:@"SubaruData.csv"];

		[picker setSubject:@"Subaru Event - Form Submission Data"];
		
		// Fill out the email body text
		//NSString *emailBody = formText;
		NSString *emailBody = @"Subaru data file attached!";
		
		[picker setMessageBody:emailBody isHTML:YES]; // depends. Mostly YES, unless you want to send it as plain text (boring)
		
		picker.navigationBar.barStyle = UIBarStyleBlack; // choose your style, unfortunately, Translucent colors behave quirky.
		
		[self presentModalViewController:picker animated:YES];
		[picker release];
		
		NSLog(@"CSV sent: %@",[NSString stringWithUTF8String:[dataBuffer bytes]]);
	}
	else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Survey Data" message:@"In order to email the survey, you must have stored data."
													   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
		[alert show];
		[alert release];	
	}
	 
}


- (void)deleteContents {
	NSLog(@"deletecontents");
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
	NSString *documentsDirectory = [paths objectAtIndex:0]; 
	NSString *filePath = [NSString stringWithFormat:@"%@/SubaruData.txt",documentsDirectory];
	
	NSString *introText = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",[questionsTextArray objectAtIndex:0],
						   [questionsTextArray objectAtIndex:1],[questionsTextArray objectAtIndex:3],
						   [questionsTextArray objectAtIndex:4],[questionsTextArray objectAtIndex:5],
						   [questionsTextArray objectAtIndex:2],[questionsTextArray objectAtIndex:6],
						   [questionsTextArray objectAtIndex:7]];
	
	/*NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {
		NSLog(@"file doesn't exist, can't delete");
	}	
	else {*/
		// get a handle to the file
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
		
        [fileHandle truncateFileAtOffset: 0];	

    	// convert the string to an NSData object
		NSData *textData = [introText dataUsingEncoding:NSUTF8StringEncoding];
	
		// write the data to the end of the file
		[fileHandle writeData:textData];
	
		[fileHandle closeFile];
	//}
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:@"You have cleared the survey data."
												   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
	[alert show];
	[alert release];
}

- (void)writeContents:(NSString *)text toFile:(NSString *)filePath {
	// NSFileHandle won't create the file for us, so we need to check to make sure it exists
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {
		NSLog(@"File doesn't exist, write out the text");
        // the file doesn't exist yet, so we can just write out the text using the 
        // NSString convenience method
		
        NSError *error = noErr;
		NSString *introText = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",[questionsTextArray objectAtIndex:0],
							   [questionsTextArray objectAtIndex:1],[questionsTextArray objectAtIndex:3],
							   [questionsTextArray objectAtIndex:4],[questionsTextArray objectAtIndex:5],
							   [questionsTextArray objectAtIndex:2],[questionsTextArray objectAtIndex:6],
							   [questionsTextArray objectAtIndex:7],text];
		
        BOOL success = [introText writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
        if (!success) {
            // handle the error
            NSLog(@"%@", error);
        }
		
    } else {
		NSLog(@"File exists, appending new text to end");
		
        // the file already exists, so we should append the text to the end
		
        // get a handle to the file
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
		
        // move to the end of the file
        [fileHandle seekToEndOfFile];
		
        // convert the string to an NSData object
        NSData *textData = [text dataUsingEncoding:NSUTF8StringEncoding];
		
        // write the data to the end of the file
        [fileHandle writeData:textData];
		
        // clean up
        [fileHandle closeFile];
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[admin release];
	[formView release];
	[formData release];
	[thankyouView release];
	[buttonsArray release];
	[submit release];
	[firstName release];
	[middleName release];
	[lastName release];
	[address1 release];
	[city release];
	[state release];
	[statesView release];
	[sArray release];
	[zip release];
	[address2 release];
	[apt release];
	[phone release];
	[phoneNum release];
	[email release];
	[answersTextArray release];
	[questionsTextArray release];
	[r1 release];
	[r2 release];
	[r3 release];
	[r4 release];
	[r5 release];
	[r6 release];
	[rt1 release];
	[rt2 release];
	[rt3 release];
	[rt4 release];
	[rt5 release];
	[rt6 release];
	[rt7 release];
	[q7 release];
	[q7_txt release];
	[qArray release];
	[rX release];
	[rY release];
	[btn0 release];
	[btn1 release];
	[btn2 release];
	[btn3 release];
	[btn4 release];
	[btn5 release];
	[btn6 release];
	[btn7 release];
	[btn8 release];
	[btn9 release];
	[btn10 release];
	[btn11 release];
	[btn12 release];
	[btn13 release];
	[btn14 release];
	[btn15 release];
	[btn16 release];
	[toolbar release];
	[super dealloc];
}

@end
