// Used to redirect and submit a form because there are approximately 1.2 million forms this the custom rigs page.
function RedirectAndSubmit(theForm, targetURL)
{
  theForm.action = targetURL;
  theForm.submit();
}

function submitCategory(tempValue, SKU, Quantity, gRig, gType, gProfileArgs)
{
	// gType should either be regular or basket
	if(gType=="regular")
	{
		document.CustomRigsForm.action = "custom.asp?id="+tempValue+"&prod="+SKU+"&quan="+Quantity+"&rig="+gRig+gProfileArgs;
	}
	else
	{
		document.CustomRigsForm.action = "custom.asp?id="+tempValue+"&prod="+SKU+"&quan="+Quantity+"&bsyscode="+gRig+gProfileArgs;
	}
	document.CustomRigsForm.submit();
}

// this function takes a category name and formats the name to reference it's DOM name
function formatCategory(catName) {
	var pattern = /\s/g;
	catName = catName.replace(pattern, "_");
	return catName;
}

// this function takes an array of categories and erases the drop down for each one.
// it also returns an array of SKUs that were selected right before the big erase.
function eraseDropDowns(catArr, gFilterCats) {
	var currentListLength, tempCategory, i, j, k, m;
	var savedSKUArr = new Array();
	var relToArr = new Array();
	relToArr = splitStr(gFilterCats, ",");
	
	for(i=0; i<catArr.length; i++) {
		tempCategory = formatCategory(catArr[i]);
		for(k=1; k<=MAX_CATEGORIES; k++)
		{
			var tempObj = tempCategory+k;
			for(m=0; m<relToArr.length; m++)
			{
				if(typeof eval("document.CustomRigsForm."+tempObj) != "undefined")
				{
					if(formatCategory(relToArr[m]) == tempCategory)
					{
						currentListLength = eval("document.CustomRigsForm." + tempObj).length;
						savedSKUArr[i] = eval("document.CustomRigsForm." + tempObj + "[document.CustomRigsForm." + tempObj + ".selectedIndex].value") + ":" + tempCategory;
						for(j=0; j<=currentListLength; j++)
						{
							eval("document.CustomRigsForm." + tempObj + "[0]=null;");
						}
					}
				}
			}
		}
	}
	return savedSKUArr;
}

// this function populates drop downs with new relationships.
// optimize by breaking out of loop once a match has been found.
function populateDropDowns(category,pos,savedSKUArr,tempRelArr, gFilterCats)
{
	var i,j,k,m;
	var opt;
	var tempCategory = "";
	var bigRelArray = new Array();
	var relDiffCatArr = new Array();
	var tempOptionArr1 = new Array();
	var tempOptionArr2 = new Array();
	var optionArr = new Array();
	var relToArr = new Array();
	var savedSKUElemArr = new Array();
	var optionsLength = 0;

	//basically, if there's no relationships don't do anything, just return.
	if(MasterArr[category][2][pos] == "") return;
	
	// here we split info out of the big array and seperate on the "?"
	for(i=0; i<tempRelArr.length; i++) {
		bigRelArray[i] = new Array();
		bigRelArray[i] = splitStr(tempRelArr[i], "?");
	}
	
	//iterates through the bigRelArray and pulls out category name and related SKUs.  These are added to the
	//multidimensional optionArr.  element 0 is for category.  element 1 is for Sku.
	for(i=0; i<bigRelArray.length; i++)
	{
		optionArr[i] = new Array();
		for(j=0; j<bigRelArray[i].length;j++)
		{
			for(k=0; k<MasterArr.length;k++)
			{
				if(bigRelArray[i][1] == MasterArr[k][0])
				{
					optionArr[i][0] = bigRelArray[i][1];
					optionArr[i][1] = bigRelArray[i][0];
				}
			}
		}
	}
	
	//iterates through the optionArr and then compares the first elementof the optionArr which should be
	//the category name with the master array.  If a match is found it then grabs the list of Skus for that 
	//category and then splits that into sku/text.  Then it iterates through that and matches the second element
	//of the optionArr (the sku) with the master arr sku-- if a match is found it adds the text for that sku to the 
	//third element of the optionArr
	for(i=0; i<optionArr.length; i++)
	{
		for(j=0; j<MasterArr.length; j++)
		{
			if(optionArr[i][0] == MasterArr[j][0]) {
				tempOptionArr1 = splitStr(MasterArr[j][1],";");
				for(k=0; k<tempOptionArr1.length; k++)
				{
					tempOptionArr2 = splitStr(tempOptionArr1[k],"?");
					if(tempOptionArr2[0] == optionArr[i][1])
					{
						optionArr[i][2] = tempOptionArr2[1];
					}
				}
			}
		}
	}
	

	// We get the SKU relationship "to" array by splitting on the ","
	relToArr = splitStr(gFilterCats, ",");

	// Main outer loop.
	for(i=0; i<optionArr.length; i++)
	{
		// Could be up to this many catories on the page, so better check all.
		for(j=1; j<=MAX_CATEGORIES; j++)
		{
			// Format the category name
			tempCategory = formatCategory(optionArr[i][0]);
			
			// Build the options from the optionsArr (sku and text).
			opt = new Option;
			opt.text = optionArr[i][2];
			opt.value = optionArr[i][1];

			var tempObj = tempCategory+j;

			for(m=0; m<relToArr.length; m++)
			{
				// well, if the category drop down doesn't exist, then we sure don't need to do anything.
				if((typeof eval("document.CustomRigsForm." + tempObj) != "undefined"))
				{
					// If you find a matching category on the page...
					if(formatCategory(relToArr[m]) == tempCategory)
					{
						// add the option.
						optionsLength = eval("document.CustomRigsForm."+tempObj).options.length;
						eval("document.CustomRigsForm."+tempObj).options[optionsLength] = opt;

						// we loop through the saved SKU array to see if a filtered product has the same SKU as one that we saved.
						// if so, we set that to selected.
						for(k=0; k<savedSKUArr.length; k++)
						{
							savedSKUElemArr = splitStr(savedSKUArr[k], ":");
							if(savedSKUElemArr[0]==opt.value && savedSKUElemArr[1]==tempCategory)
							{
								eval("document.CustomRigsForm."+tempObj).options[optionsLength].selected = 1;
							}
						}
					}
				}
			}
		}
	}
	
	// Here is yet another loop to set something to selected.  Basically we loop through every drop down category.  If nothing is selected (i.e. returns -1)
	// then we set the 1st element in the drop down to selected.  This also is a Netscape fix as well.  Stupid Netscape.
	for(i=0; i<optionArr.length; i++)
	{
		for(j=1; j<=MAX_CATEGORIES; j++)
		{
			tempCategory = formatCategory(optionArr[i][0]);
			var tempObj = tempCategory+j;
			if((typeof eval("document.CustomRigsForm." + tempObj) != "undefined"))
			{
				if(eval("document.CustomRigsForm."+tempObj).options.selectedIndex == -1)
				{
					eval("document.CustomRigsForm."+tempObj).options[0].selected = 1;
				}
			}
		}
	}
}


