AARP.Comments = {
	__version__: ''
	, defaultText: ""
	, textUnset: function( obj )
	{
		defaultText.unset( obj, this.defaultText );
	}
	, textSet: function( obj )
	{
		defaultText.set( obj, this.defaultText );
	}
}


var AARPComments = {
	host: ''
};

AARPComments.host = location.hostname;
//alert(location.hostname);

function submitDeleteProfile(commentId) {
	var errorFlag = 0;
	
	if (errorFlag == 0) {
		var formValues = $('deleteFormTestimonial' + commentId).serialize();
		var loginRequest = new Ajax.Request( '/community/profile/deleteProfileTestimonial.bt', {method: 'post', parameters: formValues, onSuccess: showTestimonialResponse} );
	}
}

function submitDeleteAll(commentId) {
	var errorFlag = 0;
	
	if (errorFlag == 0) {
		var formValues = $('deleteFormTestimonial' + commentId).serialize();
// since we can have more than one page here, we need to find out what page number we are on and pass it on. 
                var pageNum = getPageNumber();
                if (pageNum != null){ 
                 formValues = formValues + "&pageNum="+pageNum;
                  } 
		var loginRequest = new Ajax.Request( '/community/profile/deleteFromAllTestimonial.bt', {method: 'post', parameters: formValues, onSuccess: showTestimonialResponse} );
	}
}

function submitNewTestimonial() {
var errorFlag = 0;
	
	if (errorFlag == 0) {
		var formValues = $('testimonialForm').serialize();
                var pageNum = getPageNumber();
                if (pageNum != null){ 
                 formValues = formValues + "&pageNum="+pageNum;
                  } 
		var loginRequest = new Ajax.Request( '/community/profile/submitTestimonial.bt', {method: 'post', parameters: formValues, onSuccess: showTestimonialResponse} );
	}
}



function submitNewTestimonialReply(commentId) {
	var errorFlag = 0;
	
	if (errorFlag == 0) {
 		
		var formValues = $('addTestimonialForm' + commentId).serialize(); 
                var pageNum = getPageNumber();
                if (pageNum != null){ 
                 formValues = formValues + "&pageNum="+pageNum;
                  } 
		var loginRequest = new Ajax.Request( '/community/profile/submitTestimonial.bt', {method: 'post', parameters: formValues, onSuccess: showTestimonialResponse} );
	}
}


function submitDeleteComment(commentId) {
	var errorFlag = 0;
	
	if (errorFlag == 0) {
		var formValues = $('deleteFormComment' + commentId).serialize();
// since we can have more than one page here, we need to find out what page number we are on and pass it on. 
                var pageNum = getPageNumber();
                if (pageNum != null){ 
                 formValues = formValues + "&pageNum="+pageNum;
                  } 
		var loginRequest = new Ajax.Request( '/community/content/comment/delete.bt', {method: 'post', parameters: formValues, onSuccess: showCommentResponse} );
	}
}





function showTestimonialResponse(originalRequest) {
	var gotBack = originalRequest.responseText;
	if (gotBack.match(/Error/)) {
		return false;
	}
	else {

$('memberTestimonials').update(gotBack);
	}
}


function showCommentResponse(originalRequest) {
	var gotBack = originalRequest.responseText;
	if (gotBack.match(/Error/)) {
		return false;
	}
	else {

$('commentDiv').update(gotBack);
	}
}


function getPageNumber() {

            var currLocation = (window.location).toString();
            page_num_object = currLocation.toQueryParams();
            if (page_num_object.pageNum) {
               return page_num_object.pageNum;
}
            else {
	       return null;
              }
}

function checkLoginStatus(cookieName) {

	if (document.cookie.length > 0) {
	 
		headerStart = document.cookie.indexOf(cookieName+"="); 
	
		if (headerStart != -1) 
			{ 
			headerStart += cookieName.length+1; 
			headerEnd = document.cookie.indexOf(";", headerStart);
	
			if (headerEnd == -1) {
				headerEnd = document.cookie.length;
                                 }
			return unescape(document.cookie.substring(headerStart, headerEnd)); 
				} 
			
		return null; 
		}
		return null; 
	}

