﻿//constants
var DEFAULT_MSG_DIV='dedicationMessage';
var DEFAULT_MSG2_DIV='dedicationMessage2';
var DEFAULT_LINK_DIV='dedicationDefaultLink';
var AVAILABLE_FRIENDS_DIV='availableFriends';
var AVAILABLE_FRIENDS_MESSAGE_DIV='availableFriendsMessage';
var DEDICATION_INVITE_AVATAR='imgDedicationAvatar';
var DEDICATION_INVITEPENDING_AVATAR='imgDedicationPendingAvatar';
var DEDICATION_INVITE_DIV='dedicationInvite';
var DEDICATION_CANCELINVITE_DIV='dedicationInviteLink';
var DEDICATION_PENDINGINVITE_DIV='divPendingInvite';
var DEDICATION_CANCELINVITECONFIRM_DIV='dedicationInviteCancel';
var DEDICATION_CANCELINVITECONFIRMBUTTONS_DIV='dedicationInviteCancelButtons';
var MOTIVATION_MESSAGES_DIV='motivationMessages';
var MOTIVATION_SENT_DIV='motivationMessagesSent';
var MOTIVATION_QUESTION_DIV='ctl00_Column4Main_ctlMotivationCenter_motivationMessagesSent';
var MOTIVATION_RESPONSE_DIV='motivationMessageResponse';
var MOTIVATION_READYTORESPOND_DIV = 'divSendNote';
var MOTIVATION_MESSAGES2_DIV = 'motivationMessages2';
var MOTIVATION_RESPONSE2_DIV = 'motivationSentResponse';
var MOTIVATION_MESSAGE_ANSWERED_DIV = 'motivationMessageAnswered';
var INVITE_BUTTON_DIV = 'dedicationButtons';
var RIGHT_INVITE_BUTTON_DIV='dedicationInviteButtonsRight';
var RIGHT_INVITECANCEL_BUTTON_DIV='dedicationInviteCancelButtonsRight';
var RIGHT_DEFAULT_BUTTON_DIV='defaultButtonRight';
var BACKTODEDICATION_DIV='dedicationBack';
var INVITE_DECLINE_DIV='motivationConfirmDecline';
var INVITE_DECLINE_BUTTONS_DIV='dedicationConfirmDeclineButtons';
var INVITE_DECLINE_RIGHT_BUTTONS_DIV='dedicationConfirmDeclineRightButtons';
//global variables
var m_strInviteCopy;
var m_strCurrentAvatar;
var m_strCurrentDisplayName;
var m_strCurrentFID;
var m_currentFID;
m_bDoMessageAjax=false;


//Since the "Why Dedicate" link only links to itself, if we're on the
//mydedications page, hide this link.
function checkDedicationFooterLink() {
	var bDedicationPage = urlContains('/mycorner/users/mydedication.aspx');
	if (document.getElementById('dedicationFooterLink')) {
		if (bDedicationPage) {
			var anc = document.getElementById('dedicationFooterLink');
			var val = anc.innerHTML;
			if (val.indexOf('Why Dedicate?') > -1) {
				anc.style.display = 'none';
			}
		}
	}
}

function BuildDedicationHandlerURL(fid, m) {
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx';
	strHandlerPath += '?fid=' + fid;
	strHandlerPath += '&m=' + m;
	return strHandlerPath;
}

////////////////////////////////////////////////////////
/* Dedication functions */
////////////////////////////////////////////////////////
//invoked by sender onclick of "add" button.
function showInviteDedicationDialog(btn, fid, avatar, dispname) {
    m_currentFID=fid;
    m_strCurrentAvatar=avatar;
    m_strCurrentDisplayName=dispname;
    m_strInviteCopy=window.document.getElementById(DEDICATION_INVITE_DIV).innerHTML;
	closeDedicationDialog(window.document.getElementById(AVAILABLE_FRIENDS_DIV));
	closeDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
	closeDedicationDialog(window.document.getElementById(DEFAULT_LINK_DIV));
	
	replaceTemplate(window.document.getElementById(DEDICATION_INVITE_DIV),m_strCurrentDisplayName);
    replaceTemplate(window.document.getElementById(DEDICATION_PENDINGINVITE_DIV),m_strCurrentDisplayName);
    
    replaceAvatar(window.document.getElementById(DEDICATION_INVITE_AVATAR),m_strCurrentAvatar);
    showDedicationDialog(window.document.getElementById(DEDICATION_INVITE_DIV));
    showDedicationDialog(window.document.getElementById(INVITE_BUTTON_DIV));
    try{
        closeDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
        showDedicationDialog(window.document.getElementById(RIGHT_INVITE_BUTTON_DIV));
        
    }
    catch(e){
    
    }
    
	
}
function removeInitialMessage(){
    InvokeAshxRemoveInitialMessage();
}
//go back to the select friend screen
function returnToAvailableFriends(){
    
    m_strCurrentAvatar='';
    m_strCurrentDisplayName='';
    m_currentFID='';
    closeInvite();
    closeDedicationDialog(window.document.getElementById(INVITE_BUTTON_DIV));
     
    try{
        closeDedicationDialog(window.document.getElementById(RIGHT_INVITE_BUTTON_DIV));
        showDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
    }
    catch(e){
    
    }
    showDedicationDialog(window.document.getElementById(AVAILABLE_FRIENDS_DIV));
	showDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
	showDedicationDialog(window.document.getElementById(DEFAULT_LINK_DIV));
	
    
}
//go back to the select friend screen
function goToAvailableFriends(){
    
    m_strCurrentAvatar='';
    m_strCurrentDisplayName='';
    m_currentFID='';
    closeInvite();
    closeDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
	closeDedicationDialog(window.document.getElementById(DEFAULT_LINK_DIV));
    showDedicationDialog(window.document.getElementById(AVAILABLE_FRIENDS_DIV));
	showDedicationDialog(window.document.getElementById(AVAILABLE_FRIENDS_MESSAGE_DIV));
	
	
    
}
//go back to the select friend screen
function closeInvite(){
    try{
        closeDedicationDialog(window.document.getElementById(RIGHT_INVITE_BUTTON_DIV));
    }
    catch(e){
    
    }
    closeDedicationDialog(window.document.getElementById(INVITE_BUTTON_DIV));
    closeDedicationDialog(window.document.getElementById(DEDICATION_INVITE_DIV));
    restoreInviteCopy(window.document.getElementById(DEDICATION_INVITE_DIV));
    
}
function inviteDedication() {
   
    var fid=m_currentFID;
	//send an invite message to friend.
	if (fid.length > 0) {
		//make ajax invite friend request.
		InvokeAshxInviteDedication(fid)
	}
}

