/**
 * Returns true if 'e' is contained in the array 'a'
 * @author Johan Känngård, http://dev.kanngard.net
 */
function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}

/* search for related items with matching type */
function getTypeOfRelatedItems(type) {
	var arrItems = 0;
	var arrIDs = new Array();
	for (itematn=0;itematn< Related_prod.length ;itematn++)
	{			
		//if the type matches
		if(Related_prod[itematn].crosstype==type)
		{
			//go through each sku for the current product
			for (iSku=0;iSku< Related_prod[itematn].skus.length ;iSku++)
			{
				//if a sku is available in the selected color...
				if(cleanColorName(Related_prod[itematn].skus[iSku].attr_value2).toUpperCase() == qColor.toUpperCase() && Related_prod[itematn].skus[iSku].avail != '0') {
					//add the product to the array
					arrIDs[arrItems] = new arrtrack_struct();
					arrIDs[arrItems].rp_id = itematn;
					arrItems++;
					break;
				}
			}
		}
	}
	return(arrIDs);
}

/* draws related items vertically (display=1) or horizontally (display=0) on product.asp */
function draw_related(type,display,message,rel_image_enable,rel_image_width,rel_list_pricelab,rel_sale_pricelab,rel_our_pricelab,desired_cols)
{
	if (Related_prod.length >0)
	{
		//set default values if null
		if (typeof type=="undefined"){ type="0"; }
		if (typeof display=="undefined"){ display=1; }
		if (typeof message=="undefined"){ message=""; }
		if (typeof rel_image_enable=="undefined"){ rel_image_enable="PRODUCT"; }
		if (typeof rel_image_width=="undefined"){ rel_image_width=""; }
		if (typeof rel_list_pricelab=="undefined"){ rel_list_pricelab="Price "; }
		if (typeof rel_sale_pricelab=="undefined"){ rel_sale_pricelab="Sale Price"; }
		if (typeof rel_our_pricelab=="undefined"){ rel_our_pricelab="Our Price"; }
		if (typeof desired_cols=="undefined"){ desired_cols=3; }
		var show_price=1;
		
		var arrIDs = new Array();
		
		//search for related items with matching type and matching color with availability
		arrIDs = getTypeOfRelatedItems(type);
		
		//if we have some matching product then proceed
		if (arrIDs.length>0)
		{
			document.write("<div id=\"matching_message\">Matching Styles will be shown again after placing this item in Shopping Bag.</div><br />");
   
			var prodlink="";
  			//var col_width = 100/(desired_cols);	
			col_width = 155;
			var itemCount = 0; //used to keep track of number of items -- to close row after desired_cols in horizontal view
			shownItems=new Array(); //used to keep track of items that were already displayed
			var rowCount = Math.ceil(arrIDs.length/3); //used to keep track of number of rows -- for "header" rowspan
			document.write("    <div class=\"relProductlabel\">"+message+"<\/div>");
			document.write("<table  border=\"0\" cellspacing=\"3\" cellpadding=\"2\">");
			document.write("  <tr>");
			
			for(i=0;i<arrIDs.length;i++)
			{
			//if the product was not already shown...			
				if (!contains(shownItems,Related_prod[arrIDs[i].rp_id].name) && !contains(shownItems,Related_prod[arrIDs[i].rp_id].pf_id))
				{						
					itemCount++; //incr itemCount to track number of items being displayed.
					//add item to array of shown items 
					shownItems[itemCount] = Related_prod[arrIDs[i].rp_id].name;
					shownItems[itemCount] = Related_prod[arrIDs[i].rp_id].pf_id;
					prodlink="<a href=\"" + Related_prod[arrIDs[i].rp_id].link + "&attr_value2="+qColor+"\" class=\"deptlinkP\">";					
					document.write(" <td class=\"relProduct\" VALIGN=\"top\" ALIGN=\"left\" width=\""+col_width+"\"><div class=\"roundBox\"><table cellpadding='0' cellspacing='0' border='0' width='100%'><tr><td valign='top' rowspan='2'>");
					
					//product image & link
					document.write(prodlink);
					document.write("<img class='relProduct_img' ALIGN=LEFT BORDER=0 VSPACE=0 HSPACE=0 ALT=\"" + Related_prod[arrIDs[i].rp_id].name + "\" ");
					//qColor comes from lingerie_product_include.js
					document.write("SRC=\"assets/product_images/"+ Related_prod[arrIDs[i].rp_id].pf_id +"-"+qColor+"-xsm.jpg\"");
					if (rel_image_width !="")
					{
						document.write(" WIDTH =\"" + rel_image_width + "\"");
					}
					document.write(" onError=\"this.src='assets/images/spacer.gif'\"><\/a>");
					
					//product name link
					document.write("</td><td valign='top' colspan='2'>"+prodlink + Related_prod[arrIDs[i].rp_id].name + "<\/a></td></tr><tr><td valign='top' align='left'>");
					
					//product price (if enabled)
					if ((show_price==1)&&(Related_prod[arrIDs[i].rp_id].list_price!=""))
					{
						if (Related_prod[arrIDs[i].rp_id].on_sale!="False" && Related_prod[arrIDs[i].rp_id].sale_price!="" && Related_prod[arrIDs[i].rp_id].sale_price!="$0.00" && Related_prod[arrIDs[i].rp_id].sale_price!=Related_prod[arrIDs[i].rp_id].list_price)
						{
							document.write("<font class=ListPricewSaleR>" + rel_list_pricelab + Related_prod[arrIDs[i].rp_id].list_price + "<\/font>");
							document.write("<font class=SalePriceR>" + rel_sale_pricelab + Related_prod[arrIDs[i].rp_id].sale_price + "<\/font>");
						}else{
							document.write("<font class=ListPricewoSaleR>" + rel_our_pricelab + Related_prod[arrIDs[i].rp_id].list_price + "<\/font>");
						}
					}									
					
					document.write("</td><td valign='bottom' align='right'>");
					document.write(prodlink+'<img src="assets/images/round_arrow_onwhite.gif" align="right"></a>');
					
					document.write("</td></tr></table></div><div class=\"boxBottom\">&nbsp;</div><\/TD>");
					if (itemCount%desired_cols==0)
					{
						if((itemCount)!=arrIDs.length)
						{
							document.write("</tr>");
						}
					}else{
						if (itemCount==arrIDs.length)
						{
							document.write("<td colspan=" + ((itemCount%desired_cols)+1) + ">&nbsp;</td>");
						}
					}					
				}// end if !contains
			}// end for(i=0;i<arrIDs.length;i++)
			document.write("<\/TR><\/TABLE>");
		}// end if (arrIDs.length>0)		
	}// end if (Related_prod.length>0)
}//end function draw_related

