// strip quotes from product_name - used in various parts of this js file and popper_plight2.js
var tmpproduct_name = product_name.replace(/"|'/gim,"\\'");

/* new product page script.. just putting all loose functions in one js file */
	function write_lastlink(){
		document.write('<INPUT TYPE=HIDDEN id=returnpath Name=returnpath Value="product.asp?dept_id=' + dept_id + '&pf_id='+ pf_id +'&addfrom=product" >');
		
		// also remove blank h1 tag
		document.getElementsByTagName('h1')[0].style.display = "block";
	}

	function changeReturnPath(){
		document.getElementById('returnpath').value = "";
	}
	
	function draw_related(type,display,message,rel_image_enable,rel_image_width,rel_list_pricelab,rel_sale_pricelab,rel_our_pricelab){
		var prodlink="";
		var typematch=0;
		var show_price=0;

		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 (Related_prod.length >0){
			for (itematn=0;itematn< Related_prod.length ;itematn++){
				//for (atn=0;atn< Related_prod[itematn].length ;atn++){
					if (Related_prod[itematn].crosstype==type){
						typematch=1;
					}
				//}
			}
		}
		if (typematch==1) {
			document.write("<div style='text-align:center;'><img src=\"assets/images/cart_line.gif\"></div><br>");
			document.write("<table width=122 cellpadding=0 cellspacing=0><tr><td class=cartOutline>");	
			document.write("<table border=0 cellspacing=1 cellpadding=3 valign=bottom width=122>");
			document.write("<tr><td class=cartHeader align=left><img src=\"assets/images/cart_suggestions.gif\" border=\"0\" alt=\""+message+"\"></td></tr><tr>");
		}

		if (Related_prod.length>0){
			//document.write("<Table border=0 cellpadding=2 cellspacing=2>");
			//document.write("<TR valign=\"bottom\">");
			for (itematn=0;itematn< Related_prod.length ;itematn++){
				//for (atn=0;atn< Related_prod[itematn].related.length ;atn++){
					if (Related_prod[itematn].crosstype==type){
						
						if (user_guid !=' '){
								prodlink="<A HREF=\"" + Related_prod[itematn].link +"s_id=" + i_jscript_uu_sid +  "&mscssid="+ user_guid+ "&\">";
						}else{
								prodlink="<A HREF=\"" + Related_prod[itematn].link +"s_id=" + i_jscript_uu_sid +  "&\">";
						}
					
						document.write("<td class=cartItems align='center'>");
					
						//product image & link
						if ((Related_prod[itematn].image!="")&&(rel_image_enable!="NONE")){
							document.write(prodlink);
							if (rel_image_width !=""){
								document.write("<img ALIGN=CENTER WIDTH =\"" + rel_image_width + "\" BORDER=0  VSPACE=0  HSPACE=0  ALT=\"" + Related_prod[itematn].name + "\" SRC=\"assets/product_images/" + Related_prod[itematn].image + "\"></A>");
							}else{
								document.write("<img ALIGN=CENTER BORDER=0  VSPACE=0  HSPACE=0  ALT=\"" + Related_prod[itematn].name + "\" SRC=\"assets/product_images/"+ Related_prod[itematn].image +"\"></A>");
							}
							
							document.write("<BR>");
							
						}
						//product name link
						document.write(prodlink + Related_prod[itematn].name + "</A>");
						//product price (if enabled)
						if ((show_price==1)&&(Related_prod[itematn].list_price!="")){
							if (Related_prod[itematn].on_sale!="False" && Related_prod[itematn].sale_price!="" && Related_prod[itematn].sale_price!="$0.00" && Related_prod[itematn].sale_price!=Related_prod[itematn].list_price){
								document.write("<div class=ListPricewSaleR>" + rel_list_pricelab + Related_prod[itematn].list_price + " </div>");
								document.write("<div class=SalePriceR>" + rel_sale_pricelab + Related_prod[itematn].sale_price + " </B></div>");
							}else{
								document.write("<div class=ListPricewoSaleR>" + rel_our_pricelab + Related_prod[itematn].list_price + " </div>");
							}
						}
						document.write("</TD>");
						if (display==1){document.write("</tr><tr>");}		
						else
						{
							if((itematn+1)%3==0&&(itematn+1)!=packages_array[itematn].children.length)
							{document.write("</tr><tr>");}
						}			
					}
			//}
				
			}//end for			
		}
		if (typematch==1) {
			document.write("</TR></TABLE></TD></TR></TABLE>");
		}
}//end function draw_related

function getPriceDifference(){
		//get prices from price string (product_price)
		//split price string on $
		var pricestr = product_price.split('$');
			
		//get first price
		var firstPrice = pricestr[1].substring(0, pricestr[1].indexOf('.'));	
		firstPrice += pricestr[1].substr(pricestr[1].indexOf('.')+1, 2);
			
		//if there is a second price
		if(pricestr.length==3)
		{	
			var secondPrice = pricestr[2].substring(0, pricestr[2].indexOf('.'));
			secondPrice += pricestr[2].substr(pricestr[2].indexOf('.')+1, 2);
			
			document.write("<font class=\"PriceDifference\"><br>You Save: $"+OKStrOfPenny(parseInt(firstPrice) - parseInt(secondPrice))+"</font>");
		}
		//else return nothing
	}

function draw_options(){
	i=0;
	while (attr_label[i]!= "") { 
		if (attr_label[i]!= "U_Prod_Xlink"){
		if (attr_values[i].length > 0){
		document.write("<TR VALIGN=MIDDLE ><TD class=ProductAttrlabel >" + attr_label[i] + ":&nbsp;</TD><TD>");
		}
		if (attr_values[i].length==0){
		document.write("<INPUT TYPE=HIDDEN  NAME=\"attr_value" + (i+1) + "\" VALUE=\"\" >");
		}
		else if (attr_values[i].length==1){
		while (attr_values[i][0].indexOf("'")>0){
			attr_values[i][0]=attr_values[i][0].replace("'","&#39;");
		}
		document.write("<INPUT TYPE=HIDDEN  NAME=\"attr_value" + (i+1) + "\" VALUE='" + attr_values[i][0] + "' >");
		document.write(attr_values[i][0]);
		document.write("</TD>");
		}
		else if(attr_values[i].length >1) {
		document.write("<SELECT class=ProductAttr NAME=\"attr_value" + (i+1) + "\" onchange=return(argschange(\"" + (i+1) + "\")); >");
			for (atn=0;atn< attr_values[i].length ;atn++){
			writeoption(attr_values[i][atn],attr_values[i][atn],sel_values[i]);
			}
		document.write("</SELECT></TD>");
		}
		if (attr_values[i].length > 0){
		document.write("</TR>");
		}		
		}
		if (attr_label[i]== "U_Prod_Xlink" ){
		
		document.write("<TD><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 VALIGN=TOP ALIGN=CENTER width=600>");	
		i_rows=0;
			for (atn=0;atn< U_Prod_Xlink.length ;atn++){
				if (i_rows ==0){
				document.write("<TR><TD></TD>");
				} 
			i_rows++ ;		
			document.write("<TD VALIGN=TOP CELLPADDING=1 WIDTH =60 ALIGN=CENTER >");
				document.write("<A HREF=\"#\" onClick=return(visitargs('product.asp','dept_id=" + U_Prod_Xlink[atn].dept_id + "&pf_id=" + U_Prod_Xlink[atn].pf_id + "&'));>");
				document.write("<img ALIGN =CENTER WIDTH =60 BORDER=0  VSPACE=0  HSPACE=0  ALT=\""+ U_Prod_Xlink[atn].name +"\"");  
				document.write("SRC=\"assets/product_images/"+ U_Prod_Xlink[atn].image + " \"> </A>");
				document.write("<SMALL>"+ U_Prod_Xlink[atn].name +"</SMALL> </TD>");
				if (i_rows == 4) {
				document.write("</TR>"); 
				i_rows=0;
				} 
			}
		}	
		i++;	
	}
}
	
/* swaps to alternate views when thumb/link is moused over */
function swapImg(imgNum){
	//document.getElementById('main_prod_img').src = "assets/images/altview/pf_id_" + imgNum + "_sm.jpg";
	if(imgNum){
		document.getElementById('main_prod_image').src = "assets/images/altview/" + pf_id + "_" + imgNum + "_sm.jpg";
	}else{
		// if imgNum is not defined - switch back to the main product image
		document.getElementById('main_prod_image').src = "assets/images/altview/" + pf_id + ".jpg";
	}
	
}
	
function draw_image_1(){
	// minor change - specified image height and width to 250
	//if (product_image[0].image!=""){	
		document.write("<div style=\"height:250px;\"><A HREF=\"javascript:void(0);\" class=\"seelarger\" onClick=\"return(makeNewWindow2('assets/images/altview/" + pf_id + "','" + tmpproduct_name + "'));\" ><IMG ALT=\"" + tmpproduct_name + "\" ALIGN=\"" + product_image[0].align + "\" SRC=\"assets/images/altview/" + pf_id + ".jpg" + "\" usemap=\"#" + pf_id + "\" id=\"main_prod_image\" BORDER=\"0\"></a></div>");
	//}
}

/* displays the 'view larger image' link and each of the thumbnail rollovers for alternate views */
function draw_alt_views(){
	// display 'see larger image' link if enabled for product
	document.write("<A HREF=\"javascript:void(0);\" class=\"seelarger\" onClick=\"return(makeNewWindow2('assets/images/altview/" + pf_id + "','" +tmpproduct_name + "'));\" >See larger image and other views.</a><br/>");
	//enable_manual_quote=false;
	//if(enable_manual_quote == "1"){
		// show alt image views
		for(var i = 1; i <= 8; i++){
			var tmpPath = 'assets/images/altview/' + pf_id + '_' + i;
			document.write("<img src=\"assets/images/altview/" + pf_id + "_" + i + "_sm.jpg\" onclick=\"return(makeNewWindow2('" + tmpPath + "','" + tmpproduct_name + "_" + i + "'));\" class=\"altviewthumb\" onmouseover=\"swapImg(" + i + ");\" onmouseout=\"swapImg();\" onerror=\"altImgErr(this);\"></img>&nbsp;&nbsp;");
		}
	//}else{
		// if alternate view thumbs are not given on this product, display the main image again
	//	if (product_image[0].image!="" && product_image[0].image_show==1){
	//		document.write("<IMG ALT=\"" + product_name + "\" SRC=\"assets/images/altview/" + product_image[0].image + "\" usemap=\"#" + pf_id + "\" class=\"altviewthumb\">");
	//	}
	//}
}

/* is called when an alt view image errors out - this will hide it and affects the total count of alt views there are*/
var altviewTotal = 8;
function altImgErr(img){
	img.style.display = "none";
	altviewTotal--;
}
	
/* write info that is shown next to the main product image */
function write_prod_desc(){
	//var descLink = product_description.substring(0, product_description.indexOf("(full description)"));
	//var localLink = "product.asp?s_id=" + s_id + "&pf_id=" + pf_id + "&px_id=44&#fullDesc";
	//document.write("<p>" + descLink);
	//document.write("&nbsp;&nbsp;<a href=\"javascript:void(0);\" class=\"seelarger\" onclick=\"return visitargs('product.asp','pf_id=" + pf_id + "&px_id=44#fullDesc')\">(continued...)</a></p>");
	//document.write("&nbsp;&nbsp;<a href=\"javascript:void(0);\" onclick=\"return visitargs('','#fullDesc')\">(continued...)</a></p>");
	document.write(product_description);
}

/* writes out the local image links that appear beneath the shortened description at the top of the product page */
function write_local_links(){
	var localArr = new Array("features", "specs", "branddetail", "custreviews", "tip", "demo");	// holds the different anchor names
	var insertBrk = false;
	var localLink = "";
	var linkMarkup = "<table align=\"center\" BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n<tr><td>\n<br/>";
	for(var i = 0; i<localArr.length; i++){
		var curLink = localArr[i];
		var vidPath = "";	// the youtube video
		var vidPath2 = "";	// our hosted .html file that has the embedded flash
		
		if(curLink == "demo"){
			// grab all the necessary video path info from the single valued attributes
			for(var k = 0; k < product_attrbute.length; k++){
				if(product_attrbute[k].attname == "videol"){
					vidPath = product_attrbute[k].attvalue;
				}else if(product_attrbute[k].attname == "videoh"){
					vidPath2 = product_attrbute[k].attvalue;
				}
			}
			
			// create link for popup video if there is one specified for this product
			if(vidPath != "" && vidPath2 != ""){
				// dev: popVideo is called from popper_plight2.js
				linkMarkup += "<a href=\"#\" onclick=\"popVideo('" + vidPath + "', '" + vidPath2 + "'); return false;\">";
				linkMarkup += "<img src=\"assets/images/prod_nav_" + curLink + ".gif\" /></a>";
			}else{ // or else put in a blank link
				// JAT 2/12/08 - ** Putting in a default video path if one is not specified. - will need to remove at later time **
				linkMarkup += "<a href=\"#\" onclick=\"popVideo('/v/mdRl3dBGxRs&rel=1', 'avs4you_player'); return false;\">";
				linkMarkup += "<img src=\"assets/images/prod_nav_" + curLink + ".gif\" /></a>";
			}
			
		}else{
			localLink = "product.asp?s_id=" + s_id + "&pf_id=" + pf_id + "&#" + curLink;
			// add the a link 
			linkMarkup += "<a href=\"" + localLink + "\" onclick=\"return(visitargs('product.asp','pf_id=" + pf_id + "&','','#" + curLink + "'));\">";
			// add the image
			linkMarkup += "<img src=\"assets/images/prod_nav_" + curLink + ".gif\" /></a>";
		}
		
		// put in a break tag every 2 images
		if(insertBrk){
			linkMarkup += "<br/>\n";
			insertBrk = false;
		}else{
			insertBrk = true;
		}
	}
	linkMarkup += "</td></tr></table>";
	document.write(linkMarkup);
}

function write_prod_info(){
	//if(enable_quote != ""){
		// display specific lifestyle image for this product
	//	document.write('<img src="assets/images/lifestyle/'+ pf_id +'_ls.jpg" style="float:right;width:200px;height:200px;right;margin-left:5px;margin-bottom:5px;" />');
	//}else{
		// or else display a random lifestyle image instead..
		document.write('<p class="InfoTxt">');
		
		var theImages = new Array();
		theImages[0] = 'lifestyle_1.jpg';
		theImages[1] = 'lifestyle_2.jpg';
		theImages[2] = 'lifestyle_3.jpg';
		theImages[3] = 'lifestyle_4.jpg';
		theImages[4] = 'lifestyle_5.jpg';

		var j = 0;
		var p = theImages.length;
		var preBuffer = new Array();
		for (var i = 0; i < p; i++){
			preBuffer[i] = new Image();
			preBuffer[i].src = theImages[i];
		}
		var whichImage = Math.round(Math.random()*(p-1));
		
		//document.write('<img src="assets/images/lifestyles/'+theImages[whichImage]+'" style="float:right;width:100px;height:150px;right;margin-left:5px;margin-bottom:5px;" />');
		document.write('<img src="assets/images/'+theImages[whichImage]+'" style="float:right;width:200px;height:200px;right;margin-left:5px;margin-bottom:5px;" />');
	//}
			
	// display product description text
	document.write(product_description.length  + "</p>");
}

/* draw custom color swatches */
var showColorArr = new Array();		// store valid hex colors
var showColorIndex = new Array();	// store matching index of titles
function write_color_info(){
	// write features section first
	document.write('<tr><td style="padding:none;" class="featuresTD"><span class="InfoHeader"><a name="features">Features</a></span><br/>');
	document.write(info_attributes);
	
	document.write('</td></tr>');
	document.write('<tr><td style="border:none;height:3px;"></td></tr>');
	
	// color stuff
	// ** jat 2/19/08 - first go through colors and make sure they match up to a hex value
	var colorIndex;
	if(typeof(attr_values) != "undefined"){
		var regexp = /[0-9]/;
		// for loop to determine where in the attr_values object the colors are stored
		for(var i = 0; i<attr_values.length; i++){
			if(!(attr_values[i][0]).match(regexp)){
				colorIndex = i;
			}
		}
	
		for(var i = 0; i<attr_values[colorIndex].length; i++){
			var tmpColor = colorChart[(attr_values[colorIndex][i]).toLowerCase()];
			if(tmpColor != "" && typeof(tmpColor) != "undefined"){
				showColorArr.push(tmpColor);
				showColorIndex.push(i);
			}
		}
	}
	
	if(showColorArr.length > 0){
		document.write('<tr><td style="padding:none;" class="featuresTD"><span class="InfoHeader"><a name="colors">Color Choices</span><div class="infoPad">');
		
		

		document.write('<table style="border-collapse:collapse;border:1px solid #AABBAA;" border="1"><tr>');
		
		// generate the color title 
		for(var i = 0; i<showColorIndex.length; i++){
			document.write('<td class="colorTitle">' + attr_values[colorIndex][i] + '</td>');
		}
		document.write('</tr><tr>');
		// generate the color 'swatches' for each color
		for(var i = 0; i<showColorArr.length; i++){
			document.write('<td style="border:none;" align="center"><div class="color_div" style="background:' + showColorArr[i] + ';">&nbsp;</div></td>');
			if( (i / 7).ceil == 0){
				document.write('<br/>');	// make a new 'row' for the next 5 colors..
			}
		}
		document.write('</tr></table></div><br/></td></tr>');
		document.write('<tr><td style="border:none;height:3px;"></td></tr>');
	}
}

/* only show the size chart box if there is one for the product */
function write_size_chart(){
	if(porder_code != "" && porder_code != null && porder_code != "default"){
		document.write('<tr><td style="padding:none;" class="featuresTD"><span class="InfoHeader"><a name="sizes">Sizes</a></span><div class="infoPad">');	
		// Sizing info displayed here
	
		// show special size chart if specified
		//document.write('<a href="assets/images/size_charts/' + porder_code + '.html" target="_new">See Size Chart</a>');
		document.write('<a href="#" onclick="window.open(\'assets/images/size_charts/' + porder_code + '.html\',null,\'height=450,width=550,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=auto\');return false;" target="_new">See Size Chart</a>');
		
		// display link to default size chart
		//document.write('<a href="sizechart.htm" target="_new">See Size Chart</a>');
		
		document.write('</div><br/></td></tr>');
		document.write('<tr><td style="border:none;height:3px;"></td></tr>');
	}
}

/* specifications part 2 */
function write_prod_specs(){
	document.write('<tr><td style="padding:none;" class="featuresTD"><span class="InfoHeader"><a name="specs">Specifications</a></span><div class="infoPad">');	

	// display any product details that are not part of the features section (above)
	for(var i = 0; i < product_attrbute.length; i++){
		if(product_attrbute[i].attname.indexOf('Features') == -1 && ((product_attrbute[i].attname).toLowerCase() != "tagline") && ((product_attrbute[i].attname.toLowerCase()) != "video")){
			document.write('<span class="product_attrbute_info">' + product_attrbute[i].attname + ':</span> ' + product_attrbute[i].attvalue + '<br/>');
		}
	}
	
	document.write('</div><br/></td></tr>');
	document.write('<tr><td style="border:none;height:3px;"></td></tr>');
}
var pcode = "";
function write_brand_details(){
	// we will grab brand details from brandDetails object which is defined in brand_details.js (similar to how colors are shown on the page)
	pcode = pvendor_code.toUpperCase();
	
	if(brandDetails[pcode]){
		pcode = brandDetails[pcode];	//hold the vendor code
		
		document.write('<tr><td><span class="InfoHeader"><a name="branddetail">Brand Details: ' + pcode.name + '</a></span><br/><p class="brandDetailP">');
		document.write('<img src="assets/images/brand/' + pvendor_code + '.jpg" alt="' + pvendor_code + ' logo" align="right" class=\"brandImg\"/>');
		document.write(pcode.info);
	}else{
		document.write('<tr><td><span class="InfoHeader"><a name="branddetail">Brand Details</a></span><br/><p class="brandDetailP">');
	}
	
	// draw_rating_data() is defined in assets/images/includes/plight_product_review_include.js
	document.write('</p><br/></td></tr>');
	document.write('<tr><td style="border:none;height:3px;"></td></tr>');
}

function write_review_info(){
	document.write('<tr valign="top"><td><a name="custreviews">&nbsp;</a>');
	document.write("<span class=\"InfoHeader\">Customer Reviews</span><div class=\"infoPad\">");
	plight_draw_review_data();	// called from product rating js file
	// JAT 8/21/08 - Adding Write - A - Review Promo text
	document.write("<br/><span style=\"font-weight:bold;font-size:13px;\">Write a Review - Receive a discount</span>");
	
	document.write('</div><br/></td></tr>');
}

function write_slogan(){
	var prodattrlen = product_attrbute.length;
	for(var i = 0; i < prodattrlen; i++){
		if((product_attrbute[i].attname).toLowerCase() == "tagline"){
			document.write("<br/><span class=\"slogan\">" + product_attrbute[i].attvalue + "</span><br/>");
		}
	}
}

function write_tips(){
	document.write('<tr><td style="border:none;height:3px;"><a name=\"tip\"></a></td></tr>');
	document.write("<tr valign=\"top\"><td style=\"\"><span class=\"InfoHeader\">PackingLight Tips</span>&nbsp;&nbsp;&nbsp;&nbsp;");
	document.write("<a href=\"#\" onclick=\"displayPreviousTip();return false;\"><< See Previous Tip</a>&nbsp;&nbsp;&nbsp;");
	document.write("<a href=\"#\" onclick=\"displayNextTip();return false;\">See Next Tip >></a><br/><br/>");
	document.write("<div id=\"tipsDiv\">");
	displayRandomTip();	// called from tip.js
	document.write("</div><br/>");
	document.write('</td></tr>');
}

/* JAT 2/19/08 - overriding this for product page only. Need to add in anchors at end of url */
function visitargs(PageURL,args,SURL,anchor)
{
	anchor = (anchor)? anchor : "";
	
	var storeIdClause='?';
	var argsLowerCase=args.toLowerCase();
	var i=0;
	var trailingAmpersandCount=0;

	//if s_id isn't in the args, prepare it for addition
	//gets us "?s_id=nnn" for use as the first URL arg
	if(argsLowerCase.indexOf('s_id')<0 && argsLowerCase.indexOf('s%5fid')<0)
		storeIdClause += 's_id='+i_jscript_uu_sid;

	//gets use "example.asp?" or "example.asp?sid=nnn"
	PageURL += storeIdClause;
	if (args.length>0)
	{
		//at this point, PageURL will end a "?" or a "s_id=nnn" so...
		//if PageURL doesn't end in a "?" and args doesn't start with one
		//we need an initial amp
		var initialamp = (('?' == PageURL[PageURL.length-1]) && ('@' == args[0]))? '' : '&';
		PageURL += initialamp + args;
	}
	//watch out for alots of amps at the end of PageURL
	for(i=PageURL.length-1;i>=0;i--)
	{
		if (PageURL.charAt(i)=='&')
		{
			trailingAmpersandCount++;
		}
		else
		{
			break;
		}
	}
	//wipe out any trailing amps in PageURL
	PageURL=PageURL.substring(0,PageURL.length-trailingAmpersandCount)
    
	if (user_guid !=' ')
		PageURL =PageURL + '&mscssid='+ user_guid;

	if (SURL=='SURL')
		location = i_jscript_uu_rootSURL+PageURL+anchor;
	else
		location = i_jscript_uu_rootURL+PageURL+anchor;

	return(false);	
}