// handles all ajax post requests
var Action_XMLHTTP = false;
var bBlockWindowOpen = false;

function action_post(sMethod, sURL, sPostData, bBlocking, fCallback) {
	if (!Action_XMLHTTP) {
		Action_XMLHTTP = CreateXMLHTTP();
	}

	Action_XMLHTTP.open(sMethod, sURL, !bBlocking);
	if (!!fCallback) { Action_XMLHTTP.onreadystatechange = fCallback; }
	Action_XMLHTTP.send(sPostData);
	
	return Action_XMLHTTP;
}

// POPPING
function ClipPop_XMLStateChange() {
	if (Action_XMLHTTP.readyState != 4) {
		return;
	}

	if (GetAttribute(Action_XMLHTTP.responseXML.documentElement, 'error', '0') != '0') {
		ShowDialog('<div align="left" style="font-size:12px;margin-bottom:4px;"><h1 style="color:#4D82CC;">Hold your pops</h1>To try to maintain balance on the Clipmarks site, there are some limits on the number of clips each user can pop within certain time periods.<br /><br />You will be able to pop again in ' + GetAttribute(Action_XMLHTTP.responseXML.documentElement, 'timeout-minutes', '20') + ' minutes.</div>', 0, 0, false, null, 300, 190);
	} else if (GetAttribute(Action_XMLHTTP.responseXML.documentElement, 'pop-count-1', '0') == '7') {
		ShowDialog('<div align="left" style="font-size:12px;margin-bottom:4px;"><h1 style="color:#4D82CC;">Hold your pops</h1>To try to maintain balance on the Clipmarks site, there are some limits on the number of clips each user can pop within certain time periods.<br /><br />You will exceed this limit if you pop again to soon.</div>', 0, 0, false, null, 300, 190);
	} else if (GetAttribute(Action_XMLHTTP.responseXML.documentElement, 'pop-count-2', '0') == '15') {
		ShowDialog('<div align="left" style="font-size:12px;margin-bottom:4px;"><h1 style="color:#4D82CC;">Hold your pops</h1>To try to maintain balance on the Clipmarks site, there are some limits on the number of clips each user can pop within certain time periods.<br /><br />You will exceed this limit if you pop again to soon.</div>', 0, 0, false, null, 300, 190);
	} else if (GetAttribute(Action_XMLHTTP.responseXML.documentElement, 'pop-count-3', '0') == '4') {
		ShowDialog('<div align="left" style="font-size:12px;margin-bottom:4px;"><h1 style="color:#4D82CC;">Hold your pops</h1>To try to maintain balance on the Clipmarks site, there are some limits on the number of clips each user can pop within certain time periods.<br /><br />You will exceed this limit if you pop again to soon.</div>', 0, 0, false, null, 300, 190);
	} else {
		// pop went through....
		if (setting_get('user_hide_postwpop', '0') != '1') {  
			dialog_popped_open(sActiveClipGUID, true, true);
		}
	}
}

function PostClipPop() {
	if (!bClipPopped) { 
		action_post('GET', '/xml_action.aspx?action=vote_clip&c=' + sActiveClipGUID, null, false, ClipPop_XMLStateChange);

		iTotalPops++;
		
		document.getElementById('spnTotalPops').innerHTML = iTotalPops;
		document.getElementById('imgPopIcon').src = '/images/tools3/popped.png';
		document.getElementById('imgPopIcon').title = 'your pop has been recorded';
		if (!!document.getElementById('imgPopIcon2')) { 
			document.getElementById('imgPopIcon2').src = '/images/right/bottom-popped.jpg';
			document.getElementById('imgPopIcon2').title = 'your pop has been recorded';
		} 
		bClipPopped = true;
	} 
}