// this function accepts a SKU and checks to see if there is a valid relationship for that SKU.  If so, it returns it's position in the array, otherwise, -1
// a description which god himself could not disagree.  Why?  Because that's exactly what this function does.
function checkForRelationship(SKU, gFilterCats)
{
	var tempCounter, i;
	var tempSKUArr = new Array();
	var tempRelArr = new Array();
	
	if((SKU == "--") && (gFilterCats == "Memory"))
	{
		i = 1
	}
	else
	{
		i = 0
	}
	
	for(i=i; i < MasterArr.length; i++)
	{
		tempRelArr = splitStr(MasterArr[i][1], ";");
		for(tempCounter = 0; tempCounter < tempRelArr.length; tempCounter++)
		{
			tempSKUArr = splitStr(tempRelArr[tempCounter],"?");
			if(tempSKUArr[0] == SKU) {
				return tempCounter+";"+i;
			} 
		}
	}
	return -1;
}

// This function is what the onchange / onload events call on custom.asp.  It pretty much just gets sub filters to fire whenever any filter is called.
// I.E.  If you filter CPU, then Motherboard is also forced to be filtered.  This is pretty much a hack.
function MainFilterDriver(gSKU, gFilterCats)
{
	FilterProducts(gSKU, gFilterCats);
	FilterProducts(document.CustomRigsForm.Motherboard1[document.CustomRigsForm.Motherboard1.selectedIndex].value, "Memory");
}

// One of the main filter driver functions.  Coordinates program flow.
function FilterProducts(gSKU, gFilterCats) {
	var i, j;
	var returnFromCheckRel = new Array();
	var pos;
	var arrayPos;
	var getCategory;
	var relStr = "";
	
	var tempRelArr = new Array();
	var tempRelCatArr = new Array();
	var tempRelDiffCatArr = new Array();
	var tempRelDescArr = new Array();
	var savedSKUArr = new Array();
	
	// make sure there's a valid relationship before we continue.
	var bigRelArray = new Array();
	if(checkForRelationship(gSKU, gFilterCats) == -1) return;
	returnFromCheckRel = checkForRelationship(gSKU, gFilterCats).split(";");
	pos = returnFromCheckRel[0];
	getCategory = returnFromCheckRel[1];
	if(pos == -1) return;

	if(MasterArr[getCategory][2][pos] != "")
	{
		// Build the relationship array
		tempRelArr = splitStr(MasterArr[getCategory][2][pos], ";");
		// save any skus that were previously selected and erase filtered drop downs
		savedSKUArr = eraseDropDowns(getCategoriesFromRel(MasterArr[getCategory][2][pos]), gFilterCats);
		// populate drop downs with filtered products.
		populateDropDowns(getCategory,pos,savedSKUArr,tempRelArr, gFilterCats);
	}
}


// This function accepts a string of... well... something, and returns all of the categories in the string in an array format.
function getCategoriesFromRel(catStr)
{
	var catArr = new Array();
	var splitArr = new Array();
	var splitArr2 = new Array();
	var catExists = false;
	var i,j,k;

	splitArr = splitStr(catStr,";")
	for(i=0; i<splitArr.length; i++)
	{
		catExists = false;
		splitArr2 = splitStr(splitArr[i],"?");
		for(j=0; j<=catArr.length; j++)
		{
			if(catArr[j] == splitArr2[1]){
				catExists = true;
				break;
			}
		}
		if(!catExists)
		{
			catArr[catArr.length] = splitArr2[1];
		}
	}
	return catArr;
}

// gets rid of null value at end of a string and returns an array.
// the null value comes from the ending ";" from the end of each array element, so when we split along 
// the semicolon we get a null value at the end.  
function splitStr(str, delimiter)
{
	var Arr = new Array();
	var Arr2 = new Array();
	var i, j;
	j = 0;

	Arr  = str.split(delimiter);
	for(i=0; i<Arr.length; i++)
	{
		if(Arr[i] != "")
		{
			Arr2[j] = Arr[i];
			j++;
		}
	}
	return Arr2;
}


var win = null;