//an invite has been issued, but no response yet
function displayPendingInvite(){
    
    closeInvite();
    closeDedicationDialog(window.document.getElementById(DEDICATION_CANCELINVITECONFIRM_DIV));
    closeDedicationDialog(window.document.getElementById(DEDICATION_CANCELINVITECONFIRMBUTTONS_DIV));
    try{
    closeDedicationDialog(window.document.getElementById(RIGHT_INVITECANCEL_BUTTON_DIV));
    
    showDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
    }
    catch(e){
    
    }
    replaceTemplate(window.document.getElementById(DEDICATION_PENDINGINVITE_DIV),m_strCurrentDisplayName);
    
    showDedicationDialog(window.document.getElementById(DEDICATION_PENDINGINVITE_DIV));
    showDedicationDialog(window.document.getElementById(DEFAULT_LINK_DIV));
}

//cancel an unanswered invitation
function cancelInvite(fid,avatar,dispname){
    closeDedicationDialog(window.document.getElementById(DEDICATION_PENDINGINVITE_DIV));
    closeDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
	closeDedicationDialog(window.document.getElementById(DEFAULT_LINK_DIV));
	closeDedicationDialog(window.document.getElementById(DEDICATION_CANCELINVITE_DIV));
	showDedicationDialog(window.document.getElementById(DEDICATION_CANCELINVITECONFIRM_DIV));
	showDedicationDialog(window.document.getElementById(DEDICATION_CANCELINVITECONFIRMBUTTONS_DIV));
	try{
    showDedicationDialog(window.document.getElementById(RIGHT_INVITECANCEL_BUTTON_DIV));
    closeDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
    }
    catch(e){
    
    }
	m_currentFID=fid;
	m_strCurrentAvatar=avatar;
    m_strCurrentDisplayName=dispname;
}
function cancelInvitation(){
    
    var fid=m_currentFID;
    //send an cancel invite
	if (fid.length > 0) {
		//make ajax invite friend request.
		InvokeAshxCancelDedicationInvitation(fid)
	}
}
function showDefaultMessage(){
    
	closeDedicationDialog(window.document.getElementById(DEDICATION_CANCELINVITECONFIRM_DIV));
	closeDedicationDialog(window.document.getElementById(DEDICATION_CANCELINVITECONFIRMBUTTONS_DIV));
	try{
    closeDedicationDialog(window.document.getElementById(RIGHT_INVITECANCEL_BUTTON_DIV));
    showDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
    }
    catch(e){
    
    }
	showDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
	showDedicationDialog(window.document.getElementById(DEFAULT_LINK_DIV));
	
}
function motivationResponse(fid,answer,msgid){
   
    InvokeAshxMotivationResponse(fid,answer,msgid);
}
function motivationResponse2(fid,answer,msgid){
    try{
    //alert('1');
    closeDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
    //alert('2');
    }
    catch(e){}
    /*alert('4'+ 'fid= '+ fid);
    alert('answer= '+ answer);
    alert('msgid=' +msgid);*/
    InvokeAshxMotivationResponse2(fid,answer,msgid);
    //alert('5');
}
function acknowledgeComplete(){
    InvokeAshxAcknowledgeComplete();
}
function showMotivationResponse(){
//alert('in showMotivationResponse');
    closeDedicationDialog(window.document.getElementById(MOTIVATION_QUESTION_DIV));
	showDedicationDialog(window.document.getElementById(MOTIVATION_RESPONSE_DIV));
	
}
function showMotivationAnswered(){
    closeDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
	showDedicationDialog(window.document.getElementById(MOTIVATION_MESSAGE_ANSWERED_DIV));
	try{
    closeDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
    showDedicationDialog(window.document.getElementById(BACKTODEDICATION_DIV));
    
    }
    catch(e){}
}
/*MOTIVATOR FUNCTIONS*/
function acceptDedicationInvite(fid){
    m_currentFID=fid;
    //send an cancel invite
	if (fid.length > 0) {
		//make ajax invite friend request.
		InvokeAshxAcceptDedicationInvitation(fid)
	}
}
function declineDedicationInvite(fid){
    m_currentFID=fid;
    //send an cancel invite
	if (fid.length > 0) {
		//make ajax invite friend request.
		InvokeAshxDeclineDedicationInvitation(fid)
	}
}
function declineDedicationInviteConfirm(fid){
    m_currentFID=fid;
    showConfirmDeclineMessage();
}
function showConfirmDeclineMessage(){
    closeDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
	closeDedicationDialog(window.document.getElementById(DEFAULT_LINK_DIV));
    showDedicationDialog(window.document.getElementById(INVITE_DECLINE_DIV));
    try{
    closeDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
    showDedicationDialog(window.document.getElementById(INVITE_DECLINE_RIGHT_BUTTONS_DIV));
    
    }
    catch(e){}
    showDedicationDialog(window.document.getElementById(INVITE_DECLINE_BUTTONS_DIV));
}
function returnToDeclineMessage(){
    showDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
	showDedicationDialog(window.document.getElementById(DEFAULT_LINK_DIV));
	
	try{
    closeDedicationDialog(window.document.getElementById(INVITE_DECLINE_RIGHT_BUTTONS_DIV));
    showDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
    
    }
    catch(e){}
    closeDedicationDialog(window.document.getElementById(INVITE_DECLINE_DIV));
	closeDedicationDialog(window.document.getElementById(INVITE_DECLINE_BUTTONS_DIV));
}
function sendMotivation(fid){
    m_currentFID=fid;
    closeDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
	closeDedicationDialog(window.document.getElementById(DEFAULT_LINK_DIV));
	try{
    closeDedicationDialog(window.document.getElementById(RIGHT_DEFAULT_BUTTON_DIV));
    }
    catch(e){}
    showDedicationDialog(window.document.getElementById(MOTIVATION_MESSAGES_DIV));
    
}
function showMessages(fid){
    m_currentFID=fid;
    closeDedicationDialog(window.document.getElementById(MOTIVATION_READYTORESPOND_DIV));
    showDedicationDialog(window.document.getElementById(MOTIVATION_MESSAGES2_DIV));
    
}
function sendMotivationMessage(msgID,bDoAjax){
    var fid=m_currentFID;
    //send message to dedicator
    if(bDoAjax)m_bDoMessageAjax=true;
    if (fid.length > 0) {
		
		//make ajax invite friend request.
		InvokeAshxSendMotivation(fid,msgID);
		
	}
}
function showMotivationSentSuccess(){
    closeDedicationDialog(window.document.getElementById(MOTIVATION_MESSAGES2_DIV));
    showDedicationDialog(window.document.getElementById(MOTIVATION_RESPONSE2_DIV));
}
function showMotivationSent(){
    closeDedicationDialog(window.document.getElementById(MOTIVATION_MESSAGES_DIV));
    showDedicationDialog(window.document.getElementById(MOTIVATION_SENT_DIV));
    
}
function showSecondaryMessage(){
    closeDedicationDialog(window.document.getElementById(DEFAULT_MSG_DIV));
    showDedicationDialog(window.document.getElementById(DEFAULT_MSG2_DIV));
}
/*DISPLAY FUNCTIONS*/
function closeDedicationDialog(elem) {
	elem.style.display = 'none';
}
function showDedicationDialog(elem) {
	elem.style.display = 'block';
}
function replaceTemplate(elem,dispname){
    var strCopy=elem.innerHTML;
    //m_strInviteCopy=strCopy;
    strCopy=unescape(strCopy);
    strCopy=strCopy.replace("[FRIEND_DISPLAYNAME]",dispname);
    strCopy=strCopy.replace("[INSERT_FID]",m_currentFID);
    
    strCopy=strCopy.replace("[FRIEND_AVATAR]",m_strCurrentAvatar);
    
    
   
    elem.innerHTML=strCopy;
}

