
/* 'returnProductArray()' arguments: 'buildMe' contains the name of the attributeArray for which metaArray is currently being built; 'testCase' value==0 returns productArray().attribute named by 'buildMe' */
	function returnProductArray(i,buildMe,testCase,metaProductArray){
	if((buildMe=='size' && testCase==0) || (buildMe=='color' && testCase==1) || (buildMe=='type' && testCase==1)){
		return metaProductArray[i].size;
	}
	else if((buildMe=='color' && testCase==0) || (buildMe=='size' && testCase==1) || (buildMe=='type' && testCase==2)){
		return metaProductArray[i].color;
	}
	else if((buildMe=='type' && testCase==0) || (buildMe=='size' && testCase==2) || (buildMe=='color' && testCase==2)){
		return metaProductArray[i].type;
	}
}

function buildMetaArray(buildMe, selectedsize, selectedcolor, selectedtype, thisMany, resetMe, metaProductArray){
	var metaArray= new Array();
	var sortArray= new Array();
	var selectedOne;
	var selectedTwo;
	if(buildMe=='size'){
		selectedOne=selectedcolor;
		selectedTwo=selectedtype;
	}
	else if(buildMe=='color'){
		selectedOne=selectedsize;
		selectedTwo=selectedtype;
	}
	else if(buildMe=='type'){
		selectedOne=selectedsize;
		selectedTwo=selectedcolor;
	}

	/* a four case test statement. tests which (caseOne or caseTwo), or if both selectElements have changed, or if all selectElements are to be re-built */
	if (selectedOne!='NOSELECTION' && selectedTwo=='NOSELECTION' && resetMe==0) {

		/* build metaArray matching only against 'testCase'==1 from 'returnProductArray()' */
		for (i=0;i<thisMany;i++) {
			if (returnProductArray(i,buildMe,1,metaProductArray)==selectedOne) {

				/* 'makeArray' is a switch. 0=false(don't); 1=true(do) */
				var makeArray=1;

				/* check against repeating values. search metaArray with the current productArray value to check against repeating values */
				for (j=0;j<metaArray.length;j++) {
					if (returnProductArray(i,buildMe,0,metaProductArray)==metaArray[j]) {
						makeArray=0;
					}
				}

				/* where the magic happens baby.  set a value in metaArray. */
				if (makeArray==1) {
					metaArray[metaArray.length]=returnProductArray(i,buildMe,0,metaProductArray);
					sortArray[sortArray.length]= metaProductArray[i].order;
				}
			}
		}
	} else if (selectedTwo!='NOSELECTION' && selectedOne=='NOSELECTION' && resetMe==0) {

		/* build metaArray matching only against 'testCase'==2 from 'returnProductArray()' */
		for (i=0;i<thisMany;i++) {
			if (returnProductArray(i,buildMe,2,metaProductArray)==selectedTwo) {

				/* 'makeArray' is a switch. 0=false(don't); 1=true(do) */
				var makeArray=1;

				/* check against repeating values. search metaArray with the current productArray value to check against repeating values */
				for (j=0;j<metaArray.length;j++) {
					if (returnProductArray(i,buildMe,0,metaProductArray)==metaArray[j]) {
						makeArray=0;
					}
				}
				if (makeArray==1) {
					metaArray[metaArray.length]=returnProductArray(i,buildMe,0,metaProductArray);
					sortArray[sortArray.length]= metaProductArray[i].order;
				}
			}
		}
	} else if (selectedOne!='NOSELECTION' && selectedTwo!='NOSELECTION' && resetMe==0) {

		/* build metaArray matching against both 'testCase'==1 and 'testCase'==2 from 'returnProductArray()' */
		for (i=0;i<thisMany;i++) {
			if (returnProductArray(i,buildMe,1,metaProductArray)==selectedOne && returnProductArray(i,buildMe,2,metaProductArray)==selectedTwo) {

				/* 'makeArray' is a switch. 0=false(don't); 1=true(do) */
				var makeArray=1;

				/* check against repeating values. search metaArray with the current productArray value to check against repeating values */
				for (j=0;j<metaArray.length;j++) {
					if (returnProductArray(i,buildMe,0,metaProductArray)==metaArray[j]) {
						makeArray=0;
					}
				}

				/* where the magic happens baby.  set a value in metaArray. */
				if (makeArray==1) {
					metaArray[metaArray.length]=returnProductArray(i,buildMe,0,metaProductArray);
					sortArray[sortArray.length]= metaProductArray[i].order;
				}
			}
		}
	} else if (resetMe==1 || resetMe=='all') {

		/* build metaArray with all items from productArray */
		for (i=0;i<thisMany;i++) {
			/* 'makeArray' is a switch. 0=false(don't); 1=true(do) */
			var makeArray=1;

			/* check against repeating values. search metaArray with the current productArray value to check against repeating values */
			for (j=0;j<metaArray.length;j++) {
				if (returnProductArray(i,buildMe,0,metaProductArray)==metaArray[j]) {
					makeArray=0;
				}
			}
			/* where the magic happens baby.  set a value in metaArray. */
			if (makeArray==1) {
				metaArray[metaArray.length]=returnProductArray(i,buildMe,0,metaProductArray);
				sortArray[sortArray.length]= metaProductArray[i].order;
			}
		}
	}

	/* these variables and three for-loops sort metaArrayCopy into the order dictated by the sortArray */
	/* initialize metaArrayCopy and sortArrayCopy to have exactly the same length as the originals. this is important because we will be inserting values into metaArrayCopy non-sequentially */
	var metaArrayCopy = new Array(metaArray.length);
	var sortArrayCopy = new Array(sortArray.length);

	/* make a copy of sortArray */
	for(i=0;i<sortArray.length;i++){
		sortArrayCopy [i]=sortArray[i];
	}

	/* make a copy of metaArray */
	for(i=0;i<metaArray.length;i++){
		metaArrayCopy [i]=metaArray[i];
	}

	/*  sort sortArray in numerical ascending order */
	sortArray.sort(function(a,b){
			return a - b
		}
	);

	/* match a value in sortArrayCopy against a value in sortArray. use the indice from this match to place a value from metaArray into an indicie in metaArrayCopy */
	for (i=0;i<sortArray.length;i++){
		for (j=0;j<sortArray.length;j++){
			if(sortArrayCopy[i] == sortArray[j]){
				metaArrayCopy[j] = metaArray[i];
			}
		}
	}

	return metaArrayCopy;
}