function PostLiveResults() {
var sDesc = '';
var sHTML = '<div onmouseover="bPostPop=true;" onmouseout="bPostPop=false;">\
\
<table width="100%" cellpadding="0" cellspacing="0" style="margin-top:10px;">\
	<tr>\
		<td style="font-size: 16px; padding-left: 14px;"><b>Live Results are now ';

if (bLiveResults) { 
	sHTML += 'ON';
	sDesc = '<div style="margin: 12px 6px 12px 10px; font-size: 12px; text-align: left;">New results will now come in without having to refresh your browser.</div>';
} else {
	sHTML += 'OFF'; 
	sDesc = '<div style="margin: 12px 6px 12px 10px; font-size: 12px; text-align: left;">New results will not come in until you refresh your browser.</div>';
}


sHTML += '</b></td>\
	</tr>\
</table>' + sDesc;

if (bAuth) { 
	sHTML += '<div style="margin: 12px 0px 12px 10px; color: #666666; font-size: 11px; text-align: left; white-space: nowrap;">This setting will be remembered each time you visit the site when logged in.</div>';
	sHTML += '<table width="100%" cellpadding="0" cellspacing="0" style="margin: 12px 0px 0px 24px;">\
	<tr>\
		<td width="16"><input type="checkbox" id="chkPostLive" name="chkPostLive" value="1" onclick="ChangeCloseLabel2()" /></td>\
		<td width="60%" style="font-size: 12px;"><label for="chkPostLive" >Don\'t show me this window again</label></td>\
		<td width="160"><span class="DialogButton"><a href="javascript:HideLiveResults();" id="lnkPostLiveButton">Close</a></span></td>\
	</tr>\
</table>';

} else {

	sHTML += '<div align="center" style="margin-top: 20px;"><span class="DialogButton"><a href="javascript:HideDialogBox();" id="lnkPostLiveButton">Close</a></span></div>';
}

sHTML += '</div>';

ShowDialog(sHTML, 0, -100, true, null, 396, 150);
}


function ChangeCloseLabel() {}


function ChangeCloseLabel2() {
	if (document.getElementById('chkPostLive').checked) { document.getElementById('lnkPostLiveButton').innerHTML = 'Save'; 
	} else {  document.getElementById('lnkPostLiveButton').innerHTML = 'Close'; } 
}


function HidePostPop() {
	if (document.getElementById('chkPostWPop').checked) {  
		setting_set('user_hide_postwpop', '1');
	} else {
		setting_set('user_hide_postwpop', '0');
	}

	CloseLayeredDialog();
}

function HideLiveResults() {
		if (document.getElementById('chkPostLive').checked) {  
			setting_set('user_hide_postlive', '1');
		} else {
			setting_set('user_hide_postlive', '0');
		}

	HideDialogBox();
}


