/******
 *	This method takes in 2 arrays of objects.  The first array are the main options for selection
 *	and the second array are the "override" objects which, if they are not being displayed, we
 *	clear out their value
******/
function toggle_obj_related(ar1, ar2, ar3, ar4){
	flip = $(ar1[0]).style.display!='none'
	for(i = 0; i < ar1.length; i++)
		$(ar1[i]).style.display = flip ? 'none' : '';
	for(i = 0; i < ar2.length; i++)
		$(ar2[i]).style.display = flip ? '' : 'none';
	for(i = 0; i < ar3.length; i++)
		$(ar3[i]).value = flip;
	for(i = 0; i < ar4.length; i++)
		$(ar4[i]).innerHTML = flip ? 'cancel' : 'other';
}

/******
 *	These arrays are to help keep our select/textbox switching cleaner
******/
car_make_select_array		= ['entry_car_make_id', 'entry_car_model_id', 'entry_car_year_id', 'car_model_href', 'car_year_href'];
car_make_override_array		= ['entry_car_make_override', 'entry_car_model_override', 'entry_car_year_override'];
car_make_is_override_array	= ['entry_car_make_is_override', 'entry_car_model_is_override', 'entry_car_year_is_override'];
car_make_href_text_array	= ['car_make_href', 'car_model_href', 'car_year_href'];

car_model_select_array		= ['entry_car_model_id', 'entry_car_year_id', 'car_year_href'];
car_model_override_array	= ['entry_car_model_override', 'entry_car_year_override'];
car_model_is_override_array	= ['entry_car_model_is_override', 'entry_car_year_is_override'];
car_model_href_text_array	= ['car_model_href', 'car_year_href'];

car_year_select_array		= ['entry_car_year_id'];
car_year_override_array		= ['entry_car_year_override'];
car_year_is_override_array	= ['entry_car_year_is_override'];
car_year_href_text_array	= ['car_year_href'];

seat_brand_select_array		= ['entry_seat_brand_id', 'entry_seat_name_id', 'seat_name_href'];
seat_brand_override_array	= ['entry_seat_brand_override', 'entry_seat_name_override'];
seat_brand_is_override_array	= ['entry_seat_brand_is_override', 'entry_seat_name_is_override'];
seat_brand_href_text_array	= ['seat_brand_href', 'seat_name_href'];

seat_name_select_array		= ['entry_seat_name_id'];
seat_name_override_array	= ['entry_seat_name_override'];
seat_name_is_override_array	= ['entry_seat_name_is_override'];
seat_name_href_text_array	= ['seat_name_href'];

function add_select_option(select_box, display_span_id, hidden_value_field_id, display_div_id){
	display_span = $(display_span_id);
	hidden_value_field = $(hidden_value_field_id);
	$(display_div_id).style.display = "";
	if(!in_array(hidden_value_field.value.split("|"), select_box[select_box.selectedIndex].value)){
		display_span.innerHTML = display_span.innerHTML.trim() + (display_span.innerHTML.trim().length > 0 ? "<br>" : "") + 
			"<a href=\"#\" onclick=\"remove_select_option(" + select_box[select_box.selectedIndex].value
			+ ", '" + display_span_id + "', '" + hidden_value_field_id + "', '" + display_div_id + "');\"><img src='/images/backgrounds/x.gif' /> </a>" + select_box[select_box.selectedIndex].text;
		hidden_value_field.value = hidden_value_field.value + (hidden_value_field.value.length > 0 ? "|" : "") + select_box[select_box.selectedIndex].value;
	}
}
function remove_select_option(selected_value, display_span_id, hidden_value_field_id, display_div_id){
	display_span = $(display_span_id);
	hidden_value_field = $(hidden_value_field_id);
	display_span_html_array = display_span.innerHTML.split(display_span.innerHTML.match(/\<[b|B][r|R]\>/));
	hidden_value_field_value_array = hidden_value_field.value.split("|");
	new_inner_html = "";
	new_hidden_value = "";
	for(i = 0; i < hidden_value_field_value_array.length; i++){
		if(hidden_value_field_value_array[i] != selected_value){
			new_inner_html = new_inner_html + (new_inner_html.length > 0 ? "<br>" : "") + display_span_html_array[i];
			new_hidden_value = new_hidden_value + (new_hidden_value.length > 0 ? "|" : "") + hidden_value_field_value_array[i];
		}
	}
	display_span.innerHTML = new_inner_html;
	if(new_inner_html.length < 1)
		$(display_div_id).style.display = "none";
	hidden_value_field.value = new_hidden_value;
}

function in_array(arr, string){
	if (arr.constructor.toString().indexOf("Array") == -1)
		return false;
	for(i = 0; i < arr.length; i++)
		if(arr[i] == string)
			return true;
	return false;
}

// this trim was suggested by Tobias Hinnerup
String.prototype.trim = function() {
    return(this.replace(/^\s+/,'').replace(/\s+$/,''));
}

