/*Home page*/
function getForumPosts(){
$(this).addClass('justPostedSelected');
$(this).siblings('td').removeClass('justPostedSelected');
var sort; 
var action = null;
switch(this.id){
case 'popular':
sort = 'last_modified';
break;
case 'newest':
sort = 'added';
break;
case 'friends':
action='friends';
break;
case 'categories':
action='categories';
break;
}
if (typeof(justPostedReviewedOnly) != 'undefined'){
var dataString = "sort="+sort+"&action="+action+"&ro=true"
}else{
var dataString ="sort="+sort+"&action="+action; 
} 
 $.ajax({
   type: "POST",
   url: "mag/get_just_posted.php",
   data: dataString,
   success: function(posts){
    $('#justPostedBorder').html(posts)
   }
 });

}
//ADMIN HOME PAGE
function getMyContent(section,type, page, sort){
	if (!page){
	page = 1
	}
	if(!sort){
	sort ='last_modified'
	}
	
	switch(section){
		case 'myPosts':
		callback = 'myPostsContentCallBack'
		myPostsFilter = sort; 
		break;
		case 'friendsPosts':
		callback = 'friendsPostsContentCallBack'
		friendsPostsFilter = sort; 
		break;

	}

	
	commentRequest = new AJAX(0, callback);
    document.getElementById(section).innerHTML='Loading...';
commentRequest.makeRequest(site_url+"/mag/show_my_stuff.php?sec="+section+"&itm="+type+"&pg="+page+'&sortby='+sort);
}
function myPostsContentCallBack(str){
	 document.getElementById("myPosts").innerHTML=str;
}

function friendsPostsContentCallBack(str){
	document.getElementById("friendsPosts").innerHTML=str;
}
function getUserArticles(year){
	commentRequest = new AJAX(0, 'getUserArticlesCallBack');
    document.getElementById('article_list').innerHTML='Loading Articles...';
commentRequest.makeRequest(site_url+"/mag/admin/get_user_articles.php?year="+year);
}
function getUserArticlesCallBack(str){
	 document.getElementById('article_list').innerHTML=str;
}

function getUserwwyds(uid,pg){
	commentRequest = new AJAX(0, 'getUserwwydsCallBack');
    document.getElementById('wwyd_list').innerHTML='Loading WWYDs...';
commentRequest.makeRequest(site_url+"/mag/get_user_wwyds.php?wuid="+uid+"&pg="+pg);
}
function getUserwwydsCallBack(str){
	 document.getElementById('wwyd_list').innerHTML=str;
}

function getUserBurns(year){
	commentRequest = new AJAX(0, 'getUserBurnsCallBack');
    document.getElementById('burns_list').innerHTML='Loading Burns...';
commentRequest.makeRequest(site_url+"/mag/admin/get_user_burns.php?year="+year);
}
function getUserBurnsCallBack(str){
	 document.getElementById('burns_list').innerHTML=str;
}

