
/*
	Comments v1.0
	
	Written by Design Technologist
		- Seth Van Booven [svanbooven@sacbee.com]
	
	Copyright © The Sacramento Bee
	Created: March 1, 2006 (sv)  /  Last update: 10/24/06 (sv)
	
	****************************************************
		IF YOU DON'T KNOW WHAT YOU ARE DOING
		THEN YOU DON'T BELONG HERE.
		.........................CLOSE IMMEDIATELY!
	****************************************************
------------------------------------------------------------*/

var ajax = {
	req : null,
	loadRequest : function(url, method) {
		if (window.XMLHttpRequest) {
			this.req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			this.req = new ActiveXObject('Microsoft.XMLHTTP');
		}
		if (this.req) {
			this.req.onreadystatechange = function() {
				ajax.getRequest(method);
			};
			this.req.open('GET', url, true);
			this.req.send(null);
		} else {
			return;
		}
	},
	getRequest : function(method) {
	    if (this.req.readyState == 4) {
	        if (this.req.status == 200) {
				var m = eval(method);
				m(this.req.responseText);
	        } else {
				//alert('There was a problem retrieving your request. ' + this.req.statusText);
	        }
	    }
	}
}

// ++++++++++++++++++++++++++

function doCommentPreview() {
	var username = document.getElementById('username').value;
	var title = document.getElementById('title').value;
	var comment = document.getElementById('comment').value;
	var NewText = comment.replace(/\n\n/g, '</p><p>');
	NewText = NewText.replace(/\n/g, '<br />');
	var s = '<h4>Preview:</h4><p class="title">' + username + ' wrote: ' + title + '</p><p class="msg">' + NewText + '</p>';
	document.getElementById('comments_preview').innerHTML = s;
	document.getElementById('comments_preview').style.display = 'block';
}
function doCommentPreview2() {
	var username = document.getElementById('username').value;
	var title = document.getElementById('title').value;
	var comment = document.getElementById('comment').value;
	var NewText = comment.replace(/\n\n/g, '</p><p>');
	NewText = NewText.replace(/\n/g, '<br />');
	var s = '<h4>Preview: <span id="chars_left"></span></h4><p class="title">' + username + ' wrote: ' + title + '</p><p class="msg">' + NewText + '</p>';
	document.getElementById('comments_preview').innerHTML = s;
	document.getElementById('comments_preview').style.display = 'block';
	textCountUp(document.getElementById('comment'), 1000);
}
function textCountDown(field, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0,maxlimit);
	}
	var number = maxlimit - field.value.length;
	document.getElementById('chars_left').innerHTML = number + ' character' + ((number == 1) ? '' : 's') + ' left';
}
function textCountUp(field, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0,maxlimit);
	}
	var number = field.value.length;
	document.getElementById('chars_left').innerHTML = number + ' of 1000 character' + ((number == 1) ? '' : 's') + '';
}
function tieTo() {
	var u = '';
	u = document.location.pathname.replace(/^.*?(\d+)(c|-p\d)?\.html/ig, "$1");
	//alert(u);
	if (u != '') {
		if (document.location.pathname.indexOf('/weblogs') > -1) {
			u = '9999' + u;
		}
		u = '&tie_to=' + u;
	}
	return u;
}
function urlType() {
	var u = '1';
	if (document.location.pathname.indexOf('/weblogs') > -1) {
		u = '6';
	}
	return u;
}
function buildComments(s) {
	//if (isReqCompatible()) {
		var getHTML = new loadXMLDocObj('/dyn/comments/ajax/comments_simple.html?uri=' + location.protocol + '//' + location.hostname + location.pathname + '&url_type=' + urlType() + '&headline=' + escape(s) + '' + tieTo(), 'comments_here', '');
	//}
}

// ++++++++++++++++++++++++++

function getCommentTotal(v) {
	var n = v;
	if (n == undefined) {
		var obj = document.getElementById('comments_nav');
		if (!obj) return;
		var s = obj.innerHTML.split('|');
		s = s[1].split(' ');
		n = s[1];
	}
	n = parseInt(n);
	if (isNaN(n)) {
		
	} else {
		var elm = document.getElementById('comments_jump');
		if (!elm) return;
		elm.innerHTML = 'Comments (' + n + ')';
	}
}
function getManyCommentTotals(v) {
	var n = eval('(' + v + ')');
	for (var i = 0; i < n.nums.length; i++) {
		var id = n.nums[i].id;
		if (document.location.pathname.indexOf('/weblogs') > -1) {
			id = id.toString().substring(4)
		}
		var o = document.getElementById('cn_' + id);
		if (!o) continue;
		o.innerHTML = 'Comments (' + n.nums[i].n + ')';
	}
}
function doCommentTotalsStr(str) {
	var s = str;
	if (document.location.pathname.indexOf('/weblogs') > -1) {
		s = modCommentTotalsStr(str);
	}
	return s;
}
function modCommentTotalsStr(str) {
	var ta = str.split('|');
	var s = '';
	for (var i = 0; i < ta.length; i++) {
		s += '9999' + ta[i] + '|';
	}
	return s;
}
function showCommentTotals(str) {
	//if (isReqCompatible()) {
		//showCommentNums("{'nums' : [{id:11, n:1},{id:7, n:1},{id:3, n:1}]}");
		ajax.loadRequest('/dyn/comments/ajax/comment_count.html?ids=' + doCommentTotalsStr(str), 'getManyCommentTotals');
	//}
}
