function InitializeFeatures(intFeatureCount)
{
	if (document.getElementById != null) {
		if (intFeatureCount > 0) {
			objItem.FillNextFeature(document.forms['OrderForm'], -1);
			document.getElementById('ItemDetails').style.display = '';
		}
		
		document.getElementById('Pricing').style.display = '';
	}
	
	return void(0);
}

function Item(intItemID, intFeatureCount) {
	this.intItemID = intItemID;
	this.intFeatureCount = intFeatureCount;
	this.arrSKUs = new Array();
	
	return this;
}

Item.prototype.FillNextFeature = function(objForm, intFeatureIndex) {
	var objTempSelectField = new Object();
	var arrSelectOptions = new Array();
	var objNextSelectField = new Object();
	var objNextSelectOption = new Object();
	var intSKUFeatureID = 0;
	var intSelectFeatureID = 0;
	var intPos = -1;
	var bolMatch = false;
	var bolExists = false;
	
	/* If this selected feature is not the last selectable feature: find the 
	** next feature select field, clear all the following feature select 
	** fields and clear the pricing.
	**/
	if ((intFeatureIndex + 1) < this.intFeatureCount) {	
		/* Clear all the following feature select fields. */
		for (var i = (intFeatureIndex + 1); i < this.intFeatureCount; i++) {
			objForm.elements['Feature_' + i.toString()].options.length = 1;
			objForm.elements['Feature_' + i.toString()].options[0].selected = true;
		}
		
		/* Clear pricing. */
		this.arrSKUs[0].DisplayPricing(objForm, false);
	}
	
	/* Loop over the SKUs and match each SKU's features to the feature select
	** fields. If they match then add this SKU's next feature to the next feature
	** select field.
	**/
	for (var i = 0; i < this.arrSKUs.length; i++) {
		bolMatch = true;
		
		/* Loop over the feature select fields (ie fields 'Feature_1', 'Feature_2' ...)
		** and match them against the SKU's features (arrFeature[1], arrFeature[2], ...).
		**/
		for (var j = 0; j <= intFeatureIndex; j++) {
			/* Get the value of the first, second, ... feature select field. */
			intSelectFeatureID = Number(objForm.elements['Feature_' + j.toString()].value);
			
			/* Get the value of the first, second, ... feature of this SKU. */
			intSKUFeatureID = this.arrSKUs[i].arrFeatures[j].intFeatureID;
			
			/* If the first option is selected on any of the select fields (meaning
			** "none" was selected) then don't continue.
			**/
			if (intSelectFeatureID == 0) {
				/* Clear pricing. */
				this.arrSKUs[0].DisplayPricing(objForm, false);

				return void(0);
			}
			else {
				/* Compare the value of the first, second, ... feature select field to this
				** SKU's first, second, ... feature respectively. If is doesn't match then
				** exit the loop and do not add this SKU's feature to the next feature select field.
				**/
				if (intSelectFeatureID != intSKUFeatureID) {
					bolMatch = false;
					break;
				}
			}
		}
		
		/* If the SKU's features matched all the feature select fields then either fill
		** the next feature select field or if no more feature select field exist
		** display the pricing details for this SKU.
		**/
		if (bolMatch) {
			/* If this feature select field is the last feature select field then show
			** the pricing otherwise fill the next feature select field.
			**/
			if ((intFeatureIndex + 1) == this.intFeatureCount) {
				this.arrSKUs[i].DisplayPricing(objForm);
				break;
			}
			else {
				bolExists = false;
				intPos = -1;
				
				/* Loop over the values already in the next feature select list and don't
				** add this SKU's feature if it already exists.
				**/
				for (j = 0; j < arrSelectOptions.length; j++) {
					if (this.arrSKUs[i].arrFeatures[intFeatureIndex + 1].intFeatureID == arrSelectOptions[j].value) {
						bolExists = true;
						break;
					}
					else {
						if ((intPos == -1) && (this.arrSKUs[i].arrFeatures[intFeatureIndex + 1].intFeatureRank < Number(arrSelectOptions[j].getAttribute('featurerank')))) {
							intPos = j;
						}
					}
				}
				
				if (intPos == -1) {
					intPos = arrSelectOptions.length;
				}
				
				/* If the SKU's feature doesn't already exits add it to the feature select field. */
				if (!bolExists) {
					objNextSelectOption = new Option(this.arrSKUs[i].arrFeatures[intFeatureIndex + 1].strFeature, this.arrSKUs[i].arrFeatures[intFeatureIndex + 1].intFeatureID);
					objNextSelectOption.setAttribute('featurerank', this.arrSKUs[i].arrFeatures[intFeatureIndex + 1].intFeatureRank);
					
					arrSelectOptions.splice(intPos, 0, objNextSelectOption);
				}
			}
		}
	}
	
	objNextSelectField = objForm.elements['Feature_' + (intFeatureIndex + 1).toString()];
	
	for (i = 0; i < arrSelectOptions.length; i++) {
		objNextSelectField.options[objNextSelectField.length] = arrSelectOptions[i];
	}
		
	/* If the next feature select field is NOT the last feature select field check the 
	** next select field to see if there is only one option. If there is just one 
	** option (excluding the first option, which is empty) then call the
	** "objItem.FillNextFeature()" function again.
	**/
	if (((intFeatureIndex + 1) < this.intFeatureCount) && (objNextSelectField.options.length == 2)) {
		objNextSelectField.options[1].selected = true;
		objItem.FillNextFeature(objForm, intFeatureIndex + 1);
	}
	
	return void(0);
}

