<!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
<html xmlns:MMString="http://www.macromedia.com/schemes/data/string/">
<head>
<title><MMString:loadString id="Menus/MM/ccBlogEntryCategories/title" /></title>
<SCRIPT LANGUAGE="javascript" SRC="../../Shared/MM/Scripts/CMN/ccBlogMetaDataStrings.js"></SCRIPT>
<script language="JavaScript">

function receiveArguments(newcategory)
{
	if(newcategory != "" && !isValidCategory(newcategory))
	{
	    var tmp = removeInvlidCategories(newcategory);
	    newcategory = tmp;
	    
	}
	var foundIt = false;
	
	theDom = dw.getDocumentDOM();
    metatags = theDom.getElementsByTagName("meta");
    count = metatags.length;
    var resultCategory = "";
	for (i = 0; i < count; i++)
	{ 
		if (metatags[i].name == TEMPLATE_CATEGORIES)
		{
			foundIt = true;
			//commented temporarily for the multiple categories feature(begins)
			/*if((metatags[i].content == newcategory) && (newcategory != 'None'))
				metatags[i].content = 'None';
			else*/
			//commented temporarily for the multiple categories feature(ends)
			{
			   if((metatags[i].content).length == 0)
			   				metatags[i].content = newcategory;
			   else
			   {
			        if((metatags[i].content).search(newcategory) == -1)
			        {
			            //add newcategory to metatags[i].content after adding a comma
			            metatags[i].content = (metatags[i].content).concat(",");
			            metatags[i].content = (metatags[i].content).concat(newcategory);
			            
			        }
			        else
			        {
			            var temp_mul = metatags[i].content;
			            var index = (temp_mul).indexOf(newcategory);
			            if(index == 0)//if it is the first in the list
			            {
			               
			                if((temp_mul).search(",") == -1)
			                    temp_mul="";
			                else
			                    temp_mul = temp_mul.substring(index+newcategory.length+1,temp_mul.length);
			               
			            }
			            else if(index+newcategory.length == temp_mul.length)//if it is the last in the list
			            {
			                temp_mul = temp_mul.substring(0,index-1);
			            }
			            else //it is somewhere in the middle
			            {
			                var str1 = temp_mul.substring(0,index);
			                var str2 = temp_mul.substring(index+newcategory.length+1,temp_mul.length);
			                temp_mul = str1.concat(str2);
			                
			            }
			            
			            metatags[i].content = temp_mul; 
			            
			        }
			   }
			   				
				resultCategory = metatags[i].content;
			}
			break;
		}
	}
	
	if (foundIt == false)
	{
		dw.getDocumentDOM().insertBlogMetaTag(TEMPLATE_CATEGORIES,newcategory);
	}
	if( resultCategory != ""
	    && !CCWorkspaceManager.getManager(dw.getDocumentDOM()).isSecondaryMetaVisible())
	{
	    CCWorkspaceManager.getManager(dw.getDocumentDOM()).toggleSecondaryMeta();
	}		
	if(CCWorkspaceManager.getManager(dw.getDocumentDOM()) != null)
		CCWorkspaceManager.getManager(dw.getDocumentDOM()).updateUI();
	return;
		
	
}

function canAcceptCommand()
{
	
	return (dw.getDocumentDOM() != null &&	dw.getFocus() == 'document' &&
		CCWorkspaceManager.getManager(dw.getDocumentDOM()).isBlogSite() &&
		(CCWorkspaceManager.getManager(dw.getDocumentDOM()).getTargetBlogCategories().length>0));
}

function getCurrentValue()
{
	var value = "";
	
	if(dw.getFocus() != 'document')
		return value;

    	metatags = dw.getDocumentDOM().getElementsByTagName("meta");
    	count = metatags.length;
    
	for (i = 0; i < count; i++)
	{ 
		if (metatags[i].name == TEMPLATE_CATEGORIES)
		{
			value = metatags[i].content;
			break;
		}
	}
	return value;
}