function arrtrack_struct()
{
	this.rp_id
	this.r_id
}


var rel_prod_in_basket = new Array();
var rel_prod_not_in_basket = new Array();

/* for related items on gentle reminder page that have already been added to cart */
function draw_related_in_basket(){
	
	if (Related_prod.length >0)
	{
		//search for related items with matching type
		rel_prod_not_in_basket = getTypeOfRelatedItems("0");
		
		//filter out from arrIDs pf_ids that are in cart already
		rel_prod_not_in_basket = filterRelatedItemsInBasket(rel_prod_not_in_basket);
		
		if(rel_prod_in_basket.length>0) {
			var rp_output = "";
			for(prb=0;prb<rel_prod_in_basket.length;prb++)
			{				
				rp_output += '<tr><td width="60"><img src="assets/product_images/' + rel_prod_in_basket[prb].pf_id + '-' + cleanColorName(rel_prod_in_basket[prb].attr_value2) + '-xsm.jpg" border="0" onError="this.src=\'assets/images/spacer.gif\';"/></td>';
				rp_output += '<td colspan="2">'+rel_prod_in_basket[prb].quantity+' <strong>'+rel_prod_in_basket[prb].name+'</strong>';
				if (rel_prod_in_basket[prb].attr_value1 !=""){ rp_output += ", size " + rel_prod_in_basket[prb].attr_value1; }
				if (rel_prod_in_basket[prb].attr_value2 !=""){ rp_output += ", in " + rel_prod_in_basket[prb].attr_value2; }
				rp_output += '<br>has been added to your shopping bag.<br /></td>';
				rp_output += '</tr>';
			}
			document.write(rp_output);
		}//end if rel_prod_in_basket.length
	}//end if related_prod.length
}

