/*******************************

 CommentForm.js, Version 1.4.1, 12-Nov-2009
 Copyright Northgate Information Solutions UK Limited, 2008-2009

 *******************************/

/********************
 CommentForm class
 
 Constructor:
	CommentForm(config)
 Properties:
	parent				Inherited from DataObject class.
	window				Inherited from DataObject class.
	target				Inherited from DataObject class.
	controlFolder		Inherited from DataObject class.
	topicFolder			Inherited from DataObject class.
	files				Inherited from DataObject class.
	data				Inherited from DataObject class.
	destinationUrl		The destination URL for comments.
	mailSubject			The subject of comment emails.
	formUrl				The URL of the comment form, relative to the root of the help system.
	linkDivClass		The class of the <div> tag containing the comment link.
	linkClass			The class of the comment link's <a> tag.
	linkText			The text of the comment link.
	
 Methods:
	load()				Inherited from DataObject class.
	initialise()		Inherited from DataObject class.
	mouseEvent(event)	Handle a mouse event.
	addCommentLinks()	Dynamically add comment links to a topic.
	
 ********************/
 
// *** Constructor for CommentForm class ***
function CommentForm() {}

// *** CommentForm inherits from DataObject ***
CommentForm.prototype = new DataObject();
CommentForm.prototype.constructor = CommentForm;

// *** CommentForm instance methods ***
CommentForm.prototype.setup = function doSetup(config) {
	this.destinationUrl = config.destinationUrl;
	this.mailSubject = config.mailSubject;
	this.formUrl = config.formUrl;
	this.linkDivClass = config.linkDivClass;
	this.linkClass = config.linkClass;
	this.linkText = config.linkText;
}

CommentForm.prototype.mouseEvent = function cmtMouseEvent(e) {
	/* Handle a mouse event.
	   Returns true if the event was handled; otherwise, false. */
	   
	// Get a reference to the element that generated the event.
	
	var doc = this.parent.helpSystem.topicFrame.document;

	if (document.all)	// IE
		el = e.srcElement;
	else {				// Netscape, Mozilla.
		el = e.target;
		// In Firefox the target is a Text node.
		if (el.nodeType == Node.TEXT_NODE)
			 el = e.target.parentNode;
	}
	
	if (el.className == this.linkClass) {
		// Set the destination of the link.
		// Make it relative to the topic.
		var href = this.formUrl.toLowerCase();
		href = parent.helpSystem.rootFolder.pathConcat(href);
		el.href = parent.helpSystem.relativeToTopic(href);
		if (href.substring(0, 1) == "./") {
			href = href.substring(2);
		}
		// Get the id of the topic.
		var metaTags = doc.getElementsByTagName("meta");
		var tid = "";
		for (var i = 0; i < metaTags.length; i++) {
			if (metaTags[i].name == "topicid") {
				tid = metaTags[i].content;
				break;
			}
		}
		// Append it to the comment form URL.
		if (tid == "") {
			var titleTag = doc.getElementsByTagName("title")[0];
			if (document.all) {
				tid = titleTag.innerText;
			}
			else {
				tid = titleTag.firstChild.data;
			}
		}
		// Append it to the comment form URL.
		href += "?topicid=" + tid;

		// Set the link properties.
		href += "&destinationUrl=" + this.destinationUrl;
		if (this.mailSubject != "") {
			href += "&mailSubject=" + this.mailSubject;
		}
		href += "&product=" + self.documentDetails.product;
		href += "&productVersion=" + self.documentDetails.productVersion;
		href +=	"&docVersion=" + self.documentDetails.docVersion;
		href += "&defaultTopic=" + self.defaultTopic;
		
		el.href = href;
		el.target = "_blank";
		
		// Handled by this object.
		return false;
	}
	else {
		// Not handled by this object.
		return false;
	}
}

CommentForm.prototype.addCommentLinks = function cmtAddCommentLinks() {
	var win = this.parent.helpSystem.topicFrame;
	var doc = win.document;
	
	// Check whether this topic is an exception.
	// <meta name="noCommentLink" content="true">
	// Content is ignored; if a meta tag with this name exists, no comment links are added.
	var metaTags = doc.getElementsByTagName("meta");
	for (var i = 0; i < metaTags.length; i++) {
		if (metaTags[i].name.isEqualIgnoreCase("noCommentLink")) {
			return;
		}
	}
	
	var nbsp = doc.createTextNode(String.fromCharCode(160));
	
	// Create the comment link.
	var txt = doc.createTextNode(this.linkText);
	var aTag = doc.createElement("a");
	aTag.className = this.linkClass;
	aTag.href = "#";
	aTag.appendChild(txt);

	// For each <div> tag...
	var divTags = doc.getElementsByTagName("div");
	var found = false;
	for (var i = 0; i < divTags.length; i++) {
		// If its class is widgetBar...
		if (divTags[i].className == this.linkDivClass) {
			// Find the first <p> tag child.
			var pTags = divTags[i].getElementsByTagName("p");
			// Add an <a> tag as the first child of the <p> tag.
			if (pTags.length > 0) {
				var pTag = pTags[0];
				if (i == 0) {
					pTag.insertBefore(aTag, pTag.firstChild);
				}
				else {
					pTag.insertBefore(aTag.cloneNode(true), pTag.firstChild);
				}
				found = true;
			}
		// End if
		}
	// Loop
	}
	
	// If there were no <div> tags with widgetBar class...
	if (!found) {
		var pTag = doc.createElement("p");
		// If Word topic, use the linkDivClass for the paragraph.
		pTag.appendChild(aTag);
		pTag.appendChild(nbsp);
		var dTag = doc.createElement("div");
		if (this.isWordTopic(doc)) {
			pTag.className = this.linkDivClass;
		}
		else {
			dTag.className = this.linkDivClass;
		}
		dTag.appendChild(pTag);
		
		// Start of topic.
		var bodyIndent = doc.body;
		// Add a <div> tag as the first child.
		bodyIndent.insertBefore(dTag, bodyIndent.firstChild);
		
		// End of topic.
		if (DHTML.getClientHeight(win) > DHTML.getFrameDimensions(win).height) {
			var done = false;
			// Get the last child of the <body> tag.
			var bfr = bodyIndent.lastChild;
			// In Firefox the this may be a Text node.
			while (bfr.nodeType != 1 /* Node.ELEMENT_NODE */) {
				bfr = bfr.previousSibling;
			}
			// If it is a "go to top" link...
			if (bfr.tagName.isEqualIgnoreCase("p")) {
				if (bfr.className == "topbutton") {
					// Insert a <div> tag before this child.
					bodyIndent.insertBefore(dTag.cloneNode(true), bfr);
					done = true;
				}
				else {
					var img = bfr.getElementsByTagName("img");
					for (var i = 0; i < img.length; i++) {
						if (img[i].className == "topLink") {
							// Insert a <div> tag before this child.
							bodyIndent.insertBefore(dTag.cloneNode(true), bfr);
							done = true;
							break;
						}
					}
				}
			// End if
			}
			if (!done) {
				// Insert a <div> tag before the </body> tag.
				bodyIndent.appendChild(dTag.cloneNode(true));
			}
		}
	// End if
	}
}

CommentForm.prototype.isWordTopic = function cmtIsWordTopic(doc) {
	var metaTags = doc.getElementsByTagName("meta");
	for (var i = 0; i < metaTags.length; i++) {
		if (metaTags[i].name == "ProgId" && metaTags[i].content == "Word\.Document") {
			return true;
		}
	}
	return false;
}

