////////////////////////////////
// Product Reviews Functions  //
////////////////////////////////
//global vars for star images

if(typeof review_scale != "undefined"){
	var img_src = review_scale.image_path;
	if (img_src == "")
	{
		img_src = "assets/images/stars/search/star_search_full.gif";
	}
	var off_img_src = "assets/images/stars/search/star_search_point0.gif";
	var img_src2 = "assets/images/stars/search/star_search_full.gif";
	var off_img_src2 = "assets/images/stars/search/star_search_point0.gif";
	var num_stars = review_scale.scale_item_count
	var image_decimal2 = "assets/images/stars/search/star_search_point";
	var image_decimal = "assets/images/stars/search/star_search_point";
}

function draw_rating_data() {
	if (review_settings.use_ratings) {
		if (product_reviews.review_count > 0) {
			var tmp = "";
			document.write("<p>");
      //check for a rating image
      var floorRating = Math.floor(product_reviews.average_rating); //get the whole number for num images to show
      var rndRating = Math.round(product_reviews.average_rating*10)/10; //get the 1 decimal place number to show
      tmp = draw_static_stars2(rndRating);
      document.write(tmp);
      document.write("<b>"+rndRating+"</b> out of 5");
      document.write("<span class='note'> ("+product_reviews.review_count+" reviews )</span></p>");
    } else {
      //nothing, use if want content for no reviews
  	}
	}
}