function update_select_options( target, opts_array, clear_select_list, child_prompts ) {
   if( $(target).type.match("select" ) ){ // Confirm the target is a select box
        // Remove existing options from the target and the clear_select_list
        clear_select_list[clear_select_list.length] = target // Include the target in the clear list

        for( k=0;k < clear_select_list.length;k++){
            obj = $(clear_select_list[k]);
            if( obj.type.match("select") ){
				len = obj.childNodes.length;
                for( var i=0;i < len;i++){obj.removeChild(obj.firstChild);}
				if(k < child_prompts.length){
					o = document.createElement("option");
					o.appendChild(document.createTextNode(child_prompts[k] ));
					o.setAttribute( "value", "");
					obj.appendChild(o);
				}
            }
        }

        // Populate the new options
        for(i=0;i < opts_array.length;i++){
            o = document.createElement("option");
            o.appendChild( document.createTextNode(opts_array[i][0]));
            o.setAttribute("value", opts_array[i][1]);
            obj.appendChild(o);
        }
    }
}

function calTableWidth(){
	if ($('searchResults').getAttribute('width') == '800'){
		$('searchResults').setAttribute('width','1');
	}	
	else if ($('searchResults').getAttribute('width') == '1200'){
		$('searchResults').setAttribute('width','800');
	}	
	else if ($('searchResults').getAttribute('width') == '1800'){
		$('searchResults').setAttribute('width','1200');
	}
}
function calTableWidthAdded(){
	if ($('searchResults').getAttribute('width') == '1'){
		$('searchResults').setAttribute('width','800');
	}	
	else if ($('searchResults').getAttribute('width') == '800'){
		$('searchResults').setAttribute('width','1200');
	}	
	else if ($('searchResults').getAttribute('width') == '1200'){
		$('searchResults').setAttribute('width','1800');
	}
}
function showAll() {
	var seatData = $('showSeatData');
	var vehicleData = $('showVehicleData');
	var installData = $('showInstallationData');
	var showAll =  seatData && vehicleData && installData != undefined;
	
	if(seatData && vehicleData && installData != undefined) {
  		new  Insertion.Before('searchResults', '<p class="showButton"><input type="button"  id="showAll" class="showAll" value="Show All Data" /></p>');
  		$$('.showAll').find(function(data){
			data.observe('click', function(y){
				$$('.vehicleData').each(function(z){
					z.setStyle({
		  				display: ''
    				});   			
				});
				$$('.seatData').each(function(z){
					z.setStyle({
		  				display: ''
    				});   			
				});
				$$('.installationData').each(function(z){
					z.setStyle({
		  				display: ''
    				});   			
				});
  				data.parentNode.remove();
				seatData.parentNode.remove();
				vehicleData.parentNode.remove();
				installData.parentNode.remove();
				Event.stop(y); 
				$('searchResults').setAttribute('width','1800');
 			});   
 			
 			if (showAll != false){
				vehicleData.observe('click', function(y){
					if ($('showAll') != undefined){
						$('showAll').parentNode.remove();
					}
				});
				seatData.observe('click', function(y){
					if ($('showAll') != undefined){
 						$('showAll').parentNode.remove();
					}
				});
				installData.observe('click', function(y){
					if ($('showAll') != undefined){
						$('showAll').parentNode.remove();
					}
				});  
			}	
		});				
	}  
}
function collapse(){
	$$('.collapse').each(function(el){
		el.observe('click', function(event){
			assignCollapseButton(el, 'Vehicle', 'vehicleData', 'showVehicleData'),
			assignCollapseButton(el, 'Seat', 'seatData', 'showSeatData'),
			assignCollapseButton(el, 'Installation', 'installationData', 'showInstallationData')
		  	Event.stop(event);
		});
	});
}

function assignCollapseButton(el, dataLabel, className, showClassName){
	if (el.parentNode.className == className){
		$$('.'+className).each(function(i){
			i.setStyle({
				display: 'none'
			});
		});
		calTableWidth();
		new  Insertion.Before('searchResults', '<p class="showButton"><input type="button" id="' + showClassName + '" class="' + showClassName + '" value="Show ' + dataLabel + ' Data" /></p>');
		$$('.' + showClassName).find(function(data){
			data.observe('click', function(y){
				$$('.'+className).each(function(z){
     				z.setStyle({
						display: ''
			    	});   			
 				});
				data.parentNode.remove();
		  		Event.stop(y); 
		  		calTableWidthAdded();
			});
			showAll();
		});  
	}
}
function checkAll(cbox, clss){
	$$('.'+clss).each(function(i){i.checked = $(cbox).checked;})
}


/* Open external Link in a New Window */
Event.observe(window, 'load', function() {
	$$(".external").each(function(link){
		   link.setAttribute('title','This link opens in a new window');
			link.observe('click', function(c){
				window.open(link.href,"_blank","top=25,left=25,scrollbars=1,resizable=1,location=1,menubar=1,toolbar=1");
				Event.stop(c); 
		});
	});	
});	


