var equote = new Class({

    options: {
    
    	dba: null,
    	isbid: false,
    	request_type: null,
    	setup_type: null,
    	nobusinesses: true,
    	
    	state: ( $('state') ? $('state') : null ),
    	city: ( $('city') ? $('city') : null ),
    	zip: ( $('zip') ? $('zip') : null ),
    	name: ( $('name') ? $('name') : null ),
    	email: ( $('email') ? $('email') : null ),
    	phone: ( $('phone') ? $('phone') : null ),
    	notes: ( $('notes') ? $('notes') : null ),
    	reviewtext: ( $('review-text') ? $('review-text') : null ),
    
    	text: {
    		'bid': '<label style="font-weight: normal;">Okay, so we got the following from you. Your name is <b>{name}</b> and your phone number and email address are <b>{phone}</b> and <b>{email}</b> respectively. We recorded that you live in <b>{city}, {state}</b> and found <b>{num_businesses}</b> an Accredited Business near you. The description of your request is right below this:</label><label>{notes}</label>',
    		'other': '<label style="font-weight: normal;">Okay, so we got the following from you. Your name is <b>{name}</b> and your phone number and email address are <b>{phone}</b> and <b>{email}</b> respectively. We recorded that you live in <b>{city}, {state}</b> and found <b>{num_businesses}</b> of the most trustworthy and closest businesses to you. The description of your request is right below this:</label><label>{notes}</label>'
    	}
    	
    },
    
    initialize: function( options ) {
    
    	$extend( this.options, options || {} );
    
    	$include( CORE.path + 'styles/tob.css' );
    	//$include( CORE.path + 'scripts/getaquotetob.js' );
    	
    	// find the request type and setup type
    	this.options.request_type = ( $('request-type') ? $('request-type').value : 'none' );
    	this.options.setup_type = ( $('setup-type') ? $('setup-type').value : 'none' );
    	
    	// add events to checkboxes
    	this.checkEvent();
    	
    },
    
    // auto fill the city state
    locationFill: function( city, state, zip ) {
    	// only send if the zip code is longer than 4 characters
    	if( zip.value.length > 4 ) {		
    		new Request({
    			url: CORE.path + 'ext/location-autofill.php',
    			onSuccess: function( data ) {
    				data = JSON.decode( data.replace(/(<([^>]+)>)/ig,"") );
    				if( data.city.length > 0 && data.state.length == 2 ) {
        				city.value = data.city;
    					state.value = data.state;
    				}
    			}
    		}).post({
    			'zip': zip.value
    		});
    	}
    },
    
    // checkbox actions
    checkEvent: function() {
    	$$( 'ul.businesses li input' ).addEvent( 'click', function(e) {
    		var checked = this.checked;
    		$( 'num-checked' ).set( 'text', $('num-checked').get('text').toInt() + ( checked ? 1 : -1 ) );
    	});
    },
    
    // check all checkboxes
    checkAll: function() {
    	$('num-checked').set( 'text', 0 );
    	$$( 'ul.businesses li input' ).each( function(el) {
    		el.checked = true;
    		el.fireEvent( 'click' );
    	}); 
    },
    
    // uncheck all checkboxes
    uncheckAll: function() {
    	$('num-checked').set( 'text', $$( 'ul.businesses li input' ).length );
    	$$( 'ul.businesses li input' ).each( function(el) {
    		el.checked = false;
    		el.fireEvent( 'click' );
    	});
    },
    
    // make the li clickable to check the box
    businessesClickable: function() {
    	// make the list clickable
    	$$( 'ul.businesses li' ).addEvent( 'click', function(e) {
    		this.getElements('input')[0].fireEvent('click');
    	});
    },
    
    // make sure the location was supplied
    locationSupplied: function() {
    	return ( ( !$chk( this.options.city.value ) || !$chk( this.options.state.value ) ) ? false : true );
    },
    
    // make sure our details are filled out
    validDetails: function() {
    
    	return ((
    		!$chk( this.options.name.value ) ||
    		!$chk( this.options.email.value ) ||
    		!$chk( this.options.phone.value ) ||
    		!$chk( this.options.city.value ) ||
    		!$chk( this.options.state.value ) ||
    		!$chk( this.options.notes.value )
    	) ? false : true );
    },
    
   //get missing details
   displayMissingDetailsMessage: function() {
		var msg = "\n\t";
		if(!$chk( this.options.name.value ) ) msg += "* name \n\t";
    		if(!$chk( this.options.email.value ) ) msg += "* email \n\t";
    		if(!$chk( this.options.phone.value ) ) msg += "* phone \n\t";
    		if(!$chk( this.options.city.value ) ) msg += "* city \n\t";
    		if(!$chk( this.options.state.value ) ) msg += "* state \n\t";
    		if(!$chk( this.options.notes.value ) ) msg += "* description \n";
		return msg;
	},

    // get the number of businesses selected
    numSelected: function() {
    	// set the number to 0
    	var selected = 0;
    	// go through each checkbox and see if it is checked
    	$$( 'ul.businesses li input' ).each( function(el) {
    		if( el.checked ) selected++;
    	});
    	// return the number
    	return selected
    },
    
    // set the review text based on what was entered
    setReviewTxt: function() {
    	// set the replacements
    	var replacements = {
    		'name': this.options.name.value,
    		'email': this.options.email.value,
    		'phone': this.options.phone.value,
    		'city': this.options.city.value,
    		'state': this.options.state.value,
    		'num_businesses': ( this.options.isbid ? this.options.dba : this.numSelected() ),
    		'notes': this.options.notes.value.replace( /\n/g, '<br />' )
    	};
    	// replace them
    	this.options.reviewtext.set( 'html', ( this.options.isbid ? this.options.text.bid : this.options.text.other ).substitute(replacements) );
    },
    
    // get businesses
    getBusinesses: function( tob, city, state, type, obj ) {
		$('business-list').set( 'html', '<li>Finding Businesses, please wait ...</li>' );
		new Request({
			'url': CORE.path + '/ext/equotebusinesses.php',
			onSuccess: function( response ) {
				$('num-checked').set( 'text', 0 );
				$('business-list').set( 'html', response );
				obj.checkEvent();
				obj.checkAll();
			}
		}).post({
			'tob': tob,
			'type': type,
			'city': city,
			'state': state
		});
    },
    
    // different events for different tabs
    tabEvent: function( tab ) {
    	// go through each one
    	switch( tab ) {
    		// businesses
    		case "tab-businesses":
    			if( !this.validDetails() ) {
				var msg = this.displayMissingDetailsMessage();
    				alert( 'Woops! Looks like we misguided you. Seems you need to enter some details first.' + msg);
    				$('tab-details').fireEvent( 'click' );
    				return false;
    			} else if( !this.locationSupplied() ) {
    				alert( 'Please enter your whereabouts before moving on.' );
    				this.options.city.focus();
    				return false;
    			} else {
   					// start the tob search
   					if( this.options.request_type == 'none' ) {
   						// need to show the content now so that the tob search loads properly
   						$( tab.replace("tab","content") ).fireEvent( 'show' );
   						var obj = this;
				   		var tob = new tobsearch( $('search'), $('tobcode'), {
							start: '',
							onSelect: function(tob) {
								obj.getBusinesses( tob, obj.options.city.value, obj.options.state.value, obj.options.setup_type, obj );
							}
						});
					}
					// get businesses 
					if( this.options.request_type == 'tob' ) {
						if( this.options.nobusinesses ) {
							this.options.nobusinesses = false;
							this.getBusinesses( this.options.tob, this.options.city.value, this.options.state.value, this.options.setup_type, this );
						}
					}
    				return true;
    			}
    		break;
    		// review
    		case "tab-review":
    			if( !this.validDetails() ) {
				var msg = this.displayMissingDetailsMessage();
    				alert( 'Woops! Looks like we misguided you. Seems you need to enter some details first.' + msg);
    				$('tab-details').fireEvent( 'click' );
    				return false;
    			} else if( this.numSelected() < 1 && !this.options.isbid ) {
    				alert( 'Sorry, it appears we didn\'t help you choose any businesses yet. Please do that before moving on, thanks!' );
    				$( 'tab-businesses' ).fireEvent( 'click' );
    				return false;
    			} else {
    				this.setReviewTxt();
    				return true;
    			}
    		break;
    		// otherwise
    		default: return true; break;
    	}
    }
    

});

