
function draw_sku_option() {
	var dropdownattr1 = "";
	var attrname = "";
	var dropdown_label="";
	//document.write("<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0>");
	document.write("<TR VALIGN=TOP ><TD class=\"ProductAttrlabel\">Please select the following:</td></tr>");
	for (outer=0;outer<attr_values.length;outer++) {
		attrname = "attr" + (outer+1);
		document.write("<TR VALIGN=TOP ><TD>");
		//alert(outer+" "+attr_values[outer].length)
		if (attr_values[outer].length==1) {		
			if (attr_label[outer]=="Attribute") {
				dropdown_label="Color";
			} else {
				dropdown_label=attr_label[outer];
			}
			document.write("<select name="+attrname+" id="+attrname+" onchange=\"get_values("+(outer+1)+");findprice();\" class=\"ProductAttr\" style=\"display:none;\">");
			for(innerarr=0;innerarr<attr_values[outer].length;innerarr++) {
				dropdownattr1=attr_values[outer][innerarr];
			  	//loop through product_var looking for the attribute, don't display it if it is wholesale only
				for (var pvIter=0;pvIter<product_var.length;pvIter++) {
					if ((product_var[pvIter].attr_value1==dropdownattr1 || product_var[pvIter].attr_value2==dropdownattr1 || product_var[pvIter].attr_value3==dropdownattr1 && product_var[pvIter].attr_value4==dropdownattr1 || product_var[pvIter].attr_value5==dropdownattr1) && (product_var[pvIter].vvendor_code.indexOf("r")!=-1)) {
						document.write('<option value=\''+dropdownattr1+'\'>'+dropdownattr1+'</option>');
					}
				}
			}
			document.write("</select>");
			document.write("<div id=single_option"+(outer+1)+">"+dropdown_label + ': ' + dropdownattr1+"</div>");
		} else {
			document.write("<select name="+attrname+" id="+attrname+" onchange=\"get_values("+(outer+1)+");findprice();\" class=\"ProductAttr\">");
			if (attr_label[outer]=="Attribute") {
				dropdown_label="Color";
			} else {
				dropdown_label=attr_label[outer];
			}
			document.write('<option value="" selected>'+dropdown_label+"</option>");
			attr_values[outer].sort(smart_sort);
			for(innerarr=0;innerarr<attr_values[outer].length;innerarr++) {
			  dropdownattr1=attr_values[outer][innerarr];
			  //loop through product_var looking for the attribute, don't display it if it is wholesale only
				for (var pvIter=0;pvIter<product_var.length;pvIter++) {
					if ((product_var[pvIter].attr_value1==dropdownattr1 || product_var[pvIter].attr_value2==dropdownattr1 || product_var[pvIter].attr_value3==dropdownattr1 && product_var[pvIter].attr_value4==dropdownattr1 || product_var[pvIter].attr_value5==dropdownattr1) && (product_var[pvIter].vvendor_code.indexOf("r")!=-1)) {
			  			document.write('<option value=\''+dropdownattr1+'\'>'+dropdownattr1+'</option>');
						break;
					}
				}
			}
			document.write("</select>");
			document.write("<div id=single_option"+(outer+1)+"></div>");
		}
		document.write("</TD></TR>");
	}
	document.write("<tr><td>Qty: <input type=text name=qty value=1 size=1></TD></TR>");
	//document.write("</table>");
	document.write("<INPUT TYPE=HIDDEN  NAME=sku0 VALUE=''>");
} //end draw_sku_option