function replaceAvatar(elem,src){
    elem.src=src;
    
}
function restoreInviteCopy(elem){
    elem.innerHTML=m_strInviteCopy;
}




////////////////////////////////////////////////////////
/* Invite Dedication AJAX functions */
////////////////////////////////////////////////////////

var fInviteDedicationCallback = function() {
	var intMaxResponseLength = 300;			//Max allowable chars in response.
	var bCatchConnectionErrors = false;		//For development, keep false.				
	
	//Set close to redirect back to same page with query string intact.
	var btnHTML = '<p><input type="button" id="btnCloseAddFriendConfirm" value="Close" onclick="goTo(document.location.href);" /></p>';

	//Custom error message in the event of failure.
	var errormsgHTML = 'An error occurred while processing the request.';
	var divPendingInvite = document.getElementById('divPendingInvite');
	
	if (xmlhttp.readyState==4) {	//If OK...
		try {
			////////////////////////////////////////////////////////
			var strResponse = xmlhttp.responseText; //Do something with the response.
			////////////////////////////////////////////////////////
			
			if (strResponse.length > intMaxResponseLength && bCatchConnectionErrors) {
				//If the length of the response exceeds the max, it's an error. Probably a 404 error.
				//Don't populate the callback div with this error. Instead, show a message.
				//alert('An error occurred while processing the request.');
				//Display friendly failure message.
				divPendingInvite.innerHTML = errormsgHTML + btnHTML;
			}
			else {
				//The response length is good. Proceed with the callback execution.
				//Note: strResponse is holding the responseText now...
				//Show the confirmation div.
				//var strConfirmationMsg = '';
				//var spnUserDisplayName = document.getElementById('spnAddFriendDisplayName');
				//var strDisplayName = spnUserDisplayName.innerHTML;
				//var msgHTML = '<p>You have requested to be friends with ' + strDisplayName + '.</p>';
				
				//Display success.
				//displayPendingInvite()
				window.location=window.location;
				
				////////////////////////////
				//Award points for user.
				$Award(23, 5);
				////////////////////////////
			}
		}
		catch (e) {
			//Put any error handling here.
			var strErrorExplanation = 'There was a problem handling the response. \n'
			var strStatusText = 'Status Text: ' + xmlhttp.statusText + '\n'
			var strError = 'Error: ' + e.toString();
			//alert(strErrorExplanation + strStatusText + strError);
			
			//Display failure.
			divPendingInvite.innerHTML = errormsgHTML + btnHTML;
		}
	}
}

