function setCommStyle(){
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")){
			a.disabled = true;
			if(a.getAttribute("title") == "default") a.disabled = false;
			if(a.getAttribute("title") == "commCss") a.disabled = false;
		}
	}
}
function checkComm(){
	if(document.location.href.indexOf("workbench")==-1){
		if(comments_global){
			setCommStyle();
			document.getElementById("relatedContent").innerHTML='<div class="relatedBox" style="padding-bottom:4px;"><table id="commentInviteBox" cellpadding="0" cellspacing="0"><tr><td rowspan="2" style="width:auto"><a href="#commentAnchor" id="commentCount"><img style="padding-right:4px;" src="http://cache.boston.com/jobs/i/comments.jpg"width="35" height="34" /></a></td><td class="commentInvite">Discuss</td></tr><tr><td><a href="#commentAnchor" id="commentCount">COMMENTS (<span id="cCount"></span>)</a></td></tr></table></div>' + document.getElementById("relatedContent").innerHTML
			document.getElementById("articleEmbed").style.display="block";
			commTease();
		}
	}
}

function JSONscriptRequest(fullUrl,ieFlag) {
  this.fullUrl = fullUrl;
  if(ieFlag!="noflag"){this.noCacheIE = '&noCacheIE=' + (new Date()).getTime()}else{this.noCacheIE = ""};
  this.headLoc = document.getElementsByTagName("head").item(0);
  this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function () {
  this.scriptObj = document.createElement("script");
  this.scriptObj.setAttribute("type", "text/javascript");
  this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
  this.scriptObj.setAttribute("id", this.scriptId);
}

JSONscriptRequest.prototype.removeScriptTag = function () {
  this.headLoc.removeChild(this.scriptObj);
}

JSONscriptRequest.prototype.addScriptTag = function () {
  this.headLoc.appendChild(this.scriptObj);
}

var awCat=[];
awCat["auto"]="6565056355114621876";awCat["bio"]="5072358142956778366";awCat["books"]="5077343182940838572";awCat["works"]="8262457333519750313";
awCat["eco"]="6237226050950563891";awCat["cal"]="6082810676177823392";awCat["ci"]="7184121297516994406";awCat["met"]="6338769912843844152";
awCat["epg"]="9176991129978666222";awCat["food"]="5187383272648586390";awCat["mag"]="7040109996122469467";awCat["no"]="5970275345627169644";
awCat["nw"]="7353247892502483142";awCat["so"]="6470456435061152823";awCat["we"]="8717510552131961183";awCat["sci"]="7283840052317974841";
awCat["focus"]="8707101758507654705";awCat["learn"]="5518283091157329391";awCat["let"]="8944832427077164797";awCat["home"]="7823046207158046019";
awCat["liv"]="7858129930789770420";awCat["fan"]="7577050880568642942";awCat["obit"]="8198942280837113337";awCat["oped"]="5090486158680163443";
awCat["real"]="5529410133955995200";awCat["sidekick"]="7966542532468766448";awCat["spt"]="9015811964507303591";awCat["style"]="5034594608974683903";
awCat["tv"]="7308679099763264341";awCat["travel"]="8510380778365867437";awCat["world"]="5315899893841182717";

function commTease(){

	var docUrl = cciID;
	for(i=0;i<document.getElementsByTagName("meta").length;i++){
		if(document.getElementsByTagName("meta")[i].httpEquiv=="Description"){
			var aSummary = document.getElementsByTagName("meta")[i].content;
		}
	}
	var articleUrl;
	if(document.location.href.indexOf("#")>-1){
		articleUrl = document.location.href.split("#")[0]
	}else{
		if(document.location.href.indexOf("?")>-1){
			articleUrl = document.location.href.split("?")[0]	
		}else{
			articleUrl = document.location.href
		}	
	}
	var dateLine
	if(document.getElementById("dateline")){
		dateLine = document.getElementById("dateline").innerHTML.replace(/^\s+/g, "" ).replace(/\s+$/g, "" );	
		dateLine=dateLine.toLowerCase();
		if(dateLine.indexOf("</span>")>-1){
			dateLine = dateLine.split("</span>")[1];
			dateLine = dateLine.replace(/^\s+/g, "" ).replace(/\s+$/g, "" );	
		}
	}else{
		var d=new Date();
		var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");	
		dateLine = (monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear());
	}
	var fName;
	var lName;
	var uName;
	if(document.getElementById("byline")){
		var byLine = document.getElementById("byline").innerHTML.replace(/^\s+/g, "" ).replace(/\s+$/g, "" ).replace("By","").replace(/^\s+/g, "" )		
		fName = byLine.substring(0,byLine.indexOf(" "))
		lName = byLine.substring((byLine.indexOf(" ")+1),(byLine.length))
		uName = "bdc-" + fName + lName;
	}else{
		fName = "";
		lName = "";
		uName = "";
	}

	var subHed
	if(document.getElementsByTagName("H2").length>0){
		subHed = escape(document.getElementsByTagName("H2")[0].innerHTML);
	}else{
		subHed = "";		
	}

	var request = 'http://cache.boston.com/jobs/i/bcom_article_tools_comments.js';
    aObj = new JSONscriptRequest(request,"noflag");
    aObj.buildScriptTag();
    aObj.addScriptTag();

	var request = 'http://people.boston.com/default.aspx?p=BCCommentCountOnly&eid=cCount&docurl=' + docUrl;
    aObj = new JSONscriptRequest(request);
    aObj.buildScriptTag();
    aObj.addScriptTag();


	request = 'http://people.boston.com/widgetHost.aspx?eid=aComments&wn=/BCCommentWidgetSmall&docurl=' + docUrl + '&articleurl=' + articleUrl + '&fname=' + fName + '&lname=' + lName + '&username=' + uName + '&affiliation=&title=' + escape(document.title.replace(" - The Boston Globe","")) + '&subhead=' + subHed + '&summary=' + escape(aSummary) + '&pubdate=' + dateLine + '&categoryid=' + awCat[desk];
    aObj = new JSONscriptRequest(request);
    aObj.buildScriptTag();
    aObj.addScriptTag();

	request = 'http://people.boston.com/widgetHost.aspx?eid=toolsComments&wn=/BCArticleAddComment&docurl=' + docUrl + '&articleurl=' + articleUrl + '&fname=' + fName + '&lname=' + lName + '&username=' + uName + '&affiliation=&title=' + escape(document.title.replace(" - The Boston Globe","")) + '&subhead=' + subHed + '&summary=' + escape(aSummary) + '&pubdate=' + dateLine + '&categoryid=' + awCat[desk];
    aObj = new JSONscriptRequest(request);
    aObj.buildScriptTag();
    aObj.addScriptTag();

}