function draw_review_data() {
	//draw product review area if review is available
	if(typeof product_reviews != "undefined") {  
		var floorRating;
		var rndRating;
		var tmp = "";
	
		document.write("<table class=\"copy\" cellpadding=\"2\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
		document.write("<tr><td colspan=\"2\"><a href=\"#\" onClick=\"toggle(1); return false\" >Write a Review</a></td></tr>");
		if(product_reviews.review_items.length>0) {
			document.write("<tr><td colspan=\"2\"><h2>Current Reviews</h2></td></tr>");
		}
		for (i=0;i<product_reviews.review_items.length;i++) {
			floorRating = Math.floor(product_reviews.review_items[i].review_rating); //get the whole number for num images to show
			rndRating = Math.round(product_reviews.review_items[i].review_rating*10)/10; //get the 1 decimal place number to show
	
			document.write("<tr><td>");
			//draw lit up stars representing the whole number achieved
			document.write("<div class=\"reviewStars\">"+draw_static_stars(rndRating)+"</div>");
	
			document.write("&nbsp;&nbsp;<b>"+rndRating+" out of " + num_stars + "</b></td>");
	
			document.write("<td>Review By: " + product_reviews.review_items[i].reviewer_name);
			if (product_reviews.review_items[i].value2 != "") {
				document.write("</b> from <b>" + product_reviews.review_items[i].value2)
			}
			document.write("</b></p></td></tr>");
	
	
			document.write("<tr valign=\"top\"><td colspan=2><h3>" + product_reviews.review_items[i].review_subject + "</h3></td></tr>");
			document.write("<tr valign=\"top\"><td colspan=2><p>");
			if (product_reviews.review_items[i].value1.length > 250) {
				document.write(product_reviews.review_items[i].value1.substr(0,250) + "...<br>(<a href=\"product_reviews.asp\" onclick=\"return(visitargs_newwin('product_reviews.asp','pf_id="+pf_id+"&dept_id="+dept_id+"#"+ product_reviews.review_items[i].review_id +"','URL','height=600,width=1000,scrollbars=yes,resizable=yes'));\" class=revnormal>Read Full Review</a>)</p>");
			} else {
				document.write(product_reviews.review_items[i].value1 + "...<br>(<a href=\"product_reviews.asp\" onclick=\"return(visitargs_newwin('product_reviews.asp','pf_id="+pf_id+"&dept_id="+dept_id+"#"+ product_reviews.review_items[i].review_id +"','URL','height=600,width=1000,scrollbars=yes,resizable=yes'));\" class=revnormal>Read Full Review</a>)</p>");
			}
			document.write("</td></tr>");
			document.write("<tr valign=\"top\"><td colspan=2><div class=\"review_line\" style=\"height:1px; width:100%;\"><img src=\"assets/images/spacer.gif\" height=\"1\"></div></td></tr>");
			
		}//end for
		
		if(product_reviews.review_count>product_reviews.review_items.length){
			document.write("<tr valign=\"middle\"><td colspan=2 align=center class='revhead'><a href=\"product_reviews.asp\" onclick=\"return(visitargs_newwin('product_reviews.asp','pf_id="+pf_id+"&dept_id="+dept_id+"','URL','height=575,width=475,scrollbars=yes,resizable=yes'));\">View All Reviews</a></td></tr>");
		}
		document.write("</table>");
	}//end if review available
}

//function to draw star images that represent a static rating value.
function draw_static_stars(whole_num) {
    var tempStr = "";
    var whole_string = whole_num.toString();
    var num_lit = "";
    var after_decimal = "";
	if (whole_string.indexOf(".")!="-1") {
		num_lit = whole_string.substring(0,whole_string.indexOf("."));
	}
	else
	{
		num_lit = whole_string;
	}
	
	if (whole_string.indexOf(".")!="-1")
	{
		after_decimal = whole_string.substring(whole_string.indexOf(".")+1);
	}
   var my_num_lit = parseInt(num_lit);
   
    //draw lit up stars representing the whole number achieved
    for (j=1;j<=num_lit;j++)
    {

        tempStr += "<img src=\"" + img_src + "\" border=0 alt=\"\" />";
    }
    
    if (after_decimal!="")
    {
	my_num_lit = my_num_lit+1;
	tempStr += "<img src=\"" + image_decimal + after_decimal + ".gif\" border=0 alt=\"\" />";
    }

    //draw greyed out stars
    for (j=num_stars;j>my_num_lit;j--)
    {
        tempStr += "<img src=\"" + off_img_src + "\" border=0 alt=\"\" />";
    }

    return tempStr;
}

//function to draw star images that represent a static rating value.
//uses second set of stars with a different matte color so they look good on colored background.
function draw_static_stars2(whole_num)
{
   var tempStr = "";
    var whole_string = whole_num.toString();
    var num_lit = "";
    var after_decimal = "";
    

	if (whole_string.indexOf(".")!="-1")
	{
		num_lit = whole_string.substring(0,whole_string.indexOf("."));
	}
	else
	{
		num_lit = whole_string;
	}
	
	if (whole_string.indexOf(".")!="-1")
	{
		after_decimal = whole_string.substring(whole_string.indexOf(".")+1);
	}
	var my_num_lit = parseInt(num_lit);
	
    //draw lit up stars representing the whole number achieved
    for (j=1;j<=my_num_lit;j++)
    {

        tempStr += "<img src=\"" + img_src2 + "\" alt=\"\" />";
    }
    
     if (after_decimal!="")
    {
	my_num_lit = my_num_lit+1;
	tempStr += "<img src=\"" + image_decimal2 + after_decimal + ".gif\" alt=\"\" />";
    }

    //draw greyed out stars
    for (j=num_stars;j>my_num_lit;j--)
    {
        tempStr += "<img src=\"" + off_img_src2 + "\" alt=\"\" />";
    }

    return tempStr;
}

function draw_full_review_data()
{
  //draw product review area
    var floorRating;
    var rndRating;
    var tmp = "";
    document.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
    for (i=0;i<product_reviews.review_items.length;i++)
    {

        document.write("<tr valign=\"top\"><td colspan=2>&nbsp;</td></tr>");
        //subject and name line
        document.write("<tr valign=\"top\"><td align=left width=\"50%\"><a name="+product_reviews.review_items[i].review_id+"></a><span class=revbold>" + product_reviews.review_items[i].review_subject + "</span></td>");
        document.write("<td align=\"center\"><span class=revbold>" + product_reviews.review_items[i].reviewer_name);
        if (product_reviews.review_items[i].value2 != "")
        {
            document.write(" from " + product_reviews.review_items[i].value2)
        }
        document.write("</span></td></tr>");

        //individual ratings data
        document.write("<tr valign=\"middle\"><td><table>");

        for (indx=4;indx<=10;indx++)
        {
            tmplbl = eval("review_labels.field_label"+indx);
            if (tmplbl != "")
            {
                tmpfloor = eval("product_reviews.review_items["+i+"].value" + indx)
                document.write("<tr valign=\"middle\"><td nowrap width=\"50%\"><span class=\"revlbl\">" + tmplbl + "</span></td><td nowrap>");
                tmp = draw_static_stars(tmpfloor);
                document.write (tmp);
                document.write("</td></tr>");
            }

        }

        //overall rating
        floorRating = Math.floor(product_reviews.review_items[i].review_rating); //get the whole number for num images to show
        rndRating = Math.round(product_reviews.review_items[i].review_rating*10)/10; //get the 1 decimal place number to show
        document.write("</table></td><td valign=middle align=center><table width=\"100%\" border=0><tr><td nowrap align=center><span class=\"revbold\">Overall Rating</span></td></tr>");
        document.write("<tr><td align=center><table><tr><td class=\"bluecirc\">" + rndRating + "</td></tr></table></td></tr>");

        document.write("<tr><td nowrap valign=\"middle\" align=center>");
        tmp = draw_static_stars(rndRating);
        document.write(tmp);

        document.write("</td></tr></table></td></tr>");



        document.write("<tr valign=\"top\"><td colspan=2><span class=revnormal><br>");
        document.write(product_reviews.review_items[i].value1 + "</span>");

        document.write("</td></tr>");
        document.write("<tr valign=\"top\"><td colspan=2 align=left><div class=\"review_line\" style=\"height:1px; width:100%;\"><img src=\"assets/images/spacer.gif\" height=\"1\"></div></td></tr>");



    }
    document.write("<tr valign=\"middle\"><td colspan=2 align=right>");
    draw_rev_spanner();
    document.write("</td></tr>");
    document.write("</table>");
}

var arrFields = new Array();
function draw_review_form()
{
  if(typeof review_labels != "undefined"){	
    if (user_guid.length > 5 && user_email.indexOf('AUTO_') == -1 )
    {
		var tempFloor = 0;
        var tempRnd = 0;
        var tmp = "";
        document.write("<form name=review_submit id=review_submit method=post action=\"" + i_jscript_uu_rootURL + "post_review.asp\">");
        document.write("<input type=hidden name=\"s_id\" id=\"s_id\" value=\"" + i_jscript_uu_sid + "\">");
        document.write("<input type=hidden name=\"mscssid\" id=\"mscssid\" value=\"" + user_guid + "\">");
        document.write("<input type=hidden name=\"pf_id\" id=\"pf_id\" value=\"" + pf_id + "\">");
        document.write("<input type=hidden name=\"dept_id\" id=\"dept_id\" value=\"" + dept_id + "\">");
        document.write("<input type=hidden name=\"validate\" id=\"validate\" value=\"1\">");
        document.write("<input type=hidden name=\"use_form\" id=\"use_form\" value=\"1\">");
        document.write("<input type=hidden name=\"post_rating\" id=\"post_rating\" value=\"\">");

        document.write("<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\"><tr><td colspan=2>");
        document.write("<span class=\"revhead\">Please rate this product in each category.</span> <span class=\"revtxt\">(required)</span></td></tr>");
        document.write ("<tr><td colspan=2><table cellpadding=0 cellspacing=0 border=0><tr><td width=\"50%\"><table width=\"100%\">");

        //array to hold the individual rating field names
        for (i=4;i<=10;i++)
        {  
            if (eval("review_labels.field_label"+i) != "")
            {
                document.write("<input type=hidden name=\"post_value" + i + "\" id=\"post_value" + i + "\" value=0 />");
                document.write("<input type=hidden name=\"post_value" + i + "_M\" id=\"post_value" + i + "_M\" value=1 />");
                arrFields[arrFields.length] = "post_value" + i;
                document.write("<tr valign=\"middle\"><td nowrap width=\"50%\"><span class=\"revlbl\">" + eval("review_labels.field_label"+i) + "</span></td><td nowrap>");
                for (j=1;j<=num_stars;j++)
                { 
                    document.write("<a href=\"#\" onclick=\"return false;\"><img src=\"" + off_img_src + "\" name=\"post_value" + i + "_" + j + "\" border=0 id=\"post_value" + i + "_" + j + "\" onclick=\"setImgs('post_value" + i + "', " + j + ");setRating('post_value" + i + "', " + j + ");\" /></a>");
                }
                document.write("</td></tr>");
            }else{
                document.write("<input type=hidden name=\"post_value" + i + "\" id=\"post_value" + i + "\" value=\"\" />");
            }

        }
        document.write("</table></td><td valign=middle align=center><img src=\"assets/images/vertrule.gif\" border=0 alt=\"\" /></td>");
        document.write("<td valign=middle align=center><table width=\"100%\" border=0><tr><td nowrap align=center colspan=2><span class=\"revhead\">YOUR OVERALL RATING</span></td></tr>");
        document.write("<tr valign=middle><td align=center width=80><table><tr valign=middle><td class=\"bluecirc2\" id=\"bluecirc2TD\">0</td></tr></table></td>");

        document.write("<td nowrap valign=\"middle\" align=center id=\"starsTD\">");
        tmp = draw_static_stars(0);
        document.write(tmp);
        document.write("</td></tr></table></td></tr></table></td></tr>");
        document.write("<tr valign=\"top\"><td colspan=2 align=center><div class=\"review_line\" style=\"height:1px; width:100%;\"><img src=\"assets/images/spacer.gif\" height=\"1\"></div></td></tr>");
        document.write("<tr valign=\"top\"><td nowrap width=\"50%\"><span class=\"revhead\">" + review_labels.subject_label + "</span> <span class=\"revtxt\">(one line summary)</span><br />");
        document.write("<textarea name=\"post_subject\" id=\"post_subject\" rows=4 cols=18></textarea></td>");
        document.write("<td><span class=\"revhead\">Review Guidelines</span><br />");
        document.write("<span class=\"revtxt\">- Be specific<br />- Stay relevant to the product<br />- Base review on personal experience<br />- Do not list personal information<br />- Please refrain from inappropriate<br />language and comments about other <br />retailers</span></td></tr>");

        document.write("<tr valign=\"top\"><td nowrap colspan=2><span class=\"revhead\">" + review_labels.field_label1 + "</span> <span class=\"revtxt\">(500 character limit)</span><br />");
        document.write("<textarea name=\"post_value1\" id=\"post_value1\" rows=6 cols=45></textarea></td></tr>");
        document.write("<tr valign=\"top\"><td nowrap colspan=2><table width=\"100%\"><tr><td><span class=\"revhead\">Nickname</span> <span class=\"revtxt\">(optional)</span><br />");
        document.write("<input type=text name=\"display_name\" id=\"display_name\" size=16 maxlength=100 /></td>");
        document.write("<td><span class=\"revhead\">" + review_labels.field_label2 + "</span> <span class=\"revtxt\">(optional)</span><br />");
        document.write("<input type=text name=\"post_value2\" id=\"post_value2\" size=16 maxlength=100 /></td>");
        document.write("<td><span class=\"revhead\">" + review_labels.field_label3 + "</span> <span class=\"revtxt\">(optional)</span><br />");
        document.write("<input type=text name=\"post_value3\" id=\"post_value3\" size=16 maxlength=100 /></td></tr></table></td></tr>");

        //hidden input fields needed for the review
        document.write("<input type=hidden name=\"firstname\" id=\"firstname\" value=\"" + user_firstname + "\" />");
        document.write("<input type=hidden name=\"lastname\" id=\"lastname\" value=\"" + user_lastname + "\" />");
        document.write("<input type=hidden name=\"email_address\" id=\"email_address\" value=\"" + user_email + "\" />");
        document.write("<input type=hidden name=\"show_name\" id=\"show_name\" value=1 />");
        document.write("<input type=hidden name=\"show_email\" id=\"show_email\" value=0 />");

        document.write("<tr><td colspan=\"2\" align=center><table width=\"100%\"><tr><td align=left><span class=\"revtxt\">By submitting this review you are confirming that you agree to our ");
        document.write("<a href=# onclick=\"return(visitargs_newwin('text_4.asp','','URL','height=575,width=475,scrollbars=yes,resizable=yes'));\">Rating and Review Policy</a>.</span></td><td>");
        document.write("<input type=image src=\"assets/images/submit_rev.jpg\" value=\"Submit Review\" onclick=\"return(check_form());\"></td></tr></table></td></tr>");
        document.write("</table></form>");
    }else{
        document.write("You must be logged in as an Advantage Club member in order to write a review. Click <a href=shopper_lookup.asp onclick=\"return(visitargs('shopper_lookup.asp','target=product.asp&pf_id="+pf_id+"&dept_id="+dept_id+"','URL'));\">here</a> to login or register.");
    }
  }//end if review_labels != undefined
}


function check_form()
{
    var showname = document.getElementById('show_name');
    var dispname = document.getElementById('display_name').value;
    showname.value = (dispname.length > 0) ? "1" : "0";

    var numchars = document.getElementById('post_subject').value.length;
    if (numchars > 100)
    {
        alert ("You have entered " + numchars + " characters in the " + review_labels.subject_label + " field. The limit is 100. Please edit your entry before submitting.");
        return false;
    }

    var numchars = document.getElementById('post_value1').value.length;
    if (numchars > 500)
    {
        alert ("You have entered " + numchars + " characters in the " + review_labels.field_label1 + " field. The limit is 500. Please edit your entry before submitting.");
        return false;
    }

    var strRequiredFlds = "post_value4,post_value5,post_value6,post_value7"
    var strLabels = review_labels.field_label4 + "," + review_labels.field_label5 + "," + review_labels.field_label6 + "," + review_labels.field_label7;
    arrReqFlds = strRequiredFlds.split(",");
    arrReqLbls = strLabels.split(",");
    var ret_message = "You must provide/select a value for the following field(s):\n";
    var boolRet = false;

    for (i=0;i<arrReqFlds.length;i++)
    {
        if (document.getElementById(arrReqFlds[i]).value == "" || document.getElementById(arrReqFlds[i]).value == 0)
        {
            boolRet = true;
            ret_message += arrReqLbls[i] + "\n";
        }

    }

    if (boolRet)
    {
        alert(ret_message);
        return false;
    }else{
        return true;
    }

}


//val will always be a value 1-5
//field is the name of the field that has to be changed
function setRating(field, val)
{
    //var top_val = arrFields.length * num_stars;
    var sum_val = 0;
    var p_rating = document.getElementById('post_rating');
    var v_field = document.getElementById(field);
    v_field.value = val;

    for (k=0;k<arrFields.length;k++)
    {
        sum_val += parseFloat(document.getElementById(arrFields[k]).value);
    }

    p_rating.value = sum_val / arrFields.length;
    document.getElementById('bluecirc2TD').innerHTML = Math.round(p_rating.value*10)/10
    document.getElementById('starsTD').innerHTML = draw_static_stars(Math.round(p_rating.value*10)/10);
}

function setImgs(group, counter)
{
    for (i=1;i<=counter;i++)
    {
        document.getElementById(group+"_"+i).src = img_src;
    }
    for (i=num_stars;i>counter;i--)
    {
        document.getElementById(group+"_"+i).src = off_img_src;
    }
    return true;
}

function visitargs_newwin(PageURL,args,SURL,winargs)
{
  if (user_guid !=' ')
  {
       PageURL =PageURL + '?s_id='+i_jscript_uu_sid+'&mscssid='+ user_guid+ '&'+ args;
  }else{
       PageURL =PageURL + '?s_id='+i_jscript_uu_sid+'&'+ args ;
  }
  if (SURL=='SURL')
  {
    var newWin = window.open(i_jscript_uu_rootSURL+PageURL,'NewWindow',winargs);
  }else{
    var newWin = window.open(i_jscript_uu_rootURL+PageURL,'NewWindow',winargs);
  }
  if(newWin) newWin.focus();
  return(false);
}

function limitText(limitField, limitNum)
{
	var fld = document.getElementById(limitField);

	if (fld.value.length > limitNum) {
		fld.value = fld.value.substring(0, limitNum);
	}
	return;
}

function draw_rev_spanner()
{
    if (typeof currPage != "undefined" && typeof numPages != "undefined")
    {
        if (numPages > 1)
        {
            document.write ("&nbsp;&nbsp;&nbsp;<span class=revlinks id=SpannerLinks>Page:&nbsp;");

            for (i=1;i<=numPages;i++)
            {
                if (i != currPage)
                {
                    document.write ("&nbsp;<a href=\"product_reviews.asp\" onclick=\"return(visitargs('product_reviews.asp','pf_id=" + pf_id + "&dept_id=" + dept_id + "&currPage=" + i + "','URL'));\">" + i + "</a>");
                }else{
                    document.write ("&nbsp;" + i);
                }
            }
        }
    }
}

function checkSubmit()
{
    if (window.location.search.indexOf("rev_submitted=1") != -1)
    {
        var sub_pop_fill = "";
		sub_pop_fill += "<table width=200 cellpadding=0 cellspacing=2 border=0 class=\"outermostpop\">";
        sub_pop_fill += "    <tr><td><table cellpadding=2 width=200 cellspacing=2 border=0 class=\"outerpop\">";
        sub_pop_fill += "        <tr><td align=center><table cellpadding=10 width=\"200\" cellspacing=0 border=0 class=\"innerbg\">";
        sub_pop_fill += "            <tr><td align=center><span class=revhead>Thank you!</span></td></tr>";
        sub_pop_fill += "            <tr><td>";
        sub_pop_fill += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">";
		sub_pop_fill += "<tr><td id=\"info_box_no_bottom\"><table cellpadding=\"3\" cellspacing=\"1\" border=\"0\" width=\"100%\">";
		sub_pop_fill += "<tr bgcolor=\"#FFFFFF\"><TD class=revbold align=center>";
		sub_pop_fill += "Your product review has been received. It will be posted to the website upon approval.";
		sub_pop_fill += "</td></tr>";
		sub_pop_fill += "</table></td></tr></table>";
		sub_pop_fill +='<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';
		sub_pop_fill +='<td valign="top" width="8" align="left"><img src="Assets/images/info_box/info_box_higher_bottom_left2.gif" /></td><td class="info_box_higher_bottom2" align="center">';
		sub_pop_fill += "<a href=\"product.asp\" onClick=\"document.getElementById('subpop').style.visibility = 'hidden';return false;\">";
		sub_pop_fill += "<img src=\"assets/images/continue_shopping.gif\" alt=\"click to return to the catalog\" border=\"0\"></a>";
        sub_pop_fill += '</td><td valign="top" width="7" align="right"><img src="Assets/images/info_box/info_box_higher_bottom_right2.gif" />';
		sub_pop_fill += "</td></tr></table></td></tr></table>";
		
        sub_pop_fill += "      </td></tr>";
        sub_pop_fill += "  </table></td></tr></table>";
        document.getElementById("subpop").innerHTML = sub_pop_fill;		
	}else{
        document.getElementById("subpop").style.height = 0;
    }
}


function draw_image_2_review(){
	var pf_to_use = pf_id;
	if(product_image[0].image !="" && product_image[0].image.indexOf("t.jpg")==-1){
		pf_to_use = product_image[0].image;
		
	}
	draw_S7_image(pf_to_use, "s7product", product_name, "mainimage", null);
}