//CONFESSIONS
//  ajax call to get comments for confessions where cid = confession id p = page number
function getConfessionComments(cid, p){
	commentRequest = new AJAX(0, 'confessionCommentsCallback');
    document.getElementById('comments_text').innerHTML='Loading Comments...';
commentRequest.makeRequest(site_url+"/mag/confessions_get_comments.php?cid="+cid+"&cpn="+p);
}
//  calback function for displaying confession comments
function confessionCommentsCallback(str){
	document.getElementById('comments_text').innerHTML=str
	topNav();
}
//  get confessions as ajax call where cid = confession id p = page number
function getConfessions(sortby,p){
			commentRequest = new AJAX(0, 'confessionsCallback');
    document.getElementById('confessions_text').innerHTML='Loading Confessions...';
commentRequest.makeRequest(site_url+"/mag/confessions_get_confessions.php?cpn="+p+"&sort_by=" + sortby);

}
//  callback for confessions
function confessionsCallback(str){
	document.getElementById('confessions_text').innerHTML=str;
	topNav();
}
//ARTICLES
function getArticleComments(aid, p){
	commentRequest = new AJAX(0, 'articleCommentsCallback');
    document.getElementById('comments_text').innerHTML='Loading Comments...';
commentRequest.makeRequest(site_url+"/mag/articles_get_comments.php?aid="+aid+"&cpn="+p);
}
//  calback function for displaying confession comments
function articleCommentsCallback(str){
	document.getElementById('comments_text').innerHTML=str;
	topNav();
    $('div.iconPopUp').each(function(){
      var data = {userid:$(this).next('.userid').html()}; 
      $(this).mybActionIcons(data,{doIconPopUp:true,token:tok});
    });
}
//  function specifically to put a copy of the nav retunred from the ajax call into the parent template that is not refreshed on the ajax call
function topNav(){
			var cnav = document.getElementById("pageNav");
			var tnav = document.getElementById("commentsTopNav");
tnav.innerHTML = cnav.innerHTML;
}
//  get articles for a specific author for use on the author's bio page
function getArticlesForAuthor(aid,year){

	commentRequest = new AJAX(0, 'authorArticlesCallBack');
    document.getElementById('article_list').innerHTML='Getting articles...';
commentRequest.makeRequest(site_url+"/mag/author_get_archived.php?aid="+aid+"&ayear="+year);
}
function authorArticlesCallBack(str){
	document.getElementById('article_list').innerHTML=str
}
//WYYD
function getwwydComments(wid, p){

  COMSCORE.beacon({c1:2,c2:6035488,c3:"",c4:SITE_URL + 'mag/wwyd/comments',c5:'mag/wwyd/comments',c6:"",c15:""});
	commentRequest = new AJAX(0, 'wyydCommentsCallback');
	document.getElementById('comments_text').innerHTML='Loading Comments...';
	commentRequest.makeRequest(site_url+"/mag/wwyd_get_comments.php?wid="+wid+"&cpn="+p);
	
}
//  calback function for displaying WYYD comments
function wyydCommentsCallback(str){
document.getElementById('comments_text').innerHTML=str
topNav();
	$('div.iconPopUp').each(function(){
      var data = {userid:$(this).next('.userid').html()}; 
      $(this).mybActionIcons(data,{doIconPopUp:true,token:tok});
    });
}
function getwwyds(sortby,p,cat){
      
  COMSCORE.beacon({c1:2,c2:6035488,c3:"",c4:SITE_URL + 'mag/wwyd',c5:'mag/wwyd',c6:"",c15:""});
  var urlString = site_url+"/mag/wwyd_get_wwyds.php?cpn="+p;
  
  if (cat){
    urlString += "&cat="+cat;
  }
  
  if (sortby){
    urlString += "&sort_by="+sortby;
  }
  
  commentRequest = new AJAX(0, 'wwydsCallback');
  document.getElementById('wwyds_text').innerHTML='Loading WWYDs...';
  commentRequest.makeRequest(urlString);

}
//  callback for wwyd
function wwydsCallback(str){
	document.getElementById('wwyds_text').innerHTML=str;
	topNav();
	$('div.iconPopUp').each(function(){
      var data = {userid:$(this).next('.userid').html()}; 
      $(this).mybActionIcons(data,{doIconPopUp:true,token:tok});
    });
}

