/*** COPYRIGHT 2009  BY CUTSEY BUSINESS SYSTEMS LTD. - ALL RIGHTS RESERVED ***/
/* product-review.js -- Product Page Javascript - User Reviews               */
/*****************************************************************************/
/* 010000 12/17/09 SB - Ninja Created.                                       */
/*****************************************************************************/

var vPRbusy = false;

function loadReviews(vProd,cnt) {
  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")));

        if (cnt==undefined || i < cnt) {
            for (var j=0; j < $(vReview[i]).attr("rating"); j++)
                str += '<img src="images/Reviews_03.png" />';
            str += "<br />" + $(vReview[i]).attr("comment");
            str += '<div class="user"><b>' + $(vReview[i]).attr("user-id").replace("GUEST-","") + "</b>";
            str += " on: " + $(vReview[i]).attr("created_date") + "</div><br />";
        }
      });

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

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

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

      vPRbusy = false;
    }
  });
}

function valid_email(v){                                           /* E010004 */
  var p = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
  return v.length > 0 && p.test(v);
}

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

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

  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.");
    return;
  }

  /*vvv E010000 vvv*/

  if (document.prodReview.prodrevtxt.value == '') {
    alert("Please submit a comment with your rating.");
    return;
  }

  var gid = document.prodReview.uname.value.replace(/^\s*|\s*$/,"");

  if (gid == '') {
    alert("Please include an ID to post with your review.");
    document.prodReview.uname.focus();
    return;
  }

  id += gid;                                                     /* E010002 */

  /*^^^ E010000 ^^^*/

  /*vvv E010003 vvv*/

  var eml = document.prodReview.emailaddr.value.replace(/^\s*|\s*$/,"");

  if (!valid_email(eml)) {                                         /* E010004 */
    alert("Please include a valid email address with your review.");
    document.prodReview.emailaddr.focus();
    return;
  }

  /*^^^ E010003 ^^^*/

  vPRbusy = true;                                                  /* 010001 */

  $.post(hostDomain + appPath + "/b2c/product-review-ajax.w" + vSC, {
    rating: score,
    comment: document.prodReview.prodrevtxt.value,
    product: vProd,
    userid: id,                                                    /* E010000 */
    email: eml                                                     /* E010003 */
  }, function() {
    for (var i=0; i<document.prodReview.rating.length; i++) {
      document.prodReview.rating[i].checked = false;
    }
    document.prodReview.uname.value = "";
    document.prodReview.emailaddr.value = "";
    document.prodReview.prodrevtxt.value = "";

    $("#reviewForm").html(
        '<h2 class="blue">Product Review</h2>'
        + '<div class="prCaveat"><b>Thank you, your comments have been submitted'
        + ' for review and posting.</b></div>'
        + '<br /><br />'
        + '<div>'
        + '<a class="addInfo" href="review-read.html">- Read Reviews -</a>'
        + '&nbsp;&nbsp;&nbsp;'
        + '<a class="addInfo" href="products.html">- Back to the Product Page -</a>'
        + '</div>'
    );

    vPRbusy = false;
  });
}