function PostFollowUser(sUser, bFromDhtml, sIsFollowed, iOptTotalFollowers, sOptUserGuid, bFromClipcast) {
	if (!bAuth) {
		//FollowUserNoLogin();
		LoginPrompt('follow');
		return;
	}

	var bThisFollowed = bIsFollowed;

	if (bFromDhtml) { 
		if (sIsFollowed == '1') { 
			bThisFollowed = true;
		} else {
			bThisFollowed = false;
		}
	}

	if (!bThisFollowed) {
		if (!Action_XMLHTTP) {
			Action_XMLHTTP = CreateXMLHTTP();
		}
		action_post('GET', '/xml_action.aspx?action=follow_user&u=' + escape(sUser), null, false);

		if (document.getElementById('dvMenu_MyClippers')) {
			document.getElementById('dvMenu_MyClippers').setAttribute('loaded', 'false');
			document.getElementById('dvMenu_MyClippers').innerHTML = '<img src="/images/loading-small.gif" align="middle" />';
		}
	
		var objTotal = document.getElementById('spnTotalFollowers');
	
		if (sIsFollowed == '2') {
			iTotalFollowers = iOptTotalFollowers;
			objTotal = document.getElementById('spnTotalFollowers_' + sOptUserGuid);
		}
		
		iTotalFollowers++;

		if (objTotal) { 
			objTotal.innerHTML = '<b>' + iTotalFollowers + '</b>';
			if (!bFromClipcast) {
				objTotal.innerHTML += ' follower';
				if (iTotalFollowers > 1) { 
					objTotal.innerHTML += 's';
				}
			}
		}

		if (sIsFollowed == '2') {
			document.getElementById('imgFollow_' + sOptUserGuid).src='/images/in-clippers-big.gif';
		}
				
		if (!!document.getElementById('imgFollowCard')) { document.getElementById('imgFollowCard').src='/images/clipper/in-clippers-right.gif'; }
		
		if (!!document.getElementById('aFollowLnk')) { document.getElementById('aFollowLnk').innerHTML='<span class="Following">following</span>';document.getElementById('aFollowLnk').style.textDecoration='none'; }
		
		if (!!sSearchUser) {
			if (sSearchUser == sUser) { 
				if (!!document.getElementById('imgFollowClipcast')) { document.getElementById('imgFollowClipcast').src='/images/clipper/in-clippers-right.gif'; }
				if (!!document.getElementById('imgFollowLeft')) { document.getElementById('imgFollowLeft').src='/images/clipper/in-clippers-right.gif'; }
				if (!!document.getElementById('imgFollowRight')) { document.getElementById('imgFollowRight').src='/images/clipper/in-clippers-right.gif'; }
				if (!!document.getElementById('imgFollowList')) { document.getElementById('imgFollowList').src='/images/clipper/following_small.png'; }
		
				if (!!document.getElementById('tdBlockUser')) { 
					document.getElementById('tdBlockUser').innerHTML = '<a href="javascript:RemoveUser(\'' + escape(sUser) + '\');" title="Remove from My Clippers" style="font-size: 11px;">unfollow</a>';
				}	
			}
		}
		
		if (!bFromDhtml) { 
			bIsFollowed	= true;
		}
				
		iTotalClp++;
	
		var sClp = 'Guides';
		if (iTotalClp == 1) { sClp = 'Guide'; }

		var sMessage = '<div style="line-height: 26px; vertical-align: middle; font-weight: bold; font-size: 12px;">You have <a href="/clipper/' + escape(sAuthName).replace(/\%20/, '+') + '/clippers/" class="CLB">' + iTotalClp + ' ' + sClp + '</a></div>';
		
		setTimeout('OpenClippers(\'' + sUser + '\', 1, 50, \'' + sMessage + '\')', 400);
		
	} else {
		ShowDialog('<br/>You are already following <b>' + sUser + '</b>.<br/>To unfollow this user, go to their <a href="/clipper/' + sUser.replace(/ /, '+') + '/">page</a>.<br/>');
	}
}

function RemoveUser(sUser) {
	if (confirm('Are you sure you want to remove ' + sUser + ' from your Guides?')) {
		action_post('GET', '/xml_action.aspx?action=remove_users&user=' + escape(sUser), null, false);
	
		if (document.getElementById('dvMenu_MyClippers')) {
			document.getElementById('dvMenu_MyClippers').setAttribute('loaded', 'false');
			document.getElementById('dvMenu_MyClippers').innerHTML = '<img src="/images/loading-small.gif" align="middle" />';
		}
	
		var objTotal = document.getElementById('spnTotalFollowers');
	
		iTotalFollowers--;
		iTotalClp--;

		if (objTotal) { 
			objTotal.innerHTML = '<b>' + iTotalFollowers + '</b> follower';
			if (iTotalFollowers > 1) { 
				objTotal.innerHTML += 's';
			} 
		}
		
		if (!!document.getElementById('imgFollowLeft')) { document.getElementById('imgFollowLeft').src='/images/clipper/add-clipper-right.gif'; }
		if (!!document.getElementById('imgFollowRight')) { document.getElementById('imgFollowRight').src='/images/clipper/add-clipper-right.gif'; }
		if (!!document.getElementById('imgFollowList')) { document.getElementById('imgFollowList').src='/images/clipper/follow_small.png'; }

		document.getElementById('tdBlockUser').innerHTML = '';

		bIsFollowed	= false;
		
		ShowDialog('<div style="margin: 12px;"><br/><b>' + sUser + '</b> has been removed from<br/>your Guides.<br/><br/></div>', 0, -100, false, null, 300, 160);
	} 
	
}

function OpenBlockUser(sUser, sGuid) {
	bBlockWindowOpen = true;
	CollapseMenu('ClipperCard');
	excluded_open();

	sHTML = '<div id="dvBlockMenu" style="padding-bottom: 4px; margin: 12px;" onmouseover="bBlockWindow=true;" onmouseout="bBlockWindow=false;">' + get_excluded_window(sUser, sGuid) + '</div>';
	ShowLayeredDialog(sHTML);
}

