function getObject(object) {
  objectInDom = document.getElementById(object);

  return objectInDom;
}

function clearValue(value, object) {
  object = getObject(object);
  
  if(object.value == value) {
    object.value = "";
  } else if(object.value == "") {
    object.value = value;
  }
}

function changeCheckbox(object) {
  object = getObject(object);
  
  if(object.checked == true) {
    object.checked = false;
  } else if(object.checked == false) {
    object.checked = true;
  }
}

function changeElementStatus(targetElementId, checkboxId, selectedBackground, originalBackground) {
  var target = getObject(targetElementId);
  var checkbox = getObject(checkboxId);
  if(checkbox.checked == true) {
    target.style.background=selectedBackground;
  } else if(checkbox.checked == false) {
    target.style.background=originalBackground;
  }
}


function changeLinkStatus(targetElementId, observedElement, selectedBackground, originalBackground) {
  var target = getObject(targetElementId);
  var element = getObject(observedElement);
  if(element.style.display == 'block') {
    target.style.background=selectedBackground;
  } else if(element.style.display == 'none') {
    target.style.background=originalBackground;
  }
}

function changeRadioStatus(targetElementId, radioId, selectedBackground, originalBackground) {
  var target = getObject(targetElementId);
  var radioBtn = getObject(radioId);  
  $('.userGroupRadio').css('background', originalBackground);
  target.style.background=selectedBackground;
}

function hideSimilarElements(className, clickedObjectController) {
  var clickedObject = getObject(clickedObjectController);
  if(clickedObject.style.display == "none") {
    $('.'+className).hide();
  }
}
function changeDisplay(object, animation) {
  
  if(animation != 'slide') {
  
    object = getObject(object);
    if(object.style.display == "block") {
      object.style.display = "none";
    } else {
      object.style.display = "block";
    }
    
  } else if(animation == 'slide') {
    objectName = object;
    object = getObject(object);
    if(object.style.display == "block") {
      $("#"+objectName).slideUp("slow");
    } else {
      $("#"+objectName).slideDown("slow");
    }
    
  }
}

function getValue(object) {
  var value = document.getElementById(object).value;
  
  return value;
}

function controlLoader(loaderId, action) {
  if(action == "hide") {
    document.getElementById(loaderId).style.display = "none";
  } else if(action == "show") {
    document.getElementById(loaderId).style.display = "block";
  }
}

function shareOnWall(wallShareboxText) {  
 
  controlLoader('wallLoader', 'show');
  var userWall = getValue('userWall');
  var text = getValue('postText');
  var dataString = 'shareOnWall=1&postText='+text+'&userWall='+userWall;
    $.ajax({
      type: "POST",
      url: "wallAjax.html",
      data: dataString,
      success: function(data) {
        document.getElementById('postText').value = wallShareboxText;
        controlLoader('wallLoader', 'hide');
        $("#wallContentAnchor").after(data);
      }
    });  
}  

        /*
function commentOnObject(id, allowAnonymous) {
  controlLoader('objectCommentLoader_'+id, 'show');
  var objectType = getValue('objectType_'+id);
  var commentText = getValue('commentText_'+id);
  var dataString = 'commentOnObject=1&commentText='+commentText+'&postId='+id+'&objectType='+objectType;
  if(allowAnonymous) {
    var commentAuthorNames = getValue('commentName_'+id);
    dataString = dataString+'&commentAuthorNames='+commentAuthorNames;
  }  
  
  // iztrii
  if(objectType == 11) {
    urlPath = "";
  } else {
    urlPath = "../";
  }
    $.ajax({
      type: "POST",
      url: urlPath+"wallAjax.html",
      data: dataString,
      success: function(data) {
        controlLoader('objectCommentLoader_'+id, 'hide');
        document.getElementById('commentText_'+id).value= '';
        
        if(allowAnonymous) {
          document.getElementById('commentsWrap').innerHTML = data;
        } else {
          $("#objectCommentsAnchor_"+id).before(data);        
        } 
        $("#objectCommentsAnchor_"+id).before(data);        
      }
    });  
}
        */

