/*****************************************************************
Product Price Tag Functions

BY: UniteU
CREATED: 01-01-09
DESC: Writes price tag details for product page

UPDATED ON: 11-19-09
UPDATED BY: TMG - Dick's Sporting Goods
UPDATE: Comments and converting formating to div based layout (new layout)
*****************************************************************/

/***********************************************************************
FUNCTION NAME: draw_option_wrapper(str:htmlArea=open):void
Used to centralize html for wrapper div html
***********************************************************************/
function draw_option_wrapper(htmlArea) {
 if (htmlArea == "open") {
 	document.write('<div id="optionSelection">');
 } else {
 	document.write('</div>');
 }
}
/***********************************************************************
FUNCTION NAME: draw_select()

Draws Sku Options in Price Tag
Uses Sku Arrays
Porder Codes:
c*=clubs s*=shoes d*=gift cards b*=bags a*=apparel w*=womens apparel g*=gloves u*=clubs r*=apparel
***********************************************************************/
function draw_select() {
	if (page_option_10==1 && product_var.length >= 1) {        
		document.write("<input type='hidden' id='skuType' value='hidden'>");
		
		if(porder_code.length>0) {
    	var pc=porder_code.charAt(0);
			pc=pc.toLowerCase();
			//sort
			doTheSorting(product_var);
			if (pc=="c"){
				draw_sku_option_rev();
			}
			else { 
				if(!hasDupLabel()) {
					draw_sku_option();
				}
				else {
					document.write("draw sku option combo");
					draw_sku_option_combo();
				}
			}		
    } 
    else {
    	draw_sku_option();
    }
  }
}
/***********************************************************************
FUNCTION NAME: draw_sku_option()
JMS: if there is only 1 attribute, use draw_sku_option_combo() instead of this code so that messaging works properly
***********************************************************************/
function draw_sku_option() {
	getAttributes();
	
	
	if(product_attributes.length==1) {
		draw_sku_option_combo();
	}
	else {
		var dropdownattr1 = "";
		var attrname = "";
		var dropdown_label="";
		document.write("<input type=\"hidden\" id=\"sku\" name=\"sku\" value=\"\">");	
		
		for (outer=0;outer<product_attributes.length;outer++) {		
			attrname = "attr" + (outer+1);		
			if (attr_label[outer]=="Attribute"){dropdown_label="Color";}
			else {dropdown_label=attr_label[outer];}		
			
			document.write('<div class="ddDiv" id="ddDivId'+outer+'">');
			document.write("<select name="+attrname+" id="+attrname+" onchange=\"get_values("+(outer+1)+");findprice();\" class=\"ProductAttr\">");
			document.write('<option value="" selected>'+dropdown_label+"</option>");
			doTheSorting1(product_attributes[outer],outer+1);
			for(innerarr=0;innerarr<product_attributes[outer].length;innerarr++) {
			  dropdownattr1=product_attributes[outer][innerarr];
			  document.write('<option value="'+escape(dropdownattr1)+'">'+dropdownattr1+'</option>');
			}
			
			document.write("</select></div>");			
		} //end for
		
		//if there are no product attributes
		if(product_attributes.length<=0 && product_var[0].avail<=0) {
			document.write("<p class='availmessage'>"+product_var[0].avail_msg+"</p>");
		}
	} //end if only 1 attribute
}
/***********************************************************************
FUNCTION NAME: draw_sku_option_rev()

JMS: if there is only 1 attribute, use draw_sku_option_combo() instead of this code so that messaging works properly
***********************************************************************/
function draw_sku_option_rev() {
	getAttributes();
	
	if(product_attributes.length==1) {draw_sku_option_combo();}
	else {
		var dropdownattr1 = "";
		var attrname = "";
		var dropdown_label="";
		document.write("<input type=\"hidden\" id=\"sku\" name=\"sku\" value=\"\">");	
		
		for (outer=product_attributes.length-1;outer>-1;outer--) {		
			attrname = "attr" + (outer+1);		
			if (attr_label[outer]=="Attribute") {dropdown_label="Color";}
			else {dropdown_label=attr_label[outer];}					
			document.write('<div class="ddDiv" id="ddDivId'+outer+'">');
			document.write("<select name="+attrname+" id="+attrname+" onchange=\"get_values_rev("+(outer+1)+");findprice();\" class=\"ProductAttr\">");
			document.write('<option value="" selected>'+dropdown_label+"</option>");
			doTheSorting1(product_attributes[outer],outer+1);
			for(innerarr=0;innerarr<product_attributes[outer].length;innerarr++) {
			  dropdownattr1=product_attributes[outer][innerarr];
			  document.write('<option value="'+escape(dropdownattr1)+'">'+dropdownattr1+'</option>');
			}
			document.write("</select></div>");			
		}
		
		if(product_attributes.length<=0 && product_var[0].avail<=0) {
			document.write("<p class='availmessage'>"+product_var[0].avail_msg+"</p>");
		}
	}	
}
/***********************************************************************
FUNCTION NAME: draw_select_virt()
Used if !usingMono && isGiftCard
Uses SKU arrays
Placed on 
***********************************************************************/
function draw_select_virt() {
	if (page_option_10==1 && product_var.length >= 1) {       
		product_var.sort(gg_card_sort);
		document.write("<input type='hidden' id='skuType' value='select'>");
		draw_sku_option_GC();		
  }
}

