﻿// Activate TRIM function for java script
String.prototype.trim = function(){ return this.replace(/^s+|s+$/g,'') }
var ie4 = false; if(document.all) { ie4 = true; }
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
function noCache(uri){return uri.concat(/\?/.test(uri)?"&":"?","noCache=",(new Date).getTime(),".",Math.random()*1234567)};

/////////////////////////////////////////////////////
//Variables declaration
/////////////////////////////////////////////////////
var divContent;
var divTips;
var divButton;
var Comments = new Array();
var divComments;
var counterComments;

/////////////////////////////////////////////////////
//Common functions
/////////////////////////////////////////////////////
function getobject(id) { 
  if (ie4) {     
    return document.all[id];
  }else {  
    return document.getElementById(id); 
  }
}

function trim(cadena)
{
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}
/////////////////////////////////////////////////////
//Daily Practices
/////////////////////////////////////////////////////
//Get all objects from page 
function loadVarsMyMorevida(){  
  divContent = getobject("divContent");
  divTips = getobject("divTips");
  divButton = getobject("divButton");
  divComments = getobject("divComments");
  counterComments=1;
}

//************************************************************
// get all information about the current menu
function loadContentComment(){  
  PageMethods.loadContentComment(loadContentCommentSuccess,loadContentCommentFail);  
}

//when function loadContentComment is completed correctly
function loadContentCommentSuccess(result, userContext, methodName){ 
  if (result != ""){   
    divContent = getobject ("divContent");
    divContent.innerHTML = result;
  }      
}

//when function loadContentComment is not completed correctly
function loadContentCommentFail(error, userContext, methodName){ 
alert(error.getname);
}
//************************************************************

//************************************************************
// get all tips information
function loadTipsInformationComment(){  
  PageMethods.loadTipsInformationComment(loadTipsInformationCommentSuccess,loadTipsInformationCommentFail);  
}

//when function loadTipsInformationComment is completed correctly
function loadTipsInformationCommentSuccess(result, userContext, methodName){ 
  if (result != ""){     
    divTips = getobject ("divTips");
    divTips.innerHTML = result;
  }      
}

//when function loadTipsInformationComment is not completed correctly
function loadTipsInformationCommentFail(error, userContext, methodName){ 
alert(error.getname);
}
//************************************************************

//************************************************************
// get the button
function loadButtonComment(){  
  PageMethods.loadButton(loadButtonSuccess,loadButtonFail);  
}

//when function loadButton is completed correctly
function loadButtonSuccess(result, userContext, methodName){ 
    divButton = getobject ("divButton");
    divButton.innerHTML = result;
}

//when function loadButton is not completed correctly
function loadButtonFail(error, userContext, methodName){ 
alert(error.getname);
}
//************************************************************

//************************************************************
// get the button
function showCommentControls(){  
  //var txtLastComment = document.forms[0]["txtLifeVision"];
    
  counterComments += 1;
  //Comments[contador]= CommentTypeId + "╬" + CommentText;
  PageMethods.showCommentControls(counterComments, showCommentControlsSuccess,showCommentControlsFail);  
}

//when function showCommentControls is completed correctly
function showCommentControlsSuccess(result, userContext, methodName){ 
    divComments = getobject ("divComments");
    divComments.innerHTML = divComments.innerHTML + result;
}

//when function showCommentControls is not completed correctly
function showCommentControlsFail(error, userContext, methodName){ 
alert(error.getname);
}
//************************************************************

//************************************************************
// save Comments information 
function saveComment(div, CommentNumber, fileId){ 
//alert(fileId);
  var txtText = "txtBlogComment" + CommentNumber;
  //var txtTextComment = document.forms[0][txtText];
  var txtTextComment = getobject(txtText);
  if (trim(txtTextComment.value)!="")
  {
      PageMethods.saveComment(txtTextComment.value, div,fileId, saveCommentSuccess,saveCommentFail); 
  }
  else
      alert("You must enter a little description before save Comment");
}