function get_excluded_window(sUser, sGuid) { 
	var sHTML = '';
	var iExclude = exclude_get(sGuid);

	var sClipChecked = '';
	var sCommentChecked = '';
	var sCommentingChecked = '';
	if ((iExclude & 1) == 1) { sClipChecked = 'checked'; }
	if ((iExclude & 2) == 2) { sCommentChecked = 'checked'; }
	if ((iExclude & 4) == 4) { sCommentingChecked = 'checked'; }
	
	sHTML += '<h3 style="margin: 18px 0px 7px 10px;">User Management</h3>';
	sHTML += '<table width="99%" cellspacing="0" style="margin: 10px 0px; border: solid 2px #dcdcdc; border-width: 2px 0px;"><tr><td valign="top" width="45%" style="font-size: 13px; line-height: 17px; border: none; padding: 14px;">';
	sHTML += 'We are passionate about maintaining a positive, civil environment at Clipmarks. While the great majority of Clipmarks users treat each other with respect, there may be rare cases where you feel the need to protect yourself from malicious actions or personal attacks.<br><br>Here are a few tools you can use if you feel necessary in order to maintain a positive Clipmarks experience.';
	sHTML += '</td><td valign="top" width="55%" style="border: none; padding: 14px;">';
	
	if (bAuth) {	
		sHTML += '<div style="margin: 6px 8px;"><table cellpadding="0" cellspacing="0" width="97%">';
		//sHTML += '<tr><td valign="top" style="font-size: 12px;"><input type="checkbox" name="chkBlockClips" id="chkBlockClips" value="1" ' + sClipChecked + ' /></td><td valign="top" style="font-size: 12px;"><label for="chkBlockClips">Hide <b>' + sUser + '\'s</b> clips</label></td></tr>';
		sHTML += '<tr><td valign="top" style="font-size: 12px;"><input type="checkbox" name="chkBlockComments" id="chkBlockComments" value="1" ' + sCommentChecked + ' /></td><td valign="top" style="font-size: 12px;"><label for="chkBlockComments">Hide <b>' + sUser + '\'s</b> comments</label></td></tr>';
		sHTML += '<tr><td valign="top" style="font-size: 12px; border: none;"><input type="checkbox" name="chkBlockCommenting" id="chkBlockCommenting" value="1" ' + sCommentingChecked + ' /></td><td valign="top" style="font-size: 12px; border: none;"><label for="chkBlockCommenting">Prevent <b>' + sUser + '</b> from commenting on my clips</label><div style="font-size: 11px; margin: 4px;">This option is intended for use in cases where you recognize a pattern of malicious behavior that makes it impossible for you to enjoy your Clipmarks experience. It is <b>not</b> intended to prevent opposing views or as a way to personally punish someone you don\'t agree with.</div></td></tr>';
		sHTML += '</table></div>';
	} else {
		var sUrl = ExchangeUrl();
		if (sUrl && sUrl != '') { sUrl = '?url=' + encodeURI(sUrl); } else { sUrl = ''; }

		sHTML += '<div align="center"><br/><br/>';		
		sHTML += '<h4>You must log in to moderate<br/>your interaction with this user.</h4><br/><a href=""></a>';
		sHTML += '<div style="margin: 10px 0px; font-size: 16px;"><b><a href="/login.aspx' + sUrl + '" class="CLB">Login</a></b> &nbsp;or&nbsp; <b><a href="/register/" class="CLB">Sign Up</a></b></div>';		
		sHTML += '</div>';
	}
	
	sHTML += '</td></tr></table>';
	
	if (bAuth) {
		sHTML += '<table style="margin-top:12px;" border="0" cellpadding="0" cellspacing="0" align="center">';
		sHTML += '<tr><td style="padding-right: 4px; border: none;"><a href="javascript:void(0);" onclick="process_excluded(\'' + sGuid + '\');CloseLayeredDialog();" style="padding: 0px;"><img src="/images/buttons/save_green.jpg" alt="save" border="0" width="93" height="32" /></a></td>';
		sHTML += '<td style="border: none;"><a href="javascript:void(0);" onclick="CloseLayeredDialog();" style="padding: 0px;"><img src="/images/buttons/cancel_green.jpg" alt="cancel" border="0" width="93" height="32" /></a></td></tr>';
		sHTML += '</table>';
	} else {
		sHTML += '<div style="margin-top:12px; text-align: center;"><a href="javascript:void(0);" onclick="CloseLayeredDialog();" style="padding: 0px;" class="CLB">Close</a></div>';
	}
	
	return sHTML;
}

