//_searcher.js
// JavaScript Document
/*Copyright March, 2006
This javascript file is the exclusive property of GMBCS, proprietor - Glyn Barrows.
A right to use permission is granted to Gruene Acres Web Design for use with the All Star RV Web Site.
*/
function get_search_stack(frm,add_it,step)
	{
		//alert("frm = "+frm+"\nadd_it = "+add_it+"\nstep = "+step);
		for (i = 0;i<frm.elements.length;i++)
			{
				if (frm.elements[i].id == 'search_stack')
					{
						//alert("search_stack = "+frm.elements[i].value+"\nadd_it = "+add_it);
						if ((frm.elements[i].value == "") || (frm.elements[i].value == " ")|| (frm.elements[i].value == null))
							{
								//alert("HERE 1");
								search_stack =add_it;
							}
						else
							{
								//alert("HERE 5");
								//alert("1. search_stack = "+frm.elements[i].value);
								search_stack = frm.elements[i].value+"&"+add_it;
								//alert("2. search_stack = "+search_stack);
							}
						frm.elements[i].value = search_stack;
					}
				//The unit_id element will only be set when there is only one unit left which satisfied the selected criteria
				if (frm.elements[i].id == 'unit_id')
					{
						search_stack = "unit_id=".frm.elements[i].value;
						break;
					}
			}
		return search_stack;
	}
function populate_select(name, option_array)
	{
		//alert("name.name = "+name.name+"\nname.length = "+name.length);
		for (i=0;i<option_array.length;i++)
			{
				x=name.length;
				name[x] =  new Option(option_array[i]['text'],option_array[i]['value']);
				//alert(name[x].value);
				//name.options[name.length] = new Option(option_array[i],option_array[i]);
			}
	}
