//==============================================================================
//It transferes selected item values from one field to another
//- It loops through 'fromField' find get the values selected
//- Then checks if the selected value doesn't exist in 'toField'
//- If the selected value doesn't exist in 'toField' create an new option with
//      the selected values.
//- Set the background of the option to '#E7F1F3' to indicate that the feature
//      came from the database.
//==============================================================================

			function populateFeatures (toField, fromField)
				{ 
					var posCount    = toField.options.length;
					var existingArr = new Array();
					
					//loop through the selection
					for (var count = 0; count < fromField.options.length; count++)
						{
							//check if the option is selected
							if (fromField.options[count].selected)
								{
									//bool check to see if the value already exists in the 'toField'
									var check = false;
									//loop through the 'toField' to see if the option exists
									for (var checkcount = 0; checkcount < toField.options.length; checkcount++)
										{
											if (fromField.options[count].value == toField.options[checkcount].value)
												{
													//if it exists change the bool check to true
													check = true;
												}
										}

									//if the value was not found it the 'toField', add the values to the 'toField'
									if (check == false)
										{
											toField.options[posCount]       = new Option(fromField.options[count].text);
											toField.options[posCount].value = fromField.options[count].value;
											toField.options[posCount].style.backgroundColor = '#E7F1F3';
											posCount++;
										}
								}
						}
				}
				
//==============================================================================
//It removes the selected options from the field send
//- It loops through the 'selectedField' to find the values that was NOT selected 
//    and saves the values to an array.
//- It then clears all the values from the 'selectedField'
//- And populates it with the values from the array [that was not selected in 'selectedField']
//==============================================================================

			function removeFeatures(selectedField)
				{
					var newValues = new Array();
					var arrCount  = 0;

					//loop through the 'selectedField' and save the NONE selected values to a array for later use.
					for (var count = 0; count < selectedField.options.length; count++)
						{
							//check if the value was not selected
							if (!selectedField.options[count].selected)
								{
									//save the none selected values in the array
         							newValues[arrCount] = new Array(selectedField.options[count].text , selectedField.options[count].value);
         							arrCount++;
								}
						}

					//clear all values in the 'selectedField'
					for (var count = selectedField.options.length - 1; count >= 0 ; count--)
						{
							selectedField.options[count] = null;
						}

					//check to see if there were any values not deleted
					if (newValues.length > 0)
						{
							//loop through the saved values and re-populate the 'selectedField' options
							for (var count = 0; count < newValues.length; count++)
							    {
									fieldOptionBgColour = (newValues[count][1] != 0)? '#E7F1F3' : '#FFFFFF' ;
								
       								selectedField.options[count]       = new Option(newValues[count][0]);
       								selectedField.options[count].value = newValues[count][1];
       								selectedField.options[count].style.backgroundColor = fieldOptionBgColour;
       							}
						}
				}
				
//==============================================================================
//It adds the value from textbox 'fromField' to multiple select 'toField'
//- It assigns the option value to 0 to indicate that it was an added feature
//==============================================================================

			function suggestedFeatures(toField, fromField)
				{
					var position  = toField.options.length;
					var valLength = fromField.value.length
					var doesExist = false;
					
					if (valLength < 18 && valLength > 0)
					{
						for (count = 0; count < position; count++)
						{
							if (toField.options[count].text == fromField.value)
							{
								doesExist = true;
								break;
							}	
						}
						
						if (doesExist)
						{
							alert('This feature has been suggested!');
						}
						else
						{
                            toField.options[position]       = new Option(fromField.value);
							toField.options[position].value = '0';
							toField.options[position].style.backgroundColor = '#FFFFFF';
						}	
						
					}
					else if (valLength <= 0)
					{
						alert('Cant add an empty feature!');
					}	
					else if (valLength >= 18)
					{
						alert('Suggested feature to long!');
					}	
				}
				
//==============================================================================
//Finds the selected value in the multiple select 'fromField'.
//Strips the seleted option text to find the amount between '[' and ']'.
//If it doesn't have the '[' or ']' brackets, then there are only one of that
//  feature.
//When the amount is found it populates the text 'toField' with the value
//N.B. : If more than one value was selected then it takes the first value selected.
//==============================================================================
			function featuresAmountEdit(fromField, toField, indexField)
				{
					var amountArr = new Array();
					var wasSelected = false;
					
					//check if a option was selected
					if (fromField.selectedIndex >= 0)
						{
							//find the index of the brackets in the option
							var firstIndex = fromField.options[fromField.selectedIndex].text.lastIndexOf("[") + 1;
							var lastIndex  = fromField.options[fromField.selectedIndex].text.lastIndexOf("]");

							//check to see if it does contain brackets, meaning that the amount is more than 1
							//it populates the 'indexField' with the selectedIndex for later use.
							if (firstIndex > 0 && lastIndex > 0 && lastIndex > firstIndex)
								{
									indexField.value = fromField.selectedIndex;
									toField.value    = fromField.options[fromField.selectedIndex].text.substring(firstIndex, lastIndex);
								}
							//if it doesn't contain brackets, there is only once instance of the selected option
							else
								{
									indexField.value = fromField.selectedIndex;
									toField.value    = '1';
								}
						}
					//if none was selected then alert that an option has to be selected.
					else
						{
							toField.value    = '';
							indexField.value = '';
							alert('Please select an option to edit.');
						}
				}

