
EyewearLookup = function() {

	var Me = this;

	//
	// Members
	//
	this.input_id_suffix;
	this.all_selections_previously_set = 0;
	this.base_uri = 'http://www.nativeyewear.com';
	//
	// Methods
	//
	this.disable_lens_color_select = disable_lens_color_select;
	this.disable_frame_color_select = disable_frame_color_select;
	this.dropdown_clear_and_disable_by_id = dropdown_clear_and_disable_by_id;
	this.dropdown_show_please_wait = dropdown_show_please_wait;
	this.frame_color_lookup = frame_color_lookup;
	this.get_input_id = get_input_id;
	this.get_selected_style_id = get_selected_style_id;
	this.get_selected_frame_color_id = get_selected_frame_color_id;
	this.get_selected_lens_color_id = get_selected_lens_color_id;
	this.apply_frame_color_lookup = apply_frame_color_lookup;
	this.lens_color_lookup = lens_color_lookup;
	this.apply_lens_color_lookup = apply_lens_color_lookup;
	this.refresh_dropdowns = refresh_dropdowns;
	

	this.refresh_dropdowns = refresh_dropdowns;

	function get_input_id( name ) {
		
		try {
			if ( Me.input_id_suffix ) {
				name = name + Me.input_id_suffix;
			}
			
			return name;
		}
		catch( e ) {
			throw e;
		}
		
	}
	
	
	function frame_color_lookup( options ) {

		try {
			
		    var request_obj = new BRIJ_Request();
			var function_name ='brij_generate_potential_detail_options_by_filter'; 
			
			if ( typeof(options) != 'undefined' && typeof(options['input_suffix']) != 'undefined' ) {
				this.input_id_suffix = options['input_suffix'];
			}
			
			if ( typeof(options) !='undefined' && typeof(options['storefront_item_id'] != 'undefined') ) {
				function_name = 'brij_potential_detail_options_by_storefront_item_id_and_filter';				
				request_obj.add_parameter( options['storefront_item_id'] );
			}
			
			this.dropdown_show_please_wait( this.get_input_id('option_frame_color') );
			this.disable_lens_color_select();

			var filters  = new Array();
			var style_id = this.get_selected_style_id();
			
			if ( !isNaN(style_id) ) {

				filters['eyewear_style'] = style_id;

               
                request_obj.set_script_filename( this.base_uri + '/consumer_gateway/cgw-brij.php' );
                request_obj.set_function_name(function_name);
                request_obj.set_result_handler( this.apply_frame_color_lookup );

				request_obj.add_parameter( 'frame_color' );
                request_obj.add_parameter( filters );

                var result = request_obj.get_function_result();
			}
		}
		catch(e){
			alert(e.message);
		}
	}

	function apply_frame_color_lookup( result ) {

		try {
			if ( is_BRIJ_Error(result) ) {
				alert( 'BRIJ Error: ' + result.get_error_message() );
			}
			else {
				if ( (typeof(result) != 'undefined') && (result != null) ) {

					

					affected_dropdown_id = Me.get_input_id('option_frame_color');

					dropdown_clear_all_by_dom_id( affected_dropdown_id );
					apply_html_tag_array_to_dropdown_id( affected_dropdown_id, result );

					Me.refresh_dropdowns();
				}
			}
       }
       catch(e) {
       		alert(e.message);
       }

	}


	function lens_color_lookup( options ) {

		try {

			if ( typeof(options) != 'undefined' && typeof(options['input_suffix']) != 'undefined' ) {
				this.input_id_suffix = options['input_suffix'];
			}

			this.dropdown_show_please_wait( this.get_input_id('option_lens_color') );

			var filters  = new Array();

			var style_id       = this.get_selected_style_id();
			var frame_color_id = this.get_selected_frame_color_id();
			
			filters['eyewear_style'] = style_id;
			filters['frame_color']    = frame_color_id;

            var request_obj = new BRIJ_Request();

            request_obj.set_script_filename( this.base_uri + '/consumer_gateway/cgw-brij.php' );
            request_obj.set_function_name('brij_generate_potential_detail_options_by_filter');
            request_obj.set_result_handler( this.apply_lens_color_lookup );

			request_obj.add_parameter( options['item_type_key'] );
			request_obj.add_parameter( 'lens_color' );
            request_obj.add_parameter( filters );

            var result = request_obj.get_function_result();

		}
		catch(e){
			alert(e.message);
		}

	}

	function apply_lens_color_lookup( result ) {

		try {
			if ( is_BRIJ_Error(result) ) {
				alert( 'BRIJ Error: ' + result.get_error_message() );
			}
			else {
				if ( (typeof(result) != 'undefined') && (result != null) ) {

					affected_dropdown_id = Me.get_input_id('option_eyewear_lens_color');

					dropdown_clear_all_by_dom_id( affected_dropdown_id );

					
					apply_html_tag_array_to_dropdown_id( affected_dropdown_id, result );

					Me.refresh_dropdowns();
				}
			}
        }
        catch(e) {
         alert(e.message);
        }

	}

	function refresh_dropdowns() {

		try {

			var found_lens  = false;
			var found_frame = false;
			var found_style = false;

            if ( Me.get_selected_style_id() ) {
				found_style = true;
				dom_object_enable_by_id(Me.get_input_id('option_frame_color'));
			}
			else {
				Me.dropdown_clear_and_disable_by_id(Me.get_input_id('option_frame_color'));
			}

        	if ( Me.get_selected_frame_color_id() ) {
				found_frame = true;
				dom_object_enable_by_id(Me.get_input_id('option_eyewear_lens_color'));
			}
			else {
				Me.dropdown_clear_and_disable_by_id(Me.get_input_id('option_eyewear_lens_color'));
			}

        	if (Me.get_selected_lens_color_id() ) {
				found_lens = true;
			}

			//if ( !found_lens || !found_style || !found_frame ) {
			//	
			//}

                }
                catch(e) {
                        alert(e.message);
                }

	}

	function get_selected_style_id() {

		try {
			return dropdown_get_selected_value_by_dom_id(Me.get_input_id('option_eyewear_style'));
		}
		catch(e){
			cgw_claim_error( e );
		}

	}

	function get_selected_frame_color_id() {

		try {
			return dropdown_get_selected_value_by_dom_id(Me.get_input_id('option_frame_color'));
		}
		catch(e){
			cgw_claim_error( e );
		}

	}

	function get_selected_lens_color_id() {

		try {
			return dropdown_get_selected_value_by_dom_id(Me.get_input_id('option_eyewear_lens_color'));
		}
		catch(e){
			cgw_claim_error( e );
		}

	}

	function disable_lens_color_select() {

		try {
			this.dropdown_clear_and_disable_by_id( this.get_input_id('option_eyewear_lens_color') );
		}
		catch(e){
			throw e;
		}

	}

	function disable_frame_color_select() {

		try {
			this.dropdown_clear_and_disable_by_id( this.get_input_id('option_frame_color') );
		}
		catch(e){
			throw e;
		}

	}

	function dropdown_show_please_wait(dropdown_id) {

		try {
			this.dropdown_clear_and_disable_by_id(dropdown_id);
			dropdown_add_option_by_dom_id( dropdown_id, 'Please Wait...' );
		}
		catch(e) {
			cgw_claim_error( e );
		}


	}

	function dropdown_clear_and_disable_by_id( dropdown_id ) {

		try {
			dropdown_clear_all_by_dom_id( dropdown_id );
			dom_object_disable_by_id( dropdown_id );
		}
		catch(e) {
			cgw_claim_error( e );
		}

	}

}