var _numVideos = 0;
var _numAudio = 0;
var _numDocuments = 0;

function addDocumentDownload(file){
    var insertInto = document.getElementById('documentDownloadTable');
    var parentObj = insertInto.parentNode;
    var newContent = document.createElement("input");
	var newContentPrimaryFlag = document.createElement("input");
	var	newContentReplyToFlag = document.createElement("input");
	var removeButton = document.createElement("img");
	var tbody = document.createElement("tbody");
	var tr = document.createElement("tr");
	var td1 = document.createElement("td");
	var td2 = document.createElement("td");
	var td3 = document.createElement("td");

	// set input field attributes
	newContent.setAttribute("type", "text");
	newContent.setAttribute("name", "added_link_" + _numDocuments);
	newContent.setAttribute("id", "added_link_" + _numDocuments);
	newContent.setAttribute("size", "30");

	// remove button
	removeButton.setAttribute("name", _numDocuments)
	removeButton.setAttribute("src", "public/images/delete_inline.gif");
	removeButton['onclick'] = function() {
	    var oNodeToRemove = document.getElementById('fileDocumentRow' + this.name);
	    oNodeToRemove.parentNode.removeChild(oNodeToRemove);
    }
    
    tr.setAttribute("id", "fileDocumentRow" + _numDocuments);
	
	td1.appendChild(newContent);
	td1.appendChild(document.createTextNode(" "));
	td1.appendChild(removeButton);

	tr.appendChild(td1);
	tr.appendChild(td2);
	
	tbody.appendChild(tr);
	
	insertInto.appendChild(tbody);
	
	// insert the new div->input into the DOM
	parentObj.insertBefore(document.getElementById('documentTargetBody'), insertInto);

	_numDocuments++;
}

function loadArticle(article_id){
	$.post("index.php", 'module=home&action=index&event=load_article&article_id='+article_id,
		  function(data){
			 $('#article_view').html(data);
		  }, "text");
}

function followOfficial(official_id, button){
	$.post("index.php", 'module=officials&action=view&event=follow&official_id='+official_id,
		  function(data){
			button.value = data;
		  }, "text");
}

function voteForFeedback(feedback_id, module){
	if(!module){
		module = 'officials';
	}
	$.post("index.php", 'module='+module+'&action=view&event=vote_feedback&feedback_id='+feedback_id,
		  function(data){
			 $('#div_votes_'+feedback_id).html(data);
		  }, "text");
}

function loadFeedback(officialId, format, module, action, id_type, type){
	if(!module){
		module = 'officials';
	}
	if(!action){
		action = 'view';
	}
	if(!id_type){
		id_type = 'official_id';
	}
	var postUrl = 'module='+module+'&action='+action+'&event=load_feedback&format='+format+'&'+id_type+'='+officialId;
	if(type){
		postUrl += '&type='+type;
	}
	$.post("index.php", postUrl,
		  function(data){
			if(format == 'highlights'){
				 document.getElementById('feedback_highlights').className = 'on highlights';
				 document.getElementById('feedback_recent').className = 'recent';
				if(document.getElementById('feedback_polls')){
					document.getElementById('feedback_polls').className = 'recent';
				}
				 document.getElementById('feedback_data').className = 'recent';
			}else{
			 	document.getElementById('feedback_highlights').className = 'highlights';
				document.getElementById('feedback_recent').className = 'on recent';
				if(document.getElementById('feedback_polls')){
					document.getElementById('feedback_polls').className = 'recent';
				}
				document.getElementById('feedback_data').className = 'recent';
			}
			$('#community_topics').html(data);
		  }, "text");
}

function loadPollsTab(officialId, format, module, action, id_type, type){
	$.post("index.php", 'module=polls&action=view&event=load_polls_by_official&&official_id='+officialId,
		  function(data){
			document.getElementById('feedback_highlights').className = 'highlights';
			document.getElementById('feedback_recent').className = 'recent';
			document.getElementById('feedback_data').className = 'recent';
			document.getElementById('feedback_polls').className = 'on recent';
			
			$('#community_topics').html(data);
		  }, "text");
}

function loadDataTab(officialId, format, module, action, id_type, type){
	$.post("index.php", 'module='+module+'&action='+action+'&event=load_related_data&'+id_type+'='+officialId,
		  function(data){
			document.getElementById('feedback_highlights').className = 'highlights';
			document.getElementById('feedback_recent').className = 'recent';
			if(document.getElementById('feedback_polls')){
				document.getElementById('feedback_polls').className = 'recent';
			}
			document.getElementById('feedback_data').className = 'on recent';
			
			$('#community_topics').html(data);
		  }, "text");
}

function getTabCount(url, divId){
	$.post("index.php", url,
		  function(data){
			$('#'+divId).html(data);
		  }, "text");
}

function saveOfficialLinks(official_id){
	 var str = $("#linkForm").serialize();
	$.post("index.php", 'module=officials&action=view&event=add_links&official_id='+official_id+'&'+str,
		  function(data){
			$('#div_official_urls').html(data);
			for(i = 0; i < _numDocuments; i++){
			 	var oNodeToRemove = document.getElementById('fileDocumentRow' + i);
	    		oNodeToRemove.parentNode.removeChild(oNodeToRemove);
	    	}
		  }, "text");
}