function InvokeAshxInviteDedication(fid) {
    
	//If we have the lists's default value, do not make a call to the server.
	if (!fid.length > 0) { return; }

	var strHttpCommandType = 'GET';
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx' 
	strHandlerPath += '?fid=' + fid 
	strHandlerPath += '&m=invitededication';
	
	//If asynchronous, set to true. Prevents page from hanging during ashx error.
	var bAsynch = true;	
    
    
	//////////////////////////////////////////////////////////////
	//Initialize the XmlHttpRequest, Set the callback function, make the request.
	//////////////////////////////////////////////////////////////
	InitXmlHttp();
	xmlhttp.onreadystatechange = fInviteDedicationCallback;
	xmlhttp.open(strHttpCommandType, strHandlerPath, bAsynch);
	xmlhttp.send(null);	//If command is GET, set to 'null'.
	//////////////////////////////////////////////////////////////
	
}

////////////////////////////////////////////////////////
/* Cancel Invite Dedication AJAX functions */
////////////////////////////////////////////////////////

var fCancelDedicationInvitationCallback = function() {
	var intMaxResponseLength = 300;			//Max allowable chars in response.
	var bCatchConnectionErrors = false;		//For development, keep false.				
	
	//Set close to redirect back to same page with query string intact.
	var btnHTML = '<p><input type="button" id="btnCloseAddFriendConfirm" value="Close" onclick="goTo(document.location.href);" /></p>';

	//Custom error message in the event of failure.
	var errormsgHTML = 'An error occurred while processing the request.';
	var divPendingInvite = document.getElementById('divPendingInvite');
	
	if (xmlhttp.readyState==4) {	//If OK...
		try {
			////////////////////////////////////////////////////////
			var strResponse = xmlhttp.responseText; //Do something with the response.
			////////////////////////////////////////////////////////
			
			if (strResponse.length > intMaxResponseLength && bCatchConnectionErrors) {
				//If the length of the response exceeds the max, it's an error. Probably a 404 error.
				//Don't populate the callback div with this error. Instead, show a message.
				//alert('An error occurred while processing the request.');
				//Display friendly failure message.
				divPendingInvite.innerHTML = errormsgHTML + btnHTML;
			}
			else {
				//The response length is good. Proceed with the callback execution.
				//Note: strResponse is holding the responseText now...
				//Show the confirmation div.
				//var strConfirmationMsg = '';
				//var spnUserDisplayName = document.getElementById('spnAddFriendDisplayName');
				//var strDisplayName = spnUserDisplayName.innerHTML;
				//var msgHTML = '<p>You have requested to be friends with ' + strDisplayName + '.</p>';
				
				//success - reload the page
				window.location=window.location;
			}
		}
		catch (e) {
			//Put any error handling here.
			var strErrorExplanation = 'There was a problem handling the response. \n'
			var strStatusText = 'Status Text: ' + xmlhttp.statusText + '\n'
			var strError = 'Error: ' + e.toString();
			//alert(strErrorExplanation + strStatusText + strError);
			
			//Display failure.
			divPendingInvite.innerHTML = errormsgHTML + btnHTML;
		}
	}
}

function InvokeAshxCancelDedicationInvitation(fid) {
    
	//If we have the lists's default value, do not make a call to the server.
	if (!fid.length > 0) { return; }

	var strHttpCommandType = 'GET';
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx' 
	strHandlerPath += '?fid=' + fid 
	strHandlerPath += '&m=cancelinvitation';
	
	//If asynchronous, set to true. Prevents page from hanging during ashx error.
	var bAsynch = true;	
    
    
	//////////////////////////////////////////////////////////////
	//Initialize the XmlHttpRequest, Set the callback function, make the request.
	//////////////////////////////////////////////////////////////
	InitXmlHttp();
	xmlhttp.onreadystatechange = fCancelDedicationInvitationCallback;
	xmlhttp.open(strHttpCommandType, strHandlerPath, bAsynch);
	xmlhttp.send(null);	//If command is GET, set to 'null'.
	//////////////////////////////////////////////////////////////
	
}

////////////////////////////////////////////////////////
/* Accept Invite Dedication AJAX functions */
////////////////////////////////////////////////////////

var fAcceptDedicationInvitationCallback = function() {
	var intMaxResponseLength = 300;			//Max allowable chars in response.
	var bCatchConnectionErrors = false;		//For development, keep false.				
	
	//Set close to redirect back to same page with query string intact.
	var btnHTML = '<p><input type="button" id="btnCloseAddFriendConfirm" value="Close" onclick="goTo(document.location.href);" /></p>';

	//Custom error message in the event of failure.
	var errormsgHTML = 'An error occurred while processing the request.';
	var divPendingInvite = document.getElementById('divPendingInvite');
	
	if (xmlhttp.readyState==4) {	//If OK...
		try {
			////////////////////////////////////////////////////////
			var strResponse = xmlhttp.responseText; //Do something with the response.
			////////////////////////////////////////////////////////
			
			if (strResponse.length > intMaxResponseLength && bCatchConnectionErrors) {
				//If the length of the response exceeds the max, it's an error. Probably a 404 error.
				//Don't populate the callback div with this error. Instead, show a message.
				//alert('An error occurred while processing the request.');
				//Display friendly failure message.
				divPendingInvite.innerHTML = errormsgHTML + btnHTML;
				
			}
			else {
				//The response length is good. Proceed with the callback execution.
				//Note: strResponse is holding the responseText now...
				//Show the confirmation div.
				//var strConfirmationMsg = '';
				//var spnUserDisplayName = document.getElementById('spnAddFriendDisplayName');
				//var strDisplayName = spnUserDisplayName.innerHTML;
				//var msgHTML = '<p>You have requested to be friends with ' + strDisplayName + '.</p>';
				
				//success - reload the page
				
				window.location=window.location;
				
				////////////////////////////
				//Award points for user.
				$Award(24, 5);
				////////////////////////////
			}
		}
		catch (e) {
			//Put any error handling here.
			var strErrorExplanation = 'There was a problem handling the response. \n'
			var strStatusText = 'Status Text: ' + xmlhttp.statusText + '\n'
			var strError = 'Error: ' + e.toString();
			//alert(strErrorExplanation + strStatusText + strError);
			
			//Display failure.
			divPendingInvite.innerHTML = errormsgHTML + btnHTML;
		}
	}
}