/* for related items on gentle reminder page that display with dropdowns */
function draw_related_wSku (type,display,message,rel_image_enable,rel_image_width,rel_list_pricelab,rel_sale_pricelab,rel_our_pricelab,desired_cols,isWishList)
{		
	if (Related_prod.length >0)
	{
		//set default values if null
		if (typeof type=="undefined"){ type="0"; }
		if (typeof display=="undefined"){ display=1; }
		if (typeof message=="undefined"){ message=""; }
		if (typeof rel_image_enable=="undefined"){ rel_image_enable="PRODUCT"; }
		if (typeof rel_image_width=="undefined"){ rel_image_width=""; }
		if (typeof rel_list_pricelab=="undefined"){ rel_list_pricelab="Price "; }
		if (typeof rel_sale_pricelab=="undefined"){ rel_sale_pricelab="Sale Price"; }
		if (typeof rel_our_pricelab=="undefined"){ rel_our_pricelab="Our Price"; }
		if (typeof desired_cols=="undefined"){ desired_cols=4; }
		if (typeof isWishList == "undefined"){isWishList = false; }
		
		if (isWishList == true) {
			//search for related items with matching type
			rel_prod_not_in_basket = getTypeOfRelatedItems("0");
		}
		
		var show_price=1;
		desired_cols=4;
		
		var arrIDs = rel_prod_not_in_basket;
		
		//var arrIDs = new Array();
		
		//search for related items with matching type
		//arrIDs = getTypeOfRelatedItems(type);
		
		//filter out from arrIDs pf_ids that are in cart already
		//arrIDs = filterRelatedItemsInBasket(arrIDs);
		
		//if we have some matching product then proceed
		if (arrIDs.length>0)
		{
			var prodlink="";
  			var col_width = 100/(desired_cols);			
			var itemCount = 0; //used to keep track of number of items -- to close row after desired_cols in horizontal view
			shownItems=new Array(); //used to keep track of items that were already displayed
			var rowCount = Math.ceil(arrIDs.length/desired_cols); //used to keep track of number of rows -- for "header" rowspan
			document.write("    <div class=\"relProductlabel_gr\">"+message+"<\/div>");
			document.write("<table width=\"100%\" border=\"0\" cellspacing=\"6\" cellpadding=\"2\">");
			document.write("  <tr>");
			
			for(i=0;i<arrIDs.length;i++)
			{
			//if the product was not already shown...
			
				if (!contains(shownItems,Related_prod[arrIDs[i].rp_id].name) && !contains(shownItems,Related_prod[arrIDs[i].rp_id].pf_id))
				{						
					itemCount++; //incr itemCount to track number of items being displayed.
					//add item to array of shown items 
					shownItems[itemCount] = Related_prod[arrIDs[i].rp_id].name;
					shownItems[itemCount] = Related_prod[arrIDs[i].rp_id].pf_id;
					prodlink="<a href=\"" + Related_prod[arrIDs[i].rp_id].link + "&attr_value2="+qColor+"\">";
					document.write(" <td class=\"relProduct reminder\" VALIGN=\"top\" ALIGN=\"left\" width=\""+col_width+"%\">");
					
					document.write("<div class=\"roundBox\"><div id=\"product_"+Related_prod[arrIDs[i].rp_id].pf_id+"\"></div></div><div class=\"boxBottom\">&nbsp;</div>");
					
					//store pfids to display later
					addPFID(Related_prod[arrIDs[i].rp_id].pf_id);	
										
					document.write("<\/TD>");
					if (itemCount%desired_cols==0)
					{
						if((itemCount)!=arrIDs.length)
						{
							document.write("</tr>");
						}
					}else{
						if (itemCount==arrIDs.length)
						{
							document.write("<td colspan=" + ((itemCount%desired_cols)+1) + ">&nbsp;</td>");
						}
					}					
				}// end if !contains
			}// end for(i=0;i<arrIDs.length;i++)
			document.write("<\/TR><\/TABLE>");
			
			//get started on displaying the related items data
			if (isWishList == true) {getProdInfoWish();}
			else {getProdInfo();}			
			
		}// end if (arrIDs.length>0)		
	}// end if (Related_prod.length>0)		
}


