//Instance variables
var textCounterFirstTimeThru = true;


// ******* messages.jsp ******* //

function hideMessageSubmit() {
	$('#postMessageBut').hide();
	$('#hiddenText').show();
}

function chgStatusMessages(action, messageID, publicationStatus ) { 
		var newStatus="";
		   
		for (var i=0 ; i < publicationStatus.length; i++) {
			if (publicationStatus[i].selected) {     
		      newStatus = publicationStatus[i].value;           
		      break;
			}
		}     
		
		document.messages.action= action+"?messageID="+messageID+"&newStatus="+newStatus;
		document.messages.submit();                 
}

function processComments (action) {
   var len = 0;
   var checkedCounter = 0;
   
   if (document.messages.messageID != null) {
        if (document.messages.messageID.length == null) {
            len  = 0;
            if (document.messages.messageID.checked) {
             checkedCounter++;
            }
        } else {
            len = document.messages.messageID.length;
        }        
   }
   for (var i=0; i < len; i++) {               
       if (document.messages.messageID[i].checked) {
           checkedCounter++;
       }            
   }
   if (checkedCounter == 0) {
       alert ("You must choose at least one message ");
   } else {
        if (confirm ("Are you sure to continue ?")) { 
                if(action != null){
                    document.messages.action = action
                }
                document.messages.submit();
                return false;
        }                
   }
}

function chgStatusComments(action, messageID, articleID, publicationStatus ) {
        var newStatus="";

        for (var i=0 ; i < publicationStatus.length; i++) {
            if (publicationStatus[i].selected) {
              newStatus = publicationStatus[i].value;
              break;
            }
        }

        document.messages.action= action+"?messageID="+messageID+"&articleID="+articleID+"&newStatus="+newStatus;
        document.messages.submit();
}


/*

The following two functions below are related to the 
messages.jsp textarea for user messages

*/
function textCounter(frmBox,countBox,MaxLimit){
	var iLength = frmBox.value.length;
    if (iLength > MaxLimit){
		if(textCounterFirstTimeThru) {
			alert("You have reached the maximum number of characters allowed");
			removeBannedChars(frmBox,countBox, MaxLimit);
			textCounterFirstTimeThru = false;
		}
		else {
			removeBannedChars(frmBox, countBox, MaxLimit);
		}
	} else {
		countBox.value = iLength;	
	}
}	


function removeBannedChars(frmBox, countBox, MaxLimit) {
	var iLength = frmBox.value.length;
	var iString = frmBox.value;
	if(MaxLimit < iLength){
		frmBox.value = iString.substring(0,MaxLimit);
		countBox.value = frmBox.value.length;			
	}
}

// ******* topics.jsp ******* //
function chgStatusTopics (action, threadID, form) { 
	 var newStatus="";
	 for (var i=0 ; i < form.publicationStatus.length; i++) {
	 	if (form.publicationStatus[i].selected) {     
	       newStatus = form.publicationStatus[i].value;
	       form.newStatus.value=newStatus;
	       form.threadID.value=threadID;
	       break;
	 	}
	 }     
	 
	// form.action= "/discussion/thread/changeStatus.do";
	 form.action = action;
	 form.submit();      
	 return false;           
}


// ******* adminInfoThread.jsp ******* //
function deleteThread(action) { 

  if (confirm ("Are you sure you want to delete this thread ?")) {
     document.threadForm.action = action;
     document.threadForm.submit();  
     return false;
  }
}

/*
    This function is called when submitting a comments-form.  It validates that the title and content of the post
    is not empty, or reveals an error message.  If it passes this submission, it attempts to submit the form, and also
    hides elements (submit button for example) to prevent double-posting
 */
function validateFields(form) {

    var hasErrors = false;

    if ($.trim($("input[name='messageTitle']").val()).length == 0) {
        hasErrors = true;
    }

    if ($.trim($("textarea[name='messageContent']").val()).length == 0) {
        hasErrors = true;
    }
    if (hasErrors) {
        $("#comments_required").slideDown(1500);
    } else {
        $("#comments_required").slideUp(1500);
        hideMessageSubmit();
        $("#" + form).submit();
    }
}