function InvokeAshxAcceptDedicationInvitation(fid) {
    
	//If we have the lists's default value, do not make a call to the server.
	if (!fid.length > 0) { return; }

	var strHttpCommandType = 'GET';
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx' 
	strHandlerPath += '?fid=' + fid 
	strHandlerPath += '&m=acceptinvitation';
	
	//If asynchronous, set to true. Prevents page from hanging during ashx error.
	var bAsynch = true;	
    
    
	//////////////////////////////////////////////////////////////
	//Initialize the XmlHttpRequest, Set the callback function, make the request.
	//////////////////////////////////////////////////////////////
	InitXmlHttp();
	xmlhttp.onreadystatechange = fAcceptDedicationInvitationCallback;
	xmlhttp.open(strHttpCommandType, strHandlerPath, bAsynch);
	xmlhttp.send(null);	//If command is GET, set to 'null'.
	//////////////////////////////////////////////////////////////
	
}

////////////////////////////////////////////////////////
/* Accept Invite Dedication AJAX functions */
////////////////////////////////////////////////////////

var fDeclineDedicationInvitationCallback = function() {
	var intMaxResponseLength = 300;			//Max allowable chars in response.
	var bCatchConnectionErrors = false;		//For development, keep false.				
	
	//Set close to redirect back to same page with query string intact.
	var btnHTML = '<p><input type="button" id="btnCloseAddFriendConfirm" value="Close" onclick="goTo(document.location.href);" /></p>';

	//Custom error message in the event of failure.
	var errormsgHTML = 'An error occurred while processing the request.';
	var divPendingInvite = document.getElementById('divPendingInvite');
	
	if (xmlhttp.readyState==4) {	//If OK...
		try {
			////////////////////////////////////////////////////////
			var strResponse = xmlhttp.responseText; //Do something with the response.
			////////////////////////////////////////////////////////
			
			if (strResponse.length > intMaxResponseLength && bCatchConnectionErrors) {
				//If the length of the response exceeds the max, it's an error. Probably a 404 error.
				//Don't populate the callback div with this error. Instead, show a message.
				//alert('An error occurred while processing the request.');
				//Display friendly failure message.
				divPendingInvite.innerHTML = errormsgHTML + btnHTML;
				
			}
			else {
				//The response length is good. Proceed with the callback execution.
				//Note: strResponse is holding the responseText now...
				//Show the confirmation div.
				//var strConfirmationMsg = '';
				//var spnUserDisplayName = document.getElementById('spnAddFriendDisplayName');
				//var strDisplayName = spnUserDisplayName.innerHTML;
				//var msgHTML = '<p>You have requested to be friends with ' + strDisplayName + '.</p>';
				
				//success - reload the page
				
				window.location=window.location;
			}
		}
		catch (e) {
			//Put any error handling here.
			var strErrorExplanation = 'There was a problem handling the response. \n'
			var strStatusText = 'Status Text: ' + xmlhttp.statusText + '\n'
			var strError = 'Error: ' + e.toString();
			//alert(strErrorExplanation + strStatusText + strError);
			
			//Display failure.
			divPendingInvite.innerHTML = errormsgHTML + btnHTML;
		}
	}
}

function InvokeAshxDeclineDedicationInvitation(fid) {
    
	//If we have the lists's default value, do not make a call to the server.
	if (!fid.length > 0) { return; }

	var strHttpCommandType = 'GET';
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx' 
	strHandlerPath += '?fid=' + fid 
	strHandlerPath += '&m=declineinvitation';
	
	//If asynchronous, set to true. Prevents page from hanging during ashx error.
	var bAsynch = true;	
    
    
	//////////////////////////////////////////////////////////////
	//Initialize the XmlHttpRequest, Set the callback function, make the request.
	//////////////////////////////////////////////////////////////
	InitXmlHttp();
	xmlhttp.onreadystatechange = fDeclineDedicationInvitationCallback;
	xmlhttp.open(strHttpCommandType, strHandlerPath, bAsynch);
	xmlhttp.send(null);	//If command is GET, set to 'null'.
	//////////////////////////////////////////////////////////////
	
}


////////////////////////////////////////////////////////
/* Send Motivation AJAX functions */
////////////////////////////////////////////////////////