//WWYDTM
function getwwydtmResponses(wid,pg){
		commentRequest = new AJAX(0, 'wwydtmResponsesCallBack');
    document.getElementById('wwydtm_responses').innerHTML='Loading Responses...';
commentRequest.makeRequest(site_url+"/mag/wwydtm_get_responses.php?wid="+wid+"&pn="+pg);
}
function wwydtmResponsesCallBack(str){
	document.getElementById('wwydtm_responses').innerHTML=str;
}
//BURN
//  get burn comments
function getBurnComments(bid,p){
	commentRequest = new AJAX(0, 'getBurnCommentsCallBack');
    document.getElementById('comments_text').innerHTML='Loading Comments...';
commentRequest.makeRequest(site_url+"/mag/burn_get_comments.php?bid="+bid+"&cpn="+p);
}
function getBurnCommentsCallBack(str){
  document.getElementById('comments_text').innerHTML=str;
  topNav();
  $('div.iconPopUp').each(function(){
    var data = {userid:$(this).next('.userid').html()}; 
    $(this).mybActionIcons(data,{doIconPopUp:true,token:tok});
  });
}
//ARTICLE ARCHIVE
// get archived articles by month and year
function getArchivedArticles(sort_by,p){
	commentRequest = new AJAX(0, 'archivedArticlesCallBack');
    document.getElementById('archive_list').innerHTML='Getting articles...';
commentRequest.makeRequest(site_url+"/mag/articles_get_archived.php?sort_by="+sort_by+"&cpn="+p);
}
function archivedArticlesCallBack(str){
	document.getElementById('archive_list').innerHTML=str;
}
//BATTLES
//  vote on a battle
function voteOnBattle(bid, side){
	vote = new AJAX(0, 'voteOnBattleCallBack');
    document.getElementById('battle_royal').innerHTML="<table align='center' height ='100%'><tr><td class='battleHeader' valign='middle'>Getting next battle...</td></tr></table>";
vote.makeRequest(site_url+"/mag/battles_vote.php?vote=true&battle_id="+bid+"&side_voting_on="+side);
}
function voteOnBattleCallBack(str){
	document.getElementById("battle_royal").innerHTML = str;
}
//  watch a battle
function watchBattle(battle, side) {
	watchRequest = new AJAX(0,'watchBattleCallBack');

	if (side == "left") {
		document.getElementById('addBattleStatusLeft').innerHTML='<br>Adding to watched battles...';
	}
	else {
		document.getElementById('addBattleStatusRight').innerHTML='<br>Adding to watched battles...';
	}
	watchRequest.makeRequest(site_url+'/battles/watchbattle.php?battleid='+battle+'&side='+side);
}
//  watch battle callback
function watchBattleCallBack(str){
	var responsearray=str.split(",");
	if (responsearray[0] == "left") {
			document.getElementById('addBattleStatusLeft').innerHTML=responsearray[1];
	}
	else {
			document.getElementById('addBattleStatusRight').innerHTML=responsearray[1];
	}
}
//WWYD ARCHIVE
// get archived WWYDS
function getArchivedwwyds(sort_by,p){
	commentRequest = new AJAX(0, 'archivedwwydsCallBack');
    document.getElementById('archive_list').innerHTML='Getting What Would You Do\'s...';
commentRequest.makeRequest(site_url+"/mag/wwyd_get_archived.php?sort_by="+sort_by+"&cpn="+p);
}
function archivedwwydsCallBack(str){
	document.getElementById('archive_list').innerHTML=str;
	topNav();
}
//Burn ARCHIVE
// get archived burns
function getArchivedBurns(sort_by,p){
	commentRequest = new AJAX(0, 'archivedArticlesCallBack');
    document.getElementById('archive_list').innerHTML='Getting articles...';
commentRequest.makeRequest(site_url+"/mag/burns_get_archived.php?sort_by="+sort_by+"&cpn="+p);
}
function archivedBurnsCallBack(str){
	document.getElementById('archive_list').innerHTML=str;
}

//POLLS
// vote on a poll
function takePoll(){
	if(document.pollform){
		poststring="";
		for( i = 0; i < document.pollform.elements.length; i++ ){
			if( document.pollform.elements[i].name=="pollradio"){
				if(document.pollform.elements[i].checked==true){
					poststring += "&" + document.pollform.elements[i].name + "=" + document.pollform.elements[i].value;
				}
			}
			else{
				poststring += "&" + document.pollform.elements[i].name + "=" + document.pollform.elements[i].value;
			}
		}
	}
	takePollAJ = new AJAX(0,'typicalPollResponse');
	takePollAJ.makeRequestPost( site_url+"/mag/takepoll.php", poststring );
}
//  get next poll from link in take poll form
function showPoll(){
	$.ajax({
		type: 'GET',
		url: '/mag/takepoll.php?reload=1&yearbookid=0&yearbooktype=M',
		success: function(str){
			$('#pollsPanel').html(str);
		}
	});
}
function typicalPollResponse(str){
	document.getElementById("polldisplay").innerHTML=str;
}

// show hide form elements for multiple choice - had to replace the one found in common.js since that function is designed to work with a checkbox
function showHideMc(obj)
{
   if(obj.value =="mc")
   {
		document.getElementById("row0").style.display='';
		document.getElementById("row1").style.display='';
		func_display_textbox();
   }
   else
   {
   	for (x=0; x<10;x++){
   		document.getElementById("row"+x).style.display='none';
   	}
   }
}
//  validate the poll form before submitting
function validatePollForm(){
		var msg ="";

			if (document.forms["frm"].question.value ==""){
		msg ="Please provide a question.\n"
	}

		if(document.forms["frm"].qtype[1].checked){
			var answers = document.forms["frm"].elements["answer[]"];
			var non_blank =0;
	for (x=0 ; x< answers.length;x++){
		if (answers[x].value!=""){
			non_blank++
		}
		if (non_blank>=2){
			break;
		}
	}
	if(non_blank<2){
		msg+="Please provide at at least two answers for multiple choice questions."
	}
	}
		if (msg!=""){
		alert(msg);
		}else{
			document.forms["frm"].submit();
		}

}
//  toggle the visbility of a comment posting form given an ID of postComment
function toggleComment(){
var cdiv
if(document.getElementById("postComment")){
cdiv = 	document.getElementById("postComment")
}else if(document.getElementById("signin_form")){
	cdiv = document.getElementById("signin_form");
}
if (cdiv){
var setDisplay = (cdiv.style.display == 'block') ? 'none':'block';
cdiv.style.display = setDisplay;
}

}