function commentOnObject(id, allowAnonymous) {
    
  $('#wfcSendSuccess').hide();
    
  controlLoader('objectCommentLoader_'+id, 'show');
  controlLoader('bttnSubmitWFC', 'hide');
  var objectType = getValue('objectType_'+id);
  var commentText = getValue('commentText_'+id);
  var commentSecurityCode = getValue('commentSecurityCode_'+id);
  var securityCode = getValue('securityCode_'+id);
  var dataString = 'commentOnObject=1&commentText='+commentText+'&commentSecurityCode='+commentSecurityCode+'&securityCode='+securityCode+'&postId='+id+'&objectType='+objectType;
  if(allowAnonymous) {
    var commentAuthorNames = getValue('commentName_'+id);
    dataString = dataString+'&commentAuthorNames='+commentAuthorNames;
  }  
  
  // iztrii
  if(objectType == 11) {
  urlPath = "";
  } else {
    urlPath = "../";
  }
    $.ajax({
      type: "POST",
      url: urlPath+"wallAjax.html",
      data: dataString,
      success: function(data) {
        controlLoader('objectCommentLoader_'+id, 'hide');
        controlLoader('bttnSubmitWFC', 'show');
        removeErrorClass(id);
          
        if(data.substring(0, 5) == 'error'){
                       
          var errors = data.substring(6, data.length).split(',');
          for (var i = 0; i<errors.length; i++){
            $('#'+errors[i]+'Box_'+id).addClass('err');
          }
          
          document.getElementById('objectCommentsAnchor_'+id).innerHTML=errorMessageCommentForm;
          
        }else if(data.substring(0, 7) == 'success'){
            
            document.getElementById('commentName_'+id).value= '';
            document.getElementById('commentText_'+id).value= '';
            
            document.getElementById('objectCommentsAnchor_'+id).innerHTML=successMessage;
            
        } else {
            
            document.getElementById('commentName_'+id).value= '';
            document.getElementById('commentText_'+id).value= '';
            
            if(allowAnonymous) {
              document.getElementById('commentsWrap').innerHTML = data;
            } else {
              $("#objectCommentsAnchor_"+id).before(data);
            }
        }
       
        updateSecurityCode(id);
        
      }
    });  
}

function updateSecurityCode(id){
  
  var objectType = getValue('objectType_'+id);  
  // iztrii
  if(objectType == 11) {
  urlPath = "";
  } else {
    urlPath = "../";
  }

  var dataString = 'updateSecurityCode=1&postId='+id;
  
    $.ajax({
      type: "POST",
      url: urlPath+"wallAjax.html",
      data: dataString,
      success: function(data) {
          document.getElementById('commentSecurityCodeBox_'+id).innerHTML = data;
      }
    });   
    
}


function deleteComment(id) {
  var dataString = 'deleteComment=1&commentId='+id;
    $.ajax({
      type: "POST",
      url: "wallAjax.html",
      data: dataString,
      success: function(data) {
         $("#wallPostComment_"+id).hide("slow");
      }
    });    
}

function showDelete(id, action) {
  object = getObject('deleteWallPost_'+id);
  if(action == 'show') {
    object.style.display = "block";
  } else if (action == 'hide') {
    object.style.display = "none";
  }
}

function deleteWallPost(id) {
  var dataString = 'deleteWallPost=1&postId='+id;
  $.ajax({
    type: "POST",
    url: "wallAjax.html",
    data: dataString,
    success: function(data) {
      $("#wallPost_"+id).hide("slow");
    }
  });      
}

function likeWallPost(id) {
  var dataString = 'likeWallPost=1&postId='+id;
  $.ajax({
    type: "POST",
    url: "wallAjax.html",
    data: dataString,
    success: function(data) {
      wallPostLike = getObject('wallPostLikes_'+id);
      wallPostLike.innerHTML = data;
      changeLikeLink(id, "like", "Unlike");
    }
  });      
}

function unLikeWallPost(id) {
  var dataString = 'unLikeWallPost=1&postId='+id;
  $.ajax({
    type: "POST",
    url: "wallAjax.html",
    data: dataString,
    success: function(data) {
      wallPostLike = getObject('wallPostLikes_'+id);
      wallPostLike.innerHTML = data;
      changeLikeLink(id, "unlike", "Like");
    }
  });      
}