function generateArticleCommentSubmitArea() {
	if ( AARP.page.is_authoring ) return;
	
	// first let's find out if they are logged in
	var loginStatus = checkLoginStatus("header");

	if (loginStatus != null) {
		// lets see who this is, since we need to know for the form. 
	
		var userName = loginStatus.split('|')[2];
	
		document.write("<form action='#' id='articleCommentForm' method='post' onSubmit='return false;'>");
		document.write("<input type='hidden' name='membername' value='" + userName +"'>");
		document.write("<input type='hidden' name='articleUrl' value='" + AARP.page.url +"'>"); // checkArticleUrl()
		document.write("<input type='hidden' name='method' value='submitNewArticleComment'>");
		document.write("<p>Add Your Comments:</p>");
		document.write("<textarea name='comment' id='comment' class='articleCommentFormInput' rows='4' onfocus='AARP.Comments.textUnset( this );' onblur='AARP.Comments.textSet( this );'></textarea>");
		document.write("<a href=\"javascript:submitNewArticleComment();\"><img id=\"content_submit\" class=\"button\" src=\"http://assets.aarp.org/aarp.org_/build/topics/images/buttons/btn24_submit.gif\" onmouseover=\"this.src='http://assets.aarp.org/aarp.org_/build/topics/images/buttons/btn24_submit_over.gif';\" onmouseout=\"this.src='http://assets.aarp.org/aarp.org_/build/topics/images/buttons/btn24_submit.gif';\"/></a>&nbsp;");

		document.write("<a href=\"javascript:previewNewArticleComment();\"><img id=\"content_preview\" class=\"button\" src=\"http://assets.aarp.org/aarp.org_/build/topics/images/buttons/btn24_preview.gif\" onmouseover=\"this.src='http://assets.aarp.org/aarp.org_/build/topics/images/buttons/btn24_preview_over.gif';\" onmouseout=\"this.src='http://assets.aarp.org/aarp.org_/build/topics/images/buttons/btn24_preview.gif';\"/></a></form>");
		document.write("<div id='commentResponse' class='generalStatus' style='display: none;'></div>");
	
		}
	else {
		document.write("<p>Please log in to leave a comment. <a href='#' onclick='jumpToLogin();'>Log In</a> | <a href='https://login.aarp.org/community/register/index.bt'>Sign Up</a></p>");

		// return the notloggedin form.
		document.write("<textarea name='comment' id='comment' class='articleCommentFormInput' rows=4 disabled></textarea><BR>");
		document.write("<img src='http://assets.aarp.org/aarp.org_/build/topics/images/buttons/btn24_submit_off.gif' alt='Submit'>&nbsp;<img src='http://assets.aarp.org/aarp.org_/build/topics/images/buttons/btn24_preview_off.gif' alt='Preview'>");
		}
}

function grabRecentComments() {
	if ( AARP.page.is_authoring ) return;
    params = 'method=viewArticleComments&articleID=' + AARPAds.pgid;

    AJAXUrl = 'http://' + AARPComments.host + '/community/articleComments/comments.bt';

	var grabCommentsRequest = new Ajax.Request( AJAXUrl, {method: 'post', parameters: params, onSuccess: showArticleComments, onFailure: showError} );
}

function grabPagedCommentsForArticle(url, sortOrder) {
    
    AJAXUrl = 'http://' + AARPComments.host + url +'&sOr=' + sortOrder;
   
	var grabCommentsRequest = new Ajax.Request( AJAXUrl, {method: 'post', onSuccess: showArticleComments, onFailure: showError} );
}


function showError(originalRequest) {
   alert('Error ' + originalRequest.status + ' -- ' + originalRequest.statusText);
}

