var noComments = true;
function writeComments(ar) {
	if (ar.length == 0) {
		document.getElementById('videoComments').innerHTML = '';
		noComments = true;
		showPost();
	}
	else {
		noComments = false;
		str = '';
		for (var i = 0; i < ar.length; i++) {
			if(ar[i].timeStamp.indexOf('ago')>-1){
				ar[i].timeStamp = '<div style="font-weight:normal;color:#9A959F" class="cnnnotGryTmeStmp">'+ar[i].timeStamp+'</div>';
			}
			else{
				ar[i].timeStamp = ar[i].timeStamp.replace(/Gry/,"notGry").replace(/class/,"style='font-weight:normal;color:#9A959F' class");
			}
			str += '<div><img src="/video/bvp/images/comment_top.gif" width="228" height="7" alt="" border="0"></div>';
			str += '<div class="cnnVPComContent">';
			str += '<div style="color:#000000;font-weight:bold; padding-left:9px; padding-right:9px">'+ar[i].author+'<div style="font-size:10px">'+ar[i].timeStamp+'</div></div>';
			str += '<div style="line-height: 9pt; color:#666666;font-size:10px;padding-left:9px; padding-right:9px">'+ar[i].description+'</div></a>';
			str += '</div><div style="padding-bottom:10px;"><img src="/video/bvp/images/comment_bot.gif" width="228" height="7" alt="" border="0"></div>';
		}
		document.getElementById('videoComments').innerHTML = str;
	}
}

function commentObj(auth,stamp,desc) {
	this.author = auth;
	this.timeStamp = stamp;
	this.description = desc;
}

function iterateComments(obj) {
	var CNN_comment = '';
	var count = 0;
	if(!obj || !obj.rss || !obj.rss.channel || !obj.rss.channel.item) {
		writeComments(new Array());
	}
	else {
		commentArray = new Array();
    if (typeof(obj.rss.channel.item.length) == "undefined") {
			var clObject = obj.rss.channel.item;
			commentArray[commentArray.length] = new commentObj(clObject['jf:author'],cnnRenderTimeStamp(clObject['pubDate']),clObject['description']);
			count = 1;
    } else {
			var numLength = obj.rss.channel.item.length;
			count = numLength;
    	for (var xx = 0; xx < numLength; xx++) {
				var clObject = obj.rss.channel.item[xx];
				commentArray[commentArray.length] = new commentObj(clObject['jf:author'],cnnRenderTimeStamp(clObject['pubDate']),clObject['description']);
			}
    }			
		writeComments(commentArray);
	}
	//document.getElementById('numComments1').innerHTML = '('+count+')';
	//document.getElementById('numComments2').innerHTML = '('+count+')';
}

function validateComment(formName){
	$(formName).forumName.value  = 'bvpvideo';
	document.getElementById('body').value = document.getElementById("comment").value;
	var cnnSubmitForm = true;
	if (cnnSubmitForm) {
                document.commentsForm.submit();
                sub_Post();
        }
}

function validateComment1(formName) {
	var cnnSubmitForm = true;

//	$(formName).threadName.value = 'urlurl';
  $(formName).forumName.value  = 'bvpvideo';

	var errorDivs = $('commentsForm').getElementsByClassName('cnnError');
	for (var i = 0; i<errorDivs.length; i++) {
		errorDivs[i].remove();
	}

	allFormEls = Form.getElements(formName);
	for(i = 0; i < allFormEls.length; i++) {
    //do something to each form field
    allFormEls[i].value = allFormEls[i].value.strip().stripScripts().stripTags();
    if (allFormEls[i].value == "") {
    		if (allFormEls[i].name == "name") {
    			new Insertion.Before('cnnUserResponseName',' <span id="cnnUserResponseNameError" class="cnnError">&raquo;<\/span>');
    		}
    		if (allFormEls[i].name == "location") {
    			new Insertion.Before('cnnUserResponseLocation',' <span id="cnnUserResponseNameError" class="cnnError">&raquo;<\/span>');
    		}
    		if (allFormEls[i].name == "comment") {
    			new Insertion.Before('cnnUserResponseComment',' <span id="cnnUserResponseNameError" class="cnnError">&raquo;<\/span>');
    		}
    	cnnSubmitForm = false;	
		}
	}

	if (cnnSubmitForm) {
		document.commentsForm.submit();
		sub_Post();
	}  	
}

function cnnRenderTimeStamp(date) {

	cnnStoryPublishTime = (date) ? new Date(date) : cnnStoryPublishTime;
	var days = new Array('Sun','Mon','Tue','Wed','Thur','Fri','Sat');
	var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	var cnnTimeStampString;
    	var cnnTimeStampDiff = cnnCurrTime.getTime() - cnnStoryPublishTime.getTime();
    
	var daysDifference = Math.floor(cnnTimeStampDiff/1000/60/60/24);
    	cnnTimeStampDiff -= daysDifference*1000*60*60*24
    	var hoursDifference = Math.floor(cnnTimeStampDiff/1000/60/60);
    	cnnTimeStampDiff -= hoursDifference*1000*60*60
    	var minutesDifference = Math.floor(cnnTimeStampDiff/1000/60);
    	cnnTimeStampDiff -= minutesDifference*1000*60
    	var secondsDifference = Math.floor(cnnTimeStampDiff/1000);	

	var cnnDays = (daysDifference > 1) ? "days" : "day";
	var cnnHours = (hoursDifference > 1) ? "hours" : "hour";
	var cnnMinutes = (minutesDifference > 1) ? "minutes" : "minute";
	var cnnSeconds = (secondsDifference > 1) ? "seconds" : "second";
	
	if (daysDifference >= 3) {
		cnnTimeStampString = "<div>updated " + days[cnnStoryPublishTime.getDay()] + " " + months[cnnStoryPublishTime.getMonth()] + " " + cnnStoryPublishTime.getDate() + ", " + cnnStoryPublishTime.getFullYear() + "<\/div>";
	} else if(daysDifference < 3 && daysDifference >= 1) {
		cnnTimeStampString = "<div>updated " + daysDifference + " "+cnnDays+" ago<\/div>";
	} else if(hoursDifference >= 1 && hoursDifference < 24) {
		if(daysDifference < 0) {
			cnnTimeStampString = "updated less than a minute ago";
		} else if (minutesDifference > 0) {
			cnnTimeStampString = "<div>updated " + hoursDifference + " "+cnnHours+", " + minutesDifference + " "+cnnMinutes+" ago<\/div>";
		} else {
			cnnTimeStampString = "<div>updated " + hoursDifference + " "+cnnHours+" ago<\/div>";
		}
	} else if(hoursDifference < 1 && minutesDifference > 0){
		cnnTimeStampString = "updated " + minutesDifference + " "+cnnMinutes+" ago";
	} else {
		cnnTimeStampString = "updated " + secondsDifference + " "+cnnSeconds+" ago";
	}
		return cnnTimeStampString;
}

