// Filters the SKU using various rules.
function addSKU(formObj, value)
{
	if (formObj.listSKU.value == '')
		formObj.listSKU.value = value;
	else
		formObj.listSKU.value += '|' + value;
}

function trim(inputString) 
{
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function



function buildSKUArray(formObj)
{
	var skuList = String(formObj.listSKU.value);
	var arrSKUList= skuList.split('|');
	var currElem = '';
	var i = 0;

	arrSKU = new Array(arrSKUList.length);
	
	for (i=0; i < arrSKUList.length; i++)
	{
		currElem = '';
		
		arrSKU[i] = new Array(8);

		if (arrSKUList[i].indexOf('_') == -1) 		
			var currElem = eval('document.' + formObj.name + '.SKU_' + arrSKUList[i] + '.value');
		else
		{
			var tmpSKU = arrSKUList[i].split('_');
			if (formObj.name == tmpSKU[1])
				var currElem = eval('document.' + formObj.name + '.SKU_' + tmpSKU[0] + '.value');
		}

		if (currElem != '') 		
		{
			currElem = currElem.split('|');
		
			arrSKU[i][0] = currElem[0];		// SKU
			arrSKU[i][1] = trim(currElem[1]);	// Item name
			arrSKU[i][2] = trim(currElem[2]);	// Top size
			arrSKU[i][3] = trim(currElem[3]);	// Bottom size
			arrSKU[i][4] = trim(currElem[4]);	// Cup
			arrSKU[i][5] = trim(currElem[5]);	// Color
			arrSKU[i][6] = trim(currElem[6]);	// Price
			arrSKU[i][7] = trim(currElem[7]);	// Item Variant Descriptors
		}
	}
}

function findMatch(topSize, bottomSize, cup, color)
{
	var i = 0;

	if (topSize == 0) {topSize = '';}
	if (bottomSize == 0){bottomSize = '';}
	if (cup == 0) {cup = '';}
	if (color == 0) {color = '';}

	// if any fields are empty return '-2' and alert that all available dropdowns 
	// must have input.
	if (topSize == '' || bottomSize == '' || cup == '' || color == '')
		return -2;

	// reset the undefined variablesso that they can be matched against 
	// the SKU.
	if (topSize == 'undefined') { topSize = ''; }
	if (bottomSize == 'undefined') { bottomSize = ''; }
	if (cup == 'undefined') { cup = ''; }
	if (color == 'undefined') { color = ''; }

	for (i=0; i < arrSKU.length; i++)
	{
		if (arrSKU[i][2] == topSize && arrSKU[i][3] == bottomSize && arrSKU[i][4] == cup && arrSKU[i][5] == color)
			return arrSKU[i][0];
	}

	return -1;
}

function findSKURow(match)
{
	var i = 0;

	for (i=0; i < arrSKU.length; i++)
		if (arrSKU[i][0] == match)
			return i;

	return -1;
}


function getSelectedSKU(formObj)
{
	// [SKU]|[ItemName]|[TopSize]|[BottomSize]|[Cup]|[Colour]|[Price]
	var i = 0;

	buildSKUArray(formObj);

	var selCup		= 'undefined';
	var selColor		= 'undefined';
	var selTopSize		= 'undefined';
	var selBottomSize	= 'undefined';

	if (typeof(formObj.AbsCup) != 'undefined')
		selCup	 = formObj.AbsCup.value;

	if (typeof(formObj.AbsColor) != 'undefined')
		selColor = formObj.AbsColor.value;
	
	if (typeof(formObj.AbsTopSize) != 'undefined')
		selTopSize = formObj.AbsTopSize.value;
		
	if (typeof(formObj.AbsBottomSize) != 'undefined')
		selBottomSize = formObj.AbsBottomSize.value;


	var match = findMatch(selTopSize,selBottomSize,selCup,selColor);

	if (match == -2)
	{
		alert('Please ensure you have made a selection from each drop-down box.');
		return false;		
	}	
	
	if (match == -1)
	{
		alert('The combination you have selected does not exist. Please choose again.');
		return false;		
	}	
	
	// find the row that contains the SKU
	i = findSKURow(match)
	
	// Set these hidden form fields up for the shopping cart.
	formObj.ID_NUM.value = arrSKU[i][0];
	formObj.PRICE.value = arrSKU[i][6];
	formObj.RRP.value = arrSKU[i][6];
	formObj.NAME.value = arrSKU[i][1] + getVariantDescriptors(arrSKU[i][7]);
	formObj.ADDITIONALINFO.value += '&SKU=' + arrSKU[i][0];

	return true;
}

// Get the item variant descriptors for the currently selected SKU to append to
// the item name field.
function getVariantDescriptors(value)
{
	// [AbsColour]-[AbsCup]-[AbsTopSize]-[AbsBottomSize]
	var arr = String(value).split('-');
	var returnVal = '';
	
	for (var i=0; i < arr.length; i++)
		if (arr[i] != '')
			returnVal += ' ' + arr[i];	

	return returnVal;
}

function setSelectBoxes(formObj,value)
{
	if (value == '')
		return false;

	var foundSKU = '';
	
	buildSKUArray(formObj);
	
	for (i = 0; i < arrSKU.length; i++)
		if (arrSKU[i][0] == value)
			foundSKU = i;
			
	if (!foundSKU || foundSKU == '') 
		return false;
	
	var i = foundSKU;

	if (typeof(formObj.AbsCup) != 'undefined')
		setSelect(formObj.AbsCup, arrSKU[i][4]);

	if (typeof(formObj.AbsColor) != 'undefined')
		setSelect(formObj.AbsColor, arrSKU[i][5]);
	
	if (typeof(formObj.AbsTopSize) != 'undefined')
		setSelect(formObj.AbsTopSize, arrSKU[i][2]);
		
	if (typeof(formObj.AbsBottomSize) != 'undefined')
		setSelect(formObj.AbsBottomSize, arrSKU[i][3]);

	return true;
}

function setSelect(select,value)
{
	for (i = 0; i < select.length; i++) 
	{
		if (select.options[i].value == value)
		{
			select.options[i].selected = true;
			return true;
		}	
	}
	return false;
}