//==============================================================================
//It takes the amount value given in text 'fromField' and updates the amount in
//  multiple select 'toField' at the selectedIndex value received from indexField
//==============================================================================

		   function featuresAmountUpdate(fromField, toField, indexField)
		   		{
		   			var indexPos = parseInt(indexField.value, 10);
		   			
		   			//check to see if the index received is valid
		   			if (indexPos >= 0)
					   {
					   	    //finds the index of the brackets
					    	var firstIndex = toField.options[indexPos].text.lastIndexOf("[");
					    	var lastIndex  = toField.options[indexPos].text.lastIndexOf("]");
					    	
					    	//if it found brackets then get the description from the value
					    	if (firstIndex > 0 && lastIndex > 0)
								{
									feature = toField.options[indexPos].text.substring(0, firstIndex);
								}
							//if no brackets were found then just add the value
							else
								{
									feature = toField.options[indexPos].text + ' ';
								}

							//check to see if the value received from 'fromField' is greater than 1
							//if greater than one, then show brackets and the amount
							if ( parseInt(fromField.value, 10) > 1 )
								{
									toField.options[indexPos].text = feature + '[' + fromField.value + ']';
								}
							//if the amount is 1 or smaller, then don't show the amount
							else
								{
									toField.options[indexPos].text = Trim(feature);
								}	
					   }	
				}
				
//==============================================================================
//It either incriments or decriments the value selected in 'fromField'
// 'action' can be 'inc' or 'dec'
//N.B. - If more than one value was selected, then it will only affect the first
//           index selected.
//==============================================================================
				
			function featureAmountIncDec(fromField, action)
				{
					//check to see if an index was selected
					if (fromField.selectedIndex >= 0)
						{
							//get the position of the brackets
							var firstIndex = fromField.options[fromField.selectedIndex].text.lastIndexOf("[");
							var lastIndex  = fromField.options[fromField.selectedIndex].text.lastIndexOf("]");
							
							//if brackets were found then it had an amount value of greater than 1
							if (firstIndex > 0 && lastIndex > 0 && firstIndex < lastIndex)
								{
									//get the amount from the brackets and the description from the option
									amount      = parseFloat(fromField.options[fromField.selectedIndex].text.substring(firstIndex + 1 , lastIndex), 10);
									description = fromField.options[fromField.selectedIndex].text.substring(0, firstIndex);
									
									//if the action is to 'inc', increment the original amount and give the value
									if (action == 'inc')
										{
											amount += 0.5;
											fromField.options[fromField.selectedIndex].text = description + '[' + amount + ']';
										}
									//if the action is to 'dec', decrement the original value
									else if (action == 'dec')
										{
											//if the current amount is greater than two, then show brackets again
											if (amount >= 2)
												{
													amount -= 0.5;
													fromField.options[fromField.selectedIndex].text = description + '[' + amount + ']';
												}
											//if it's 2 then dont show brackets
											else
												{
													fromField.options[fromField.selectedIndex].text = Trim(description);
												}
										}
								}
							//if no brackets were found then there were only one instance of that value
							else
								{
									//get the description of the feature
									description = Trim(fromField.options[fromField.selectedIndex].text);
									
									//if 'inc' then amount will be 2 and add brackets
									if (action == 'inc')
										{
											amount = 1.5;
											fromField.options[fromField.selectedIndex].text = description + ' [' + amount + ']';
										}
									//if the user tries to decriment, then alert that it's not possible
									else if (action == 'dec')
										{
											alert("You can't decriment this value.");
										}
								}	
						}
					//if none were selected to increment or decrement then alert the user
					else
						{
							alert('Please select an option.');
						}	
				}	
				
//==============================================================================
//It takes the submitted values and adds all the values in a string value, where
//FeatureID, Description ,Amount is seperated by a comma and different options
//seperated by '/'.
//The string is posted where it is devide and inserted or deleted in the db
//==============================================================================

			function sortFeatures(selectField, convertedField)
					{
						var convertedString = "";
						//Loop through the values from the selected field (multiple select)
						for (var count = 0; count < selectField.options.length ;count++)
							{
								//To make sure that a '/' is not inserted before the first value
								if (count > 0)
									{
										//Use the '/' to seperate the different
										convertedString += "|";
									}
								//Find the position of the brackets
								//If the amount is 1, then no brackets will be found
								firstIndex = selectField.options[count].text.lastIndexOf("[");
								lastIndex  = selectField.options[count].text.lastIndexOf("]");

								//Check to see if the amount is greater than 1
								if (firstIndex > 0 && lastIndex > 0 && lastIndex > firstIndex)
									{
										amount      = parseFloat(selectField.options[count].text.substring(firstIndex + 1 , lastIndex), 10);
										description = selectField.options[count].text.substring(0, firstIndex);
										valueID     = selectField.options[count].value;
										//Add the ID, value description , amount seperated by ','
										convertedString += valueID + "," + description + "," + amount;
									}
								else
									{
										description = Trim(selectField.options[count].text);
										valueID     = selectField.options[count].value;
                                        //Add the ID, value description , amount of 1 seperated by ','
										convertedString += valueID + "," + description + ",1";
									}
							}
						//Assign the string to hidden variable
						convertedField.value = convertedString;
					}