function update_search_lists(a,b,c)
	{
		//alert("a.name = "+a.name+"\nb.name = "+b.name+"\nc.length = "+c.length);
		//a is the form
		//b is the select element that was clicked
		//c is the array that holds all the SELECTS lists of available options.  
		//NOTE: This array "c" holds the data that was used to populate the selects prior to this selection
		//So, when this function runs, a new array is built that holds the options that are left once a slection is made
		//This is temp_array1.  This array is returned and assigned to the all_units_array which then becomes the "c" array when this
		//function is called again.
		//This new array will be used to repopulate the selects.  If only one option remains it will be disabled.
		var stng = "\n";
		var stng1 = "\n";
		var stng2 = "\n";
		var stuff;
		var selected_value;
		var selected_text;
		var temp_array = new Array(); //The new rebuilt main array
		var temp_array1 = new Array(); //The new rebuilt main array
		var temp_array2 = new Array(); //Used to temporarily hold available options in a select element
		var temp_array3  = new Array(); //Used to hold the final "scrubbed" options in a select element.
		//Scrubbed means that the primary array was gone through to determine the options that should still be available
		selected_value = b.value;
		selected_text = b.options[b.selectedIndex].text;
		//Rebuild the BASE data array by eliminating all rows that do not contain the selected option
		//Once this is built it will be used to rebuild the selects that are still active.  
		//If only one option remains in a select that is still active (hasn't been selected yet), then the select will be disabled.

//		for testing - show the form's elements
//		stng = "\n";
//		for (i=0;i<a.elements.length;i++)
//			{
//				stng += "a.elements["+i+"].type = "+a.elements[i].type+"  id = "+a.elements[i].id+"\n";
//			}
//		alert(stng);

		//Go through the inbound array of select data values prior to this selection
		for (i=0;i<c.length;i++)
			{
				//alert(i);
				//go through the elements in the c[i] arrays and find a match to this select box
				for (stuff in c[i])
					{
						//if this element is the same as the "selected" select boxes name
						if (stuff == b.name)
							{
								//then is this value in the array a match to the value selected
								if (c[i][stuff] == b.options[b.selectedIndex].value)
									{
										//alert("c["+i+"].stuff  = "+c[i][stuff]);
										//alert("b.options[b.selectedIndex].text = "+b.options[b.selectedIndex].value);
										//if this is a match, then put this array elements values into the temp_array1
										//This will populate the array with all of the other unit data for the units that matche this selection
										temp_array1[temp_array1.length] = c[i];
									}
							}
					}
			}
		//alert("temp_array1.length = "+temp_array1.length);
		//At this point the "new" array of unit data is constructed.
		//Reset this select element, leaving the single option and then disable the select
		b.options.length = 0;
		//Put the selection in this selects "zewroth" element
		b.options[0] = new Option(selected_text, selected_value);
		//Now disable this select element
		b.disabled = true;

		//Now go through the other selects.
		//Find the ones that are not already disabled
		//Use the "current" data array ("temp_array1") and SCRUB (delete non selected elements) the array
		//This will be done by copying the elements that satisfy the selection into the temp_array3 from temp_array1
		//Go through the form elements
		for (i=0;i<a.elements.length;i++)
			{
				//Since some of the elements may be disabled and there is no need to do anything with these
				//Find the next element that is NOT disabled
				if (a.elements[i].disabled == false)
					{
						//Make sure this is a select element (could be a button or some hidden thingamadouchy)
						if (a.elements[i].type.substr(0,6) == "select")
							{
								//This is a select box, and it is not disabled so get the options for this box and put them in the temp_array2
								//Go through each of the select 's options
								loop1:
								for (j=0;j<a.elements[i].options.length;j++)
									{
										//Go through the main array and find the select 
										for (k=0;k<	temp_array1.length;k++)
											{
												//a.elements[i].name]  is this select's name and it is also the name of the one of the unit arrays elements
												//So find a matche between the unit array(temp_array1) element name VALUE  and this selects available options
												//Since the temp_array1 has only the remaining units, this will find only the select values that are available on those units
												if (temp_array1[k][a.elements[i].name] == a.elements[i].options[j].value)
													{
														//This value is still in the hunt, so put it in the temp_array3
														//When the select box name is the same as the field in the temp_array1
														//When the temp_array1[x][stuff] is the same as the select option being looked at
														//Record the temp_array1 row to the temp_array1
														temp_array3[temp_array3.length] = new Array();
														temp_array3[temp_array3.length] =a.elements[i].options[j];
														//and then set the selects options
														var found_it = false;
														for (l=0;l<temp_array2.length;l++)
															{
																if (temp_array2[x].text == a.elements[i].options[j].text)
																	{
																		found_it = true;
																		break;
																	}
															}
														if (found_it == false)
															{
																x = temp_array2.length
																temp_array2[x] = new Array();
																temp_array2[x].text = a.elements[i].options[j].text;
																temp_array2[x].value = a.elements[i].options[j].value;
																
															}
														else
															{
																 found_it = false;
															}
													}
											}
									}
								if (temp_array2.length > 1)
									{
										//alert("temp_array2.length - "+temp_array2.length);
										stng1= a.elements[i].options[0].text;
										stng2 = a.elements[i].options[0].value;
										a.elements[i].options.length = 0;
										a.elements[i][0] = new Option(stng1,stng2);
										//alert("SETTING OPTIONS FOR - "+a.elements[i].name);
										for (j=0;j<temp_array2.length;j++)
											{
												stng1= temp_array2[j].text;
												stng2 = temp_array2[j].value;
												a.elements[i][a.elements[i].length] = new Option(stng1,stng2);
											}
										//a.elements[i][j] = new Option(selected_text, selected_value);
										temp_array2.length	 = 0;
									}
								else
									{
										//alert("SETTING SINGLE ITEM FOR - "+a.elements[i].name);
										stng1= temp_array2[0].text;
										stng2 = temp_array2[0].value;
										a.elements[i].options.length = 0;
										a.elements[i][0] = new Option(stng1,stng2);
										//alert("stng1 = "+stng1+"\nstng2 = "+stng2);
										temp_array2.length	 = 0;
										a.elements[i].disabled = true;
									}
							}
					}
			}
		if (temp_array1.length == 1)
			{
					window.location="inv2.php?unit_id="+temp_array1[0].id_number ;
			}
		return temp_array1;
		//Set the options in the select element
	}
function get_units(a)
	{
		//a is the form
		var stng= "";
		for (i=0;i<a.elements.length;i++)
			{
				if (a.elements[i].type.substr(0,6) == "select")
					{
						if (a.elements[i].options.length == 1)
							{
								stng +="search_"+a.elements[i].name+"="+a.elements[i].options[0].value+"&";
							}
					}
			}
		a.action = "inv.php?search_request=true&"+stng;
		a.submit;
	}
function reset_selects()
	{
		window.location="index.php";
	}