﻿jQuery(document).ready(function(){
    // Show link video popup an window when page loaded.
    jQuery('#relateVideo').css({"display": "block"});   
    
    // save comment when user want send contact 
    jQuery('#btnSaveComment').click(function() {
        var Product = jQuery('#ProductId').val();            
        var Comment = jQuery("#txtComment").val();
        
        if(Comment!=undefined && Comment!='')
        {       
            var win = confirm("Do you really want to submit this feedback?");
            if(win)
            {
                jQuery.ajax({
                    url: '/Customer/SaveComment',
                    type: 'POST',
                    dataType: 'json',
                    data: { productId: Product, comment: Comment
                    },
                    success: function(data) {
                        alert("Thank you! JUST A REMINDER...We read all submissions but regrettably cannot reply.");
                        //Remove text on textarea.
                        jQuery("#txtComment").val('');
                    }
                });
            }
        }
        return false;
    });
    
    // When user click show all items.
    jQuery('#showallreview').click(function() {
        var productid = jQuery('#ProductId').val();
        var itemCount =jQuery('#itemCount2').val();        
        var selectid = jQuery('#sortKey').val();
        
        if(selectid=='') selectid='0';
            
        jQuery("div#messagebox2").css({ 'display': 'block' });        
        jQuery.getJSON('/Product/GetReviewList/' + productid + '/' + selectid + '/' + itemCount + '/', function(data) {
            jQuery("div#reviewcontent").html('');
            var itemcount = 0;
            jQuery.each(data, function(index, review) {
                jQuery("div#reviewcontent").append(showCustomerReviews(index, review, productid));
                                
                itemcount++;
            });
            jQuery('#itemCount3').val(itemcount);
            jQuery('#linkMoreReview').css({ 'display': 'none' });
            jQuery('#linkLessReview').css({ 'display': 'inline' });
            jQuery("div#messagebox2").css({ 'display': 'none' });
        });
    });
    
    // When user click hide all items.
    jQuery('#hideallreview').click(function() {
       var productid = jQuery('#ProductId').val();
        var itemCount =jQuery('#itemCount').val();       
        var selectid = jQuery('#sortKey').val();
        
        if(selectid=='') selectid='0';

        jQuery("div#messagebox2").css({ 'display': 'block' });        
        jQuery.getJSON('/Product/GetReviewList/' + productid + '/' + selectid + '/' + itemCount + '/', function(data) {
            jQuery("div#reviewcontent").html('');
            var itemcount = 0;
            jQuery.each(data, function(index, review) {
                jQuery("div#reviewcontent").append(showCustomerReviews(index, review, productid));
                itemcount++;
            });
            jQuery('#itemCount3').val(3);
            jQuery('#linkMoreReview').css({ 'display': 'inline' });
            jQuery('#linkLessReview').css({ 'display': 'none' });
            jQuery("div#messagebox2").css({ 'display': 'none' });
        });
    });

});
    
// Add to cart
function AddToCart()
{
    var checkoutUrl = document.getElementById("checkoutUrl").value;
    if(checkoutUrl==undefined)
        checkoutUrl='/checkout';
    var total = parseInt(document.getElementById("txtQuantity").value);
    var productId = document.getElementById("ProductId").value;
    var locationUrl = "";
    
    if (total >= 1 && total < 1000) {
        locationUrl = checkoutUrl + '/add.aspx?a=a&pid=' + productId + '&qty=' + total; 
        window.location = locationUrl;
    }
    return true;
}

// Change image when product have more an image.
function changeImages(imageUrl)
{
    var target = document.getElementById("lightwindow_image_0");
    if(target)
    {
        target.src = imageUrl;
    }
    return false;
}

// Save review when user click review on product items.
function saveReview(category, productid, reviewid) {
    var item = true;
    if (category == 3) {
        item = confirm("Are You Sure That You Want To Report This?");
    }
    
    if (item) {
        jQuery.getJSON('/Product/CustomerReviewReport/' + reviewid + '/' + category, function(data) {
            item = data;
            if (category == 1 || category == 2) {
                alert('Thank you very much !');
            }
            else if (category == 3) {
                alert('Thanks for Reporting');
            }
        });
    }
}

// Show one customer review items
function showCustomerReviews(index, review, productId)
{
    var author = '';
    author = review.VchFirtname;
    if(author == ''){ author = 'Anonymous';}   
    
    var contents = "";
    contents = "<div id='vchreview'>"
                    + "<div class='dash_separator'></div>"
                    + "<div id='vchdate'>" + review.Date + "</div>"
                    + "<div id='vchsubject'>" + review.VchSubject + "</div>"
                    + "<div id='vchfirstname'><img src='http://iweb.pauladeenstore.com/images/PaulaDeenStore/" + review.ProductRating + ".gif' width='73' height='12' border='0' align='absbottom'><span style='padding-left:10px; line-height:12px'><b>By " + author + " (" + review.VCHCity + ',' + review.VCHState + ")</b></span></div>"
                    + "<div id='vchcomment'>" + review.VchComments + "</div>"
                    + "<div style='float:left' class='brownlinks'>" + review.CountHelpful + " of " + review.HelpfulCount + " users found this helpful.&nbsp;&nbsp;&nbsp;Was it helpful to you? "
                    + "<a onclick='saveReview('1', '" + productId + "', '" + review.ProductReviewId + "'); return false;' href='javascript:void(0)'>Yes</a> | "
                    + "<a onclick='saveReview('2', '" + productId + "', '" + review.ProductReviewId + "'); return false;' href='javascript:void(0)'>No</a></div>"
                    + "<div style='float:right; padding-top:10px'><a onclick=\"saveReview('3', '" + productId + "', '" + review.ProductReviewId + "'); return false;\" href='javascript:void(0)'>Report Violation</a></div>"
                    + "<div style='clear: both;'></div>"
                + "</div>";
    
    return contents;
}
