
function advSearch() {
	if((document.getElementById("advancedSearch").style.display == "none") || (!document.getElementById("advancedSearch").style.display)) {
		document.getElementById("advancedSearch").style.display = "block";
		document.getElementById("advancedSearchSwitch").style.display = "none";
		document.getElementById("simpleSearchSwitch").style.display = "block";
	}
	else {
		document.getElementById("advancedSearch").style.display = "none";
		document.getElementById("advancedSearchSwitch").style.display = "block";
		document.getElementById("simpleSearchSwitch").style.display = "none";
	}
}

function showUpdatingPopup() {
	if(!document.getElementById("search_gif")) {return false;}
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0) {
		if (window.pageYOffset) {ScrollTop = window.pageYOffset;}
		else {ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;}
	
	}
	var width = (screen.width - 150) / 2;
	var height = screen.height / 2 - 200;
	popup = document.getElementById("search_gif");
	popup.style.left = width + 'px';
	popup.style.top = height + 'px';
	popup.style.display = "block";
}
function hideUpdatingPopup() {
	if(!document.getElementById("search_gif")) {return false;}
	document.getElementById("search_gif").style.display = "none";
}
function showSearchingPopup() {
	if(!document.getElementById("searching_gif")) {return false;}
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0) {
		if (window.pageYOffset) {ScrollTop = window.pageYOffset;}
		else {ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;}
	
	}
	var width = (screen.width - 150) / 2;
	var height = screen.height / 2 - 200;
	popup = document.getElementById("searching_gif");
	popup.style.left = width + 'px';
	popup.style.top = height + 'px';
	popup.style.display = "block";
}
function hideSearchingPopup() {
	if(!document.getElementById("searching_gif")) {return false;}
	document.getElementById("searching_gif").style.display = "none";
}
function showProcessingDonationPopup() {
	if(!document.getElementById("processing_donation_gif")) {return false;}
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0) {
		if (window.pageYOffset) {ScrollTop = window.pageYOffset;}
		else {ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;}
	
	}
	var width = (screen.width - 450) / 2;
	var height = screen.height / 2 - 200;
	popup = document.getElementById("processing_donation_gif");
	popup.style.left = width + 'px';
	popup.style.top = height + 'px';
	popup.style.display = "block";
}
function hideProcessingDonationPopup() {
	if(!document.getElementById("processing_donation_gif")) {return false;}
	document.getElementById("processing_donation_gif").style.display = "none";
}

function selectAllInvites() {
	checkboxes = $('delete_invites_form').getElementsByTagName("input");

	for(var i = 0; i < checkboxes.length; i++) {
		if(checkboxes[i].type == 'checkbox' && checkboxes[i].name == 'invitation_ids[]') {
			checkboxes[i].checked = true;
		}
	} 
	if($('delete_all_invites')) { Element.show('delete_all_invites'); }
}

function unselectAllInvites() {
	checkboxes = $('delete_invites_form').getElementsByTagName("input");

	for(var i = 0; i < checkboxes.length; i++) {
		if(checkboxes[i].type == 'checkbox' && checkboxes[i].name == 'invitation_ids[]') {
			checkboxes[i].checked = false;
		}
	} 
	if($('delete_all_invites')) { Element.hide('delete_all_invites'); }
}

function inviteUnselected() {
	$('select_all').checked = false;
	if($('delete_all_invites')) { Element.hide('delete_all_invites'); }
}

