
$g_search_candidate_init_msg = " zip code, state, or name";

function check_search_candidate_box () {
	var search_box = $('search_candidate');
	if (search_box.value == $g_search_candidate_init_msg) {
		search_box.value = '';
		search_box.removeClassName("initial_hint");
		search_box.addClassName("user_input");
	}
	else {
		search_box.select();
	}
}

function init_search_candidate_box () {
	var search_box = $('search_candidate');
	if (search_box.value == '') {
		search_box.value = $g_search_candidate_init_msg;
		search_box.addClassName("initial_hint");
	}
	else {
		search_box.addClassName("user_input");
	}
}

function select_other_party(party_select)
{
	var name = party_select.options[party_select.selectedIndex].value;
	if (name == 'Other') {
		$('other_party').show();
	}
	else {
		$('other_party').hide();
	}
}


function link_candidate(article_id, candidate_id)
{
	var url = '/linkCandidate.do?articleID=' + article_id + '&candidateID=' + candidate_id;
	new Ajax.Request(url, {method:'get', onSuccess: link_candidate_response});
 
	return (false);
}

function unlink_candidate(article_id, candidate_id)
{
	var url = '/linkCandidate.do?unlink=true&articleID=' + article_id + '&candidateID=' + candidate_id;
	new Ajax.Request(url, {method:'get', onSuccess: unlink_candidate_response});
 
	return (false);
}

function link_candidate_response(transport)
{
	var result = eval( '(' + transport.responseText + ')' );
	if (result.status) {
		var candidate_id = result.id;
		var candidate_div = $('candi_' + candidate_id);
		candidate_div.down("span.link_op").hide();
		
		var linked_div = $('linked_candidates');
		candidate_div.parentNode.removeChild(candidate_div);
		linked_div.appendChild(candidate_div);
		
		var edit_article = $('edit_article_op');
		if (edit_article) {
			edit_article.hide();
		}
		var view_article = $('view_article_op');
		if (view_article) {
			view_article.show();
		}
	}
	else {
		alert('Sorry, cannot add more than 7 politicians.');
	}
}

function unlink_candidate_response(transport)
{
	var result = eval( '(' + transport.responseText + ')' );
	if (result.status) {
		var candidate_id = result.id;
		var candidate_div = $('candi_' + candidate_id);
		candidate_div.hide();
	}
	else {
		alert('Sorry, cannot remove this politician to the article because unexpected server error.');
	}
}

function review_candidate(ac, rank)
{
	if (g_current_user) {
		var review_id = $(ac).up('div').id;
		var url = '/reviewCandidate.do?reviewID=' + review_id + '&rank=' + rank;
		new Ajax.Request(url, {method:'get', onSuccess: review_candidate_response});
	}
	else {
		alert ('Please sign in before you vote');
	}
	return (false);
}


function review_candidate_response(transport)
{
	var result = eval( '(' + transport.responseText + ')' );
	if (result.status) {
		var review_div = $(result.reviewID);
		var num_box = null;
		if (result.rank == -1) {
			num_box = review_div.down('span.negative');
		}
		else if (result.rank == 1) {
			num_box = review_div.down('span.positive');
		}
		num_box.innerHTML = parseInt(num_box.innerHTML) + 1;
	}
	else {
		alert('Sorry, you have already voted for this politician in the article.');
	}
}

function remove_supporter(supporter_id)
{
	if (confirm ('Are you sure you want to remove this supporter')) {
		var url = '/removeDelegate.do?supporterID=' + supporter_id;
		new Ajax.Request(url, {method:'get', onSuccess: remove_supporter_response});
	} 
	return (false);
}


function remove_supporter_response(transport)
{
	var result = eval( '(' + transport.responseText + ')' );
	if (result.status) {
		var supporter_id = result.id;
		var supporter_div = $('supporter_' + supporter_id);
		if (supporter_div) {
			supporter_div.hide();
		}
	}
	else {
		alert('Sorry, remove supporter failed, please refresh the page and try again.');
	}
}

function remove_delegate(delegate_id)
{
	if (confirm ('Are you sure you want to remove this voice')) {
		var url = '/removeDelegate.do?delegateID=' + delegate_id;
		document.location = url;
 	}
	return (false);
}

function show_article_widget()
{
	var widget_box = $('article_widget_box');
	if (! widget_box.visible()) {
		widget_box.show();
	}
	else {
		widget_box.hide();
	}
}
