﻿var activeBasketItemID = 0;
var quantityC = $("#quantityC"),
			tipsC = $("#validateTipsC"),
			allFieldsC = $([]).add(quantityC).add(tipsC);
			
function hideActivePanel() {
    if (activeBasketItemID > 0) {
        var pnlSpecsView = '#pnlSpecsView' + activeBasketItemID;
        var pnlSpecsEdit = '#pnlSpecsEdit' + activeBasketItemID;
        var pnlSpecsEditContent = '#pnlSpecsEditContent' + activeBasketItemID;

        $(pnlSpecsEditContent).html('');
        $(pnlSpecsEdit).hide();
        $(pnlSpecsView).show();
        activeBasketItemID = 0;
    }
}
function removeAll() {
    var answer = confirm("This will remove all products from your cart.  Do you want to continue?");
    if (answer) {
        var serviceUrl = FullCartPath;
        var fullCart = '#fullCart';

        $.ajax({
            type: "POST",
            url: serviceUrl + '/ClearCart',
            data: "{'usrname':'" + username + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(result) {
                
                return true;
            },
            error: function(result) {
                //alert(result.status + ' ' + result.statusText);
                return false;
            }
        });
        return true;
    }
    else {
        return false;
    }

    //return false;
}
function AddToWishList(BasketItemID, ProductID) {
    if (isAuthenticated == 1) {
        var serviceUrl = FullCartPath;
        var fullCart = '#fullCart';
        $.ajax({
            type: "POST",
            url: serviceUrl + '/MoveToWishList',
            data: "{'usrname':'" + username + "', 'BasketItemID':'" + BasketItemID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(result) {
                
                return true;
            },
            error: function(result) {
                //alert(result.status + ' ' + result.statusText);
                return false;
            }
        });
        return true;
    }
    else {
        location.href = loginPath + '?type=2';
    }
    return false;
}
function removeItem(BasketItemID) {
    var answer = confirm("This will remove this product from your cart.  Do you want to continue?");
    if (answer) {
        var serviceUrl = FullCartPath;
        var fullCart = '#fullCart';

        $.ajax({
            type: "POST",
            url: serviceUrl + '/RemoveProduct',
            data: "{'usrname':'" + username + "', 'BasketItemID':'" + BasketItemID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(result) {
                //alert(result);
                //var myObject = eval("(" + result + ")");
                //var data = myObject[1].Data;
                //$(fullCart).html(data);


                //var data = "[{name : 'firstname', value : 'Hello'},{name : 'lastname', value : 'World'}]";
                //data = eval('(' + data + ')');
                //alert(data[1].value);

                //eval('var z=' + result);
                //$(fullCart).html(result);
                //var myObject = eval('(' + result + ')');
                //var oResultData = eval('(' + result + ')');
                //var myObject = eval('{' + eval(result) + '}');
                //alert(myObject);
                //LoadMiniCart();
                return true;
            },
            error: function(result) {
                //alert(result.status + ' ' + result.statusText);
                return false;
            }
        });
        return true;
        //removeRow(BasketItemID);
    }
    else {
        return false;
    }
    
    //return false;
}
function showEditForm(BasketItemID, productID) {
    var pnlSpecsView = '#pnlSpecsView' + BasketItemID;
    var pnlSpecsEdit = '#pnlSpecsEdit' + BasketItemID;
    var pnlSpecsEditContent = '#pnlSpecsEditContent' + BasketItemID;
    var serviceUrl = FullCartPath;
    hideActivePanel();
    $.ajax({
        type: "POST",
        url: serviceUrl + '/GetProduct',
        data: "{'usrname':'" + username + "', 'ProductID':'" + productID + "', 'BasketItemID':'" + BasketItemID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
        $(pnlSpecsView).hide();
        $(pnlSpecsEdit).show();
        $(pnlSpecsEditContent).html(result);
        activeBasketItemID = BasketItemID;
        quantityC = $("#quantityC"),
			tipsC = $("#validateTipsC"),
			allFieldsC = $([]).add(quantityC).add(tipsC);
        },
        error: function(result) {

            //alert(result.status + ' ' + result.statusText);
            return false;
        }
    });

    //$(pnlSpecsView).hide();
    return false;
}
function removeRow(BasketItemID, ProductID) {
    var row = '#row' + BasketItemID;
    var serviceUrl = FullCartPath;
    
    $(row).remove();
}
function CancelEdit(BasketItemID) {
    hideActivePanel();
    //var pnlSpecsView = '#pnlSpecsView' + BasketItemID;
    //var pnlSpecsEdit = '#pnlSpecsEdit' + BasketItemID;
    //$(pnlSpecsEdit).hide();
    //$(pnlSpecsView).show();
    return false;
}
function SaveEdit(BasketItemID, productID) {
var bValid = true;
    allFieldsC.removeClass('ui-state-error');
	updateTipsC("", tipsC);
	bValid = bValid && checkRegularExpression(quantityC, /^[-+]?[1-9]\d*.?[0]*.?$/i, "Invalid quantity!", tipsC);
	if (bValid) {
	    var defaultValue = '';
	    var price = 0;
	    var weight = 0;
	    var qt = 0;
	    var optionGroupsCount = 0;
	    var options = '';
	    optionGroupsCount = $('#1optionsQuantityC').attr('value');
	    if (optionGroupsCount > 0) {
	        for (i = 0; i < optionGroupsCount; i++) {
	            var ctl = '1idOptionC' + i;
	            var option = $("#" + ctl + " option:selected").attr('value');
	            options += option + ',';
	        }
	    }
	    var index = 0;
	    $("input[name*='rdlPriceOptionsC']").each(function() {
	        if (this.checked == true) {
	            defaultValue = $("label[for*='" + this.id + "']").html();
	            price = $(this).val();
	            var ctl = 'rdlWeightOptionsC_' + index;
	            weight = $("#" + ctl).attr('value');
	            //alert(weight);
	        }
	        index += 1;
	    });
	    qt = $(quantityC).val();

	    //send to server
	    var serviceUrl = FullCartPath;
	    $.ajax({
	        type: "POST",
	        url: serviceUrl + '/AddToCart',
	        data: "{'usrname':'" + username + "', 'productID':'" + productID + "', 'priceOption':'" + defaultValue + "', 'price':'" + price + "', 'quantity':'" + qt + "', 'groups':'" + optionGroupsCount + "', 'options':'" + options + "', 'weight':'" + weight + "', 'BasketItemID':'" + BasketItemID + "'}",
	        contentType: "application/json; charset=utf-8",
	        dataType: "json",
	        success: function(result) {
	            //alert(result);
	        //return true;
	        location.href = cartPath;
	        },
	        error: function(result) {
	            //alert(result.status + ' ' + result.statusText);
	            return false;
	        }
	    });
	    //return true;  
	}
	return false;
}
