/*** COPYRIGHT 2009  BY CUTSEY BUSINESS SYSTEMS LTD. - ALL RIGHTS RESERVED ***/
/* product-review.js -- Product Page Javascript - User Reviews               */
/*****************************************************************************/
/* 020001 01/31/10 JD2- Move review helpful section under each user review.  */
/* 020000 01/19/10 JD2- Review title and helpful changes.                    */
/* 010001 06/08/09 SB - Reset the semaphore in the callback.                 */
/* 010000 04/07/09 SB - Created.                                             */
/*****************************************************************************/

var vPRbusy = false;
var vPRvisible = true;

function loadReviews(vProd) {
  if (vPRbusy) return;
  vPRbusy = true;

  var vdate = new Date();
  var vSC = "?sponsor=" + fdmSponsor + "&nocache="
    + (((vdate.getHours() * 60) + vdate.getMinutes()) * 60)
    + vdate.getSeconds();

  $.ajax({
    type: "GET",
    url: hostDomain + appPath + "/b2c/product-review-ajax.w" + vSC,
    dataType: "xml",
    data: ({product: vProd}),
    complete: function(data) {
      var vReview = $(data.responseXML).find("product_reviewRow");
      var str = "";
      var mean = new Array();

      $.each(vReview,function(i) {
        mean.push(parseInt($(vReview[i]).attr("rating")));

        str += "<p>Rated: " + $(vReview[i]).attr("rating");
        str += "<br>User: " + $(vReview[i]).attr("user-id");
        str += "<br>Review status: " + $(vReview[i]).attr("review_status");
        str += "<br>Date: " + $(vReview[i]).attr("created_date");
        str += " " + $(vReview[i]).attr("created_time");
        str += "<br>Comment:<br>" + $(vReview[i]).attr("comment") + "</p>";
      });

      var avg = 0;
      for (var i=0; i<mean.length; i++) avg += mean[i];
      avg /= mean.length;

      if (isFinite(avg)) $("#reviewAverage").html(
        "<h3>Overall customer rating is: " + avg.toFixed(1) + "</h3>"
      );

      $("#userComments").html(str);

      if (vPRvisible = !vPRvisible) {
        $("#prodReview").slideUp("slow");
      } else {
        $("#reviewAverage").show();
        $("#userComments").show();
        $("#reviewForm").show();
        $("#prodReview").slideDown("slow");
      }

      vPRbusy = false;
    }
  });
}

function postReview(vProd,vMode) {
  if (vPRbusy) return;

  var vdate = new Date();
  var vSC = "?sponsor=" + fdmSponsor + "&nocache="
    + (((vdate.getHours() * 60) + vdate.getMinutes()) * 60)
    + vdate.getSeconds();

  for (var i=0; i<document.prodReview.rating.length; i++) {
    if (document.prodReview.rating[i].checked)
      var score = document.prodReview.rating[i].value;
  }

  if (score==undefined || score=="") {
    alert("Please submit a rating with your comment.");
    vPRbusy = false;                                               /* 020000 */
    return;
  }
  /******v 020000 v******/

  var vReviewTitle = $("#reviewTitle").val();
  var vReviewComment = $("#prodrevtxt").val();
  if (vReviewTitle.indexOf("Type review title here.") >=0 || vReviewTitle == "") {
    alert("Please enter a review title.");
    vPRbusy = false;
    return;
  }
  if (vReviewComment.indexOf("Type review here.") >=0 || vReviewComment == "") {
    alert("Please enter a review.");
    vPRbusy = false;
    return;
  }
  /******^ 020000 ^******/

  vPRbusy = true;                                                  /* 010001 */

  $.post(hostDomain + appPath + "/b2c/product-review-ajax.w" + vSC, {
    rating: score,
    comment: vReviewComment,                                       /* 020000 */
    title: vReviewTitle,                                           /* 020000 */
    mode: vMode,                                                   /* 020000 */
    product: vProd
  }, function(responseData) {                                      /* 020000 */

    for (var i=0; i<document.prodReview.rating.length; i++) {
      document.prodReview.rating[i].checked = false;
    }
    document.prodReview.prodrevtxt.value = "";

    $("#reviewForm").html(
      "<hr><p><b>Thank you, your comments have been"
      + " submitted for review and posting.</b></p>"
    );

    vPRbusy = false;        
  },"xml");                                                        /* 020000 */
}

/******v 020000 v******/

function postHelpful(vMode,vReviewHelpful,vRowId,vCookieName,vCookieDomain) { /* 020001 */ 
  if (vPRbusy) return;

  var vdate = new Date();
  var vSC = "?sponsor=" + fdmSponsor + "&nocache="
    + (((vdate.getHours() * 60) + vdate.getMinutes()) * 60)
    + vdate.getSeconds();

  vPRbusy = true;     
  
  /******v 020001 v******/
  
  var vRowIdList = getCookie(vCookieName);

  if (vRowIdList.indexOf(vRowId) >= 0) {
     alert("You have already submitted feedback for this review.");
     vPRbusy = false; 
     return;
  }
  else {
     if (vRowIdList == "") vRowIdList = vRowId;
     else vRowIdList += "," + vRowId;
     setCookie(vCookieName,vRowIdList,null,"/",vCookieDomain,null);
  }
  
  /******^ 020001 ^******/

  $.post(hostDomain + appPath + "/b2c/product-review-ajax.w" + vSC, {                   
    helpful: vReviewHelpful,                                                                 
    mode: vMode,                                           
    reviewRowId: vRowId
  }, function(responseData) {                                

    $("#reviewHelpful" + vRowId).html(                             /* 020001 */
      "<p><b>Thank you for your feedback.</b></p>"                 /* 020001 */
    );

    vPRbusy = false;        
  },"xml");
}

/******^ 020000 ^******/