function changeLikeLink(id, action, value) {
  object = getObject('wallPostLikeLink_'+id);
  if(action == "like") {
    object.innerHTML = "<a style='cursor: pointer;' onclick='javascript:unLikeWallPost(\""+id+"\")'>"+value+"</a>";
  } else if(action == "unlike") {
    object.innerHTML = "<a style='cursor: pointer;' onclick='javascript:likeWallPost(\""+id+"\")'>"+value+"</a>";
  }
}

function showRecordedFiles(categoryFiles) {
  changeDisplay(categoryFiles);
}


function saveVoiceToLibrary(id) {
  var categoryId = getValue('voiceCategory_'+id);
  var dataString = 'saveVoiceToLibrary=1&voiceId='+id+'&categoryId='+categoryId;
  var statusWrap = getObject('saveVoiceStatus_'+id);
    $.ajax({
      type: "POST",
      url: "my-library.html",
      data: dataString,
      success: function(data) {
        statusWrap.innerHTML = data;
      }
    });  
}

function saveToLibraryForm(id) {
  object = 'saveVoiceWrap_'+id;
  changeDisplay(object);
}

function formSubmit(id)	{
	document.getElementById(id).submit();
}

function validateFriendsGroup(form, errorMessage, newGroupFieldDefaultMessage) {
  var checked = false; 
  var buttons = form.elements.selectedGroup; 
  var newGroupField = form.elements.newGroup; 
  for (var i=0; i<buttons.length; i++) {  
    if (buttons[i].checked) {  
      checked = true; 
      break;  
    }  
  } 
  
  if(!checked && newGroupField.value != newGroupFieldDefaultMessage) {
    checked = true; 
  }
  
  if(!checked) 
    alert(errorMessage);  
    return checked; 
}

function sendPrivateMessage() {
  var messageSubject = getValue('messageSubject');
  var messageBody = getValue('messageBody');
  var messageReceiver = getValue('messageReceiver');
  var messageParent = getValue('messageParent');
	var loader = "";
  var dataString = 'sendMessage=1&messageSubject='+messageSubject+'&messageBody='+messageBody+'&messageReceiver='+messageReceiver+'&messageParent='+messageParent;

		$.ajax({
      type: "POST",
      url: "inbox.html",
      data: dataString,
      success: function(data) {
				if(messageParent > 0) {
					if(messageBody != '') {
							$("#messageRepliesAnchor").before(data);
							document.getElementById('messageBody').value = "";
					} else {	
						document.getElementById('messageStatus').innerHTML = data;
						setTimeout('document.getElementById("messageStatus").innerHTML = ""', 3000);
					}
				} else {
          document.getElementById('messageStatus').innerHTML = "Your message has been sent successfuly";
          setTimeout('$("#sendMessageForm").slideUp("slow");', 2000);          
        }
      }
    });  

}

function forwardFile(voiceId) {
  
  var fileReceiver = getValue('fileReceiver_'+voiceId);
  var dataString = 'forwardFile=1&fileReceiver='+fileReceiver+'&voiceId='+voiceId;

		$.ajax({
      type: "POST",
      url: "inbox.html",
      data: dataString,
      success: function(data) {
        originalForm = document.getElementById('forwardFileWrap_'+voiceId).innerHTML;
				document.getElementById('forwardFileWrap_'+voiceId).innerHTML = "<span style='padding: 0px 10px;'>Sent</span>";
        setTimeout("$('#forwardFileWrap_"+voiceId+"').slideUp('slow');", 2000);  
      }
    });  

}

function deleteMessage(id) {
	var loader = "";
  var dataString = 'deleteMessage=1&messageId='+id;

		$.ajax({
      type: "POST",
      url: "inbox.html",
      data: dataString,
      success: function(data) {
				$("#inboxMessage_"+id).hide("slow");
      }
    });  
}


function removeErrorClass(id) {
  $('#commentNameBox_'+id).removeClass('err');
  $('#commentTextBox_'+id).removeClass('err');
  $('#commentSecurityCodeBox_'+id).removeClass('err');
}