function removeOfficialUrl(officialUrlId, official_id){
	 var str = $("#linkForm").serialize();
	$.post("index.php", 'module=officials&action=view&event=remove_link&official_url_id='+officialUrlId+'&official_id='+official_id,
		  function(data){
			$('#div_official_urls').html(data);
		  }, "text");
}

function postReply(feedback_id){
	$.post("index.php", 'module=officials&action=view&event=get_feedback_item&feedback_id='+feedback_id,
		  function(data){
		  	if(data){
		  		var div = document.getElementById('parent_id');
				div.value = feedback_id;
				var div2 = document.getElementById('inline_search_results');
				div2.style.display = 'block';
				div2.innerHTML = '<br><img src="public/images/delete_inline.gif" onClick="javascript:clearReply();"`>&nbsp;&nbsp;Replying To: '+data;
		  	}
		  }, "text");
}

function hide_page_notification(){
	var div = document.getElementById('page_notification');
	div.style.display ='none';
}

function submitAjaxForm(button){
if(button.form.description.value == ''){
 	alert('Please enter some text');
 	return;
}
	var dataString = 'module='+ button.form.module.value + '&related_id=' + button.form.related_id.value + '&description=' + button.form.description.value+ '&related_type=' + button.form.related_type.value + '&event='  + button.form.event.value+ '&feedback_type='  + button.form.feedback_type.value;
		var responseString = '';
		if(button.form.feedback_type.value == 'question'){
			responseString = "Your Question has been submitted!";
		}else if(button.form.feedback_type.value == 'idea'){
			responseString = "Your Idea has been submitted!";
		}else{
			responseString = "Your Comment has been submitted!";
		}
	$.ajax({
		type: "POST",
		url: "index.php",
		data: dataString,
		success: function(){
			var div1 = document.getElementById('div_form');
			div1.style.display = 'none';
			
		 	$('#start_topic_box').html("<div id='message'></div>");
		 	$('#message').html("<h1>"+responseString+"</h1>")  
     .append("<p>Login to <a href='http://govbe.com'>Govbe</a> to communicate some more.</p>")  
     .hide()  
     .fadeIn(1500, function() {  
         
    });  		
		// var div2 = document.getElementById('div_results');
		//div2.style.display = 'block';
		//div2.innerHTML = 'roggg';
		}
		});
}

function clearReply(){
	var div = document.getElementById('parent_id');
	div.value = '';
	var div2 = document.getElementById('inline_search_results');
	div2.style.display = 'none';
	div2.innerHTML = '';
}

function loadOfficialsActivity(officialId, divId, count){
	$.post("index.php", 'module=officials&action=view&event=load_activity&official_id='+officialId+'&count='+count,
		  function(data){
			$('#'+divId).html(data);
		  }, "text");
}

function loadRelatedStories(officialId, divId){
	$.post("index.php", 'module=officials&action=view&event=load_related_stories&official_id='+officialId,
		  function(data){
			$('#'+divId).html(data);
		  }, "text");
}

function loadFollowers(officialId, divId){
	$.post("index.php", 'module=officials&action=view&event=load_followers&official_id='+officialId,
		  function(data){
			$('#'+divId).html(data);
		  }, "text");
}

function loadLocalOfficials(location_id, divId){
	$.post("index.php", 'module=local&action=index&event=load_local_officials&location_id='+location_id,
		  function(data){
			$('#'+divId).html(data);
		  }, "text");
}

function setQuestionActiveTab(){
	var liItem = document.getElementById('li_question');
	liItem.className = 'question active';
	var liItem1 = document.getElementById('li_idea');
	liItem1.className = 'idea';
	var liItem2 = document.getElementById('li_problem');
	liItem2.className = 'problem';
	document.getElementById('feedback_type').value = 'question';
	var liItem3 = document.getElementById('li_message');
	liItem3.className = 'praise';
}	

function setProblemActiveTab(){
	var liItem = document.getElementById('li_question');
	liItem.className = 'question';
	var liItem1 = document.getElementById('li_idea');
	liItem1.className = 'idea';
	var liItem2 = document.getElementById('li_problem');
	liItem2.className = 'problem active';
	document.getElementById('feedback_type').value = 'feedback';
	var liItem3 = document.getElementById('li_message');
	liItem3.className = 'praise';
}

function setIdeaActiveTab(){
	var liItem = document.getElementById('li_question');
	liItem.className = 'question';
	var liItem1 = document.getElementById('li_idea');
	liItem1.className = 'idea active';
	var liItem2 = document.getElementById('li_problem');
	liItem2.className = 'problem';
	document.getElementById('feedback_type').value = 'idea';
	var liItem3 = document.getElementById('li_message');
	liItem3.className = 'praise';
}

function setMessageActiveTab(){
	var liItem = document.getElementById('li_poll');
	liItem.className = 'question';
	var liItem3 = document.getElementById('li_message');
	liItem3.className = 'praise active';
	document.getElementById('feedback_type').value = 'message';
}

function setPollActiveTab(){
	var liItem = document.getElementById('li_poll');
	liItem.className = 'question active';
	var liItem3 = document.getElementById('li_message');
	liItem3.className = 'praise';
}

function hideShowDiv(showDivId, hideDivId){
	var div = document.getElementById(showDivId);
	div.style.display = 'block';
	var div2 = document.getElementById(hideDivId);
	div2.style.display = 'none';
}

function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}

