// product.asp javascript



var price_string = "";

function draw_related()
{
	if (nRelated>0)
	{
  	document.write(" <table border=0 width=490 cellpadding=0 cellspacing=0><tr>");
    var related_link = "";
  	//begin for loop
		for (atn=0;atn< nRelated ;atn++)
		{
			related_link = "<A HREF=\"product.asp?pf_id=" + Related_prod[atn].pf_id + "&dept_id=" + Related_prod[atn].dept_id + "&\" onClick=return(visitargs('product.asp','pf_id=" + Related_prod[atn].pf_id + "&dept_id=" + Related_prod[atn].dept_id + "&','URL')); class=relProduct>";
  		document.write("<td valign=top>");
  		document.write("<Table border=0 cellpadding=1 cellspacing=0>");
  		if(Related_prod[atn].image !="" && rel_image_enable !="NONE" )
			{
  			document.write("<TR VALIGN=top><TD WIDTH=100% VALIGN=TOP ALIGN=CENTER>");
				document.write(related_link);
				document.write("<img ALIGN=CENTER ");
  			if (rel_image_width !="")
  			{
  				document.write("WIDTH =\"" + rel_image_width + "\" ");
				}else{
				document.write("WIDTH =\"55\" ");
				}
				document.write("BORDER=0 VSPACE=3 HSPACE=3 class=swatchImage ALT=\"" + Related_prod[atn].name + "\" SRC=\"assets/product_images/" + Related_prod[atn].image + "\"></A>");
			}

  		document.write("<br>" + related_link + Related_prod[atn].name + "</A>");

			//adds price
      if (page_option_5== 1)
      {
				if (Related_prod[atn].list_price!="" && Related_prod[atn].sale_price!="" )
				{
  				document.write("<span class=ListPricewSale>"+ rel_list_pricelab + Related_prod[atn].list_price + " </span>");
  				document.write("<span class=SalePrice>"+ rel_sale_pricelab + Related_prod[atn].sale_price + "</span>");
				}else{
  				if (Related_prod[atn].list_price!="" && Related_prod[atn].sale_price=="" )
  				{
  					document.write("<font class=ListPricewoSaleR >" + rel_our_pricelab + Related_prod[atn].list_price + " </font>");
  				}
			  }
			}
      document.write("</TD></TR></table></td>");
		}
		//end for loop

		document.write("</TR></table>");
	}
}

var sizelist=",WXS,XS,WS,Small,S,WM,M,Medium,ML,WL,Large,L,Large-Tall,LT,LL,WXL,XL,X-Large,X-Large Tall,XLT,XLL,XXL,XXLL,2XLT,XX-Large,XX-Large Tall,XXXL,3XL,XXXLL,3X Large,3X Large Tall,3XLT,XXXXL,4XL,4X Large,30,30 Regular,30 Long,32,32 Regular,32 Long,33,33 Regular,33 Long,34,34 Regular,34 Long,35,35 Regular,35 Long,36,36 Regular,36 Long,38,38 Regular,38 Long,40,40 Regular,40 Long,42,42 Regular,42 Long,44,44 Regular,44 Long,46,46 Regular,46 Long,48,48 Regular,48 Long,One Size,";
var number_rows=0;

//After the buy button is hit, it goes through all the rows of products and makes the list of skus to add and their quantities.
function qt_validator(addto){
	var skustr="";
	var qtystr="";

	//number_rows is actually the number of unique styles (not colors) for this product.  This is done on the display section in draw_sku_option2
	for(i=0;i<number_rows;i++){
		//alert(eval("document.all.form1.qty_" + i + ".value") + "-" + eval("document.all.form1.sku_" + i + "[document.all.sku_" + i + ".selectedIndex].value"));
		if(eval("document.forms.form1.qty_" + i + ".value") != "0"){
			//we got a value that's NOT zero.  do note that we're not checking for negatives, characters, etc.
			// The ASP on orderform_additem should take care of anything funny.  default add-to is 1.
			if (skustr!=""){skustr=skustr + ","; qtystr=qtystr + ",";}
			if (eval("document.forms.form1.sku_" + i + ".type") != "hidden")
			{
			  skustr=skustr + eval("document.forms.form1.sku_" + i + "[document.forms.form1.sku_" + i + ".selectedIndex].value");
			}else{
			  skustr=skustr + eval("document.forms.form1.sku_" + i + ".value");
			}
			qtystr=qtystr + eval("document.forms.form1.qty_" + i + ".value");
		}
	}
	
	//document.getElementById("addtoform").value=addto;
	
	//assign the strings we've built to the normal sku and quantity inputs.
	document.forms.form1.qty.value=qtystr;
	document.forms.form1.sku.value=skustr;
	document.forms.form1.addtoform.value=addto;
	//return true to commit the submit to the next page.  If there are any errors then this is the place to stop it.  return false to stay on the page.
	return true;
}

