//
// Copyright 1999, 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
// ----------------------------------------------------
//
// Accessibility.js
//
// This command adds 
// 
//
// Version 1.0
// Added functions...
// ----------------------------------------------------

//todo get help
var helpDoc = MM.HELP_objAddTrackback;

var isCanceled=false;

function isDOMRequired() { 
	// Return false, indicating that this object is available in code view.
	return false;
}

function showIf()
{
	return (dw.getDocumentDOM()!=null && CCWorkspaceManager.getManager(dw.getDocumentDOM()) != null && CCWorkspaceManager.getManager(dw.getDocumentDOM()).isBlogSite()) && CCWorkspaceManager.getManager(dw.getDocumentDOM()).shouldUseDefaultEditor();
}


function canAcceptCommand()
{
	return  (CCWorkspaceManager.getManager(dw.getDocumentDOM()) != null) && 
	        (CCWorkspaceManager.getManager(dw.getDocumentDOM()).IsTrackbackEnabled()) && 
	        (CCWorkspaceManager.getManager(dw.getDocumentDOM()).getState() == 'edit') && 
	        (CCWorkspaceManager.getManager(dw.getDocumentDOM()).shouldUseDefaultEditor()) && 
	        (CCWorkspaceManager.getManager(dw.getDocumentDOM()).isTrackbacksSupported());
}

function commandButtons(){
	return new Array(	"PutButtonsOnBottom", 
						"OkButton", MM.BTN_OK, "setTrackback(); window.close();",
						"CancelButton", MM.BTN_Cancel, "isCanceled=true; window.close();",
						 "PutButtonOnLeft", MM.BTN_Help,    "displayHelp()");
          

}

function setTrackback()
{	
	// Check if the document contains a value for the enclosure meta-tag
    theDom = dw.getDocumentDOM();
    metatags = theDom.getElementsByTagName("meta");
    count = metatags.length;
    var trackbackVal;
    var foundTrackback = 0;
    var nodeToRewrite;
    for (i = 0; i < count; i++)
    { 
        if (metatags[i].name == TEMPLATE_TRACKBACK)
        {
            trackbackVal = metatags[i].content;
            if(trackbackVal.length > 0)
            {
                foundTrackback = 1;
                nodeToRewrite = metatags[i];
            }
            break;
        }
    }
    
    var trackbackValEntered = document.forms[0].trackback.value;
    
    // empty value should not be allowed
    //code added for bug no. 213616. 
    if(trackbackValEntered == "")
    {
		if(foundTrackback)
		    nodeToRewrite.content = trackbackValEntered;
		else 
			return true;
    }
   
	else
	{	
        if(!foundTrackback)    
		    dw.getDocumentDOM().insertBlogMetaTag(TEMPLATE_TRACKBACK, trackbackValEntered);
	    else
	        nodeToRewrite.content = trackbackValEntered;
	}
		
	dw.addTrackback();
	
	
	CCWorkspaceManager.getManager(dw.getDocumentDOM()).updateUI();
	return true;
}

function initUI()
{
	// Check if the document contains a value for the enclosure meta-tag
    theDom = dw.getDocumentDOM();
    metatags = theDom.getElementsByTagName("meta");
    count = metatags.length;
    var trackbackVal;
    var foundTrackback = false;    
    for (i = 0; i < count; i++)
    { 
        if (metatags[i].name == TEMPLATE_TRACKBACK)
        {
            trackbackVal = metatags[i].content;
            if(trackbackVal.length > 0)            
                foundTrackback = true;              
            break;
        }
    }
    if(foundTrackback)
        document.forms[0].trackback.value = trackbackVal;            
    else
		document.forms[0].trackback.value = "";
	
	document.forms[0].trackback.focus();
}