function showArticleComments(originalRequest) {
	var gotBack = originalRequest.responseText.replace(/href=\"\/community/g,'href=\"http://www.aarp.org/community');

$('recentCommentsSlot').update(gotBack);
	
}

function successfulCommentAddition(originalRequest) {
   //alert('successfulCommentAddition');
   showArticleComments(originalRequest);
   $('comment').value="";
   $('commentResponse').update( "Thanks for your comment!" ); //<P><B></B></P>
	Effect.Appear( 'commentResponse' );
	setTimeout( "Effect.Squish( 'commentResponse' );", 4000 );
}

var debug_url = '';

function submitNewArticleComment() {
	// NEW (2007-12-13): issues with serialize() on Safari 2.x and other WebKit browsers
	//var formValues = $('articleCommentForm').serialize() + '&articleID=' + AARPAds.pgid;
	var formValues = Form.serialize( 'articleCommentForm', null ) + '&articleID=' + AARPAds.pgid;
	AJAXUrl = 'http://' + AARPComments.host + '/community/articleComments/comment/submit.bt';
	// alert(AJAXUrl + "\n" + formValues);
	debug_url = AJAXUrl + '?' + formValues;
	
	var submitNewArticleCommentRequest = new Ajax.Request( 
		AJAXUrl, { method: 'post'
		, parameters: formValues
		, onSuccess: successfulCommentAddition
		//, onComplete: function( o ) { alert( 'complete: ' + o ); }
		, onFailure: function( o )
			{
				if ( document.location.toString().match( /debugNow/ ) )
				{
					$( 'comment' ).value = debug_url + "\n\n" + o.responseText;
				}
			} 
		, onException: function( o, e )
			{
				if ( document.location.toString().match( /debugNow/ ) )
				{
					alert( 'submit new comment exception: ' + e.message ); 
				}
			}
		} 
	);
	
	// debugging
	//$('previewArea').style.display = 'block';
	//$('previewArea').innerHTML = 'Debug:<br />' + AJAXUrl + '?' + formValues;
	$('previewArea').style.display="none";
}

function submitNewContentComment() {

    var commentValue = $('postCommentForm').comment.value;
	    if(commentValue == '')
	    {
	        alert("You cannot post a blank comment.  Please try again!");
	        return false;
	    }else
	    {
	      var formValues = $('postCommentForm').serialize();
		AJAXUrl = 'http://'+AARPComments.host+'/community/content/comment/submit.bt';
		var submitNewContentComment = new Ajax.Request(AJAXUrl, {method:'post', parameters: formValues, onSuccess: showCommentResponse});
	    }
}

function previewNewArticleComment() {
	  
               var previewComment = $('comment').value.replace(/\n/g, "<br />");
	       
	       $('previewArea').style.display="block";
	       $('previewComment').update(previewComment);
			}

function checkArticleUrl() {

		var tmpArticleUrl = location.href.match(new RegExp('(.*\.html)(.*)'));
		var articleUrl = tmpArticleUrl[1];
		return articleUrl.replace( ':4402/author/home', '' );
}

function jumpToLogin() {
		
                openLogin();
		}

function submitDeleteArticleComment(commentId) {
		
			var params = "method=deleteArticleComment&commentId=" + commentId + "&articleID=" + AARPAds.pgid;
	// since we can have more than one page here, we need to find out what page number we are on and pass it on. 
	               // var pageNum = getPageNumber();
	                //if (pageNum != null){ 
	                 //formValues = formValues + "&pageNum="+pageNum;
	                 // } 
			var loginRequest = new Ajax.Request( '/community/articleComments/comment/delete.bt', {method: 'post', parameters: params, onSuccess: showArticleComments} );
		}

function showArticleCommentDeleteConfirmation(commentId) {
        document.getElementById("deleteButton" + commentId).style.display = 'none';
        document.getElementById("deleteConfirmationFormDiv" + commentId).style.display = 'block';
    }

function showArticleCommentDeleteForm(commentId) {
    document.getElementById("deleteConfirmationFormDiv" + commentId).style.display = 'none';
    document.getElementById("deleteButton" + commentId).style.display = '';
}

function showFlagConfirmation(commentId) {
    document.getElementById("flagButton" + commentId).style.display = 'none';
    document.getElementById("flagConfirmationFormDiv" + commentId).style.display = 'block';
}

function showFlagForm(commentId) {
    document.getElementById("flagConfirmationFormDiv" + commentId).style.display = 'none';
    document.getElementById("flagButton" + commentId).style.display = '';
}


function showArticleCommentFlagConfirmation(commentId) {
        document.getElementById("flagButton" + commentId).style.display = 'none';
        document.getElementById("flagConfirmationFormDiv" + commentId).style.display = 'block';
    }

function showArticleCommentFlagForm(commentId) {
    document.getElementById("flagConfirmationFormDiv" + commentId).style.display = 'none';
    document.getElementById("flagButton" + commentId).style.display = '';
}



function submitFlagProfile(commentId) {
	var errorFlag = 0;
	
	if (errorFlag == 0) {
		var formValues = $('flagFormTestimonial' + commentId).serialize();
		formValues = formValues + "&itemTypeId=3";
		
		var loginRequest = new Ajax.Request( '/community/profile/flagProfileTestimonial.bt', {method: 'post', parameters: formValues, onSuccess: showTestimonialResponse} );
	}
}

function submitFlagAll(commentId) {
	var errorFlag = 0;
	
	if (errorFlag == 0) {
		var formValues = $('flagFormTestimonial' + commentId).serialize();
// since we can have more than one page here, we need to find out what page number we are on and pass it on. 
                var pageNum = getPageNumber();
                if (pageNum != null){ 
                 formValues = formValues + "&pageNum="+pageNum;
                  }
                 formValues = formValues + "&itemTypeId=3";
		var loginRequest = new Ajax.Request( '/community/profile/flagFromAllTestimonial.bt', {method: 'post', parameters: formValues, onSuccess: showTestimonialResponse} );
	}
}

function submitFlagComment(commentId) {
	var errorFlag = 0;
	
	if (errorFlag == 0) {
		var formValues = $('flagFormComment' + commentId).serialize();
// since we can have more than one page here, we need to find out what page number we are on and pass it on. 
                var pageNum = getPageNumber();
                if (pageNum != null){ 
                 formValues = formValues + "&pageNum="+pageNum;
                  }
                 formValues = formValues + "&itemTypeId=2";
		var loginRequest = new Ajax.Request( '/community/content/comment/flag.bt', {method: 'post', parameters: formValues, onSuccess: showCommentResponse} );
	}
}

function submitFlagArticleComment(commentId) {
		
		var params = "commentId=" + commentId + "&itemTypeId=4&method=flagArticleComments";
			// since we can have more than one page here, we need to find out what page number we are on and pass it on. 
			               // var pageNum = getPageNumber();
			                //if (pageNum != null){ 
			                 //formValues = formValues + "&pageNum="+pageNum;
			                 // } 
					var loginRequest = new Ajax.Request( '/community/flagItems/flagContent.bt', {method: 'post', parameters: params, onSuccess:showArticleCommentFlagForm(commentId)} );
					
		}




