
/* These two functions really should be in a global location (they're both in blog and discuss.js right now), but
I didn't know how the vars that sit outside the functions would interact with functions in different js files, so
for now, in the interest of time, they're in multiple places
*/
var BACKSPACE_KEYCODE = 8;
var maxlength = 4000;

function textareakeypress (field, event)
{
	var keycode = event.keyCode;

	if (keycode === 0) {keycode = event.which;} // Firefox, so use "event.which".

	if ((field.value.length >= maxlength) && (keycode != BACKSPACE_KEYCODE))
	{
		alert ("That keypress would exceed the maximum allowed length of " + maxlength + ".");
		return false;
	}
}

function textareapaste (field)
{ 	//IE only

	var clipboardlength = window.clipboardData.getData ("text").length;
	if (field.value.length + clipboardlength > maxlength)
	{
		alert ("That paste would exceed the maximum allowed length of " + maxlength + ".");
		return false;
	}
}

gcommentformsubmittedp = false;

function submitcomment(userp){
	
	if(gcommentformsubmittedp){
		alert("Your request was already submitted; please wait.");
		return(false);
	}
	
	var blogform = document.getElementById('commentform');
	var errstring = '';
	var loggedinp = userp;
	var newuserp = false;
	
	if( blogform.commentbody.value.trim() === '') {
		errstring += 'You cannot submit an empty comment.\n\n';
	}
	
	if( blogform.commentbody.value.trim().length > maxlength){
		errstring += 'You cannot submit a post longer than ' + maxlength + ' characters.  Please shorten your comment before submitting.\n\n';
	}

	if(errstring !== ''){
		alert(errstring);
		return(false);
	}else{
		//before submitting, update hidden field to set to "new" (it defaults to "returning")
		blogform.commentsubmit.disabled = true;
		gcommentformsubmittedp = true;
		if(!userp && newuserp) {blogform.usertype.value = 'new';}
		return(true);
	}
}



function collapseallcomments(classname){
	
	hideobjsbyclassname('div','commentbody');
	
	document.getElementById('togglecomments').href = "javascript:expandallcomments();";
	document.getElementById('togglecomments').innerHTML = 'expand all comments';
	
	var anchors = document.getElementsByClassName('commenterlink');
	for (var i = 0; i<anchors.length;i++){
		anchors[i].href = 'javascript:expandcomment(' + anchors[i].id.substring(13) + ')';
	}
	
	var imgs = document.getElementsByClassName('commenttoggleimg');
	for (var j = 0; j<imgs.length;j++){
		imgs[j].src = 'http://www.trumpuniversity.com/lib/images/icons/expand.gif';
	}

}

function expandallcomments(classname){

	showobjsbyclassname('div','commentbody');

	document.getElementById('togglecomments').href = "javascript:collapseallcomments();";
	document.getElementById('togglecomments').innerHTML = 'collapse all comments';
	
	var anchors = document.getElementsByClassName('commenterlink');
	for (var i = 0; i<anchors.length;i++){
		anchors[i].href = 'javascript:collapsecomment(' + anchors[i].id.substring(13) + ')';
	}
	
	var imgs = document.getElementsByClassName('commenttoggleimg');
	for (var j = 0; j<imgs.length;j++){
		imgs[j].src = 'http://www.trumpuniversity.com/lib/images/icons/collapse.gif';
	}	
	
}

function collapsecomment(id){

	var thisbody = document.getElementById('commentbody' + id);
	var thislink = document.getElementById('commenterlink' + id);
	var thisimg = document.getElementById('commenttoggleimg' + id);
	
	if(thisbody && thislink && thisimg){
		thisbody.style.display = 'none';
		thislink.href = 'javascript:expandcomment(' + id + ')';
		thisimg.src = 'http://www.trumpuniversity.com/lib/images/icons/expand.gif';
	}
}

function expandcomment(id){

	var thisbody = document.getElementById('commentbody' + id);
	var thislink = document.getElementById('commenterlink' + id);
	var thisimg = document.getElementById('commenttoggleimg' + id);	
	
	if(thisbody && thislink && thisimg){
		thisbody.style.display = 'block';
		thislink.href = 'javascript:collapsecomment(' + id + ')';
		thisimg.src = 'http://www.trumpuniversity.com/lib/images/icons/collapse.gif';
	}
}

function loadblogads(){
	/* This is a bit convoluted, but...
	This function is called on the body onload event because calling it prior
	to the page fully loading can cause "Operation aborted" errors in IE6 and 7.
	The "real" solution is to not use body onload, but instead use mootools "onDomReady"
	event, but until we can integrate mootools, we have to do it this way.
	Anyway, the "blogadarray" array is defined in cf_blogtemplate, and then added to in the 
	auto-generated files in secure/admin/blog/manageads.cfm.
	*/
	for(var i=0;i<blogadarray.length;i++){
		loadrandomad(blogadarray[i].container,blogadarray[i].dataarray,blogadarray[i].pos);
	}
	
}