//get_values receives the attr number of the dropdown that has changed.
//
function get_values(ddnum) {
  var thisattr,nextdd,tempattr,tempdd;
  var ctr = 0;
  var optstr = "";
  var tempopt;
  var attr_flag = true;
  var selection = "";
  var sel_flag = false;
  switch(attr_values.length) {
    case 5:
      var dd5 = document.getElementById('attr5');
    case 4:
      var dd4 = document.getElementById('attr4');
    case 3:
      var dd3 = document.getElementById('attr3');
    case 2:
    	var dd2 = document.getElementById('attr2');
      	var dd1 = document.getElementById('attr1');
      	for (i=ddnum+1;i<=attr_values.length;i++) {
        	//clear out the next dropdown
        	nextdd = eval("dd"+i);
        	//store the selected value so that it will remain selected if available
			selection = nextdd.value;
        	for (j=nextdd.options.length-1;j>=0;j--) {
          		nextdd.options[j] = null;
        	}
			var sortarray= new Array();
			//~ sortarray[0]=attr_label[i];
        	//loop thru product array
        	for (atn=0;atn< product_var.length;atn++) {
				attr_flag = true;
				for (k=1;k<=i-1;k++) {
					//check each dropdown up to one before nextdd
					tempattr = eval("product_var[atn].attr_value"+k);
					tempdd = eval("dd"+k).value;
					if (tempattr != tempdd) {
				 		//this product doesn't match our selected values
				  		attr_flag = false;
					}
				}
				if (attr_flag == true)  {
					//store the attr if it is new AND if available
					thisattr = eval("product_var[atn].attr_value"+i);
					//jas 5.15.07 - changed the code to see all variants in dropdown, regardless of availability
					//if (optstr.indexOf(";" + thisattr + ";") == -1 && product_var[atn].avail!=0) {
					if (optstr.indexOf(";" + thisattr + ";") == -1 && product_var[atn].vvendor_code.indexOf("r")!=-1) {
					//if (optstr.indexOf(";" + thisattr + ";") == -1) {
						optstr += ";" + thisattr + ";";
						//add to array here and sort instead of writing option
						sortarray[ctr]=thisattr;
						ctr++;
					}
				}
        	}
			//write options here and clear array
			if (sortarray.length==1) {
				nextdd.style.display = "none";
				for (var s=0;s<sortarray.length;s++){		
					if (sortarray[s] == selection) { sel_flag = true; }else{ sel_flag = false; }
					tempopt = new Option(sortarray[s],sortarray[s],sel_flag);
					nextdd.options[s] = tempopt;
					document.getElementById("single_option"+i).innerHTML = attr_label[i-1] + ": " + sortarray[s];
				}
			} else {
				if (attr_label[i-1]=="Attribute") {
					tempopt = new Option("Color","Color",false);
				} else {
					tempopt = new Option(attr_label[i-1],attr_label[i-1],false);
				}
				nextdd.options[0] = tempopt;
				sortarray.sort(smart_sort);
				if (sortarray.length==0) {
					//nextdd.style.display = "none";	
				} else {
					nextdd.style.display = "block";
					document.getElementById("single_option"+i).innerHTML="";
				}
				for (s=0;s<sortarray.length;s++){
					//var a = 0;
					if (sortarray[s] == selection) { sel_flag = true; }else{ sel_flag = false; }
					tempopt = new Option(sortarray[s],sortarray[s],sel_flag);
					for (var pvIter=0;pvIter<product_var.length;pvIter++) {
						if ((sortarray[s]==product_var[pvIter].attr_value2 || sortarray[s]==product_var[pvIter].attr_value3) && product_var[pvIter].vvendor_code.indexOf("r")!=-1) {
							//nextdd.options[a+1] = tempopt;
							nextdd.options[s+1] = tempopt;
							//a += 1;
							//break;
						}
					}
				}
			}
			//sortarray.clear();
        	optstr = "";
       		ctr = 0;
        	selection = "";
      	}
      	break;
    case 1: //1 dropdown - do nothing here
    case 0: //no dropdowns - do nothing here
    default:

      break;
  }
}

function findsku()
{
	if (typeof attr_values!="undefined"){
		if(product_var.length>1){
			//var sku_flds = document.getElementsByName("sku");
			//var qty_flds = document.getElementsByName("qty");
			var matchsku=false;
			var eachsku=0;
			var selectedsku;
			while (matchsku==false && eachsku<product_var.length)
			{
				var matching=true;
				for (drop=1;drop<=attr_values.length ;drop++)
				{
					//document.getElementById('this').value=document.getElementById('dropsku'+drop).options[document.getElementById('dropsku'+drop).selectedIndex].value;
					if (eval("product_var["+eachsku+"].attr_value"+drop).toUpperCase()!=document.getElementById('attr'+drop).options[document.getElementById('attr'+drop).selectedIndex].value.toUpperCase()){matching=false;}
				}
				if (matching==true){selectedsku=eval("product_var["+eachsku+"].sku");matchsku=true;
				//sku_flds.value="selectedsku";
				//qty_flds.value="1";
				//alert(selectedsku);
				document.form1.sku0.value=selectedsku;
				//document.form1.submit();
				return true;
		
				}
				eachsku=eachsku+1;
			}//end while
		}//end if
		else if(product_var.length==1){
			document.form1.sku0.value=product_var[0].sku;
			return true;
		}
	}//end if
}

