

/* Resource 3016 */

;function GetStateCompareCart(){
    $.ajax({
        url: "/ActionInvokerGenericAPI.ashx?input=dyn-post&output=json&ActionID=5042",
        type: "post",
        //data: 'pvpPriceType='+pvpPriceType_comp+'&DocumentType='+DocumentType_comp+'&showTotalLinesPerCart='+showTotalLinesPerCart,
        data: "WarehouseID="+WarehouseID+"&priceTypeID="+priceTypeID+"&DocumentType=31&docID=",
        success: function(response, textStatus, jqXHR) {  
            console && console.log(response);
            if (response != ''){
                	$("#stateCompareCartNItems").html(response.total);
            }else{
                console && console.log("error");
             }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console && console.log("error - GetStateCompareCart");
        }
    });    
}

function CleanALLCompareCart(){
    //jQuery('#cleanCompCart ').replaceWith('<img src="/images/loader.gif">');
    DocActions_Compare(3,0,0);
    //updateCompareBox();
}

function DocActions_Compare(DocumentAction,itemID,packid,nitems){
    console.log('so nitems..');
    console.log(nitems);
    var SendString ='ref=' +// $('#table_product_pc #'+itemID).attr("type")+
        			'&refAlt=' +// $('#table_product_pc #'+itemID).attr("type")+
        			'&DocumentAction=' + DocumentAction +
        			'&DocumentType=' +31+
        			'&ColorID='+
        			'&ColorName='+
        			'&Packsid='+
        			'&totalQuantity='+
        			'&Quantity=1'+
        			'&LogisticUnitTypeID='+
        			'&extraData='+itemID+
            		'&isB2C=1'+
                  	'&continueOnlyIfLogged=0'+
					'&pricetype=5' +
                    '&onlyDiscountsFromSpecialPrices='+onlyDiscountsFromSpecialPrices+
                    '&showAccumulativeDiscounts='+showAccumulativeDiscounts;
    $.ajax({
        url: "/ActionInvokerGenericAPI.ashx?input=dyn-post&output=string&ActionID=4885",
        type: "post",
        data: SendString,
        success: function(response, textStatus, jqXHR) {
            console && console.log('result...');
            console && console.log(response);
            if (response == '0'){
                //console && console.log("##removed_to_cart##");
                //$('#load_'+itemID).parent().parent().remove();
                //$('#table_product_pc tr#'+itemID).remove();
                //if($("#table_product_pc tr").length == 1){CleanALLQuotationCartGraph();}
            }
            else if (response == '3'){
                console && console.log(" Carrinho limpo");
                $("#popup_message .message").html("Comparador de produtos limpo");
                $("#popup_message").modal();
				GetStateCompareCart();
                //location.reload();
                //updateCompareBox();
            }
            else if (response == '1'){
                console && console.log("Adicionado Ao Carrinho");
            }
            else if (response == '2'){
                console && console.log("Actualizado com Sucesso.");
            }
            else if (response == '4'){
                $("#popup_message .message").html(login_required);
                $("#popup_message").modal();
            }
            else{} 
        },
        complete: function(jqXHR, textStatus, errorThrown) {
            if($('#contentmaster_compare_cart').length > 0){
                $('#contentmaster_compare_cart').append('<center style="color: white;margin: 10px;">A atualizar  <img src="/images/loader.gif"></center>');
                updatefieldsCart('contentmaster_compare_cart', "/info-carrinho-comparador",'');  
                setTimeout(function(){$("#popup_message").modal('hide');},1500);
            } 
            else{
                location.reload();
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            if(console.log) console.log("The following error occured: " + textStatus, errorThrown);
            //$("#rmv_"+ref).html(HTMLfromCOLrmv_pc);
        }
    });    
}

/*function updateCompareBox(){
    $.ajax({
        url: "/ActionInvokerGenericAPI.ashx?input=dyn-post&output=json&ActionID=5042",
        type: "post",
        data: "WarehouseID=3&priceTypeID=5&DocumentType=31&docID=",
        success: function(response, textStatus, jqXHR) {   
            $("#stateCompareCartNItems").html(response.data.length);
            for (i = 0; i < (4 - response.data.length); i++) { 
            $("#productsBox").prepend('<div class="col-xs-6 col-sm-3 col-md-3 col-lg-3">'+
										'<div class="winsig_compare_products_item_list_grid_empty">'+
											'<div class="winsig_product_item_list_grid">'+
												'<div class="thumbnail">'+
													'<span class="image">'+
														'<img src="http://staging.winsig.pt/office_direct/maquetas_finais/images/menus_images/no_image_thumb.jpg" class="img-responsive" width="100%" height="auto">'+
													'</span>'+
													'<span class="caption">'+
														'<span class="product_name"></span>'+
														'<span class="product_field link"></span>'+
														'<span class="clearfix"></span>'+
														'<span class="price_one"></span>'+
														'<span class="price_two"></span>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</div>');
			}
            
            if(response != ''){
                for (i = 0; i < response.data.length; i++) { 
                        $("#no_products_available").remove();
                        $("#linkToCompare").removeClass('inactive');
                        $("#productsBox").prepend('<div class="col-xs-6 col-sm-3 col-md-3 col-lg-3">'+
                                                  						'<div class="winsig_compare_products_item_list_grid">'+
                            										   	'	<div class="winsig_product_item_list_grid">'+
                                                                       	'		<a href="/'+productUrl+'/'+response.data[i].reference+'" class="thumbnail">'+
                                                                       	'			<span class="image">'+
                                                                       	'				<center>'+
                                                                       	'					<img src="'+$(".winsig_responsive_image img").attr("src")+'" class="img-responsive" style="max-width: 100%;" height="auto">'+
                                                                       	'				</center>'+
                                                                       	'			</span>'+
                                                                       	'			<span class="caption">'+
                                                                       	'				<span class="product_name">'+response.data[i].productName+'</span>'+
                                                                       	'				<span class="price_one"><span>'+response.data[i].ProductOldDefaultPrice+'</span>/'+
                                                  						'				<span class="price_two"><span>'+response.data[i].PVP+'</span></span>'+
                                                                       	'				<span class="product_field link" onClick="removeItem(\''+response.data[i].itemID+'\',\''+response.data[i].reference+'\',\''+response.data[i].reference.ProductReferenceAlt+'\')">'+
                                                                       	'					<span><i class="fa fa-trash"></i>Remover</span>'+
                                                                       	'				</span>'+
                                                                       	'			</span>'+
                                                                       	'		</a>'+
                            										   	'	</div>'+
                                                  						'</div>'+
                                                                       	'</div>');
                }
            }
        },error: function(jqXHR, textStatus, errorThrown) {
            if(console.log) console.log("The following error occured: " + textStatus, errorThrown);
            //location.reload();
        }
    }); 
}


$(document).ready(function(){
$.ajax({
        url: "/ActionInvokerGenericAPI.ashx?input=dyn-post&output=json&ActionID=5042",
        type: "post",
        data: "WarehouseID=3&priceTypeID=5&DocumentType=31&docID=",
        success: function(response, textStatus, jqXHR) {   
            debugger;
            document.getElementById('productsBox').innerHTML = "";
            $("#stateCompareCartNItems").html(response.data.length);
            
            for (i = 0; i < (4 - response.data.length); i++) {
            $("#productsBox").prepend('<div class="col-xs-6 col-sm-3 col-md-3 col-lg-3">'+
										'<div class="winsig_compare_products_item_list_grid_empty">'+
											'<div class="winsig_product_item_list_grid">'+
												'<div class="thumbnail">'+
													'<span class="image">'+
														'<img src="http://staging.winsig.pt/office_direct/maquetas_finais/images/menus_images/no_image_thumb.jpg" class="img-responsive" width="100%" height="auto">'+
													'</span>'+
													'<span class="caption">'+
														'<span class="product_name"></span>'+
														'<span class="product_field link"></span>'+
														'<span class="clearfix"></span>'+
														'<span class="price_one"></span>'+
														'<span class="price_two"></span>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</div>');
			}
            if(response != ''){
                for (i = 0; i < response.data.length; i++) { 
                    //if(productReference  == response.data[i].reference){
                        debugger;
                        $("#no_products_available").remove();
                        $("#linkToCompare").removeClass('inactive');
                        $("#productsBox").prepend('<div class="col-xs-6 col-sm-3 col-md-3 col-lg-3">'+
                                                  						'<div class="winsig_compare_products_item_list_grid">'+
                            										   	'	<div class="winsig_product_item_list_grid">'+
                                                                       	'		<a href="/'+productUrl+'/'+response.data[i].reference+'" class="thumbnail">'+
                                                                       	'			<span class="image">'+
                                                                       	'				<center>'+
                                                                       	'					<img src="'+$(".winsig_responsive_image img").attr("src")+'" class="img-responsive" style="max-width: 100%;" height="auto">'+
                                                                       	'				</center>'+
                                                                       	'			</span>'+
                                                                       	'			<span class="caption">'+
                                                                       	'				<span class="product_name">'+response.data[i].productName+'</span>'+
                                                                       	//'				<span class="price_one"><span>'+response.data[i].PVP+'</span>/'+
                                                  						'				<span class="price_two"><span>'+response.data[i].PVP+'</span></span>'+
                                                                       	'				<span class="product_field link" onClick="removeItem(\''+response.data[i].itemID+'\',\''+response.data[i].reference+'\',\''+response.data[i].reference.ProductReferenceAlt+'\')">'+
                                                                       	'					<span><i class="fa fa-trash"></i>Remover</span>'+
                                                                       	'				</span>'+
                                                                       	'			</span>'+
                                                                       	'		</a>'+
                            										   	'	</div>'+
                                                  						'</div>'+
                                                                       	'</div>');
                }
            }
        },error: function(jqXHR, textStatus, errorThrown) {
            if(console.log) console.log("The following error occured: " + textStatus, errorThrown);
            location.reload();
        }
    }); 
});
*/

function removeItem(itemID,ref,refAlt){
    var SendString ='ref=' + ref+
        '&refAlt=' + refAlt+
        '&DocumentAction=' + 0 +
        '&DocumentType=' + 28 +
        '&ColorID='+
        '&ColorName='+
        '&Packsid='+
        '&totalQuantity='+
        '&Quantity='+
        '&LogisticUnitTypeID='+
        '&isB2C=1'+
		'&continueOnlyIfLogged=0'+
        '&extraData='+itemID+
        '&pricetype='+priceTypeID;
    
    $("#popup_message .message").html("<img src=\"/images/loader.gif\" title=\"A carregar\" alt=\"A carregar informação\">");
    $("#popup_message").modal();
    $.ajax({
        url: "/ActionInvokerGenericAPI.ashx?input=dyn-post&output=string&ActionID=4885",
        type: "post",
        data: SendString,
        success: function(response, textStatus, jqXHR) { 
            console && console.log(response);
            if (response == '0'){
                console && console.log("##removed_by_comparator##");
                $("#popup_message .message").html("Item removido do comparador.");
                $("#popup_message").modal();
                $('[ref='+ref+']').remove();
                GetStateCompareCart();
            }else if (response == '4'){
                console && console.log("##comparator_clean##");
            }else if (response == '1'){
                console && console.log("##add_to_comparator##");
            }else if (response == '2'){
                console && console.log("##comparator_updated##");
            }else if (response == '4'){
                $("#popup_message .message").html("A sua sessão expirou. Tem que efetuar login.");
                $("#popup_message").modal();
            }
        },complete: function(jqXHR, textStatus, errorThrown) {
            if($('#contentmaster_compare_cart').length > 0){
                $('#contentmaster_compare_cart').append('<center style="color: white;margin: 10px;">A atualizar  <img src="/images/loader.gif"></center>');
                updatefieldsCart('contentmaster_compare_cart', "/info-carrinho-comparador",'');  
                setTimeout(function(){$("#popup_message").modal('hide');},500);
            } 
            else{
                location.reload();
            }
        },error: function(jqXHR, textStatus, errorThrown) {
            $("#popup_message .message").html("De momento não é possivel realizar esta acção. Pedimos desculpa pelo incómodo.");
            $("#popup_message").modal();
        }
    }); 
}

$(document).ready(function(){
    if (showContentMasterOnCart_comp == "1"){
    	$("#StateCompareCart").click(function(){
            if($('#contentmaster_compare_cart').length > 0 && ($("#StateCompareCart").attr("aria-expanded") == 'false' || $("#StateCompareCart").attr("aria-expanded") == undefined)){
                $('#contentmaster_compare_cart').append('<center style="color: white;margin: 10px;">A atualizar  <img src="/images/loader.gif"></center>');
                updatefieldsCart_compare('contentmaster_compare_cart', "/info-carrinho-comparador",'');  
            }
    	}); 
    }
    
    var cleanComparatorCart;
    var seeComparatorCart;
    
    cleanComparatorCart = $("#cleanCompCart").html();
    $("cleanCompCart").click(function(event){
        $("#cleanCompCart").html("<img src=\"/images/loader.gif\" title=\"A carregar\" alt=\"A carregar informação\">");
    });
    
    seeComparatorCart = $("#seeCompList").html();
    $("#seeCompList").click(function(event){
        $("#seeCompList").html("<img src=\"/images/loader.gif\" title=\"A carregar\" alt=\"A carregar informação\">");
    });
    
    
});

function updatefieldsCart_compare(element,url,filter1){
    $("#" +element).fadeTo("fast", 0.3, function() {});
    $.get(url+'?ajaxget=1&'+filter1, function( data ) {
        $("#" +element).html('');
        $("#" +element).fadeTo("fast", 1, function() {});
        $("#" +element).html( data );
        console && console.log( "Load was performed." );
    }).done(function() { 
            /*ajustes scroll*/
            if (typeof readjustLargeOptionsMenuUserLogged == 'function' && username != '') 
                setTimeout(readjustLargeOptionsMenuUserLogged(),200);
            else if (typeof readjustLargeOptionsMenuUserNotLogged == 'function')
                setTimeout(function(){readjustLargeOptionsMenuUserNotLogged();},200);
        });
}