/* search for related items with matching type */
function getTypeOfRelatedItems_basket(type) {
	var arrItems = 0;
	var arrIDs = new Array();
	for (itematn=0;itematn< Related_prod.length ;itematn++)
	{	
		for(itematnInner=0;itematnInner<Related_prod[itematn].related.length;itematnInner++) {
			if(Related_prod[itematn].related[itematnInner].crosstype==type)
			{
				arrIDs[arrItems] = new arrtrack_struct();
				arrIDs[arrItems].rp_id = itematn;
				arrIDs[arrItems].r_id = itematnInner;
				arrItems++;
			}
		}
	}
	return(arrIDs);
}

/* takes out related items that are already added to cart and returns that array -- also fills rel_prod_in_basket array*/
function filterRelatedItemsInBasket(originalArray) {
	var tempArray = new Array();
	var alreadyInBasket = false;
	
	//loop through originalArray
	for(oaInd=0;oaInd<originalArray.length;oaInd++) {
		//set flag to false
		alreadyInBasket = false
		
		//go through basket items
		for(sbInd=0;sbInd<shopping_basket.items.length;sbInd++) {
			//if current related item is in basket...
			if(Related_prod[originalArray[oaInd].rp_id].pf_id == shopping_basket.items[sbInd].pf_id) {
				alreadyInBasket = true; //set flag to true
				//add to rel_prod_in_basket
				rel_prod_in_basket.push(shopping_basket.items[sbInd]);
			}			
		}//end for
		
		//if flag is not true...
		if(alreadyInBasket == false) {
			//add current item to temp array
			tempArray.push(originalArray[oaInd]);
		} 
	}//end for
	
	return tempArray;
}


