/*** NOTE TO DEV: These functions are essentially the same as in pm_presspage.js. If you are fixing a bug or changing core functionality, you will need to make the same change in pm_presspage.js *********/

function hideImg(img){
	img.parentNode.style.display = "none";	// JAT 10/11/07 - called when images error out, will hide the image instead of showing a broken image on the page
}

var skucounter;	// used to count the number of sku ids there will be on the page. Used in this script and on the deptsku page.
function display_products_gen(Dept_products_gen,p_display_type){ 
	if (p_display_type==null){p_display_type=display_type;}
	if (Dept_products_gen.length >= 1){
		var n_product=0;
		var atn;      
		skucounter = 0;
		
		document.write(style_UL_open);	
		
		//addto form
		document.write("<Form name=addtobasket method=GET action=\"shopper_lookup.asp\" >");
		document.write("<Input type=hidden name=target value='xt_orderform_additem.asp'>");
		
		// for floating basket - return to this page
		//document.write('<INPUT TYPE=HIDDEN Name=returnpath Value="dept.asp?dept_id=' + dept_id + '&addfrom=product" >');
		document.write("<Input type=hidden name=auto_reg value='1'>");
		document.write("<Input type=hidden name=sku id=sku value=''>");		
		document.write("<Input type=hidden name=qty id=qty value=''>");
		document.write("<Input type=hidden name=addto id=addtoform value='1'>");
		document.write("<Input type=hidden name=s_id value='" + s_id + "'>");
		
		for (atn=0;atn< Dept_products_gen.length ;atn++){
			document.write("<Input type=hidden name=dept_id id=dept_id value='"+Dept_products_gen[atn].dept_id+"'>");
			document.write("<Input type=hidden name=pf_id id=pf_id value='"+Dept_products_gen[atn].pf_id+"'>");
		
			if (n_product==0 || p_display_type != 4){document.write(style_LI_2_open + style_font_2_open);}

			if (page_option_5==1){
				if  (p_image_align=="CENTER"){          
					document.write("<TABLE width=280 BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"0\" ALIGN=left> <TR><TD ALIGN=right>" + style_font_2_open);
				}
				else if (p_image_align == "LEFT")
				{
					document.write("<TABLE width=280 BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" ALIGN=left class='lbProdTbl'><TR valign=top><TD ALIGN='left' VALIGN='top'");
					//if(p_image_width!=""){ document.write(" WIDTH='"+(parseInt(p_image_width)+10)+"' "); }
					document.write(" width=70>");
				}
				
				//draw image
				if (Dept_products_gen[atn].image!=""){
					document.write("<A HREF=\"#\" onClick=return(visitargs('product.asp','dept_id="+ Dept_products_gen[atn].dept_id +"&pf_id="+Dept_products_gen[atn].pf_id +"&"+ ptargetc +"','URL'));  target=_top >");
				}
				
				document.write("<IMG class=\"lbProdImg\" ALIGN="+ p_image_align +" ");
				
				if(p_image_width==""){
					if (Dept_products_gen[atn].image_width!=""){
						document.write(" WIDTH="+ Dept_products_gen[atn].image_width  +" ");				
					}
				}else{
					document.write(" WIDTH ="+ p_image_width + " ");
				}
				document.write(" BORDER=0   ALT=\""+ Dept_products_gen[atn].name +" \" ");  	
				
				if (Dept_products_gen[atn].image!=""){
					document.write(" SRC=\"assets/product_images/"+ Dept_products_gen[atn].image +"\"></A>");  	
				}
				else
				{
					//display a spacer to hold size and area where image should be
					document.write(" SRC=\"assets/images/spacer.gif\" style=\"border:0px !important;\" class=\"lbProdImgSpacer\">");
				}
				
				if  (p_image_align=="CENTER"){ 
					document.write(" </TD></TR> <TR valign=\"top\"><TD ALIGN=CENTER >"+ style_font_2_open);
				}
				else if (p_image_align == "LEFT")
				{
					document.write("</TD><TD ALIGN='left' VALIGN='top' class='lbProdInfo' width=200>");
				}
				
				
				display_product_link("product.asp?s_id="+ s_id +"&dept_id="+ Dept_products_gen[atn].dept_id +"&pf_id="+ Dept_products_gen[atn].pf_id +"&"+ ptargetc,Dept_products_gen[atn].name,Dept_products_gen[atn].short_desc,Dept_products_gen[atn].price_str,s_id,Dept_products_gen[atn].dept_id,Dept_products_gen[atn].pf_id,Dept_products_gen[atn].enable_basket);
			    
				//draw sku option dropdown
				(Dept_products_gen[atn].skus).sort(UUSort);
				draw_sku_option(Dept_products_gen[atn], skucounter);	
				skucounter++;
				
				
				//draw button		// JAT 10/12/07 - moving over to draw_sku_option
				/*
				if (page_option_4== 1 && Dept_products_gen[atn].enable_basket==1 ){
					if (p_buy_target== "PRODUCT BUY"){
						document.write("<input type='image' SRC=\"assets/images/"+ p_buy_image +"\" ");							
						document.write("BORDER=\"0\" ALT=\""+ p_buy_image_alt +"\" ALIGN =\""+ p_buy_image_align +"\">");
						
					}
				}	*/

				
				//document.write("<br><br>")
				if( p_image_align=="CENTER")
				{
					document.write(" </TABLE>");
				}
				else if (p_image_align == "LEFT")
				{
					document.write("</TD></tr></table>");
				}
				
				//end form
				document.write("</Form>");		
			
				n_product=n_product+1;
				if(p_display_type==4){
					if (n_product==dept_cols){
						n_product=0;
						document.write(style_LI_2_close);
					}else{
						document.write(style_font_2_close +" </TD><TD>" +style_font_2_open);
					}
				}else{
					document.write(style_LI_2_close);
				}			
			}						
							
		} //end for loop
		document.write(style_UL_close);
		
		//draw section to display below all products
		document.write("<div style='display: block;' class='lookbook_products'>&nbsp;</div>");
	}else{
		document.write(style_font_1_open + p_no_product_HTML + style_font_1_close);
	}
} //end display_products_gen



function Display_sku(pf_name,pf_id,dept_id,sku,price_level,price_level2,price_level3,AvailabilitySTR,attr_value1,attr_value2,attr_value3,attr_value4,attr_value5,enable_basket,available){
 	document.write(attr_value1 +" "+ attr_value2 +"SKU:"+ sku );
	document.write(AvailabilitySTR);

	document.write("</TD><TD>$"+ OKStrOfPenny(price_level) +"</TD><TD>");
	
	onc ="return(visitargs('shopper_lookup.asp','target=xt_orderform_additem.asp&addto=1&auto_reg=1&pf_id="+ pf_id +"&dept_id="+ dept_id +"&attr_value1="+attr_value1+"&attr_value2="+attr_value2+"&attr_value3="+attr_value3+"&attr_value4="+attr_value4+"&attr_value5="+attr_value5+"'));"
	document.write("<A href=\"#\" onclick=\""+ onc +"\" ><b>Order Now</b></font></A>");
	document.write("</TD><TD>");
	onc="return(visitargs('product.asp','pf_id="+ pf_id +"&dept_id="+ dept_id +"&attr_value1="+attr_value1+"&attr_value2="+attr_value2+"&attr_value3="+attr_value3+"&attr_value4="+attr_value4+"&attr_value5="+attr_value5+"'));"
	document.write("<A href=\"#\" onclick=\""+ onc +"\" ><b>More Info</b></font></A>");
	document.write("</TD><TD>"); 
}

function display_dept_gen(display_dept_gen){  
	if (display_dept_gen.length >= 1){
		var n_dept=0;
		var atn; 
		document.write(s_top_HTML);
		document.write(style_UL_open);	
		   
		for (atn=0;atn< display_dept_gen.length ;atn++){
			if (n_dept==0 || d_display_type!=4){document.write(style_LI_1_open + style_font_2_open);}
			n_dept=n_dept+1 ;
			if (s_image_align=="CENTER"){             
				document.write("<TABLE width=100% BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" ALIGN=left > <TR><TD ALIGN=CENTER >"+ style_font_2_open);
			}
			if (display_dept_gen[atn].dept_image_1_file!="" ){
				document.write("<A HREF=\"#\" onClick=return(visitargs('dept.asp','dept_id=" + display_dept_gen[atn].dept_id +"&','URL')); class=\"deptlinkM\" target=_top >");
				document.write("<IMG ALIGN="+ s_image_align +" ");
				if(s_image_width==""){
					if (display_dept_gen[atn].dept_image_1_width!=""){
						document.write(" WIDTH="+ display_dept_gen[atn].dept_image_1_width +" ");				
					}
				}else{
					document.write(" WIDTH ="+ s_image_width + " ");
				}
				document.write(" BORDER=0  VSPACE=5  HSPACE=5  ALT=\""+ display_dept_gen[atn].dept_name +"\" ");  	
				if (s_image_src=="PRODUCT"){
					document.write(" SRC=\"assets/product_images/"+ display_dept_gen[atn].dept_image_1_file +"\"></A>");  	
				}else{
					document.write(" SRC=\"assets/product_images/"+ display_dept_gen[atn].dept_image_1_file +"\"></A>");  				    
				}
				if (s_image_align=="CENTER"){ 
					document.write(" </TD></TR> <TR><TD ALIGN=CENTER >"+ style_font_2_open);
				}		
			}
			if(display_dept_gen[atn].dept_disable_name=="" || display_dept_gen[atn].dept_disable_name < 2){
				if (display_dept_gen[atn].dept_display_type==1 || display_dept_gen[atn].dept_display_type==4){
					document.write("<A HREF=\"#\" onClick=return(visitargs('dept.asp','dept_id="+ display_dept_gen[atn].dept_id +"&','URL')); class=\"deptlinkM\" target=_top >"+ display_dept_gen[atn].dept_name +"</A>");
				}else if (display_dept_gen[atn].dept_display_type==2){
					document.write("<A HREF=\"#\" onClick=return(visitargs('frameset.asp','target="+ display_dept_gen[atn].dept_hyperlink +"&','URL')); class=\"deptlinksub\" target=_top >"+ display_dept_gen[atn].dept_name +"</A>");	
				}else{
					document.write("<A HREF=\"#\" onClick=return(visitargs('"+ display_dept_gen[atn].dept_hyperlink +"','','URL')); class=\"deptlinkLB\" target=_top >"+ display_dept_gen[atn].dept_name +"</A>");							
				}
			}
			//document.write(display_dept_gen[atn].dept_short_description);
			
			if (display_dept_gen[atn].attached_products!=null && display_dept_gen[atn].attached_products.length>0){
				display_products_gen(display_dept_gen[atn].attached_products,1);
			}
			if (display_dept_gen[atn].attached_depts!=null && display_dept_gen[atn].attached_depts.length>0){
				display_dept_2nd(display_dept_gen[atn].attached_depts);
			}
			if  (s_image_align=="CENTER"){document.write(" </TABLE>");}
			if(d_display_type==4){
				if (n_dept==dept_cols){
					n_dept=0;
					document.write(style_LI_1_close);
				}else{
					document.write(style_font_2_close +"</TD><TD>" + style_font_2_open);
				}
			}else{
				document.write(style_LI_1_close);
			}	
		} //end loop
		document.write(style_UL_close);
	}
} //end display_dept_gen 



//flag set when product available - used by draw_sku_option() to show/hide select input and can be used by product page to show/hide add to basket button
var isAvail = false;

//overridden to hide out of stock 
  //will show up to attribute 5
  //will not show empty select if not attributes are available
  //can use isAvail on product page to hide add to cart button if isAvail is false - all product skus are not available
function draw_sku_option(product_info, skucounter)
{
	var product_var = product_info.skus;
	
	//flag set when select is displayed
	var displaySelect = 0;
		
	if (product_var.length > 1 ){ 
		document.write("<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 width='100%'>");
		
		for (atn=0;atn< product_var.length ;atn++){
			if (product_var[atn].avail != '0'){ //if product available...
				//set flag
				isAvail = true;
				
				//only start the select if there is an available option to show
				//if select already displayed, don't display it again
				if(displaySelect==0){
					// * jat 1/18/08 - display color (attribute2) before the select - the select will only show size
					document.write("<TR valign=top><TD class=\"lbAttrTd\" width='50%'><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width='100%'><tr valign=middle><td class=\"lbColorAttrLabel\">" + product_var[atn].attr_value2 + "</td><td valign=middle><SELECT class=ProductAttr NAME=sku_" + skucounter + " id=sku_" + skucounter + ">");
			
					i=0;
					labelstr="";
					
					var attr_label = new Array();
					//if product_info.attr_label1 is empty, fill it with Size anyway.
					if(product_info.attr_label1 == "") product_info.attr_label1 = "Size";
					attr_label.push(product_info.attr_label1); // * jat 1/18/08 - only display label for size
					//attr_label.push(product_info.attr_label2);
					//attr_label.push(product_info.attr_label3);
					//attr_label.push(product_info.attr_label4);
					//attr_label.push(product_info.attr_label5);	

					var sel_values = new Array();
					sel_values.push("");
				
					for(var label in attr_label){
						labelstr += attr_label[label] + " ";
					}
					
					writeoption("",labelstr ,sel_values[0]);
					
					displaySelect=1;
					
				}
				
				//if (useVariantPrice=="True"){
					if (product_info.on_sale=="True"){
						if (product_var[atn].attr_value5!=""){
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1 + " - " + product_var[atn].attr_value2 + " - " + product_var[atn].attr_value3 + " - " + product_var[atn].attr_value4 + " - " + product_var[atn].attr_value5), sel_values[0]);
							// original code
							//writeoption(product_var[atn].sku,(product_var[atn].attr_value1 + " - " + product_var[atn].attr_value2 + " - " + product_var[atn].attr_value3 + " - " + product_var[atn].attr_value4 + " - " + product_var[atn].attr_value5), sel_values[0]);
						}
						else if (product_var[atn].attr_value4!=""){
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1 + " - " + product_var[atn].attr_value2 + " - " + product_var[atn].attr_value3 + " - " + product_var[atn].attr_value4),sel_values[0]);
						}
						else if (product_var[atn].attr_value3!=""){
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1 + " - " + product_var[atn].attr_value2 + " - " + product_var[atn].attr_value3),sel_values[0]);
						}
						else if (product_var[atn].attr_value2!=""){
							// jat 1/3/07 - change this to show only the first attribute (size)
							//writeoption(product_var[atn].sku,(product_var[atn].attr_value1 + " - " + product_var[atn].attr_value2),sel_values[0]);
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1),sel_values[0]);
						}
						else{
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1),sel_values[0]);
						}
					}else{
						if (product_var[atn].attr_value5!=""){
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1 + " - " + product_var[atn].attr_value2 + " - " + product_var[atn].attr_value3 + " - " + product_var[atn].attr_value4 + " - " + product_var[atn].attr_value5),sel_values[0]);
						}
						else if (product_var[atn].attr_value4!=""){
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1 + " - " + product_var[atn].attr_value2 + " - " + product_var[atn].attr_value3 + " - " + product_var[atn].attr_value4),sel_values[0]);
						}
						else if (product_var[atn].attr_value3!=""){
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1 + " - " + product_var[atn].attr_value2 + " - " + product_var[atn].attr_value3),sel_values[0]);
						}
						else if (product_var[atn].attr_value2!=""){
							// jat 1/3/07 - change this to show only the first attribute (size)
							//writeoption(product_var[atn].sku,(product_var[atn].attr_value1 + " - " + product_var[atn].attr_value2),sel_values[0]);
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1),sel_values[0]);
						}
						else{
							writeoption(product_var[atn].sku,(product_var[atn].attr_value1),sel_values[0]);
						}
					}
				//}// end if (useVariantPrice=="True")*/
			} //end if (product_var[atn].avail>0)
		}// end for
		
		//only close the select if the select was opened
		if(displaySelect==1) {
			document.write("</SELECT>");	
			document.write(" Qty <Input type='text' class='qtyinpt' name='qty_" + skucounter + "' id='qty_" + skucounter + "' value='1' size='1'>");	
			//draw button	
			if (page_option_4== 1 && product_info.enable_basket==1 ){
				if (p_buy_target== "PRODUCT BUY"){
					document.write("</td><tr><td colspan=2><input type='image' SRC=\"assets/images/"+ p_buy_image +"\" ");	
					document.write("BORDER=\"0\" name=\"addall\" ALT=\""+ p_buy_image_alt +"\" class=\"lbBuyNow\" onclick=\"return add_single_item(" + skucounter + ",'1');\">");
				}
			}
		}else{
			document.write("</td><tr><td colspan=2 class=lbProdAttrLabel>This product is currently out of stock.");
		}
		
		
		document.write("</td></tr></table></TD>");	
		document.write("</TR></TABLE>");
		
	}else if(product_var.length==1){
		
		document.write("<TABLE class=\"lbProdInfo\" BORDER=0 CELLPADDING=0 CELLSPACING=0>");
		
		if (product_var[0].avail != '0'){
			//set flag
			isAvail = true;
			
			i=0;
			
			var attr_label = new Array();
			attr_label.push(product_info.attr_label1);
			attr_label.push(product_info.attr_label2);
			attr_label.push(product_info.attr_label3);
			attr_label.push(product_info.attr_label4);
			attr_label.push(product_info.attr_label5);	
			
			while (attr_label[i]!= "") { 
				var tmpattrLabel = attr_label[i].replace("Available", "");
				document.write("<TR VALIGN=middle>");
				document.write("<TD class=lbProdAttrLabel>" + tmpattrLabel + "");				
				var theAttr = eval("product_var[0].attr_value"+(i+1)).toString();
				document.write("");
				document.write(theAttr);
				document.write("</TD>");
				i++;
			}
		}else{
			document.write("<TR VALIGN=middle>");
			document.write("<TD class=lbProdAttrLabel></TD><TD>");
			document.write("&nbsp;</TD></tr>");

			
		}
		
		//document.write("<tr><td colspan=2>"+product_var[0].avail_msg+"<br>");
		document.write("<td colspan=2 valign=middle><INPUT TYPE=HIDDEN  NAME='sku_" + skucounter + "' VALUE='" + product_var[0].sku + "' id='sku_" + skucounter + "' >");

		document.write("Qty <Input type='text' class='qtyinpt' name='qty_" + skucounter + "' id='qty_" + skucounter + "' value='1' size='1'>");
		//draw button	
		if (page_option_4== 1 && product_info.enable_basket==1 ){
			if (p_buy_target== "PRODUCT BUY"){
				document.write("</td><tr><td colspan=3><input type='image' SRC=\"assets/images/"+ p_buy_image +"\" ");	
				document.write("BORDER=\"0\" name=\"addall\" ALT=\""+ p_buy_image_alt +"\" onclick=\"return add_single_item(" + skucounter + ",'1');\" class=\"lbBuyNow\">");
			}
		}
		document.write("<td></TR></TABLE>");
	} // end if (product_var.length >= 1 )
	
} //end draw_sku_option