var fSendMotivationCallback = function() {
	var intMaxResponseLength = 300;			//Max allowable chars in response.
	var bCatchConnectionErrors = false;		//For development, keep false.				
	
	//Set close to redirect back to same page with query string intact.
	var btnHTML = '<p><input type="button" id="btnCloseAddFriendConfirm" value="Close" onclick="goTo(document.location.href);" /></p>';

	//Custom error message in the event of failure.
	var errormsgHTML = 'An error occurred while processing the request.';
	var divPendingInvite = document.getElementById('divPendingInvite');
	
	if (xmlhttp.readyState==4) {	//If OK...
		try {
			////////////////////////////////////////////////////////
			var strResponse = xmlhttp.responseText; //Do something with the response.
			////////////////////////////////////////////////////////
			
			if (strResponse.length > intMaxResponseLength && bCatchConnectionErrors) {
				//If the length of the response exceeds the max, it's an error. Probably a 404 error.
				//Don't populate the callback div with this error. Instead, show a message.
				//alert('An error occurred while processing the request.');
				//Display friendly failure message.
				//divPendingInvite.innerHTML = errormsgHTML + btnHTML;
				
			}
			else {
				//The response length is good. Proceed with the callback execution.
				//Note: strResponse is holding the responseText now...
				//Show the confirmation div.
				//var strConfirmationMsg = '';
				//var spnUserDisplayName = document.getElementById('spnAddFriendDisplayName');
				//var strDisplayName = spnUserDisplayName.innerHTML;
				//var msgHTML = '<p>You have requested to be friends with ' + strDisplayName + '.</p>';
				
				//success - reload the page
				
				if (m_bDoMessageAjax) {
					showMotivationSentSuccess();
					
					////////////////////////////
					//Award points for user.
					$Award(26, 5);
					////////////////////////////
				
					//refreshMe(); //Do this to update totals. //Re-commented out to resolve DHIYC-819
				}
				else {
					window.location = window.location;
				}
				
			}
		}
		catch (e) {
			//Put any error handling here.
			var strErrorExplanation = 'There was a problem handling the response. \n'
			var strStatusText = 'Status Text: ' + xmlhttp.statusText + '\n'
			var strError = 'Error: ' + e.toString();
			//alert(strErrorExplanation + strStatusText + strError);
			
			//Display failure.
			divPendingInvite.innerHTML = errormsgHTML + btnHTML;
		}
	}
}

function InvokeAshxSendMotivation(fid,msg_id) {
    
	//If we have the lists's default value, do not make a call to the server.
	if (!fid.length > 0) { return; }

	var strHttpCommandType = 'GET';
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx' 
	strHandlerPath += '?fid=' + fid 
	strHandlerPath += '&m=sendmotivation';
	strHandlerPath += '&mid='+msg_id;
	
	//If asynchronous, set to true. Prevents page from hanging during ashx error.
	var bAsynch = true;	
    
    
	//////////////////////////////////////////////////////////////
	//Initialize the XmlHttpRequest, Set the callback function, make the request.
	//////////////////////////////////////////////////////////////
	InitXmlHttp();
	xmlhttp.onreadystatechange = fSendMotivationCallback;
	xmlhttp.open(strHttpCommandType, strHandlerPath, bAsynch);
	xmlhttp.send(null);	//If command is GET, set to 'null'.
	//////////////////////////////////////////////////////////////
	
}

////////////////////////////////////////////////////////
/* Motivation Response AJAX functions */
////////////////////////////////////////////////////////

var fMotivationResponseCallback = function() {
	var intMaxResponseLength = 300;			//Max allowable chars in response.
	var bCatchConnectionErrors = false;		//For development, keep false.				
	
	//Set close to redirect back to same page with query string intact.
	var btnHTML = '<p><input type="button" id="btnCloseAddFriendConfirm" value="Close" onclick="goTo(document.location.href);" /></p>';

	//Custom error message in the event of failure.
	var errormsgHTML = 'An error occurred while processing the request.';
	var divPendingInvite = document.getElementById('divPendingInvite');
	
	if (xmlhttp.readyState==4) {	//If OK...
		try {
			////////////////////////////////////////////////////////
			var strResponse = xmlhttp.responseText; //Do something with the response.
			////////////////////////////////////////////////////////
			
			if (strResponse.length > intMaxResponseLength && bCatchConnectionErrors) {
				//If the length of the response exceeds the max, it's an error. Probably a 404 error.
				//Don't populate the callback div with this error. Instead, show a message.
				//alert('An error occurred while processing the request.');
				//Display friendly failure message.
				//divPendingInvite.innerHTML = errormsgHTML + btnHTML;
				
			}
			else {
				//The response length is good. Proceed with the callback execution.
				//Note: strResponse is holding the responseText now...
				//Show the confirmation div.
				//var strConfirmationMsg = '';
				//var spnUserDisplayName = document.getElementById('spnAddFriendDisplayName');
				//var strDisplayName = spnUserDisplayName.innerHTML;
				//var msgHTML = '<p>You have requested to be friends with ' + strDisplayName + '.</p>';
				
				//show answered state.
				//showMotivationAnswered(); 
				//7/7/09: Commented out above call to prevent placeholder text from temporarily displaying.
				//7/7/09: Instead, reloading the page after points are awarded.
				//7/7/09: This allows dedication control on home page to update as well.
				
				////////////////////////////
				//Award points for user.
				$Award(27, 5);
				////////////////////////////
				
				//Reload the page
				refreshMe();
				
			}
		}
		catch (e) {
			//Put any error handling here.
			var strErrorExplanation = 'There was a problem handling the response. \n'
			var strStatusText = 'Status Text: ' + xmlhttp.statusText + '\n'
			var strError = 'Error: ' + e.toString();
			//alert(strErrorExplanation + strStatusText + strError);
			
			//Display failure.
			divPendingInvite.innerHTML = errormsgHTML + btnHTML;
		}
	}
}