/* draws related items vertically (display=1) or horizontally (display=0) on basket.asp */
function draw_related_basket(type,display,message,rel_image_enable,rel_image_width,rel_list_pricelab,rel_sale_pricelab,rel_our_pricelab,desired_cols)
{
	if (Related_prod.length >0)
	{
		//set default values if null
		if (typeof type=="undefined"){ type="0"; }
		if (typeof display=="undefined"){ display=1; }
		if (typeof message=="undefined"){ message=""; }
		if (typeof rel_image_enable=="undefined"){ rel_image_enable="PRODUCT"; }
		if (typeof rel_image_width=="undefined"){ rel_image_width=""; }
		if (typeof rel_list_pricelab=="undefined"){ rel_list_pricelab="Price "; }
		if (typeof rel_sale_pricelab=="undefined"){ rel_sale_pricelab="Sale Price"; }
		if (typeof rel_our_pricelab=="undefined"){ rel_our_pricelab="Our Price"; }
		if (typeof desired_cols=="undefined"){ desired_cols=3; }
		var show_price=1;
		
		var arrIDs = new Array();
		
		//search for related items with matching type
		arrIDs = getTypeOfRelatedItems_basket(type);
		
		//if we have some matching product then proceed
		if (arrIDs.length>0)
		{
			var prodlink="";
  			var col_width = 100/(desired_cols);			
			var itemCount = 0; //used to keep track of number of items -- to close row after desired_cols in horizontal view
			shownItems=new Array(); //used to keep track of items that were already displayed
			var rowCount = Math.ceil(arrIDs.length/3); //used to keep track of number of rows -- for "header" rowspan
			document.write("<div class='bag_suggest_top'>&nbsp;</div><div class='bag_suggest_center'>");
			document.write("<table align='center' border=\"0\" cellspacing=\"0\" cellpadding=\"4\" class=\"relProduct\">");
			document.write("  <tr><td class=\"relProductlabel\" align=\"center\">"+message+"</td>");
			document.write("  <tr>");
			
			
			for(i=0;i<arrIDs.length;i++)
			{
				//if the product was not already shown...
				if (!contains(shownItems,Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].name) && !contains(shownItems,Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].pf_id))
				{					
					itemCount++; //incr itemCount to track number of items being displayed.
					//add item to array of shown items 
					shownItems[itemCount] = Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].name;
					shownItems[itemCount] = Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].pf_id;
					prodlink="<a href=\"" + Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].link + "\">";					
					document.write(" <td class=\"relProduct\" VALIGN=\"top\" ALIGN=\"left\" width=\""+col_width+"\"><div class=\"roundBox\"><table cellpadding='0' cellspacing='0' border='0' width='100%'><tr><td valign='top' rowspan='2'>");
					
					//product image & link
					document.write(prodlink);
					document.write("<img class='relProduct_img' ALIGN=LEFT BORDER=0 VSPACE=0 HSPACE=0 ALT=\"" + Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].name + "\" ");
					//qColor comes from lingerie_product_include.js
					document.write("SRC=\"assets/product_images/"+ Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].pf_id +"-"+cleanColorName(Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].skus[0].attr_value2)+"-xsm.jpg\" onError=\"this.src='assets/images/spacer.gif'\"><\/a>");
					
					//product name link
					document.write("</td><td valign='top' colspan='2'>"+prodlink + Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].name + "<\/a></td></tr><tr><td valign='top' align='left'>");
					
					//product price (if enabled)
					if ((show_price==1)&&(Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].list_price!=""))
					{
						if (Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].on_sale!="False" && Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].sale_price!="" && Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].sale_price!="$0.00" && Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].sale_price!=Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].list_price)
						{
							document.write("<font class=ListPricewSaleR>" + rel_list_pricelab + Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].list_price + "<\/font>");
							document.write("<font class=SalePriceR>" + rel_sale_pricelab + Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].sale_price + "<\/font>");
						}else{
							document.write("<font class=ListPricewoSaleR>" + rel_our_pricelab + Related_prod[arrIDs[i].rp_id].related[arrIDs[i].r_id].list_price + "<\/font>");
						}
					}									
					
					document.write("</td><td valign='bottom' align='right'>");
					document.write(prodlink+'<img src="assets/images/round_arrow_onwhite.gif" align="right"></a>');
					
					document.write("</td></tr></table></div><div class=\"boxBottom\">&nbsp;</div><\/TD>");
					if (itemCount%desired_cols==0)
					{
						if((itemCount)!=arrIDs.length)
						{
							document.write("</tr>");
						}
					}else{
						if (itemCount==arrIDs.length)
						{
							document.write("<td colspan=" + ((itemCount%desired_cols)+1) + ">&nbsp;</td>");
						}
					}					
				}// end if !contains
			}// end for(i=0;i<arrIDs.length;i++)
			
			document.write("<\/TR><\/TABLE></div><div class='bag_suggest_bottom'>&nbsp;<div>");
		}// end if (arrIDs.length>0)		
	}// end if (Related_prod.length>0)
}//end function draw_related