/***********************************************************************
FUNCTION NAME: draw_sku_option_GC()
draw sku option override - for gift certificates
***********************************************************************/
function draw_sku_option_GC() {
	document.write("<div class=\"ddDiv\" id=\"ddDivId0\">");
	document.write("<select class=\"ProductAttr\" name=\"sku\" id=\"sku\" onchange=\"findprice();\">");
	labelstr="Amount";
	writeoption(labelstr,labelstr, sel_values[0]);
	for (atn=0;atn< product_var.length ;atn++) {
		//if sku is out of stock use the sku level OOSMessage
		ModOOSMessage(product_var[atn]);
		writeoption(product_var[atn].sku,CreateOptionString(product_var[atn]),sel_values[0]);
	}
	document.write("</select></div>");
}

/***********************************************************************
FUNCTION NAME: draw_sku_option_virt()

Details
***********************************************************************/
function draw_sku_option_virt() {
  if (product_var.length >= 1) {
		document.write("<div class=\"ddDiv\" id=\"ddDivId0\">");
  	document.write("<select class=\"ProductAttr\" name=\"sku\" id=\"sku\" >");
		labelstr="Choose One";
		writeoption("Choose One",labelstr, sel_values[0]);
		for (atn=0;atn< product_var.length ;atn++) {
		  writeoption(product_var[atn].sku,"$"+OKStrOfPenny(product_var[atn].price_list),sel_values[0]);
		}
	  document.write("</select></div>");
  }
}
/***********************************************************************
FUNCTION NAME: draw_sku_option_combo()

draw sku option override - for products having multiple of same labeled attributes
***********************************************************************/
function draw_sku_option_combo() {
	document.write('<div class="ddDiv" id="ddDivId0">');
	
 	//change skuType value just incase we came here from a different draw_sku_option function - like if we only have one attribute
 	document.getElementById("skuType").value = 'select';
	document.write("<select name='sku' id='sku' onchange=\"findprice(); callChangeUnavailMessage(this);\">");
	
	labelstr=attr_label[0];
	sortarrayUnavail = new Array();
	sortarrayBack = new Array();
	
	writeoption(attr_label[0],labelstr, sel_values[0]);
	for (atn=0;atn< product_var.length ;atn++) {
		//if sku is out of stock use the sku level OOSMessage
		ModOOSMessage(product_var[atn]);
		
		//build attribute display
		var atOut = "";
		if(product_var[atn].attr_value1!="") atOut +=product_var[atn].attr_value1;
		if(product_var[atn].attr_value2!="") {if(atOut !=""){atOut +=" - ";} atOut +=product_var[atn].attr_value2;}
		if(product_var[atn].attr_value3!="") {if(atOut !=""){atOut +=" - ";} atOut +=product_var[atn].attr_value3;}
		if(product_var[atn].attr_value4!="") {if(atOut !=""){atOut +=" - ";} atOut +=product_var[atn].attr_value4;}
		if(product_var[atn].attr_value5!="") {if(atOut !=""){atOut +=" - ";} atOut +=product_var[atn].attr_value5;}

		var theClass = "";
		if(product_var[atn].avail==0) {
			sortarrayUnavail.push(new anOption(product_var[atn].sku,atn));
			theClass = "unavailOption";
		}
		if(product_var[atn].avail<0 || hasLimit(product_var[atn].avail_msg) || (product_var[atn].avail>0 && product_var[atn].avail_msg != "")) {
			sortarrayBack.push(new anOption(product_var[atn].sku,atn));
			theClass = "backOption";
		}
		writeoption_2(product_var[atn].sku,atOut,theClass,sel_values[0]);		
	}
	document.write("</select></div>");
	
}
/***********************************************************************
FUNCTION NAME: writeoption_2(aoption,adisplay,aclass,aselect)

writeoption_2 is same as writeoption but adds ability to specify class of option
***********************************************************************/
function writeoption_2(aoption,adisplay,aclass,aselect){
	var aselected = "";
	if (aselect==aoption){aselected=" SELECTED";}else{aselected="";}
	/*JMS added the \ before the single quote to stop JS error in IE*/
	aoption=String(aoption).replace(/\'/g,"&#39;"); 
	document.write("<option value='"+aoption+"'"+ aselected +" class='"+aclass+"'>"+ adisplay + "</option>");
} 
/***********************************************************************
FUNCTION NAME: callChangeUnavailMessage

used by draw_sku_option_combo to call the change unavail message function and possibly display a message to user
***********************************************************************/
function callChangeUnavailMessage(theSelect) {
	if(theSelect.options[theSelect.selectedIndex].className=="unavailOption") {
		changeUnavailMessage('u',theSelect.options[theSelect.selectedIndex].value);
	}
	else if(theSelect.options[theSelect.selectedIndex].className=="backOption") {			
		changeUnavailMessage('b',theSelect.options[theSelect.selectedIndex].value);
	}
	else {
		changeUnavailMessage('a');
	}
}

/***********************************************************************
FUNCTION NAME: hasLimit(msg)

Function to check to see if there is a qty limit for this product
***********************************************************************/
function hasLimit(msg) {
	//if the msg contains the word limit...
	if(msg.search(/limit/ig)!=-1) return true;
	return false;
}

/***********************************************************************
FUNCTION NAME: hasDupLabel()

Determines if there should be a combined dropdown
***********************************************************************/
function hasDupLabel() {
	var theLabels = "";
	
	//if the first label has text that is found in the second label, then we have a dup
		//example, first label is color, second is color1 - duplicate
	if(attr_label[1].toLowerCase().search(attr_label[0].toLowerCase())!=-1 && (attr_label[0]!="" && attr_label[1]!="")) return true;
	
	//if any of the attribute lables that are not empty are the same...
	/*for(al=0; al<attr_label.length; al++)
	{
		if(attr_label[al]!="" && theLabels.indexOf(","+attr_label[al]+",")==-1)
			theLabels+=","+attr_label[al]+",";
		else if(attr_label[al]!="" && theLabels.indexOf(","+attr_label[al]+",")!=-1)
			return true;
	}*/
	return false;
}


////////** sku option functions **////////

/***********************************************************************
FUNCTION NAME: getAttributes

used to retreave 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
		//if sku is out of stock use the sku level OOSMessage
		ModOOSMessage(product_var[pv]);
				
		//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 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

/***********************************************************************
FUNCTION NAME: contains(a,e)

Returns true if 'e' is contained in the array 'a'
@author Johan KÃƒÆ’Ã‚Â¤nngÃƒÆ’Ã‚Â¥rd, http://dev.kanngard.net
***********************************************************************/
function contains(a, e) {
	if(a!=null)
		for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}

/***********************************************************************
VARS
***********************************************************************/
var sortarrayUnavail = new Array();
var sortarrayBack = new Array();

/***********************************************************************
FUNCTION NAME: get_values(ddnum)

get_values receives the attr number of the dropdown that has changed.
***********************************************************************/
//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(product_attributes.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');
	
	  //get current dropdown
	  var changedDrop = document.getElementById("attr"+ddnum);
	  
	  //if current dropdown selection is grayed out and there are NO MORE DROPS TO SEARCH THROUGH...
	  if(changedDrop.options[changedDrop.selectedIndex].className!="" && (ddnum+1)>product_attributes.length) 
	  {
	 	 if (changedDrop.options[changedDrop.selectedIndex].className=="unavailOption")
			 changeUnavailMessage('u',changedDrop.options[changedDrop.selectedIndex].value);
		 else if(changedDrop.options[changedDrop.selectedIndex].className=="backOption")
		 	changeUnavailMessage('b',changedDrop.options[changedDrop.selectedIndex].value);
	  }
	  else
	  {
	  	changeUnavailMessage('a');
	  }
	  
	  
	  for (i=ddnum+1;i<=product_attributes.length;i++)
	  {		
		//clear out the next dropdown
		nextdd = eval("dd"+i);
		
		//store the selected value so that it will remain selected if available
		selection = unescape(nextdd.value);
		
		//clear out select
		nextdd.options.length = 0;
	   
		//put a label option in select if not on msg			
		//write options here and clear array
		//change attribute to color
		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;
	
		
		var sortarray= new Array();					
		sortarrayUnavail = new Array();
		sortarrayBack = new Array();
		
		//loop thru product array
		for (atn=0;atn< product_var.length;atn++)
		{
		  attr_flag = true;

		  //loop through attribute values to see if there is NOT a match
		  for (k=1;k<=i-1;k++)
		  {  
			  //check each dropdown up to one before nextdd
			  tempattr = (product_var[atn])["attr_value"+k];
			  tempdd = unescape(eval("dd"+k).value);
			  
			  //alert("tempattr =" + tempattr + " && tempdd =" + tempdd);
			  
			  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 = (product_var[atn])["attr_value"+i];
			  if (optstr.indexOf(";" + thisattr + ";") == -1/* && parseInt(product_var[atn].avail)>0*/)
			  {					
				//add to array here and sort instead of writing option
				if(parseInt(product_var[atn].avail)>0)
				{
					if(product_var[atn].avail_msg == "")
					{
						optstr += ";" + thisattr + ";";						
						sortarray.push(new anOption(thisattr,'a'));
					} else {
						//add to backorder array so that messaging shows on select
						sortarrayBack.push(new anOption(thisattr,atn));
					}
				} 
				else if(parseInt(product_var[atn].avail)<0 || hasLimit(product_var[atn].avail_msg))
				{
					sortarrayBack.push(new anOption(thisattr,atn));
				}
				else if(parseInt(product_var[atn].avail)==0)
				{						
					sortarrayUnavail.push(new anOption(thisattr,atn));
				}
				ctr++;
			  }
		  }
		}//end for product_var			
		
				
		//combine avail and back options
		for (su=0;su<sortarrayBack.length;su++){
			//if optstr does not contain the back option...add new option to sortarray
			if(optstr.indexOf(";" + sortarrayBack[su].value + ";") == -1){
				optstr += ";" + sortarrayBack[su].value + ";";						
				sortarray.push(new anOption(sortarrayBack[su].value,'b'));
			}					
		}
		
		//combine avail and unavail options
		for (su=0;su<sortarrayUnavail.length;su++){
			//if optstr does not contain the unavail option...add new option to sortarray
			if(optstr.indexOf(";" + sortarrayUnavail[su].value + ";") == -1){
				optstr += ";" + sortarrayUnavail[su].value + ";";						
				sortarray.push(new anOption(sortarrayUnavail[su].value,'u'));
			}
		}
		
		//sort the array of options
		//sortarray.sort(smart_sort);
		doTheSorting2(sortarray,i)
		
		//put a label option in select if not on msg		
		for (var s=0;s<sortarray.length;s++){
			if (sortarray[s].value == selection) { sel_flag = true; }else{ sel_flag = false; }															
			tempopt = new Option(sortarray[s].value,escape(sortarray[s].value),sel_flag);					
			nextdd.options[s+1] = tempopt;			
			if(sortarray[s].avail=='u'){nextdd.options[s+1].className = "unavailOption";}
			else if(sortarray[s].avail=='b'){nextdd.options[s+1].className = "backOption";}
			else {nextdd.options[s+1].className = "";}
		}	
				
		//if next selection is grayed out...
		if(nextdd.options[nextdd.selectedIndex].className=="unavailOption" || changedDrop.options[changedDrop.selectedIndex].className=="unavailOption")
		{
			var value="";
			if(nextdd.options[nextdd.selectedIndex].className=="unavailOption")
				value = nextdd.options[nextdd.selectedIndex].value;
			else if (changedDrop.options[changedDrop.selectedIndex].className=="unavailOption")
				value = changedDrop.options[changedDrop.selectedIndex].value;
				
			changeUnavailMessage('u',value);
		}
		else if(nextdd.options[nextdd.selectedIndex].className=="backOption" || changedDrop.options[changedDrop.selectedIndex].className=="backOption")
		{
			var value="";
			if(nextdd.options[nextdd.selectedIndex].className=="backOption")
				value = nextdd.options[nextdd.selectedIndex].value;
			else if (changedDrop.options[changedDrop.selectedIndex].className=="backOption")
				value = changedDrop.options[changedDrop.selectedIndex].value;
				
			changeUnavailMessage('b',value);
		}
		else
		{
			changeUnavailMessage('a');
		}		
		
		optstr = "";			
		ctr = 0;
		selection = "";			
		
	  }//end for
	  
      break;
    case 1: //1 dropdown - do nothing here
    case 0: //no dropdowns - do nothing here
    default:

      break;
  }
}//end get_values
/***********************************************************************
FUNCTION NAME: function get_values_rev(ddnum)

receives the attr number of the dropdown that has changed.
***********************************************************************/
//get_values receives the attr number of the dropdown that has changed.
function get_values_rev(ddnum)
{
  var thisattr,nextdd,tempattr,tempdd;
  var ctr = 0;
  var optstr = "";
  var tempopt;
  var attr_flag = true;
  var selection = "";
  var sel_flag = false;


  switch(product_attributes.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');
	
	  //get current dropdown
	  var changedDrop = document.getElementById("attr"+ddnum);
	  
	  //if current dropdown selection is grayed out and there are NO MORE DROPS TO SEARCH THROUGH...
	  if(changedDrop.options[changedDrop.selectedIndex].className!="" && (ddnum-1)==0) 
	  {
	 	 if (changedDrop.options[changedDrop.selectedIndex].className=="unavailOption")
			 changeUnavailMessage('u',changedDrop.options[changedDrop.selectedIndex].value);
		 else if(changedDrop.options[changedDrop.selectedIndex].className=="backOption")
		 	changeUnavailMessage('b',changedDrop.options[changedDrop.selectedIndex].value);
	  }
	  else
	  {
	  	changeUnavailMessage('a');
	  }
	  
	  for (i=ddnum-1;i>0;i--)
	  {		
		//clear out the next dropdown
		nextdd = eval("dd"+i);
		
		//store the selected value so that it will remain selected if available
		selection = unescape(nextdd.value);
		
		//clear out select
		nextdd.options.length = 0;
	   
		//put a label option in select if not on msg			
		//write options here and clear array
		//change attribute to color
		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;
		
		var sortarray= new Array();		
		sortarrayUnavail = new Array();
		sortarrayBack = new Array();
		
		//loop thru product array
		for (atn=0;atn< product_var.length;atn++)
		{
		  attr_flag = true;

		  //loop through attribute values to see if there is NOT a match
		  for(k=product_attributes.length;k>i;k--)
		  {  
			  //check each dropdown up to one before nextdd
			  tempattr = (product_var[atn])["attr_value"+k];
			  tempdd = unescape(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 = (product_var[atn])["attr_value"+i];
			  if (optstr.indexOf(";" + thisattr + ";") == -1/* && parseInt(product_var[atn].avail)>0*/)
			  {					
				//add to array here and sort instead of writing option
				if(parseInt(product_var[atn].avail)>0)
				{
					if(product_var[atn].avail_msg == "")
					{
						optstr += ";" + thisattr + ";";						
						sortarray.push(new anOption(thisattr,'a'));
					} else {
						//add to backorder array so that messaging shows on select
						sortarrayBack.push(new anOption(thisattr,atn));
					}
				} 
				else if(parseInt(product_var[atn].avail)<0 || hasLimit(product_var[atn].avail_msg))
				{				
					sortarrayBack.push(new anOption(thisattr,atn));
				}
				else if(parseInt(product_var[atn].avail)==0)
				{						
					sortarrayUnavail.push(new anOption(thisattr,atn));
				}	
				ctr++;
			  }
		  }
		}//end for product_var			
		
		
		//combine avail and back options
		for (su=0;su<sortarrayBack.length;su++){
			//if optstr does not contain the back option...add new option to sortarray
			if(optstr.indexOf(";" + sortarrayBack[su].value + ";") == -1){
				optstr += ";" + sortarrayBack[su].value + ";";						
				sortarray.push(new anOption(sortarrayBack[su].value,'b'));
			}					
		}
		
		
		//combine avail and unavail options
		for (su=0;su<sortarrayUnavail.length;su++){
			//if optstr does not contain the unavail option...add new option to sortarray
			if(optstr.indexOf(";" + sortarrayUnavail[su].value + ";") == -1){
				optstr += ";" + sortarrayUnavail[su].value + ";";						
				sortarray.push(new anOption(sortarrayUnavail[su].value,'u'));
			}					
		}		
		
		
		//sort the array of options
		//sortarray.sort(smart_sort);
		doTheSorting2(sortarray,i)
		
		//put a label option in select if not on msg		
		for (s=0;s<sortarray.length;s++){
			if (sortarray[s].value == selection) { sel_flag = true; }else{ sel_flag = false; }															
			tempopt = new Option(sortarray[s].value,escape(sortarray[s].value),sel_flag);					
			nextdd.options[s+1] = tempopt;
			if(sortarray[s].avail=='u'){nextdd.options[s+1].className = "unavailOption";}
			else if(sortarray[s].avail=='b'){nextdd.options[s+1].className = "backOption";}
			else {nextdd.options[s+1].className = "";}
		}	
				
		//if next selection is grayed out...
		if(nextdd.options[nextdd.selectedIndex].className=="unavailOption" || changedDrop.options[changedDrop.selectedIndex].className=="unavailOption")
		{
			var value="";
			if(nextdd.options[nextdd.selectedIndex].className=="unavailOption")
				value = nextdd.options[nextdd.selectedIndex].value;
			else if (changedDrop.options[changedDrop.selectedIndex].className=="unavailOption")
				value = changedDrop.options[changedDrop.selectedIndex].value;
				
			changeUnavailMessage('u',value);
		}
		else if(nextdd.options[nextdd.selectedIndex].className=="backOption" || changedDrop.options[changedDrop.selectedIndex].className=="backOption")
		{
			var value="";
			if(nextdd.options[nextdd.selectedIndex].className=="backOption")
				value = nextdd.options[nextdd.selectedIndex].value;
			else if (changedDrop.options[changedDrop.selectedIndex].className=="backOption")
				value = changedDrop.options[changedDrop.selectedIndex].value;
				
			changeUnavailMessage('b',value);
		}
		else
		{
			changeUnavailMessage('a');
		}
			
		optstr = "";			
		ctr = 0;
		selection = "";
	  }//end for
		  
      break;
    case 1: //1 dropdown - do nothing here
    case 0: //no dropdowns - do nothing here
    default:

      break;
  }
}//end get_values


/***********************************************************************
FUNCTION NAME: anOption(value,avail)

Details
***********************************************************************/
function anOption(value,avail){
	this.value = value;
	this.avail = avail;
}
/***********************************************************************
FUNCTION NAME: changeUnavailMessage(str:type,str:message)

Shows the unavailable message
paramiters:
1) type = u (unavailable) or b (backorder) or a (available) -- if a, hide pop, else show
2) message = the value of the bad option
***********************************************************************/
function changeUnavailMessage(type,message){	
	if(typeof message == "undefined"){message = ""};
	var outputMessage = "";
	var outputTitle = "";
	
	//if message is not empty...it is the unavail options's value
	if(message!="") {
		message = unescape(message);
		if(type=='u') {
			//find the unavail option's message
			for(uo=0; uo<sortarrayUnavail.length; uo++) {
				if(sortarrayUnavail[uo].value == message) {
					outputMessage = product_var[sortarrayUnavail[uo].avail].avail_msg;		
				}
			}		
		} else if(type=='b') {
			//find the unavail option's message
			for(uo=0; uo<sortarrayBack.length; uo++) {				
				if(sortarrayBack[uo].value == message) {					
					outputMessage = product_var[sortarrayBack[uo].avail].avail_msg;
				}
			}		
		}
	}
	
	//change title
	if(type=='u') outputTitle = "<p>The selection you have made is not available:</p>";
	else if(type=='b') outputTitle = "";
	
	if(type=='a' || (type=='b' && outputMessage=="")){
		if(document.getElementById('subpop').style.visibility != 'hidden') {
			document.getElementById('subpop').style.visibility = 'hidden';
		}
	}
	else{		
		var sub_pop_fill = "";
		sub_pop_fill += "<table width=200 cellpadding=0 cellspacing=2 border=0 class=\"outermostpop\">";
		sub_pop_fill += "    <tr><td><table cellpadding=2 width=200 cellspacing=2 border=0 class=\"outerpop\">";
		sub_pop_fill += "        <tr><td align=center><table cellpadding=10 width=\"200\" cellspacing=0 border=0 class=\"innerbg\">";
		sub_pop_fill += "            <tr><td align=center><span class=revhead>Important!</span></td></tr>";
		sub_pop_fill += "            <tr><td>";
		sub_pop_fill += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">";
		sub_pop_fill += "<tr><td id=\"info_box_no_bottom\"><table cellpadding=\"3\" cellspacing=\"1\" border=\"0\" width=\"100%\">";
		sub_pop_fill += "<tr bgcolor=\"#FFFFFF\"><TD class=revbold align=center>";
		sub_pop_fill += outputTitle;
		sub_pop_fill += outputMessage;
		sub_pop_fill += "</td></tr>";
		sub_pop_fill += "</table></td></tr></table>";
		sub_pop_fill +='<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';
		sub_pop_fill +='<td valign="top" width="8" align="left"><img src="Assets/images/info_box/info_box_higher_bottom_left2.gif" /></td><td class="info_box_higher_bottom2" align="center">';
		sub_pop_fill += "<a href=\"product.asp\" onClick=\"document.getElementById('subpop').style.visibility = 'hidden';return false;\">";
		sub_pop_fill += "<img src=\"assets/images/continue_shopping.gif\" alt=\"click to return to the catalog\" border=\"0\"></a>";
		sub_pop_fill += '</td><td valign="top" width="7" align="right"><img src="Assets/images/info_box/info_box_higher_bottom_right2.gif" />';
		sub_pop_fill += "</td></tr></table></td></tr></table>";
		
		sub_pop_fill += "      </td></tr>";
		sub_pop_fill += "  </table></td></tr></table>";
		document.getElementById("subpop").innerHTML = sub_pop_fill;		
		document.getElementById('subpop').style.visibility = 'visible';
	}
		
}
/***********************************************************************
FUNCTION NAME: findsku()

Details
***********************************************************************/
function findsku() {
	//this is for when the sku is NOT a select and is a hidden input
	if (typeof product_attributes!="undefined" && document.getElementById("skuType").value!="select"){		
		if(product_var.length>1){

			var matchsku=false;
			var eachsku=0;
			var selectedsku;
			while (matchsku==false && eachsku<product_var.length) {
				var matching=true;
				for (drop=1;drop<=product_attributes.length ;drop++) {					
					if ((product_var[eachsku])["attr_value"+drop]!=unescape(document.getElementById('attr'+drop).options[document.getElementById('attr'+drop).selectedIndex].value)){matching=false;}
				}
				if (matching==true) {
					selectedsku=product_var[eachsku].sku;
					matchsku=true;
					document.getElementById("sku").value=selectedsku;
					return true;		
				}
				eachsku=eachsku+1;
			}//end while
		}//end if
		else {
			document.getElementById("sku").value=product_var[0].sku;
			return true;
		}
	}//end if
	
	//this is for when the sku is a select and not a hidden input
	if(document.getElementById("skuType").value=="select"){return true;}
}
/***********************************************************************
FUNCTION NAME: findprice(){

Details
***********************************************************************/
function findprice(){
	var matchsku=false;
	var eachsku=0;
	var selectedskuprice;
	var listprice;
	var avail_message;
		
	//if there are product_attributes and the prod_price id is on page and if showing price && sku field is not a dropdown itself
	if (typeof product_attributes!="undefined" && document.getElementById("prod_price") && product_price != "" && document.getElementById("skuType").value!="select"){	
		while (matchsku==false && eachsku<product_var.length) {
			var matching=true;
			for (drop=1;drop<=product_attributes.length ;drop++) {
				if ((product_var[eachsku])["attr_value"+drop]!=unescape(document.getElementById('attr'+drop).options[document.getElementById('attr'+drop).selectedIndex].value)){matching=false;}
			}
			
			if (matching==true){
				avail_message = product_var[eachsku].avail_msg;
				//if on sale
				if (on_sale=="True"){			
					listprice=OKStrOfPenny(product_var[eachsku].price_list);
					matchsku=true;
					selectedskuprice=OKStrOfPenny(product_var[eachsku].price_sale);
					matchsku=true;				
					document.getElementById("prod_price").innerHTML = "<span class='ListPricewSale'>"+rel_list_pricelab+"$"+listprice+"</span><span class='SalePrice'>"+rel_sale_pricelab+"$"+selectedskuprice+"</span>"; 
				}else{
					selectedskuprice=OKStrOfPenny(product_var[eachsku].price_list);
					matchsku=true;
					document.getElementById("prod_price").innerHTML = "<span class='ListPricewoSale'>"+rel_our_pricelab+"$"+selectedskuprice+"</span>";
				}
				return true;
			}
			eachsku++;
		}
	}// end if
	
	//if prod_price container is available and the product_price ! empty and the sku field is a dropdown...
	if(document.getElementById("prod_price") && product_price != "" && document.getElementById("skuType").value=="select")
	{
		//go through each sku
		for(eachsku=0; eachsku<product_var.length;eachsku++)
		{
			//if sku matches selected sku...
			if(product_var[eachsku].sku == document.getElementById('sku').value)
			{
				//if on sale
				if (on_sale=="True"){			
					//get sale pricing
					listprice=OKStrOfPenny(product_var[eachsku].price_list);					
					selectedskuprice=OKStrOfPenny(product_var[eachsku].price_sale);
					document.getElementById("prod_price").innerHTML = "<span class='ListPricewSale'>"+rel_list_pricelab+"$"+listprice+"</span><span class='SalePrice'>"+rel_sale_pricelab+"$"+selectedskuprice+"</span>"; 
				}else{
					//get regular pricing
					selectedskuprice=OKStrOfPenny(product_var[eachsku].price_list);
					document.getElementById("prod_price").innerHTML = "<span class='ListPricewoSale'>"+rel_our_pricelab+"$"+selectedskuprice+"</span>";
				}
			}
		}
	}
	
}