function display_product_link(purl,pname,short_desc,price_str,s_id,dept_id,pf_id,basket_enable){
	//document.write("<a HREF=\"" + purl + "\" class=\"deptlinkLB\" target=\"_top\">" + pname + "</a>&nbsp;" + short_desc );
	document.write("<a HREF=\"" + purl + "\" class=\"deptlinkLB\" target=\"_top\">" + pname + "</a>");
	document.write("<br/>" + price_str);
}

/* jat 11/16/07 - draws out the back and forward arrows on lookbook */
function display_arrows(){
	//display previous and next buttons
	//if current department is not lookbooks[0], then show previous button
	if(dept_id != lookbooks[0]){
		//find current department in lookbooks array
		for(var inc=0; inc<lookbooks.length; inc++){
			if(dept_id==lookbooks[inc]){
				var prev = inc-1;
				document.write("<a href=\"dept.asp?dept_id="+lookbooks[prev]+"\" onClick=\"return(visitargs('dept.asp','dept_id="+lookbooks[prev]+"','URL'));\"><img src=\"assets/images/arrow_pink_left.gif\" class=\"LBarrow\"></a> ");
				break;
			}
		}
	}
	else{
		//use spacer to hold place
		//document.write("<img src=\"assets/images/spacer.gif\" border=\"0\" align=\"left\" width=\"58\" height=\"22\" style=\"border: none !important;\"> ");
	}

	// jat 2/4/08 - insert 'the lookbook' title image
	document.write("<img src='assets/images/next.gif' alt='next'/>");
	
	//if current department is not lookbooks[lookbooks.length-1], then show next button
	if(dept_id != lookbooks[lookbooks.length-1]){
		//find current department in lookbooks array
		for(var inc=0; inc<lookbooks.length; inc++){
			if(dept_id==lookbooks[inc]){
				var next = inc+1;
				document.write("<a href=\"dept.asp?dept_id="+lookbooks[next]+"\" onClick=\"return(visitargs('dept.asp','dept_id="+lookbooks[next]+"','URL'));\"><img src=\"assets/images/arrow_pink_right.gif\"   style=\"\" class=\"LBarrow\"></a> ");
				break;
			}
		}
	}
	else{
		//use spacer to hold place
		//document.write("&nbsp;&nbsp;&nbsp;<img src=\"assets/images/spacer.gif\" border=\"0\" align=\"left\" width=\"58\" height=\"22\" style=\"border: none !important;\"> ");
	}
}