function SKU(sku, fltPrice, fltDiscountPrice, strDiscount, bolInStock) {
	this.sku = sku;
	this.fltPrice = fltPrice;
	this.fltDiscountPrice = fltDiscountPrice;
	this.strDiscount = strDiscount;
	this.bolInStock = bolInStock;
	this.arrFeatures = new Array();
	
	return this;
}

SKU.prototype.DisplayPricing = function(objForm, bolDisplay) {
	var objPriceTD = document.getElementById('PriceTD');
	var objDiscountPriceTR = document.getElementById('DiscountPriceTR');
	var objDiscountP = document.getElementById('DiscountP');
	var objSKUINPUT = objForm.elements['SKU'];
	var objQuantityINPUT = objForm.elements['Quantity'];
	bolDisplay = (bolDisplay == null) ? true : bolDisplay;
	
	/* If the pricing cell exist try to set the pricing. */
	if (objPriceTD != null) {
		/* Display/Hide price based on if the item details have been selected.
		** If not in stock display 'Out of Stock' message.
		**/
		if (bolDisplay) {
			if (this.bolInStock) {
				objPriceTD.replaceChild(document.createTextNode('$' + this.fltPrice), objPriceTD.firstChild);
			}
			else {
				objPriceTD.replaceChild(document.createElement('b'), objPriceTD.firstChild);
				objPriceTD.firstChild.className = 'Alert';
				objPriceTD.firstChild.appendChild(document.createTextNode('Out of Stock'));
			}
		}
		else {
			objPriceTD.replaceChild(document.createTextNode(objPriceTD.getAttribute('DisplayPrice')), objPriceTD.firstChild);
		}
		
		/* Disable/Enable SKU and quantity input based on if the item details have been
		** selected and if in stock.
		**/
		if ((bolDisplay) && (this.bolInStock)) {
			objSKUINPUT.value = this.sku;
			objQuantityINPUT.value = '1';
			objQuantityINPUT.disabled = false;
			objQuantityINPUT.className = 'Number';
		}
		else {
			objSKUINPUT.value = '';
			objQuantityINPUT.value = '';
			objQuantityINPUT.disabled = true;
			objQuantityINPUT.className = 'ReadOnly';
		}
		
		/* Display/Hide discount price based on if the item details have been selected, the 
		** item is in stock and there's a discount price.
		**/
		if ((bolDisplay) && (this.bolInStock) && (this.fltDiscountPrice > 0.00)) {
			objDiscountPriceTR.style.display = '';
			objDiscountPriceTR.childNodes[0].replaceChild(document.createTextNode('New Price:'), objDiscountPriceTR.childNodes[0].firstChild);
			objDiscountPriceTR.childNodes[1].replaceChild(document.createTextNode('$' + this.fltDiscountPrice), objDiscountPriceTR.childNodes[1].firstChild);
		}
		else {
			objDiscountPriceTR.style.display = 'none';
			objDiscountPriceTR.childNodes[0].replaceChild(document.createTextNode('\xA0'), objDiscountPriceTR.childNodes[0].firstChild);
			objDiscountPriceTR.childNodes[1].replaceChild(document.createTextNode('\xA0'), objDiscountPriceTR.childNodes[1].firstChild);
		}
		
		/* Display/Hide discount description based on if the item details have been selected. */
		if ((bolDisplay) && (this.bolInStock) && (this.strDiscount.length > 0)) {
			objDiscountP.style.display = '';
			objDiscountP.replaceChild(document.createTextNode(this.strDiscount), objDiscountP.firstChild);
		}
		else {
			objDiscountP.style.display = 'none';
			objDiscountP.replaceChild(document.createTextNode('\xA0'), objDiscountP.firstChild);
		}
	}
	
	return void(0);
}