//when function saveComments is completed correctly
function saveCommentSuccess(result, userContext, methodName){
  var info = new Array();
  info = result.split("╬¤");
    loadCommentsFile(info[3]);
//  divComments = getobject("divContent");
//  divComments.innerHTML= divComments.innerHTML + info[2];
//  stopEditComment(info[1], info[0]);
}

//when function saveComments is not completed correctly
function saveCommentFail(error, userContext, methodName){ 
    alert(error.get_message());
}
//************************************************************

//************************************************************
//delete a Comment
function removeComment(CommentId, div){ 
  var divDeleted = getobject(div);
  divComments = getobject("divComments");
  divComments.removeChild(divDeleted);
}
//************************************************************

//************************************************************
// load Comments of a member
function loadCommentsMember(){ 
    PageMethods.loadCommentsMember(loadCommentsMemberSuccess,loadCommentsMemberFail); 
}

//when function loadCommentsMember is completed correctly
function loadCommentsMemberSuccess(result, userContext, methodName){ 
  divComments = getobject("divComments");
  divComments.innerHTML = result;
}

//when function loadCommentsMember is not completed correctly
function loadCommentsMemberFail(error, userContext, methodName){ 
  divComments = getobject("divComments");
  divComments.innerHTML = error.getname();
}
//************************************************************

//************************************************************
//show edit mode of a Comment
function startEditComment(divToShow, CommentId)
{
    PageMethods.editModeComment(CommentId, divToShow, startEditCommentSuccess,startEditCommentFail); 
}
//when function startEditComment is completed correctly
function startEditCommentSuccess(result, userContext, methodName){ 
  var info = new Array();
  info = result.split("╬¤");
  var div = getobject(info[0]);
  div.innerHTML = info[1];
}

//when function startEditComment is not completed correctly
function startEditCommentFail(error, userContext, methodName){ 
  divComments = getobject("divComments");
  divComments.innerHTML = error.getname();
}
//************************************************************

//************************************************************
//show show mode of a Comment
function stopEditComment(divToShow, commentId)
{
    PageMethods.showModeComment(2, commentId, divToShow, stopEditCommentSuccess,stopEditCommentFail); 
}
//when function stopEditComment is completed correctly
function stopEditCommentSuccess(result, userContext, methodName){ 
  var info = new Array();
  info = result.split("╬¤");
  var div = getobject(info[0]);
  div.innerHTML = info[1];
}

//when function stopEditComment is not completed correctly
function stopEditCommentFail(error, userContext, methodName){ 
  divComments = getobject("divComments");
  divComments.innerHTML = error.getname();
}
//************************************************************

//************************************************************
//show show mode of a Comment
function updateComment(blogId, CommentNumber, div)
{
    var txtTitle = "txtTitleComment" + CommentNumber;
    var txtText = "txtComment" + CommentNumber;
    var txtTitleComment = document.forms[0][txtTitle];
    var txtTextComment = document.forms[0][txtText];
    if (trim(txtTitleComment.value)!="" && trim(txtTextComment.value)!="")
        PageMethods.updateComment(blogId, txtTitleComment.value, txtTextComment.value, div, updateCommentSuccess,updateCommentFail); 
    else
        alert("You must enter a little description before save Comment");
}
//when function stopEditComment is completed correctly
function updateCommentSuccess(result, userContext, methodName){ 
  var info = new Array();
  info = result.split("╬¤");
  stopEditComment(info[0],info[1]);
}

//when function stopEditComment is not completed correctly
function updateCommentFail(error, userContext, methodName){ 
  divComments = getobject("divComments");
  divComments.innerHTML = error.getname();
}
//************************************************************

//************************************************************
// get all information about the current menu
function loadCommentsFile(fileId){  
  PageMethods.loadCommentsFile(fileId, loadCommentsFileSuccess,loadCommentsFileFail);  
}

//when function loadCommentsFile is completed correctly
function loadCommentsFileSuccess(result, userContext, methodName){ 
  if (result != ""){   
    divContent = getobject ("divContent");
    divContent.innerHTML = result;
  }      
}

//when function loadCommentsFile is not completed correctly
function loadCommentsFileFail(error, userContext, methodName){ 
alert(error.getname);
}
//************************************************************