/* jat 11/13/07 - this is called on the individual item's 'add to cart' button */
function add_single_item(itemNum,addto){
	var errmsg = "";
	var selected_item = document.getElementById("sku_" + itemNum);
	var selected_item_qty = document.getElementById("qty_" + itemNum);

	if(selected_item.value == ""){
		errmsg = "Please select a size.";
	}else if(selected_item_qty.value == "0"){
		errmsg = "Please specify a quantity greater than zero.";
	}
	
	/* JAT 3/20/08 - Verify that the user did not choose a quantity higher than the # availble for the product they wish to buy */
	var tmpavail = "";
	var tmpval = selected_item.value;
	var tmpprodname = "";
	
	// 3/20/08 - first go through all the products and find the availability & name of the one they chose
	for(var k = 0; k < Dept_products.length; k++){
		for(var j = 0; j < Dept_products[k].skus.length; j++){
			if(parseInt(Dept_products[k].skus[j].sku) == parseInt(tmpval)){
				tmpavail = Dept_products[k].skus[j].avail;
				tmpprodname = Dept_products[k].name;
			}
		}
	}
	
	// 3/20/08 - now determine if the user wants more than is availble and if they do, prevent from adding to basket  & provide error message
	if(parseInt(selected_item_qty.value) > parseInt(tmpavail)){
		if(tmpavail == 1){
			errmsg = "Sorry, there is only " + tmpavail + " " + tmpprodname + " available in that size/color.";
		}else{
			errmsg = "Sorry, there are only " + tmpavail + " " + tmpprodname + "s available in that size/color.";
		}
		
		// change qty input box to show max # of available
		document.getElementById("qty_" + itemNum).value = tmpavail;
	}
	
	/* END VERIFY 3/2/08 */
	
	//if we have all skus properly, submit the add, otherwise give msg and return false.
	if(errmsg.length>0){
		//alert(errmsg);
		document.getElementById('addoneErrDiv').innerHTML = errmsg;
		document.getElementById('addallErrDiv').innerHTML = "";
		return false;
	}else{
		document.getElementById("sku").value=selected_item.value;
		document.getElementById("qty").value=selected_item_qty.value;
		document.getElementById("addtoform").value=addto;
		document.addtobasket.submit();
		//return true;
	}
}