function toggleElementVisibility(elemName){
var cdiv = document.getElementById(elemName);
var setDisplay = (cdiv.style.display == 'block') ? 'none':'block';
cdiv.style.display = setDisplay;
}
function makeArchiveMonthNav(){
	var tmn = document.getElementById("monthNavSrc").innerHTML;
document.getElementById("monthNav").innerHTML = tmn;
}
//  validate burn sumission form Used to ensure snap shot is uploaded or is intentionally excluded. Placed in this file since it is used on multiple pages
function validateBurnForm(){
	var snapshot = document.forms["burnForm"].snapshot.value
	if (snapshot.indexOf("\\") >=0 || snapshot.indexOf("/") >=0){
		document.forms["burnForm"].submit();
	}else{
	 var submit = confirm("You have not supplied a snapshot file. Submit anyway?")
	if (submit){
		document.forms["burnForm"].submit();
	}
	}
}
//confirm delete of burn, article or wwyd
function confirmDelete(formName){

	var submit = confirm("Are you sure you want to delete this item?")
	if (submit){
		document.forms[formName.name].submit();
	}
}

var warningMyMagText = "Inappropriate postings will be removed, and can result in your profile being closed.\n\nWe want you to have fun and express yourself in this forum. However, if you are vulgar or disrespectful you will be blocked from coming back.\n\nmyYearbook Admins regularly do spot checks.";
var warningTitleText = "Title cannot be blank.";
var warningPostText = "Post cannot be blank.";

function postComment( )
{
  
  comment = $("#wwyd_text").val( );
  
  if ( comment == warningMyMagText || comment == '' )
  {
  	
  	// only show the error once
  	if ( $( "#commentError" ).length == 0 )
  	{
    	$("#wwyd_submit").prepend("<span id='commentError' class='alert'>Please enter your comment below</span><br />");
  	}
  	
  } else {
  
    document.wwyd_submit.submit();
  
  }

}

function postThread( )
{
  
  error = false;  
  
  title = $( '#wwyd_title' ).val( );
  post = $( '#wwyd_text' ).val( );
    
  if ( title == '' || title == warningTitleText )
  {
  
    error = true;
    $( '#wwyd_title' ).val( warningTitleText );
    $( '#wwyd_title' ).css({ color:"red", fontWeight:"bold", width:"350px" });
    
  }
  
  if ( post == '' || post == warningMyMagText || post == warningPostText )
  {

    error = true;
    $( '#wwyd_text' ).val( warningPostText );
    $( '#wwyd_text' ).css({ color:"red", fontWeight:"bold" });
    
  }  
  
  if ( error == false )
  {

    document.add_wwyd.submit();
  
  }

}

function clear_wwyd_text( type )
{
  
  title = $("#wwyd_title").val( );
  post = $("#wwyd_text").val( );
  
  if ( type == 'title' )
  {
    
    if ( title == warningTitleText )
    {
    
      $( 'input#wwyd_title' ).val('');
      $( '#wwyd_title' ).text('');
      $( '#wwyd_title' ).css('color','#000');
      $( '#wwyd_title' ).css('font-weight','normal'); 
      $( '#wwyd_title' ).removeClass('wwyd_text');
    
    }
  
  } else {
  
    if ( post == warningPostText || post == warningMyMagText )
    {
  
      $( 'textarea#wwyd_text' ).val('');
      $( '#wwyd_text' ).text('');
      $( '#wwyd_text' ).css('color','#000');
      $( '#wwyd_text' ).css('font-weight','normal'); 
      $( '#wwyd_text' ).removeClass('wwyd_text');    
    
    }
  
  }

  
}