function showReportPopup(obj) {
	var popup = document.getElementById(obj.id+"p");
	popup.style.left = findPosX(obj) + 'px';
	popup.style.top = findPosY(obj) + 16 + 'px';
	document.body.appendChild(popup);
	popup.style.display = "block";
}
function closeReportPopup(obj) {
	//document.getElementById("bg").style.display = "none";
	obj.parentNode.style.display = "none";
}
function showHelp(obj) {
	var popup = document.getElementById(obj.id+"p");

	xPos = findPosX(obj);
	if(xPos > 900) {xPos = xPos - 200;} else {xPos = xPos - 20;}
	popup.style.left = xPos + 'px';
	popup.style.top = findPosY(obj) + 'px';
	popup.style.display = "block";
	popup.onclick = function() {this.style.display = "none";}
}
function showCommonHelp(obj, popupId) {
	var popup = document.getElementById(popupId);
	popup.style.left = findPosX(obj) - 20 + 'px';
	popup.style.top = findPosY(obj) + 'px';
	popup.style.display = "block";
	popup.onclick = function() {this.style.display = "none";}
}
function showPopup(id, scrolling) {
	if(!document.getElementById("pop-up-" + id)) {return false;}

	if($("black") == null) {
		var black = document.createElement('div');
		black.id = "black";
		black.style.height = (document.body.offsetHeight+50) + 'px';
		document.getElementsByTagName("body")[0].appendChild(black);
	}

	if($('vid-embed')) { $('vid-embed').style.visibility = 'hidden'; }
	var popup = document.getElementById("pop-up-" + id);

	if(oldBrowser()) {scrolling = 'absolute';}

	if(scrolling == undefined || scrolling == null || scrolling == 'fixed') {
		popup.style.position = 'fixed';
		popup.style.display = "block";
		offsetHeight = Math.floor((document.documentElement.clientHeight - popup.offsetHeight) / 2);
		popup.style.top = offsetHeight + 'px';
	} else {
		popup.style.top = windowYOffset() + 150 + 'px';
		popup.style.position = 'absolute';
		popup.style.display = "block";
	}	
	
}
function hidePopup(id) {
	if(!document.getElementById("pop-up-" + id)) {return false;}

	var black = document.getElementById("black");
	document.getElementsByTagName("body")[0].removeChild(black);
	black.style.display = "none";

	if($('vid-embed')) { $('vid-embed').style.visibility = 'visible'; }
	document.getElementById("pop-up-" + id).style.display = "none";
}
function showLockedPopup(obj, user_id) {
	if($('private_popup_link'))
		$('private_popup_link').href = '/people/add_friends?friend_id=' + user_id;
	var popup = document.getElementById("lockedUserp");
      popup.style.left = findPosX(obj) + 'px';
	popup.style.top = findPosY(obj) + 16 + 'px';
	document.body.appendChild(popup);
	popup.style.display = "block";
}
function showComplimentPopup(context, recipient_id, recipient_name, complimentable_id) {
	$('complimentRecipientName').innerHTML = recipient_name;
	$('complimentForm').complimentText.value = '';
	$('complimentForm').complimentContext.value = context;
	$('complimentForm').complimentRecipient.value = recipient_id;
	$('complimentForm').complimentableID.value = complimentable_id;
	showPopup('complimentForm');
}
function closeComplimentPopup() {
	hidePopup('complimentForm');
	$('complimentForm').complimentText.value = '';
	$('complimentBody').innerHTML = '';
}
function disableComplimentButtons() {
	$('complimentForm').elements['send'].disabled = true;
	$('complimentForm').elements['cancel'].disabled = true;
}
function enableComplimentButtons() {
	$('complimentForm').elements['send'].disabled = false;
	$('complimentForm').elements['cancel'].disabled = false;
}
function disableSubmit() {
	$('submit_payment').disabled = true;
	$('cancel_payment').disabled = true;
}
function enableSubmit() {
	$('submit_payment').disabled = false;
	$('cancel_payment').disabled = false;
}
function switchToReplyEditor(id) {
	tinyMCE.execCommand("mceAddControl", false, "comment_comment_"+id);

	Element.show('replyForm' + id); 
	Element.hide('commentForm');     
}
function switchToCommentEditor(id) {
	Element.hide('replyForm' + id); 
	Element.show('commentForm');        
}
function windowYOffset() {
	if(window.pageYOffset)
		return window.pageYOffset;
	else
		return (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
}
function findPosY(obj) {
	var posTop = 0;
	while (obj.offsetParent) {posTop += obj.offsetTop; obj = obj.offsetParent;}
	return posTop;
}
function findPosX(obj) {
	var posLeft = 0;
	while (obj.offsetParent) {posLeft += obj.offsetLeft; obj = obj.offsetParent;}
	return posLeft;
}
function checkFieldLength(fieldID, counterID, maxLength) {
	if($(fieldID)) {
		content = $(fieldID).value;
		length = content.length;

		if(length > maxLength) {
			$(fieldID).value = content.substring(0, maxLength);
			length = maxLength;
		} 
		
		if($(counterID)) {
			$(counterID).innerHTML = (maxLength - length);
		}
	}
}
function askCommentFocus(askID) {
	formID = 'askResponse' + askID;
	if($(formID)) {
		comment_field = $(formID).comment_comment;
		comment_field.style.height = '30px';
		comment_field.style.color = '#000';
		if(comment_field.value == 'Write a comment') { comment_field.value = ''; }
		comment_field.focus();

		$('askResponseSubmit' + askID).style.display = 'block';
	}
}
function askCommentFocusLost(askID) {
	formID = 'askResponse' + askID;
	if($(formID)) {
		comment_field = $(formID).comment_comment;
		if(comment_field.value == '') {
			comment_field.style.height = '15px';
			comment_field.style.color = '#666666';
			comment_field.value = 'Write a comment';
			$('askResponseSubmit' + askID).style.display = 'none';
		}
	}
}
function replaceExtChars(text) {
	fromTo = new Array('&amp;', '&', '&gt;','>','&lt;','<','&quot;','\"');

	for(i=0; i < fromTo.length; i=i+2) {
		text = text.replace(eval('/'+fromTo[i+1]+'/g'), fromTo[i]);
	}
	
	return text;
}
function profileNoteFocus(defaultText) {
	if($('profileNote')) {
		comment_field = $('profileNote').comment_comment;
		//comment_field.style.height = '60px';
		comment_field.style.color = '#000';
		if(comment_field.value == defaultText) { comment_field.value = ''; }
		comment_field.focus();
		//$('profileNoteSubmit').style.display = 'inline';
		//$('character-count').style.display = 'block';
	}
}
function profileNoteFocusLost(defaultText) {
	if($('profileNote')) {
		comment_field = $('profileNote').comment_comment;
		if(comment_field.value == '') {
			//comment_field.style.height = '15px';
			comment_field.style.color = '#666666';
			comment_field.value = defaultText;
			//$('profileNoteSubmit').style.display = 'none';
			//$('character-count').style.display = 'none';
		}
	}
}

///// Petition Target Selection

function numCheckedBoxesOfGivenClass(node, searchClass) {
	var els = node.getElementsByTagName('input');

	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	var numChecked = 0;
	for (i = 0, j = 0; i < els.length; i++) {
		if ( pattern.test(els[i].className) && els[i].type == 'checkbox') {
			if(els[i].checked)
				numChecked += 1;
		}
	}
	return numChecked;
}

function oldBrowser() {
	if(navigator.appVersion.indexOf('MSIE 6') >= 0)
		return true;
	else if(navigator.appVersion.indexOf('Safari') >= 0 && navigator.appVersion.indexOf('Version/2.') >= 0)
		return true;
	else
		return false;
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function getTextAreaSelection(txtarea)
{
	txt = '';
	if (typeof txtarea.selectionStart != "undefined") {
		txt = (txtarea.value).substring(txtarea.selectionStart, txtarea.selectionEnd);
	} else if (document.selection)
    { txt = document.selection.createRange().text; }

	return txt;
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function loadScript(src) {
  var e = document.createElement('script'); 
  e.setAttribute('language','javascript'); 
  e.setAttribute('type', 'text/javascript');
  e.setAttribute('src',src); 
  document.body.appendChild(e); 
}