/* this is called on the 'add all items to cart' button */
function add_all_items(count,addto){
	var errmsg="";
	var elt;
	var prodname="";
	var skulist="";
	var sku9="";
	var qtystr="";
	var qtyi;
	
	//first check to make sure all items have a chosen sku
	
	
	for (kk=0;kk<count;kk++){
		sku9="";
		if(document.getElementById("sku_" + kk).value=="multiple"){
			elt=document.getElementById("sel_" + kk);
			if(elt){sku9=elt[elt.selectedIndex].value;}
		}else{
			sku9=document.getElementById("sku_" + kk).value;
		}
		if(sku9==''){
			//prodname=document.getElementById("prodname_" + kk).value
			errmsg= "\nPlease specify size and quantity for all items.\n";
		}else{
			qtyi=document.getElementById("qty_" + kk)
  			//set tempvar to qty if possible
  			if(qtyi){tempvar=qtyi.value}else{tempvar=0;}
  			//if qty is valid then add the item
  			if((tempvar != "0") && (IsNumeric(tempvar)) && (tempvar.length>0)){
  				if (skulist!=""){
  				  skulist=skulist + ",";
  				  qtystr=qtystr + ",";
  				}
  				qtystr=qtystr + qtyi.value;
  				skulist=skulist + sku9;
  			}
  		}
		
		/* JAT 3/20/08 - Verify that the user did not choose a quantity higher than the # availble for the products they wish to buy */
		var tmpavail = "";
		var tmpval = sku9;
		var tmpprodname = "";
		
		// 3/20/08 - first go through all the products and find the availability & name of the one they chose
		for(var k = 0; k < Dept_products.length; k++){
			for(var j = 0; j < Dept_products[k].skus.length; j++){
				if(parseInt(Dept_products[k].skus[j].sku) == parseInt(tmpval)){
					tmpavail = Dept_products[k].skus[j].avail;
					tmpprodname = Dept_products[k].name;
					
					if(parseInt(qtyi.value) > parseInt(tmpavail)){
						// 3/20/08 - now determine if the user wants more than is availble and if they do, prevent from adding to basket  & provide error message
						if(tmpavail == 1){
							errmsg += "<br/>Sorry, there is only " + tmpavail + " " + tmpprodname + " available in that size/color.";
						}else{
							errmsg += "<br/>Sorry, there are only " + tmpavail + " " + tmpprodname + "s available in that size/color.";
						}
						
						// change the quantity input as well
						document.getElementById("qty_" + k).value = tmpavail;
					}
				}
			}
		}
		
		
		/* END VERIFY 3/2/08 */
	}

	if(skulist.length==0)
		errmsg = "Please specify size and quantity for all items.";
	//if we have all skus properly, submit the add, otherwise give msg and return false.
	
	if(errmsg.length>0){
		document.getElementById('addallErrDiv').innerHTML = errmsg;
		document.getElementById('addoneErrDiv').innerHTML = "";
		console.log('err: ' + errmsg);
		return false;
	}else{
		document.getElementById("sku").value=skulist;
		document.getElementById("qty").value=qtystr;
		document.getElementById("addtoform").value=addto;
		document.addtobasket.submit();
		//return true;
	}
}

//////// HELPER FUNCTION FOR qt_validator //////////////
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var myChar;

   for (q = 0; q < sText.length && IsNumber == true; q++)
   {
     myChar = sText.charAt(q);
     if (ValidChars.indexOf(myChar) == -1)
     {
       IsNumber = false;
     }
   }
   return IsNumber;
}