function adjustSelect(thisValue,thisAttribute,resetMe,metaProductArray,tempsize,tempcolor,temptype,tempquantity) {

	/* set a bunch of variables */
	var thisMany = metaProductArray.length;
	var sizeArray = new Array();
	var colorArray = new Array();
	var typeArray = new Array();

	/* variable 'buildMe' used by function 'buildMetaArray()' to know for which attributeArray to build  */
	var buildMe;

	/* store current selected states of selectElements.  Used in TWO places.  Used in function 'buildMetaArray()' to test for a change from default values for all the selectElements.  Used at the end of this function for resetting to these values after rebuilding selectElement optionsArray. */
	if (tempsize.options != null) {
		var selectedsize=tempsize.options[tempsize.selectedIndex].value;
	}

	if (tempcolor.options != null) {
		var selectedcolor=tempcolor.options[tempcolor.selectedIndex].value;
	}

	if (temptype.options != null) {
		var selectedtype=temptype.options[temptype.selectedIndex].value;
	}

	if (tempquantity.options != null) {
		var selectedquantity=tempquantity.options[tempquantity.selectedIndex].value;
	}

	/* if returning to the page with pre-selected values for the selcetElements then dump out of the function */
	if (thisValue=='pageload'){
		var returnMe = 1;
		if (tempsize.type != 'hidden' && selectedsize == 'NOSELECTION'){
			returnMe = 0;
		}
		if (tempcolor.type != 'hidden' && selectedcolor == 'NOSELECTION'){
			returnMe = 0;
		}
		if (temptype.type != 'hidden' && selectedtype == 'NOSELECTION'){
			returnMe = 0;
		}
		if (temptype.quantity != 'hidden' && selectedquantity == 'NOSELECTION'){
			returnMe = 0;
		}
		if (returnMe==1){
			return;
		}
	}

	/* this satement causes all selecet elements to be reset to default values if all the select element values equal 'NOSELECTION', but if anything less than all have a value of 'NOSELECTION' exit the function without effecting any changes */
	if ((thisValue.toString().indexOf('NOSELECTION')!=-1) && resetMe!='all'){
		/*JC:28.4 Had to add this to fix errors in Mozilla... sometimes the hidden form elements don't exist so we must test. */
		var Size=document.productAttributeDropdown.size;
		var Type=document.productAttributeDropdown.type;
		var Color=document.productAttributeDropdown.color;
		var Quantity=document.productAttributeDropdown.quantity;
		if(
				(selectedsize=='NOSELECTION' || (Size && Size.type == 'hidden'))
				&&
				(selectedtype=='NOSELECTION' || (Type && Type.type == 'hidden'))
				&&
				(selectedcolor=='NOSELECTION' || (Color && Color.type == 'hidden'))
				&&
				(selectedquantity=='1' || (Quantity && Quantity.type == 'hidden'))
			)
			{
			resetMe='all';
			}
		else {
			return;
		}
	}
	

	/* a series of seven test statements.  all build a discrete set of the attributeArrays and resets the value of variable 'buildMe' to corrospond to the attributeArray to be built by function 'buildMetaArray()'  */
	if (thisAttribute=='size'&&resetMe==0&&temptype!=null) {
		buildMe='color';
		colorArray = buildMetaArray(buildMe, selectedsize, selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
		buildMe='type';
		typeArray = buildMetaArray(buildMe, selectedsize, selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
	} else if (thisAttribute=='color'&&resetMe==0&&temptype!=null) {
		buildMe='size';
		sizeArray = buildMetaArray(buildMe, selectedsize, selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
		buildMe='type';
		typeArray = buildMetaArray(buildMe, selectedsize,selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
	} else if (thisAttribute=='type'&&resetMe==0&&temptype!=null) {
		buildMe='size';
		sizeArray = buildMetaArray(buildMe, selectedsize,selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
		buildMe='color';
		colorArray = buildMetaArray(buildMe, selectedsize,selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
	} else if (thisAttribute=='size'&&resetMe==0&&temptype==null) {
		buildMe='color';
		colorArray = buildMetaArray(buildMe, selectedsize, selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
	} else if (thisAttribute=='color'&&resetMe==0&&temptype==null) {
		buildMe='size';
		sizeArray = buildMetaArray(buildMe, selectedsize, selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
	} else if(resetMe=='all') {
		buildMe='size';
		sizeArray = buildMetaArray(buildMe, selectedsize,selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
		buildMe='color';
		colorArray = buildMetaArray(buildMe, selectedsize,selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
		buildMe='type';
		typeArray = buildMetaArray(buildMe, selectedsize,selectedcolor,selectedtype,thisMany,resetMe, metaProductArray);
	}

	/* clear current selectElement opitonsArray then re-build from sizeArray */
	if (((resetMe==0 && (thisAttribute=='color'||thisAttribute=='type')) || (resetMe=='all')) && (tempsize.options != null && (tempsize.options.toString().indexOf('Select Color')==-1) && (tempsize.options.toString().indexOf('Select Type')==-1))) {
		/* set 'size' selectElement options array to null */
		for (m=tempsize.options.length;m>0;m--) {

			tempsize.options[m]=null;
		}

		/* rebuild 'size' selectElement options array */
		for (m=0;m<sizeArray.length;m++){
			n=(m+1);
			tempsize.options[n]=new Option(sizeArray[m],sizeArray[m]);
		}
	}

	/* clear current selectElement opitonsArray then re-build from colorArray */
	if (((resetMe==0 && (thisAttribute=='size'||thisAttribute=='type')) || (resetMe=='all')) && (tempcolor.options != null && (tempcolor.options.toString().indexOf('Select Size')==-1) && (tempcolor.options.toString().indexOf('Select Type')==-1))) {

		/* set 'color' selectElement options array to null */
		for (m=tempcolor.options.length;m>0;m--) {
			tempcolor.options[m]=null;
		}

		/* rebuild 'color' selectElement options array */
		for (m=0;m<colorArray.length;m++){
			n=(m+1);
			tempcolor.options[n]=new Option(colorArray[m],colorArray[m]);
		}
	}

	/* clear current selectElement opitonsArray then re-build from typeArray */
	if (((resetMe==0 && (thisAttribute=='size'||thisAttribute=='color')) || (resetMe=='all')) && (temptype.options != null && (temptype.options.toString().indexOf('Select Color')==-1) && (temptype.options.toString().indexOf('Select Size')==-1))) {

		/* set 'type' selectElement options array to null */
		for (m=temptype.options.length;m>0;m--) {
			temptype.options[m]=null;
		}
		/* rebuild 'type' selectElement options array */
		for (m=0;m<typeArray.length;m++) {
			n=(m+1);
			temptype.options[n]=new Option(typeArray[m],typeArray[m]);
		}
	}

	/* reset previously selected selectedIndex */
	if (resetMe==0 && (thisAttribute=='color' || thisAttribute=='type')) {
		var n=0;
		for (i=0;i<sizeArray.length;i++) {
			if (sizeArray[i]==selectedsize) {
				n=i+1;
			}
		}
		tempsize.selectedIndex=n;
	}

	if (resetMe==0 && (thisAttribute=='size' || thisAttribute=='type')) {
		var n=0;
		for (i=0;i<colorArray.length;i++) {
			if (colorArray[i]==selectedcolor) {
				n=i+1;
			}
		}
		tempcolor.selectedIndex=n;
	}
	if ((temptype!=null)&&(resetMe==0 && (thisAttribute=='size' || thisAttribute=='color'))) {
		var n=0;
		for (i=0;i<typeArray.length;i++) {
			if (typeArray[i]==selectedtype) {
				n=i+1;
			}
		}
		temptype.selectedIndex=n;
	}
	if (resetMe=='all') {
		if (tempsize.options !=null){
			tempsize.selectedIndex=0;
		}
		if (tempcolor.options !=null){
			tempcolor.selectedIndex=0;
		}
		if (temptype.options !=null) {
			temptype.selectedIndex=0;
		}
		if (tempquantity.options !='1') {
			tempquantity.selectedIndex=0;
		}
	}
	return;
}

		

/* JC:28.4  CODE TO CONDITIONALLY DISPLAY THE AVAILABILITY MESSAGE. */

function LineItem(obj) {
	/* JC:28.4 THIS IS THE MAIN CLASS USED TO DISPLAY PRODUCT AVAILABILITY ON THE PDP PAGES.
	           IT HOLDS PROPERTIES AND METHODS PERTAINING TO THE CURRENTLY SELECTED PRODUCT:
	           CURRENT DROPDOWN SELECTIONS, QUANTITY, AVAILABILITY MESSAGE, ETC.
	*/
	var DEBUG=0;
	var IDString="status";
	var regex=/(^[a-zA-Z]+)([0-9]+)/;  /* Break up the dropdown name to figure out what the product ID/type is... */
	var objAttributes=regex.exec(obj.id);
	this.prodAttrType= (objAttributes != undefined) ? objAttributes[1] : obj.name ;
	this.ID= (objAttributes != undefined) ? objAttributes[2] : "" ;
	//this.prodAttrType=objAttributes[1];
	//this.ID=objAttributes[2];
	//remove grouping substring
	this.IdWithoutGroup = this.ID.substring(1);
	this.productArray=eval("productArray_"+this.ID);
	this.quantityObj=eval("obj.form.Quantity"+this.ID);
	this.quantityIdx=this.quantityObj.selectedIndex;
	this.isQuantityValue=(this.quantityObj.options[this.quantityIdx].value!="")?1:0;
	this.msgContainer=document.getElementById(IDString+this.ID);
	this.displayedMessage=(this.msgContainer.hasChildNodes())?this.msgContainer.firstChild.nodeValue.toString():"";
	this.showSpecificType=0;
	/* GLOBAL VARS TO TELL US WHICH ATTRIBUTES EXIST IN THE LINE ITEM */
	this.currentSelections=new getCurrentSelections(obj,this.ID);
	this.sizes=0;
	this.colors=0;
	this.types=0;
	this.isSizes=0;
	this.isColors=0;
	this.isTypes=0;
	this.isAnyAttributes=1;
	/* look at all items in array, test all items to see if there is more than one attribute dropdown */
	for (i=0;i<this.productArray.length;i++) {
		var p=this.productArray[i];
		if (p.size)  { this.sizes++ ;this.isSizes=1 ;this.isAnyAttributes=1; }
		if (p.color) { this.colors++;this.isColors=1;this.isAnyAttributes=1; }
		if (p.type)  { this.types++ ;this.isTypes=1 ;this.isAnyAttributes=1; }
	}
	/* Current dropdown object */
	this.currentDropdown=new getCurrentDropdown(obj,this.prodAttrType);
	/* Methods */
	this.autoSelectQuantity=function autoSelectQuantity() { this.quantityObj.selectedIndex=1; }; //Method to change quantity to 1 if person starts messing with attributes
	this.getAvailabilityMessage=function getAvailabilityMessage() {
		/*
		NOTE: This code should be improved to use "this.currentDropdown.prodAttrType" OR
		loop through all available types to see if one is applicable BEFORE finally setteling for the generic "style".
		*/		
		var specificType=(this.useSpecificType)?this.currentDropdown.prodAttrType:"style";
		//var msg="Please select your desired "+specificType+".";
		var msg="";
		var prodAttrType;
		for (i=0;i<this.productArray.length;i++) {
			var p=this.productArray[i];
			if (p.size==this.currentSelections.size&&p.color==this.currentSelections.color&&p.type==this.currentSelections.type){
				msg=p.status;
				break;
			}
		}
		return msg;
	};
	this.hideMsgContainer=function hideMsgContainer() {
		this.msgContainer.className='hide';
	};
	this.showMsgContainer=function showMsgContainer() {
		this.msgContainer.className='show';
	};
	this.populateMsgContainer=function populateMsgContainer() {
		/* Make sure the container has a text node */
		this.msgContainer.appendChild(document.createTextNode(""),this.msgContainer.firstChild);
		this.msgContainer.replaceChild(document.createTextNode(this.getAvailabilityMessage()),this.msgContainer.firstChild);
	};
	if(DEBUG) {
		var str="VARS:";
			str+="\nprodAttrType:"+this.prodAttrType;
			str+="\nID:"+this.ID;
			str+="\nproductArray:"+this.productArray;
			str+="\nquantityIdx:"+this.quantityIdx;
			str+="\nisQuantityValue:"+this.isQuantityValue;
			str+="\nmsgContainer:"+this.msgContainer.nodeName;
			str+="\ndisplayedMessage:"+this.displayedMessage;
			str+="\nshowSpecificType:"+this.showSpecificType;
			str+="\nisSizes:"+this.isSizes;
			str+="\nsizes:"+this.sizes;
			str+="\nisColors:"+this.isColors;
			str+="\ncolors:"+this.colors;
			str+="\nisTypes:"+this.isTypes;
			str+="\ntypes:"+this.types;
			str+="\nisAnyAttributes:"+this.isAnyAttributes;
			str+="\ndropdown.prodAttrType:"+this.currentDropdown.prodAttrType;
			str+="\ndropdown.idx:"+this.currentDropdown.idx;
			str+="\ndropdown.selectedValue:"+this.currentDropdown.selectedValue;
			str+="\ndropdown.isSelectedValue:"+this.currentDropdown.isSelectedValue;
			str+="\ndropdown.isOnlyAttrType:"+this.currentDropdown.isOnlyAttrType;
			str+="\ndropdown.isQuantity:"+this.currentDropdown.isQuantity;
		alert(str);
	}
}
function getCurrentSelections(obj,ID){
	/* JC:28.4 THIS IS AN OBJECT USED BY LineItem CLASS.
	           IT HOLDS CURRENT PROPERTIES OF THE LINE ITEM SUCH AS WHICH OPTIONS ARE SELECTED IN WHICH DROPDOWNS.
	           OR IF NONE ARE SELECTED.
	*/
	var sizeObj=eval("obj.form.size"+ID);
	var typeObj=eval("obj.form.type"+ID);
	var colorObj=eval("obj.form.color"+ID);
	/* Handle dropdowns AND hidden inputs */
	if(sizeObj.options) {
		this.size=sizeObj.options[sizeObj.selectedIndex].value;
	}else if(sizeObj.value){
		this.size=sizeObj.value;
	}
	if(colorObj.options) {
		this.color=colorObj.options[colorObj.selectedIndex].value;
	}else if(colorObj.value){
		this.color=colorObj.value;
	}
	if(typeObj.options) {
		this.type=typeObj.options[typeObj.selectedIndex].value;
	}else if(typeObj.value){
		this.type=typeObj.value;
	}
	this.isSizeValue=(this.size&&this.size!="NOSELECTION")?1:0;
	this.isColorValue=(this.color&&this.color!="NOSELECTION")?1:0;
	this.isTypeValue=(this.type&&this.type!="NOSELECTION")?1:0;
	this.isNoneSelected=(this.isSizeValue||this.isColorValue||this.isTypeValue)?0:1;
}

function getCurrentDropdown(obj,prodAttrType) {
	/* JC:28.4 THIS IS AN OBJECT USED BY LineItem CLASS.
	           IT HOLDS PROPERTIES OF THE DROPDOWN THAT WAS JUST CHANGED BY THE USER.
	*/
	var items=obj.length - 1;
	this.prodAttrType=prodAttrType;
	this.idx=obj.selectedIndex;
	this.selectedValue=obj.options[this.idx].value;
	this.isSelectedValue=(this.selectedValue!="NOSELECTION" && this.selectedValue!="")?1:0;
	this.isOnlyAttrType=0;
	this.isQuantity=(prodAttrType=="Quantity")?1:0;
	this.isOnlyAttrType=(items==(this.sizes+this.colors+this.types))?1:0;
}

function updateAvailability(obj) {
	/* JC:28.4 THIS IS THE MAIN FUNCTION WHICH IS CALLED FROM THE onChange ATTRIBUTE OF THE DROPDOWN.
	           SHOULD BE CALLED LIKE: onchange="updateAvailability(this)"
	           INSIDE THIS FUNCTION WE INITIALIZE THE LINE ITEM OBJECT AND THEN DETERMIN THE DISPLAY LOGIC
	           FOR THE PRODUCT AVAILABILITY MESSAGE.
	*/
	var lineItem = new LineItem(obj);
	if (lineItem.msgContainer) {
		/* Initialize the message container with a message */
		if ( ! lineItem.displayedMessage) {
			lineItem.populateMsgContainer();
		}
		/* IS a quantity dropdown. */
		if (lineItem.currentDropdown.isQuantity) {
			if (lineItem.isQuantityValue) {
				lineItem.showMsgContainer();
			}else{
				lineItem.hideMsgContainer();
			}
		/* NOT a quantity dropdown. */
		}else if (lineItem.currentDropdown.isSelectedValue) {
			/* If user changes an option, auto select quantity of 1 
			if ( ! lineItem.isQuantityValue) {
				lineItem.autoSelectQuantity();
			}  */
			lineItem.populateMsgContainer();
			lineItem.showMsgContainer();
		/* NON-Quantity dropdown that was set to "NOSELECTION" */
		}else if ( ! lineItem.currentDropdown.isSelectedValue) {
			/* use lineItem.currentSelections.isNoneSelected to determin Nothing at all is selected */
			lineItem.useSpecificType=1;
			lineItem.populateMsgContainer();
		}else{
			alert("UNKNOWN ERROR");
		}
	}
}