function double_sort(a1,b1,a2,b2){
	var intval = 1; //intval is the value to be returned.  It starts at 1 and is only changed if a is NOT greater than b.
	//If a1 equals b1 then look at a2 & b2.  Otherwise look at a1&b1.
	if(a2==b2){
		//a2=b2, so look at a1<>b1

		//SECOND TERM SORT:
		intval= sizechart_sort(a1,b1);
	}else{
		//a2&b2 are different, if b2>a2 then make intval=-1.

		//FIRST TERM SORT:
		intval=	string_sort(a2,b2);
	}
	return intval;
}

//draw_sku_option3
function draw_sku_option3()
{
	if (product_var.length >= 1 )
	{
	
		if(product_var.length==1)
		{
			var atn=0;
			if (on_sale=="True")
			{
				if (parseInt(product_var[atn].price_sale) > 0 && parseInt(product_var[atn].price_sale) < parseInt(product_var[atn].price_list))
				{
				  price_string = "$" + OKStrOfPenny(product_var[atn].price_sale);
			  }
			}
			//else{
			//	price_string = "$" + OKStrOfPenny(product_var[atn].price_list);
			//}
			document.write("<INPUT TYPE=HIDDEN NAME='sku' VALUE='' >");
			document.write("<INPUT TYPE=HIDDEN NAME='qty' VALUE='0'>");
			document.write("<Input type=hidden name=addto id=addtoform value='1'>");

			// ***  setting outer table to maroon from front page #a3543f
			document.write("<TD  ALIGN=center><TABLE CELLPADDING=0 CELLSPACING=0 bgcolor=#a3543f ID=prod_tbl_hdr BORDER=0 width=100% >");
			//print column headers
			document.write("<tr bgcolor=a3543f><TD class=prod_tbl_hdr><B>Qty.</B></TD><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
			document.write("<td class=prod_tbl_hdr><B>Description</B></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
			document.write("<td class=prod_tbl_hdr><B>Regular Price</B></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
			document.write("<td class=prod_tbl_hdr><B>On Sale</B></td><td>&nbsp;&nbsp;</td></tr>");

	// *** SWAP to display color first and put size in drop down (TABLE FOR ONE ROW)
			// quantity box
			document.write("<TR VALIGN=middle><TD><Input type=text size=2 maxlength=2 name='qty' value=1></TD><TD>&nbsp;</TD>");
			document.write("<TD>&nbsp;&nbsp;");
			if (product_var[0].attr_value2 != "")
			{
			  document.write(product_var[0].attr_value2 + ", ");
			}
			document.write(product_var[0].attr_value1 + "</TD><TD>&nbsp;</TD>");
			//regular price
			document.write("<TD align=left>&nbsp;&nbsp;$" + OKStrOfPenny(product_var[atn].price_list) + "</TD><TD>&nbsp;</TD>");
			// our price
			document.write("<TD align=left>&nbsp;&nbsp;" + price_string + "</TD><TD>&nbsp;</TD>");
			document.write("<INPUT TYPE=HIDDEN  NAME='sku' VALUE='" + product_var[0].sku + "'>");
			document.write("</TR></TABLE></TD>");
    }else{
      //we have more than 1 sku, draw the multi-row table, 1 line for each product that has inventory
			document.write("<INPUT TYPE=HIDDEN NAME='sku' VALUE=''>");
			document.write("<INPUT TYPE=HIDDEN NAME='qty' VALUE='0'>");
			document.write("<Input type=hidden name=addto id=addtoform value='1'>");
			//document.write("<Input type=hidden name=dept_id id=dept_id value='" + dept_id + "'>");
			//document.write("<Input type=hidden name=pf_id id=pf_id value='" + pf_id + "'>");
			//sort the array by color then by size
			product_var.sort(UUSort);
			//*** setting inside table background color to front page grey #cccccc
			document.write("<TD ALIGN=center> <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 bgcolor=#cccccc width=100%>");
			i=attr_values.length-1;
			labelstr="";
			while (attr_label[i]!= "" && i >=0 )
			{
				labelstr=labelstr + attr_label[i] + " ";
				i=i-1;
			}
			//document.write(sel_values[0]);
			//var used to know when to switch to next listing
			var next_line=false;

			//option text, to be built iwth attr_value1 (sizes) every loop, and emptied into select options at the end of  attr_value2 is set
			var opt_txt="";
			var opt_count=0; //option count, to be able to count it easily to determine size

			//print column headers Qty. Colors Sizes Price  uses css style to make white & add padding
			document.write("<tr bgcolor=a3543f><TD class=prod_tbl_hdr><B>Qty.</B></TD><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
			document.write("<td class=prod_tbl_hdr><B>Colors</B></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
			document.write("<td class=prod_tbl_hdr><B>Sizes</B></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
			document.write("<td class=prod_tbl_hdr><B>Regular Price</B></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
			document.write("<td class=prod_tbl_hdr><B>On Sale</B></td><td>&nbsp;&nbsp;</td></tr>");

			for (atn=0;atn< product_var.length ;atn++)
			{
				price_string = "";
				if (on_sale=="True")
				{
						if (parseInt(product_var[atn].price_sale) > 0 && parseInt(product_var[atn].price_sale) < parseInt(product_var[atn].price_list))
						{
						  price_string = "$" + OKStrOfPenny(product_var[atn].price_sale);
						}
					}
					//else{
					//	price_string = "$" + OKStrOfPenny(product_var[atn].price_list);
					//}
				if (atn+1 == product_var.length){
					next_line=true;
				}else{
				// *** SWAP replaced attr_value1 with attr_value2 (show color first)
					if (product_var[atn].attr_value2 != "")
					{
					  if(product_var[atn].attr_value2 != product_var[atn+1].attr_value2){next_line=true;}
					}else{
					  if(product_var[atn].attr_value1 != product_var[atn+1].attr_value1){next_line=true;}
				  }
				}
				//** ADD SORT


				//increment the count and make the option
				opt_count++;
				var sizeSorted="";

				// *** SWAP replaced attr_value2 (color) with attr_value1 (size) puts size in drop-down menu
				//Check if in stock >0
				if (parseInt(product_var[atn].avail)>0) {
				  //TEST LINE alert(atn + ":::" +parseInt(product_var[atn].avail));
				  if (product_var[atn].attr_value2 != "")
				  {
				    opt_txt=opt_txt + "<Option value='" + product_var[atn].sku + "'>" + product_var[atn].attr_value1 + "</Option>";
				  }
				}
				//If this IS the last part of a color option, then write the row.
				if (next_line)
				{
					// if we had at least 1 option that was in stock then draw the line
					if (opt_txt.length > 0)
					{
  					document.write("<TR>");
  					//(TABLE FOR MORE THAN ONE ROW)
  					// *** SWAP replaced attr_value1 (size) with attr_value2 (show color first)
  					document.write ("<TD><Input type=text size=2 maxlength=2 name='qty_" + number_rows + "' value=0></TD><td>&nbsp;&nbsp;</td><td valign=middle>" + product_var[atn].attr_value2 + "</td><td>&nbsp;&nbsp;</td><td valign=middle>");
  					document.write("<Select style='width: 100px' name='sku_" + number_rows + "'>" + opt_txt + "</Select>");
  					document.write ("</td><td>&nbsp;&nbsp;</td><TD ALIGN='center'>$" + OKStrOfPenny(product_var[atn].price_list) + "</td>");
  					document.write ("</td><td>&nbsp;&nbsp;</td><TD ALIGN='center'>" + price_string + "</td></tr>");
   					//increment number of rows.
  					number_rows++;
          }else if (product_var[atn].attr_value2 == "" && parseInt(product_var[atn].avail)>0)
					{
					  //alert(number_rows + ":::" +parseInt(product_var[atn].avail));
					  //alert(number_rows + ":::" +parseInt(product_var[atn].sku));
					  document.write("<TR>");
  					//(TABLE FOR MORE THAN ONE ROW)
  					// *** SWAP replaced attr_value1 (size) with attr_value2 (show color first)
  					document.write ("<TD><Input type=text size=2 maxlength=2 name='qty_" + number_rows + "' value='0'></TD><td>&nbsp;&nbsp;</td><td valign=middle>" + product_var[atn].attr_value1 + "</td><td>&nbsp;&nbsp;</td><td valign=middle>");
  					document.write("<input type=hidden name='sku_" + number_rows + "' value='" + product_var[atn].sku + "'>");
  					document.write ("</td><td>&nbsp;&nbsp;</td><TD ALIGN='center'>$" + OKStrOfPenny(product_var[atn].price_list) + "</td>");
  					document.write ("</td><td>&nbsp;&nbsp;</td><TD ALIGN='center'>" + price_string + "</td></tr>");
   					//increment number of rows.
  					number_rows++;

					}

					//reset variables
					opt_count=0;
					opt_txt="";
					next_line=false;
				}
			}//end for
			if (number_rows == 1)
			{
			  document.forms.form1.qty_0.value = 1;
			}
			document.write("</td></TR></TABLE></TD>");
		}//end else
	}//end if
}//end function

/*
function isBad(){document.getElementById("popupdiv").style.visibility="hidden";}
function isGood(){document.getElementById("popupdiv").style.visibility="visible";}

function popup_swatch(objImage){
  if (objImage){
	settings = 'height=' + (objImage.height + 50) + ',width=' + (objImage.width + 50) + ',top=0,left=0,scrollbars=no,toolbar=no,resizable=yes';
	swatch_window = window.open(objImage.src,'bigimage',settings);
	swatch_window.window.focus();
  }
  return false;
}
*/