function findprice(){
	if (typeof attr_values!="undefined"){
		//var sku_flds = document.getElementsByName("sku");
		//var qty_flds = document.getElementsByName("qty");
		var matchsku=false;
		var eachsku=0;
		var selectedskuprice;
		var listprice;
		var avail_message;
		while (matchsku==false && eachsku<product_var.length) {
			var matching=true;
			for (drop=1;drop<=attr_values.length;drop++) {
				//document.getElementById('this').value=document.getElementById('dropsku'+drop).options[document.getElementById('dropsku'+drop).selectedIndex].value;
				if (eval("product_var["+eachsku+"].attr_value"+drop)!=document.getElementById('attr'+drop).options[document.getElementById('attr'+drop).selectedIndex].value){matching=false;}
			}
			if (matching==true){
				avail_message = eval("product_var["+eachsku+"].avail_msg");
				if (eval("product_var["+eachsku+"].price_sale")!="" && eval("product_var["+eachsku+"].price_sale")!="0" && eval("product_var["+eachsku+"].price_sale")<eval("product_var["+eachsku+"].price_list")){
		
					listprice=eval("OKStrOfPenny(product_var["+eachsku+"].price_list)");matchsku=true;
					selectedskuprice=eval("OKStrOfPenny(product_var["+eachsku+"].price_sale)");matchsku=true;
					//document.getElementById("prod_price").innerHTML = "<font class='ListPricewSale'><br>Regular Price:&nbsp;$"+listprice+"</font><font class='SalePrice'><br>ON SALE!&nbsp;$"+selectedskuprice+"</font><br>"+avail_message;
				}else{
					selectedskuprice=eval("OKStrOfPenny(product_var["+eachsku+"].price_list)");matchsku=true;
					//document.getElementById("prod_price").innerHTML = "<font class='ListPricewoSaleP'><br>Selected Item Price&nbsp;$"+selectedskuprice+"</font><br><br>"+avail_message;
				}		
				display_avail(avail_message);
				//sku_flds.value="selectedsku";
				//qty_flds.value="1";
				//document.form1.sku.value=selectedsku;
				//document.form1.submit();
				return true;
			}
			eachsku=eachsku+1;
		}
	}
}

function display_avail(avail_message)
{
	//if the availability message is not empty...
	if(avail_message!="" && avail_message!="&nbsp;")
		document.getElementById("availability_message").innerHTML = "<font class=availability_msg>Availability: " + avail_message + "</font>";
	else
		document.getElementById("availability_message").innerHTML = "&nbsp;";
}



/* JMS NOTE: Add function to get all attributes to replace product_attributes array (structure the same as product_attributes) */

var product_attributes = new Array();

function getAttributes(){
	//see which variables we should fill and create array structure
	var intTemp = 0;
	while (intTemp<5){ //go through all 5 attributes of first sku
		//if((product_var[0])["attr_value"+(intTemp+1)] != "") //cant do this here as there may be empty values in the middle!!!
			product_attributes[intTemp] = new Array();
		intTemp++;
	}
	var curAttr = "";
	//go through product_var
	for(pv=0; pv<product_var.length;pv++) {
		intTemp = 0; //reset intTemp		
		/************ VARIANT VENDOR CODE TEST ************
		the if statement below examines the variant level vendor code for w/wr/r to determine whether to display a sku on the wholesale site or not
		we use this instead of storefront code because some products can appear on both wholesale and retail but their skus (colors) can sometimesbe only one or the other
		************ VARIANT VENDOR CODE TEST ************/
		//if(product_var[pv].vvendor_code=="wr" || product_var[pv].vvendor_code=="r"){		
		if(product_var[pv].vvendor_code.indexOf("wr")!=-1 || product_var[pv].vvendor_code.indexOf("r")!=-1){		
			//if(product_var[pv].avail<'0'){ //if available (comment this if out if you don't want to filter by availability)			
				//go through all 5 attributes
				while (intTemp<5){
					//add each attribute if not empty
					curAttr = (product_var[pv])["attr_value"+(intTemp+1)];
					//if(curAttr != ""){ //can't do this here as there may be empty values in the middle!!
						//add attribute if not already in array
						if(!contains(product_attributes[intTemp],curAttr)) 
							product_attributes[intTemp].push(curAttr);
					//}//end if curAttr != ""
					intTemp++;
				}//end while	
			//}//end if available (comment this if out if you don't want to filter by availability)
		}//end vendor code if
	}//end for
	//go through product_attributes in reverse order and nullify any empty arrays 
	//there array indexes are 0-4 (as there are up to 5 elements in the array)
	//(need to deal with this here as there may be empty values in the middle of the attr_value stuff...)
	for(pa=4; pa>=0; pa--){
		var testValues = "";
		for(x=0; x<product_attributes[pa].length;x++){
			testValues += product_attributes[pa][x];
		}		
		//if all the values in the current element's array are empty, remove this element from array
		if(testValues == "") product_attributes.splice(pa,1);		
	}
}//end getAttributes