function process_excluded(sGuid) {
	var iExclude = 0;
	iExclude = (document.getElementById('chkBlockComments').checked ? 2 : 0) + (document.getElementById('chkBlockCommenting').checked ? 4 : 0);
	exclude_set(sGuid, iExclude);
	if (iExclude != 4) {
		window.location.reload(true);
	}
}

function PostDeleteClipmark(bPermanent) {
	action_post('GET', '/xml_action.aspx?action=' + (bPermanent ? 'delete_clip' : 'remove_library_clip') + '&c=' + sActiveClipGUID, null, true);

	if (!!Action_XMLHTTP.responseXML && bPermanent) {
		if (Action_XMLHTTP.responseXML.documentElement.getAttribute('error') == '1') {
			alert('This clip could not be deleted because it has already had action on it in the public. To remove this clip from your own collection, please choose the "Remove from My Clips" option in the Edit menu below the clip.\n\nIf you need help, please email support@clipmarks.com');
			return false;
		}
	}
	
	window.location.href = window.location.href;
}

function action_clip_moderate() {
	if (confirm('Are you sure you want to make this clip Public Limited?  Doing so will keep it public on the user\'s page and in their clipcast but will remove it from all public areas of clipmarks.com.  Clipmarks support will be notified about this clip and may take further action on it and/or the user who posted it.')) {
		action_post('GET', '/xml_action.aspx?action=clip_moderate&c=' + sActiveClipGUID, null, true);
		alert('This clip has been set to Public Limited.  Thank you for helping to moderate the environment on Clipmarks.');
		/*if (confirm('Would you like to reload the page?')) {
			window.location.href = window.location.href;
		}*/
	}
}

function PostSubComments() {
	if (!!document.getElementById('chkSubComments')) {
		bSubComments = document.getElementById('chkSubComments').checked;

		action_post('GET', '/xml_action.aspx?action=sub_com&sub=' + bSubComments + '&c=' + sActiveClipGUID, null, false);

		if (bSubComments) { 
			ShowDialog('<br/>You have subscribed to receive email notifications each time this clip is commented on.<br/><div style="margin: 10px 0px; font-size: 10px;">To unsubscribe, uncheck the box at the bottom of this clipmark, or click the unsubscribe link located at the bottom<br/>of each comments email.</div>', 0, 0, false, null, 290, 180);
		} else {
			ShowDialog('<br/>You will no longer receive email notifications each time this clip is commented on.<br/>');
		}
	}
}

function user_watch(sUserGUID) {
	action_post('GET', '/xml_action.aspx?action=user_watch&u=' + sUserGUID + '&r=' + escape(prompt('Reason for watching this user')), null, false);
}

function user_ban(sUserGUID, bReturn, bShowPosting, bReload) {
    var sReason = prompt('Reason for banning this user\nNOTE: This action can\'t be undone!')
    if (!!sReason && sReason != '') {
        if (bShowPosting) {
            ShowLayeredDialog('<b>Banning user, please wait....<b/>');
        }
	    action_post('GET', '/xml_action.aspx?action=user_ban&u=' + sUserGUID + '&r=' + escape(sReason), null, false, (!!bReload ? user_ban_callback : null));
	    if (bReturn) {
	        return true;
        }
    }
    if (bReturn) {
        return false;
    }
}

function user_ban_callback() {
    //window.location.href = window.location.href;
}

function user_nipsa(sUserGUID) {
	action_post('GET', '/xml_action.aspx?action=user_nipsa&u=' + sUserGUID + '&r=' + escape(prompt('Reason you nipsa\'d this user')), null, false);
}
