﻿        function AddMyProduct(catalogKey, customSegment, path)
        {
            //LoadProperties();
            
            var keys = Sys.Services.ProfileService.properties.MyProductCatalogKeys;
            var comments = Sys.Services.ProfileService.properties.MyComments;
            var customSegments = Sys.Services.ProfileService.properties.MyCustomSegments;
            var myPath = Sys.Services.ProfileService.properties.MyPath;
            
            if ( keys != null )
            {
                Array.add(customSegments, customSegment);
                Array.add(keys,catalogKey);
                Array.add(comments, '');
                Array.add(myPath, path);
                               
                Sys.Services.ProfileService.properties.MyProductCatalogKeys = keys;
                Sys.Services.ProfileService.properties.MyComments = comments;
                Sys.Services.ProfileService.properties.MyCustomSegments = customSegments;
                Sys.Services.ProfileService.properties.MyPath = myPath;
                Sys.Services.ProfileService.save(null,SaveCompletedCallback,ProfileFailedCallback,null);
            }
        }
    
        function LoadProperties()
        {
            Sys.Services.ProfileService.load(Sys.Services.ProfileService.properties.MyProductCatalogKeys, null, null, null);
            Sys.Services.ProfileService.load(Sys.Services.ProfileService.properties.MyComments, null, null, null);
            Sys.Services.ProfileService.load(Sys.Services.ProfileService.properties.MyCustomSegments, null, null, null);
            Sys.Services.ProfileService.load(Sys.Services.ProfileService.properties.MyPath, null, null, null);        
        }
        
        function RemoveMyProduct(catalogKey)
        {
            
            var keys = Sys.Services.ProfileService.properties.MyProductCatalogKeys;
            var comments = Sys.Services.ProfileService.properties.MyComments;
            var customSegments = Sys.Services.ProfileService.properties.MyCustomSegments;
            var path = Sys.Services.ProfileService.properties.MyPath;
            
            var index = Array.indexOf(keys,catalogKey);
            
            if (index >= 0)
            {
                Array.removeAt(keys, index);
                
                if (comments != null)
                {
                    Array.removeAt(comments, index);
                    Array.removeAt(customSegments, index);
                    Array.removeAt(path, index);
                }
                
                Sys.Services.ProfileService.properties.MyProductCatalogKeys = keys;
                Sys.Services.ProfileService.properties.MyComments = comments;
                Sys.Services.ProfileService.properties.MyCustomSegments = customSegments;
                Sys.Services.ProfileService.properties.MyPath = path;
                
                Sys.Services.ProfileService.save(null,SaveCompletedCallback,ProfileFailedCallback,null); 
            }   
        }
        
        function AddMyComment(catalogKey, comment)
        {
            var keys = Sys.Services.ProfileService.properties.MyProductCatalogKeys;
            var comments = Sys.Services.ProfileService.properties.MyComments;
            var index = Array.indexOf(keys,catalogKey);
            
            if (index >= 0)
            {
                if (comments != null)
                {
                    comments[index] = comment;
                }
                
                Sys.Services.ProfileService.properties.MyComments = comments;
                
                Sys.Services.ProfileService.save(null,SaveCompletedCallback,ProfileFailedCallback,null);
            }    
        }
        
        function ShowMyProducts()
        {
            var keys = Sys.Services.ProfileService.properties.MyProductCatalogKeys;
            var comments = Sys.Services.ProfileService.properties.MyComments;

            alert('keys: ' + keys);
            alert('comments: ' + comments);
        }
        
        function SaveCompletedCallback()
        {
            // alert('Save Success');
            // UpdateMyProductListCount();
        }
        
        function ProfileFailedCallback()
        {
            // alert('Save Failed');
        }

        function GetMyProductCount()
        {
            var count = 0;
            var keys = Sys.Services.ProfileService.properties.MyProductCatalogKeys;
            
            if (keys != null)
            {
                count = keys.length;
            }
            
            return count;
        }
    
        function UpdateMyProductListCount()
        {
            /* TODO: mni, disabled coutning due to problem with caching
            var MyList = document.getElementById('ctl00_MyProductList1_productListHyperLink');
            
	        var firstIndex = MyList.innerHTML.indexOf('(');
	        var listCount = GetMyProductCount();
	        var newText;
        	
	        if(firstIndex == -1)//Nothing added yet
	        {
	            MyList.innerHTML = MyList.innerHTML + ' (' + listCount + ')';
	        }
	        else
	        {
	            newText = MyList.innerHTML.substring(0, firstIndex);
	            newText = newText + ' (' + listCount + ')';   
	            MyList.innerHTML = newText;
	        }
	        */
        }
