/*!-- Copyright (c) 1999-2005 UniteU Technologies Inc.  All rights reserved.   //-->
* HICKEY STYLE 
* DESCR: added s_id=2 & width and height size to Draw Related, editor:cls, date:26.OCT.2007  
//<% version = " 4.91dg 05/17/05 " %>
**************************************************************/

// WJS - This is a duplicate of hickeystyle_product_include


function draw_related(type,display,message){
	var prodlink="";
	var typematch=0;
	//page_option_4: 0=horizontal 1=vertical
	if(typeof type=="undefined"){
	type="0";
	}
	if (typeof display=="undefined"){
	display=page_option_4;
	}
	if (typeof message=="undefined"){
	message="";
	}
	if (nRelated>0){
		for (atn=0;atn< nRelated ;atn++){
			if (Related_prod[atn].crosstype==type){
				typematch=1;
			}
		}
	}
	if (typematch==1) {
	document.write(message);
	}
	
	if (nRelated>0){
		document.write("<Table border=0 cellpadding=2 cellspacing=2>");
		if (display!=1){document.write("<TR>");}
		for (atn=0;atn< nRelated ;atn++){
			if (Related_prod[atn].crosstype==type){
					prodlink="<A HREF=\"" + Related_prod[atn].link + "s_id=2\">";
			
					if (display==1){
						document.write("<TR class=relProduct><TD class=relProduct WIDTH=\"100%\" VALIGN=TOP ALIGN=CENTER>");
					}else{
						document.write("<TD class=relProduct VALIGN=TOP ALIGN=CENTER>");
					}
					//product image & link
           
					if ((Related_prod[atn].image != "") && (rel_image_enable == "PRODUCT")){
						document.write(prodlink);
						//if (rel_image_width > 0){
                       //          alert("2 "+ rel_image_width);
							//document.write("<img ALIGN=CENTER WIDTH =\"" + rel_image_width + "\" BORDER=0  VSPACE=0  HSPACE=0  ALT=\"" + Related_prod[atn].name + "\" SRC=\"assets/product_images/" + Related_prod[atn].image + "\"><\/A>");
						//}else{
                                
							document.write("<img ALIGN=CENTER width=120 height=120 BORDER=0  VSPACE=0  HSPACE=0  ALT=\"" + Related_prod[atn].name + "\" SRC=\"assets/product_images/"+ Related_prod[atn].image +"\"><\/A>");
						//}
						if (display==1){
							document.write("<\/TD><\/TR><TR class=relProduct><TD class=relProduct WIDTH=100% VALIGN=TOP ALIGN=CENTER>");
						}else{
							document.write("<BR>");
						}
					}
			
					//product name link
					document.write(prodlink + Related_prod[atn].name + "<\/A>");
			
					//product price (if enabled)
					if ((page_option_5==1)&&(Related_prod[atn].list_price!="")){
						if (Related_prod[atn].on_sale!="False" && Related_prod[atn].sale_price!=""){
							document.write("<font class=ListPricewSaleR>" + rel_list_pricelab + Related_prod[atn].list_price + " <\/font>");
							document.write("<font class=SalePriceR>" + rel_sale_pricelab + Related_prod[atn].sale_price + " <\/B><\/font>");
						}else{
							document.write("<font class=ListPricewoSaleR>" + rel_our_pricelab + Related_prod[atn].list_price + " <\/font>");
						}
					}
					document.write("<\/TD>");
					if (display==1){document.write("<\/TR>");}
				}	
		}
		if (display!=1){document.write("<\/TR>");}
		document.write("<\/Table>");
	}
}//end function draw_related


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 options:</td></tr>");
	//for (outer=0;outer<attr_values.length;outer++)
	for (outer=attr_values.length-1;outer>-1;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];
			  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];
			  document.write('<option value=\''+dropdownattr1+'\'>'+dropdownattr1+'</option>');
			}
			
			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=sku 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++)
	  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 = 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++)
		  for(k=attr_values.length;k>i;k--)
          {
            //check each dropdown up to one before nextdd
            tempattr = eval("product_var[atn].attr_value"+k);
            tempdd = eval("dd"+k).value;
            if (tempattr.toLowerCase != tempdd.toLowerCase)
            { 
              //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 && parseInt(product_var[atn].avail)>0)
	      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 (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++){
			if (sortarray[s] == selection) { sel_flag = true; }else{ sel_flag = false; }
			tempopt = new Option(sortarray[s],sortarray[s],sel_flag);
			nextdd.options[s+1] = tempopt;
		}
	}
//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++)
				for (drop=attr_values.length;drop>0;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.sku.value=selectedsku;
					
					//alert(document.form1.sku0.value);
					//document.form1.submit();
					return true;
				}
				eachsku=eachsku+1;
			}//end while
		}//end if
		else if(product_var.length==1){
			document.form1.sku.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++)
		for (drop=attr_values.length;drop>0;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;";
}