﻿//JScript File
//This JS file contains code for saving the items rating thumbs up & thumbs down....
Ajax.Commands.SaveRating=function(elementId, ajaxCommand, cmdCounter){
	this.priority=net.CommandQueue.PRIORITY_IMMEDIATE;
	this.type=net.CommandQueue.TYPE_SINGLEPROCESS;
	this.id=cmdCounter;
	this.ajaxCommand = ajaxCommand;		
	var saveRatingParams = elementId.toString().split('|');	
	this.elementId = saveRatingParams[0];
	this.entityId = this.elementId;
	this.entityType = saveRatingParams[1];
	this.ratingValue = saveRatingParams[2];	
	this.likeCount = saveRatingParams[3];
	this.disLikeCount = saveRatingParams[4];
	this.cacheName = saveRatingParams[5];
	this.vendorlocationId = saveRatingParams[6];
	this.reqDeliveryDateTime = saveRatingParams[7];
	this.targetPage = saveRatingParams[8];
	this.categoryId = saveRatingParams[9];
	this.internalReturnUrl = saveRatingParams[10];
}

Ajax.Commands.SaveRating.prototype.QueryStringVariables=function() {
    var commandVariables = new Hashtable();
    commandVariables.put("entityId", this.entityId);
    commandVariables.put("entityType", this.entityType);
    commandVariables.put("ratingValue", this.ratingValue);
    commandVariables.put("likeCount", this.likeCount);
    commandVariables.put("disLikeCount", this.disLikeCount);
    commandVariables.put("cacheName", this.cacheName);    
    commandVariables.put("vendorlocationId", this.vendorlocationId);
    commandVariables.put("reqDeliveryDateTime", this.reqDeliveryDateTime); 
    commandVariables.put("targetPage", this.targetPage); 
    commandVariables.put("categoryId", this.categoryId); 
    commandVariables.put("internalReturnUrl", this.internalReturnUrl); 
      
    return commandVariables;    
}

Ajax.Commands.SaveRating.prototype.ParseResponse=function(docEl) {
    var attrs = docEl.attributes;
    var status = attrs.getNamedItem('status').value;
    var message = attrs.getNamedItem('message').value;
    if(status == 'success') {

	//Special case only for step 3B - No change to this...
	var isStep3b = attrs.getNamedItem('isStep3b').value;	
	if(isStep3b == 'True') {
	    var htmlContent = attrs.getNamedItem('likeUserControlContent').value;
	    document.getElementById('LikeUserControl').innerHTML = htmlContent;  
	}	
		
	var step3html = attrs.getNamedItem('likeDisLikeHtmlContent').value;
	var targetPage = attrs.getNamedItem('targetPage').value;
	
	//Dynamically update the html element with like/un like image and rating... 
	var htmlElementId =  attrs.getNamedItem('htmlElementId').value;	
	var criteriaSpanElement = 'span[id=' + htmlElementId + ']';
	if(targetPage.match("OrderHistory") || targetPage.match("RecentOrder")) {
	    var myElements = $$(criteriaSpanElement);
	    for(i=0; i<myElements.length; i++) {
		myElements[i].innerHTML = step3html;
	    }
	} else {
	    document.getElementById(htmlElementId).innerHTML = step3html;
	}
	
	//update boxover content with like / unlike an item
	var productBoxOverContent = attrs.getNamedItem('productBoxOverContent').value;
	var productBoxOverElementId = attrs.getNamedItem('productBoxOverElementId').value;
	var criteriaBoxOverElement = 'span[id=' + productBoxOverElementId + ']';
	if(targetPage.match("Liked") || targetPage == "3"){
	    if(document.getElementById(productBoxOverElementId) != null) {
		document.getElementById(productBoxOverElementId).innerHTML = productBoxOverContent;
	    }
	}
	var mostLikedItemsBoxOverElementId = attrs.getNamedItem('mostLikedItemsBoxOverElementId').value;
	if(targetPage == "3" && document.getElementById(mostLikedItemsBoxOverElementId) != null) {
	    var likeCount = attrs.getNamedItem('likeCount').value;
	    document.getElementById(mostLikedItemsBoxOverElementId).innerHTML = productBoxOverContent + "(" + likeCount + ")";
	}
	
	net.Base.ReleaseQueue(); 
    } else {
	alert(message);
    }
}