function getDynamicContent()
{
	var items = new Array;
	
	if(dw.getFocus() != 'document')
			return items;
			
	var dom = dw.getDocumentDOM();
	if (dom)
	{
		var wm = CCWorkspaceManager.getManager(dom);
		if (wm)
		{
			if(!wm.isBlogSite())
				return items;
				
			var categories = wm.getTargetBlogCategories();
			var i;
			var j = categories.length;
			if(j>0)
			    categories = categories.sort();
			
			for (i = 0; i < categories.length; i++)
			{
				items[i] = categories[i] + ";id='" + categories[i] + "'";
				
			}
		}
	}
	
	return items;
}

function addRecentAddress(address)
{
	return 0;
}

function isCommandChecked(){
//here first two lines commented for the multiple categories feature
	/*if(getCurrentValue() == 'None')
		return false;*/
		//code added for multiple categories(begins) 
		var value = getCurrentValue();
		if(value.length == 0)
			return false;

		if(value.search(arguments[0]) == -1)
		    return false;
		else
		    return true;
		//coded added for multiple categories(ends)
	//return (getCurrentValue() == arguments[0]);		
}


function isValidCategory(value){
    
      var items = new Array;
      var result = false;
      var dom = dw.getDocumentDOM();
      if (dom)
      {
            var wm = CCWorkspaceManager.getManager(dom);
            if (wm)
            {
                  var categories = wm.getTargetBlogCategories();
                  var mulCategory = value;
                  var start_pos = 0;
                  var end_pos = -1;
                  /* parsing the list of categories entered by the user delimited by ,
                  and checking if each of them is a valid category. even if one is invalid 
                  setting the category to None only */
                  result = false;
                  for(var j=0;j<mulCategory.length; )
                {
                    end_pos = mulCategory.indexOf(",",start_pos);
                    if(end_pos == -1)
                            end_pos = mulCategory.length;
                    curCategory = mulCategory.substring(start_pos,end_pos);
                    //compare indivisual categories with blog categories
                if(curCategory != "")
                {
                    var found = false;
                    for (var i = 0; i < categories.length; i++)
                    {
                        if(categories[i] == curCategory)
                            found = true;

                    }
                     if(!found) 
                       return false;
                      }

                      //setting the postion for next run     
                    start_pos = end_pos + 1;
                    j = start_pos;
            }
            result = true;
            }
      }
      return result;
}

function removeInvlidCategories(value)
{
    var items = new Array;
	var result = "";
	var dom = dw.getDocumentDOM();
	if (dom)
	{
		var wm = CCWorkspaceManager.getManager(dom);
		if (wm)
		{
			var categories = wm.getTargetBlogCategories();
			var mulCategory = value;
			var start_pos = 0;
			var end_pos = -1;
			/* parsing the list of categories entered by the user delimited by ,
			and checking if each of them is a valid category. even if one is invalid 
			setting the category to None only */
			result = "";
			for(var j=0;j<mulCategory.length; )
            {
                end_pos = mulCategory.indexOf(",",start_pos);
                if(end_pos == -1)
                    end_pos = mulCategory.length;
                curCategory = mulCategory.substring(start_pos,end_pos);
                
                //compare indivisual categories with blog categories
                if(curCategory != "")
                {
                    var found = false;
                    for (var i = 0; i < categories.length; i++)
			        {
				        if(categories[i] == curCategory)
				        {
					        found = true;
					        break;
					    }
			        }
			        if(found && result=="")
			        {
			            result = curCategory;
			        } 
			        else if(found)
			        {
			            result += ",";
			            result += curCategory;
			        }
			           
			    }
			    //setting the postion for next run     
		        start_pos = end_pos + 1;
                j = start_pos;
            }
            
		}
	}
	return result;
}

</script>
</head>

<body>
</body>
</html>