function InvokeAshxMotivationResponse(fid,answer,msg_id) {
    
	//If we have the lists's default value, do not make a call to the server.
	
	var strHttpCommandType = 'GET';
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx' 
	strHandlerPath += '?fid=' +  fid
	strHandlerPath += '&answer=' + answer 
	strHandlerPath += '&m=motivationresponse';
	strHandlerPath += '&mid='+msg_id;
	
	//If asynchronous, set to true. Prevents page from hanging during ashx error.
	var bAsynch = true;	
    
    
	//////////////////////////////////////////////////////////////
	//Initialize the XmlHttpRequest, Set the callback function, make the request.
	//////////////////////////////////////////////////////////////
	InitXmlHttp();
	xmlhttp.onreadystatechange = fMotivationResponseCallback;
	xmlhttp.open(strHttpCommandType, strHandlerPath, bAsynch);
	xmlhttp.send(null);	//If command is GET, set to 'null'.
	//////////////////////////////////////////////////////////////
	
}
////////////////////////////////////////////////////////
/* Motivation Response2 AJAX functions */
////////////////////////////////////////////////////////

var fMotivationResponseCallback2 = function() {
	var intMaxResponseLength = 300;			//Max allowable chars in response.
	var bCatchConnectionErrors = false;		//For development, keep false.				
	
	//Set close to redirect back to same page with query string intact.
	var btnHTML = '<p><input type="button" id="btnCloseAddFriendConfirm" value="Close" onclick="goTo(document.location.href);" /></p>';

	//Custom error message in the event of failure.
	var errormsgHTML = 'An error occurred while processing the request.';
	var divPendingInvite = document.getElementById('divPendingInvite');
	
	if (xmlhttp.readyState==4) {	//If OK...
		try {
			////////////////////////////////////////////////////////
			var strResponse = xmlhttp.responseText; //Do something with the response.
			////////////////////////////////////////////////////////
			
			if (strResponse.length > intMaxResponseLength && bCatchConnectionErrors) {
				//If the length of the response exceeds the max, it's an error. Probably a 404 error.
				//Don't populate the callback div with this error. Instead, show a message.
				//alert('An error occurred while processing the request.');
				//Display friendly failure message.
				//divPendingInvite.innerHTML = errormsgHTML + btnHTML;
				
			}
			else {
				//alert('in fMotivationResponseCallback2');
				refreshMe();
				showMotivationResponse();
			}
		}
		catch (e) {
			//Put any error handling here.
			var strErrorExplanation = 'There was a problem handling the response. \n'
			var strStatusText = 'Status Text: ' + xmlhttp.statusText + '\n'
			var strError = 'Error: ' + e.toString();
			//alert(strErrorExplanation + strStatusText + strError);
			
			//Display failure.
			divPendingInvite.innerHTML = errormsgHTML + btnHTML;
		}
	}
}

function InvokeAshxMotivationResponse2(fid,answer,msg_id) {
    
	//If we have the lists's default value, do not make a call to the server.
	
	//alert('fid= '+ fid + 'answer= '+ answer + 'msg_id=' + msg_id);
	
	var strHttpCommandType = 'GET';
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx' 
	strHandlerPath += '?fid=' +  fid
	strHandlerPath += '&answer=' + answer 
	strHandlerPath += '&m=motivationresponse';
	strHandlerPath += '&mid='+msg_id;
	
	//If asynchronous, set to true. Prevents page from hanging during ashx error.
	var bAsynch = true;	
    
    
	//////////////////////////////////////////////////////////////
	//Initialize the XmlHttpRequest, Set the callback function, make the request.
	//////////////////////////////////////////////////////////////
	InitXmlHttp();
	xmlhttp.onreadystatechange = fMotivationResponseCallback2;
	xmlhttp.open(strHttpCommandType, strHandlerPath, bAsynch);
	xmlhttp.send(null);	//If command is GET, set to 'null'.
	//////////////////////////////////////////////////////////////
	
}
////////////////////////////////////////////////////////
/* Acknowledge Complete AJAX functions */
////////////////////////////////////////////////////////

var fAcknowledgeCompleteCallback = function() {
	var intMaxResponseLength = 300;			//Max allowable chars in response.
	var bCatchConnectionErrors = false;		//For development, keep false.				
	
	//Set close to redirect back to same page with query string intact.
	var btnHTML = '<p><input type="button" id="btnCloseAddFriendConfirm" value="Close" onclick="goTo(document.location.href);" /></p>';

	//Custom error message in the event of failure.
	var errormsgHTML = 'An error occurred while processing the request.';
	var divPendingInvite = document.getElementById('divPendingInvite');
	
	if (xmlhttp.readyState==4) {	//If OK...
		try {
			////////////////////////////////////////////////////////
			var strResponse = xmlhttp.responseText; //Do something with the response.
			////////////////////////////////////////////////////////
			
			if (strResponse.length > intMaxResponseLength && bCatchConnectionErrors) {
				//If the length of the response exceeds the max, it's an error. Probably a 404 error.
				//Don't populate the callback div with this error. Instead, show a message.
				//alert('An error occurred while processing the request.');
				//Display friendly failure message.
				//divPendingInvite.innerHTML = errormsgHTML + btnHTML;
				
			}
			else {
				//The response length is good. Proceed with the callback execution.
				//Note: strResponse is holding the responseText now...
				//Show the confirmation div.
				//var strConfirmationMsg = '';
				//var spnUserDisplayName = document.getElementById('spnAddFriendDisplayName');
				//var strDisplayName = spnUserDisplayName.innerHTML;
				//var msgHTML = '<p>You have requested to be friends with ' + strDisplayName + '.</p>';
				
				//success - reload the page
				
				window.location='/mycorner/users/mydedication.aspx'; //window.location;
				//showMotivationSent();
			}
		}
		catch (e) {
			//Put any error handling here.
			var strErrorExplanation = 'There was a problem handling the response. \n'
			var strStatusText = 'Status Text: ' + xmlhttp.statusText + '\n'
			var strError = 'Error: ' + e.toString();
			//alert(strErrorExplanation + strStatusText + strError);
			
			//Display failure.
			divPendingInvite.innerHTML = errormsgHTML + btnHTML;
		}
	}
}

