var iREPLY_BLOCK	= 10;
var iACTION_BLOCK	= 20;
var iACTION_UNBLOCK	= 30;
var iACTION_DELETE	= 40;

var _blockRowsSelectedCount  = 0;
var _unblockRowsSelectedCount= 0;
var alwaysEnableUnblock		 = false;

function doReplyWhenBlocked(blockedProfileName) {
	var choice = confirm("You have blocked "+blockedProfileName+" from communicating with you through ApnaMatch.com"
						+"\nDo you want to Unblock them before sending your message?");
	
	if (choice==false) {return;}
	
	document.getElementById("action").value   = iACTION_UNBLOCK;
	document.getElementById("action2").value  = iREPLY_BLOCK;
	document.f.submit();
}

function doBlock  (isRecieved) { return doAction(iACTION_BLOCK);	}
function doUnblock(isRecieved) { return doAction(iACTION_UNBLOCK);	}
function doDelete (isRecieved) { return doAction(iACTION_DELETE); 	}

function doAction(iAction, isRecieved) {
	document.getElementById("action").value = ""+iAction;
	document.f.submit();
}

function toggleApplyToAll(contactID, contactAll) {
	var e = document.forms[0].elements[0];
	var shouldCheck = document.getElementById(contactAll).checked;
	
	var iElementIndex = 0;
	for (iElementIndex=0,iLen=document.forms[0].elements.length; iElementIndex<iLen; iElementIndex++) {
		e = document.forms[0].elements[iElementIndex];
		
		if (e.name==contactID) {
			if (e.checked!= shouldCheck) {
				e.checked = shouldCheck;
				enableButtons(e, contactAll);
			}
		}
	}
}

function enableButtons(checkBox, allCheckbox) {
	var btnBlock   = document.getElementById("block");
	var btnUnBlock = document.getElementById("unblock");
	var btnDelete  = document.getElementById("delete");

	var isBlockedSymbolVisible = document.getElementById("block_"+checkBox.value).style.visibility!="hidden";
	updateListCounters(isBlockedSymbolVisible, checkBox.checked);
	var totalSelected = _blockRowsSelectedCount+_unblockRowsSelectedCount;
	
	//alert("[totalSelected="+totalSelected+"] [block#="+_blockRowsSelectedCount+"] [unblock="+_unblockRowsSelectedCount+"]\n");
	
	if (btnBlock!=null) {
		btnBlock.disabled = !(totalSelected>0 && _unblockRowsSelectedCount<=0);
	}
	if (btnUnBlock!=null) {
		if (alwaysEnableUnblock) {
			btnUnBlock.disabled = false;
		}
		else {
			btnUnBlock.disabled = !(totalSelected>0 && _blockRowsSelectedCount<=0);
		}
	}
	if (btnDelete!=null) {
		btnDelete.disabled  = !(totalSelected>0);
	}
	
	var allCheck = document.getElementById(allCheckbox);
	if (allCheck.checked && !checkBox.checked) {
		allCheck.checked = false;
	}
}

function updateListCounters(isBlockedSymbolVisible, isCheckBoxChecked) {
	if (isBlockedSymbolVisible) {
		if (isCheckBoxChecked) { _unblockRowsSelectedCount++; }
		else 				   { _unblockRowsSelectedCount--; }
	}
	else {
		if (isCheckBoxChecked) { _blockRowsSelectedCount++; }
		else 				   { _blockRowsSelectedCount--; }
	}
}