function Feature(intFeatureID, strFeatureType, strFeature, intFeatureRank) {
	this.intFeatureID = intFeatureID;
	this.strFeatureType = strFeatureType;
	this.strFeature = strFeature;
	this.intFeatureRank = intFeatureRank;
	
	return this;
}

/* Validates the product page form to make sure the features were selected and a quantity was entered. */
function ValidateForm(objForm) {
	for (var i = 0; i < objForm.elements.length; i++) {
		objField = objForm.elements[i];
		
		if ((objField.name != null) && (objField.name.indexOf('Feature_') == 0) && (objField.selectedIndex < 1)) {
			DisplayMessage('Please ' + objField.options[0].text + '.');
			objField.focus();
			return false;
		}
	}
	
	if ((IsEmptyField(objForm.elements['Quantity'])) || (isNaN(objForm.elements['Quantity'].value))) {
		DisplayMessage('Please enter a quantity.');
		objForm.elements['Quantity'].focus();
		return false;
	}
	
	return true;
}

/* Validates the product page form to make sure at least one quantity was entered for the select mutiple form
** This form has one row in the table for each sku with an quantity input box (similar to the no JavaScript table).
**/
function ValidateForm2(objForm) {
	var bolQtyEntered = false;
	
	for (var i = 0; i < objForm.elements.length; i++) {
		objField = objForm.elements[i];
		
		if ((objField.name != null) && (objField.name.indexOf('Quantity_') == 0) && (!IsEmptyField(objField)) && (!isNaN(objField.value))) {
			bolQtyEntered = true;
			break;
		}
	}
	
	if (!bolQtyEntered) {
		DisplayMessage('Please enter at least one quantity.');
		objForm.elements['Quantity_1'].focus();
		return false;
	}
	
	return true;
}

/* Function that swaps out the large product image on a mouse over of the small thumbnails. */
function ReplaceImage(strImgName,strImgPath,strImgTitleID,strImgTitle) {
	var objImgTitle = new Object();

	if ((document.images) && (document.getElementById)) {
		if (document.images[strImgName].src.indexOf(strImgPath) == -1) {
			document.images[strImgName].src = strImgPath;
			objImgTitle = document.getElementById(strImgTitleID);
			objImgTitle.replaceChild(document.createTextNode(strImgTitle), objImgTitle.firstChild);
		}
	}
	
	return void(0);
}

function SwapClass(strParentID,strTag,intIndex) {
	var objParent = new Object();
	var objElement = new Object();

	if (document.getElementById) {
		objParent = document.getElementById(strParentID);
		
		for (var i = 0; i < objParent.getElementsByTagName(strTag).length; i++) {
			objElement = objParent.getElementsByTagName(strTag)[i];
			
			if (objElement.className.length > 2) {
				/* if the index of the element is the same as the current element append "On" to the class */
				if (i == intIndex) {
					if (objElement.className.substring(objElement.className.length-2,objElement.className.length) != 'On') {
						objElement.className += 'On';
					}
				}
				/* For all elements execpt for the current element remove the "On" */
				else if (objElement.className.substring(objElement.className.length-2,objElement.className.length) == 'On') {
					objElement.className = objElement.className.substring(0,objElement.className.length-2);
				}
			}
		}
	}
	
	return void(0);
}