function bidtool() {

	this.namespace = "bidtool_";

	if ( typeof( bidtool._initialized ) == "undefined" ) {

		bidtool.prototype.build = function() {

			this.gencat = 0;
			this.sercat = 0;

			this._clicked = false;

			this.tip = dash_loader.tooltip;
			this.checker = new checker(); // create new checker so that setOnFail is not corrupted on current tab
			this.checker.setOnFail( this.fail, this );

			this.request_dialog = new dialog(); this.request_dialog.fixedcenter = false; this.request_dialog.setParent( this ); this.request_dialog.build();
			this.request_dialog.obj.hideEvent.subscribe( function(e,z,obj) { obj.request_dialog.obj.setBody( "" ); }, this	);
		}

		//////////////////////////////////////////////////////////////////////////////////
		// Event Callbacks																															//
		//////////////////////////////////////////////////////////////////////////////////

		bidtool.prototype.gen_cat_dropdown = function(e, obj) {
			
			if (document.getElementById(this.name)) {
				document.getElementById(this.name).value = this.value;
			}
			obj.gencat = this.value;
			obj.sercat = -1;
			obj.pop_ser_cat_dropdown();
			document.getElementById(obj.namespace+"sercats_drop").innerHTML = "";
			document.getElementById( obj.namespace+"gencat_loading" ).style.display = "inline";
		}

		bidtool.prototype.state_dropdown = function(e,obj) {
			if (document.getElementById(this.name)) {
				document.getElementById(this.name).value = this.value;
			}
			obj.state_id = this.value;
			obj.pop_city_dropdown();
			document.getElementById(obj.namespace+"city_drop").innerHTML = "";
			document.getElementById( obj.namespace+"state_loading" ).style.display = "inline";
		}

		bidtool.prototype.cleanDetails = function(e, obj) {
			var message = document.getElementById( obj.namespace + "message_hj" );
			message.innerHTML = "";
			message.style.color = "#000000";
			obj._clicked = true;
			obj.tip.hide();
			YAHOO.util.Event.removeListener( message, "click" );
		}

		//////////////////////////////////////////////////////////////////////////////////
		// General Functions																														//
		//////////////////////////////////////////////////////////////////////////////////

		bidtool.prototype.open_request = function() {
			this.build();
			this.loading( 'Loading . . .' );
			this.get_request_form(); // return call will populate form
		}

		bidtool.prototype.toggle_zip = function( checked ) {
			var zipform = document.getElementById( "bidtool_zipform_hj" );

			if ( checked ) {
				zipform.style.display = "block";
			} else {
				zipform.style.display = "none";
			}
		}

		bidtool.prototype.loading = function( header ) {
			// setup loading vars
			var header = '<div style="text-align:center;width:100%;">'+header+'</div>';
			var body = '<div style="text-align:center;">';
			body += '<img alt="Loading..." src="/www/images/ajax-loader-formboard.gif"/>';
			body += '</div>';
			var type = "Loading";
			var callback = null;
			// create the loading dialog
			this.request_dialog.create( header, body, type, callback );
		}

		//////////////////////////////////////////////////////////////////////////////////
		// Checker Functions																														//
		//////////////////////////////////////////////////////////////////////////////////

		bidtool.prototype.check_zip = function() {

			var zip = document.getElementById( this.namespace + "zip_hj" );
			this.checker.addTest( zip, "is_valid_zip", "Enter a valid zip code." );

			if ( this.checker.run() ) {
				document.getElementById( this.namespace + "update_zip_btn" ).disabled = true;
				document.getElementById( this.namespace + "update_zip_loading" ).style.display = "inline";
				this.submit_location( zip.value );
			}
		}

		bidtool.prototype.check_submit = function() {

			// add some tests to our checker
			this.checker.addTest( this.namespace+"gen_cat_id_drop", "is_selected", "Select a category" );
			var sercat = document.getElementById( this.namespace+"ser_cat_id_drop" ); 
			if ( sercat ) this.checker.addTest( sercat, "is_selected", "Select a category" );
			var eDescription = "Write a description of your service needed";
			if ( this._clicked ) this.checker.addTest( this.namespace + "message_hj", "is_populated", eDescription );

			if ( this.checker.run() ) { // run our tests
				if ( !this._clicked ) { // make sure the message doesn't just contain prompt message
					this.tip.create( eDescription, this.namespace + "message_hj" );
				} else { // passed all tests
					this.submit_request();
					this.loading( 'Submitting Request . . .' );
				}
			}
		}

		//////////////////////////////////////////////////////////////////////////////////
		// Ajax Calls																																		//
		//////////////////////////////////////////////////////////////////////////////////

		bidtool.prototype.submit_location = function( zip ) {
			if ( typeof( this.loc_call ) == "undefined" ) this.loc_call = new send_call( this, "location" );
			this.loc_call.set_call( "city_location" );
			var pass_vars = new Array();
			pass_vars["zipcode"] = zip;
			this.loc_call.set_url_vars( pass_vars );
			this.loc_call.send(dash_loader.ajax.ajax_support);
		}

		bidtool.prototype.pop_ser_cat_dropdown = function() {
			if (typeof(this.results_call) == "undefined") this.results_call = new send_call(this, "results");
			this.results_call.set_call("sercats_dropdown");
			var pass_vars = new Array();
			pass_vars['gen_cat_id'] = this.gencat;
			pass_vars['namespace'] = this.namespace;
			this.results_call.set_url_vars(pass_vars);
			this.results_call.send(dash_loader.ajax.ajax_support);
		}

		bidtool.prototype.pop_city_dropdown = function() {
			if (typeof(this.results_call) == "undefined") this.results_call = new send_call(this, "results");
			this.results_call.set_call("city_dropdown");
			var pass_vars = new Array();
			pass_vars['state_id'] = this.state_id;
			pass_vars['namespace'] = this.namespace;
			this.results_call.set_url_vars(pass_vars);
			this.results_call.send(dash_loader.ajax.ajax_support);
		}


		bidtool.prototype.get_request_form = function() {
			if ( typeof( this.send_call ) == "undefined" ) this.send_call = new send_call( this, "bidtool" );
			this.send_call.set_call( "get_request_form" );
			var url_vars = new Array();
			var market_url = document.getElementById( "market_url" );
			if ( market_url ) url_vars['market_url'] = market_url.value;
			this.send_call.set_url_vars( url_vars );
			this.send_call.send(dash_loader.ajax.ajax_support);
		}

		bidtool.prototype.submit_request = function() {
			if ( typeof( this.post_call ) == "undefined" ) this.post_call = new post_call( this, "bidtool" );
			this.post_call.set_call( "request_quote" );
			this.post_call.post();
		}

		//////////////////////////////////////////////////////////////////////////////////
		// Return Calls																																	//
		//////////////////////////////////////////////////////////////////////////////////

		bidtool.prototype.return_call = function( response, call ) {
			switch( call ) {
			case "get_request_form":
				try { // json data means response is bad
					var m = YAHOO.lang.JSON.parse( response );
					this.doRequestFormFail( m );
				} catch( e ) { // html data means response is good.
					this.doRequestForm( response );
				}
				break;
			case "city_location": // change our request location
				try {
					var m  = YAHOO.lang.JSON.parse( response );
					this.doChangeLocationCleanup(); // cleanup needed for both pass and failed
					if ( m.passed ) {
						this.doChangeLocation( m );
					} else { // show error
						dash_loader.tooptip.create( "Enter a valid zip code", this.namespace + "zip_hj" );
					}
				} catch( e ) {}
				break;
			case "sercats_dropdown": // populate the service category dropdown after selecting a general category
				this.doSercatsDropdown( response );
				break;
			case "city_dropdown":
				this.doCityDropdown( response );
				break;
			case "request_quote": // response for requesting a quote
				//try {
					var m = YAHOO.lang.JSON.parse( response );
					if ( m.passed ) {
						this.doRequestResponse( m );
					} else {
						this.doRequestResponseFail( m );
					}
				//} catch ( e ) {}
				break;
			}
		}

		//////////////////////////////////////////////////////////////////////////////////
		// Return Call Helper Functions																									//
		//////////////////////////////////////////////////////////////////////////////////

		bidtool.prototype.doRequestResponseFail = function( m ) { // m is json parsed data

			var header = '<div style="width:100%;text-align:center;color:#5B5444;">Your Quote Request . . .</div>';
			var type = "Ok";
			var callback = new Array( this.request_dialog.obj.hide );
			this.request_dialog.obj.cfg.setProperty( "width", "540px" );

			var body = '<div class="mess_skin"><div class="err"><div class="flag">'+m.message+'</div></div></span>'
			this.request_dialog.create( header, body, type, callback );

		}

		bidtool.prototype.doRequestResponse = function( m ) { // m is json parsed data
			var header = '<div style="width:100%;text-align:center;color:#5B5444;">Your Quote Request . . .</div>';
			var type = "Ok";
			var callback = new Array( this.request_dialog.obj.hide );
			this.request_dialog.obj.cfg.setProperty( "width", "540px" );

			var body = '<div class="mess_skin"><div class="att"><div class="flag">'+m.message+'</div><div class="message">'+m.more+'</div></div></span>'; 
			this.request_dialog.create( header, body, type, callback );
		}

		bidtool.prototype.doSercatsDropdown = function( response ) { // response is html
			document.getElementById( this.namespace+"sercats_drop" ).innerHTML = response;
			document.getElementById( this.namespace+"gencat_loading" ).style.display = "none";
		}

		bidtool.prototype.doCityDropdown = function( response ) { // response is html
			document.getElementById( this.namespace+"city_drop" ).innerHTML = response;
			document.getElementById( this.namespace+"state_loading" ).style.display = "none";
		}

		bidtool.prototype.doRequestFormFail = function( m ) { // m is json parsed data
			var header = '<div style="width:100%;text-align:center;color:#5B5444;">Your Quote Request . . .</div>';
			var type = "Ok";
			var callback = new Array( this.request_dialog.obj.hide );
			this.request_dialog.obj.cfg.setProperty( "width", "540px" );
			var body = '<div class="mess_skin"><div class="err"><div class="flag">'+m.message+'</div></div></span>'
			this.request_dialog.create( header, body, type, callback );
		}

		bidtool.prototype.doRequestForm = function( response ) { // response is html

			// callback for location dialog 
			var submitClick = function( e, obj ) {
				obj.check_submit();
			}

			// setup dialog variables
			var header = '<div>Quote Request</div>';
			var dialog_type = "Form";
			var callbacks = new Array( submitClick, this.request_dialog.obj.hide );
			// create request dialog
			this.request_dialog.create( header, response, dialog_type, callbacks );

			this.createListeners();
		}

		bidtool.prototype.createListeners = function() {
			// add category onchange listener
			if ( document.getElementById( this.namespace+"gen_cat_id_drop" ) ) {
				YAHOO.util.Event.addListener(this.namespace + "gen_cat_id_drop", "change", this.gen_cat_dropdown, this);
			}
			if ( document.getElementById( this.namespace+"state_id_drop" ) ) {
				YAHOO.util.Event.addListener( this.namespace + "state_id_drop", "change", this.state_dropdown, this );
			}
			// click to initialize message area
			YAHOO.util.Event.addListener(this.namespace + "message_hj", "click", this.cleanDetails, this );
		}

		bidtool.prototype.doChangeLocationCleanup = function() {
			document.getElementById( this.namespace + "update_zip_loading" ).style.display = "none";
			document.getElementById( this.namespace + "update_zip_btn" ).disabled = false;
		}

		bidtool.prototype.doChangeLocation = function( m ) { // m is json data
			document.getElementById( this.namespace + "loc_state" ).innerHTML = m.state;
			document.getElementById( this.namespace + "loc_city" ).innerHTML = m.city;
			document.getElementById( this.namespace + "zipcode_hj" ).value = m.zipcode;
			document.getElementById( this.namespace + "zip_checkbox" ).click();
			document.getElementById( this.namespace + "zip_hj" ).value = "";
		}


		//////////////////////////////////////////////////////////////////////////////////
		// Checker Fail Callback																												//
		//////////////////////////////////////////////////////////////////////////////////

		bidtool.prototype.fail = function( obj ) {
			obj.tip.create( this.getError(), this.getElement() );
		}

		//////////////////////////////////////////////////////////////////////////////////
 
    bidtool._initialized = true;
  }

} 