function InvokeAshxAcknowledgeComplete() {
    
	//If we have the lists's default value, do not make a call to the server.
	
	var strHttpCommandType = 'GET';
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx' 
	strHandlerPath += '?fid='
	strHandlerPath += '&m=acknowledgecomplete';
	
	//If asynchronous, set to true. Prevents page from hanging during ashx error.
	var bAsynch = true;	
    
    
	//////////////////////////////////////////////////////////////
	//Initialize the XmlHttpRequest, Set the callback function, make the request.
	//////////////////////////////////////////////////////////////
	InitXmlHttp();
	xmlhttp.onreadystatechange = fAcknowledgeCompleteCallback;
	xmlhttp.open(strHttpCommandType, strHandlerPath, bAsynch);
	xmlhttp.send(null);	//If command is GET, set to 'null'.
	//////////////////////////////////////////////////////////////
	
}

//Toggles FAQ style and answer--I think
//(function($) 
//{toggle();
//     $("#dedicationfaqs > dt").toggleClass("hoverable");  
//     $("#dedicationfaqs > dd").toggleClass("on");
//})(jQuery); 

////////////////////////////////////////////////////////
/* Remove Initial Message AJAX functions */
////////////////////////////////////////////////////////

var fRemoveInitialMessageCallback = function() {
	var intMaxResponseLength = 300;			//Max allowable chars in response.
	var bCatchConnectionErrors = false;		//For development, keep false.				
	
	//Set close to redirect back to same page with query string intact.
	var btnHTML = '<p><input type="button" id="btnCloseAddFriendConfirm" value="Close" onclick="goTo(document.location.href);" /></p>';

	//Custom error message in the event of failure.
	var errormsgHTML = 'An error occurred while processing the request.';
	var divPendingInvite = document.getElementById('divPendingInvite');
	
	if (xmlhttp.readyState==4) {	//If OK...
		try {
			////////////////////////////////////////////////////////
			var strResponse = xmlhttp.responseText; //Do something with the response.
			////////////////////////////////////////////////////////
			
			if (strResponse.length > intMaxResponseLength && bCatchConnectionErrors) {
				//If the length of the response exceeds the max, it's an error. Probably a 404 error.
				//Don't populate the callback div with this error. Instead, show a message.
				//alert('An error occurred while processing the request.');
				//Display friendly failure message.
				//divPendingInvite.innerHTML = errormsgHTML + btnHTML;
				
			}
			else {
				//The response length is good. Proceed with the callback execution.
				//Note: strResponse is holding the responseText now...
				//Show the confirmation div.
				//var strConfirmationMsg = '';
				//var spnUserDisplayName = document.getElementById('spnAddFriendDisplayName');
				//var strDisplayName = spnUserDisplayName.innerHTML;
				//var msgHTML = '<p>You have requested to be friends with ' + strDisplayName + '.</p>';
				
				//success - reload the page
				showSecondaryMessage();
				refreshMe();
				//window.location=window.location;
				//showMotivationSent();
			}
		}
		catch (e) {
			//Put any error handling here.
			var strErrorExplanation = 'There was a problem handling the response. \n'
			var strStatusText = 'Status Text: ' + xmlhttp.statusText + '\n'
			var strError = 'Error: ' + e.toString();
			//alert(strErrorExplanation + strStatusText + strError);
			
			//Display failure.
			divPendingInvite.innerHTML = errormsgHTML + btnHTML;
		}
	}
}

function InvokeAshxRemoveInitialMessage() {
    
	//If we have the lists's default value, do not make a call to the server.
	
	var strHttpCommandType = 'GET';
	var strHandlerPath = '/mycorner/handlers/Dedication.ashx' 
	strHandlerPath += '?m=removeinitialmessage';
	
	//If asynchronous, set to true. Prevents page from hanging during ashx error.
	var bAsynch = true;	
    
    
	//////////////////////////////////////////////////////////////
	//Initialize the XmlHttpRequest, Set the callback function, make the request.
	//////////////////////////////////////////////////////////////
	InitXmlHttp();
	xmlhttp.onreadystatechange = fRemoveInitialMessageCallback;
	xmlhttp.open(strHttpCommandType, strHandlerPath, bAsynch);
	xmlhttp.send(null);	//If command is GET, set to 'null'.
	//////////////////////////////////////////////////////////////
	
}

//Toggles FAQ style and answer--I think
//(function($) 
//{toggle();
//     $("#dedicationfaqs > dt").toggleClass("hoverable");  
//     $("#dedicationfaqs > dd").toggleClass("on");
//})(jQuery); 


//Conditional Q&A Link
function hideQALink() {
	var url = window.document.location.href;
	if (url.indexOf('/mycorner/index.aspx')) {
		var pars = document.getElementsByTagName('p');
		for (var i in pars) {
			if (pars[i].className == 'ConditionalQandALink') {
				pars[i].style.display = 'none';
			}
		}
	}
}