﻿function validateDiary()
{
    if (getUpdateEntry() == true && checkDiaryDate() == true)
    {
        return true;
    }
    else
    {
        return false;
    }

}

function getUpdateEntry() {
    var entry = document.getElementById("UpdateActivity_UpdateEntry"); 
    var strEntry = entry.value.trim();
    if (strEntry.length == 0 || strEntry == "your thoughts (maximum 250 characters)") {
        entry.focus();
        return false;
    }
    else {

        return true;
    }

}

function getAuxiUpdateEntry() {
    
    var entry = document.getElementById("AllAuxilliaryControls_AuxilliaryUpdate_UpdateEntry");
    
    var strEntry = entry.value.trim();
    if (strEntry.length == 0 || strEntry == "your thoughts (maximum 250 characters)") {
        entry.focus();
        return false;
    }
    else {

        return true;
    }

}
function checkActivityEntry()
{
  var num = document.getElementById("UpdateActivity_TextBoxActivityDiary");
  
  if (isNaN(num.value) || num.value == "" || num.value <= 0)
  {
    alert("Please insert a positive number");
    num.select();
    num.focus();
    return false;
  }    
  else
  {
    return true;
  }

}
function checkAuxiActivityEntry() {
    var num = document.getElementById("AllAuxilliaryControls_AuxilliaryUpdate_AuxiTextBoxActivityDiary");

    if (isNaN(num.value) || num.value == "" || num.value <= 0) {
        alert("Please insert a positive number");
        num.select();
        num.focus();
        return false;
    }
    else {
        return true;
    }

}
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function validateComment(id) {

    var diaryId;
    var comment;
    var commentObj;
    diaryId = id.split("_")[1];
    commentObj = document.getElementById("CommentBox_" + diaryId);
    comment = commentObj.value.trim();
    comment = comment.replace(/(\r\n|[\r\n])/g, "<<<nl>>>");

    if(comment == "")
    {
        commentObj.focus();
        return;
    }
    else
    {
        submitComment(id, comment); //ajax.js
    }
}
function getTomorrow()
{
    var endDate = document.getElementById("UpdateActivity_tomorrow");
    if (endDate == null)return;
    return endDate.value;
}
function showComment(id) {
    var splitId = id.split("_");
    id = splitId[0];
    id = id + "_Comments";
    var comment = document.getElementById(id);
    comment.style.display = "block";
}

function getEventSender(id) {
    var sender = id.split("_");
    var senderId = sender[0]
    document.getElementById('EventSenderId').value = senderId;
}
function hideComment(id) {
    var splitId = id.split("_");
    id = splitId[0];
    id = id + "_Comments";
    var comment = document.getElementById(id);
    comment.style.display = "none";
}
function onHoverMode(id)
{
    var ctrl = document.getElementById(id);
    if(!ctrl) return;
    ctrl.className = "btnActivityHover";
}
function onNormalMode(id)
{
    var ctrl = document.getElementById(id);
    if(!ctrl) return;
    ctrl.className = "btnActivity";
}
function validate()
{
    if (checkActivityEntry() == true && checkDate() == true)
    {
        return true;
    }
    else
    {
        return false;
    }

}

function checkDate()
{
    var date = document.getElementById("UpdateActivity_ActivityDate");
    //if date contains data and the date is different from 'DD/MM/YYYY' (the watermark)
    if (date.value.length > 0 && date.value != "dd/mm/yyyy")
    {
        if (isDate(date.value))
        {
            return true;
        }
        else
        {
            alert("Please enter date in 'DD/MM/YYYY' format.");
            date.select();
            date.focus();
            return false;
        }
    }
    else
    {
        return true; //allow the user to update with the current date and time
    }
}
function checkDiaryDate()
{
    var date = document.getElementById("UpdateActivity_DiaryDate");
    var correctDate = true; //we allow the user to update without entering date (we use current time stamp of server side);
    if (date.value.length > 0 && date.value != "dd/mm/yyyy")
    {
        if (isDate(date.value))
        {
            correctDate = true;
        }
        else
        {
            alert("Please enter date in 'DD/MM/YYYY' format.");
            date.select();
            date.focus();
            correctDate = false;
        }
    }
    else
    {
    correctDate = true; //allow current date and time entry (default)
    }
   return correctDate; 
}

function isDate(txtDate){   
  var objDate;  
  var mSeconds;
  
 
  if (txtDate.length != 10) return false;   
  

  
  var day   = txtDate.substring(0,2)  - 0;   
  var month = txtDate.substring(3,5)  - 1; // because months in JS start with 0   
  var year  = txtDate.substring(6,10) - 0;   
 
  // third and sixth character should be /   
  if (txtDate.substring(2,3) != '/') return false;   
  if (txtDate.substring(5,6) != '/') return false;   
  
  // test year range   
  if (year < 999 || year > 3000) return false;   
 
  // convert txtDate to the milliseconds   
  mSeconds = (new Date(year, month, day)).getTime();   
  
 // set the date object from milliseconds   
  objDate = new Date();   
  objDate.setTime(mSeconds);   
  
  // if there exists difference then date isn't valid   
  if (objDate.getFullYear() != year)  return false;   
  if (objDate.getMonth()    != month) return false;   
  if (objDate.getDate()     != day)   return false;   
  
  // otherwise return true   
  return true;   
}  

function showDiv(ctrlName) {
    var ctrl;
    ctrl = document.getElementById(ctrlName);
    if (ctrl != null) {

    ctrl.className = 'displayed';
    }
}

function hideDiv(ctrlName) {
    var ctrl;
    ctrl = document.getElementById(ctrlName);
    if (ctrl != null) {
       
       ctrl.className = 'hidden';
    }
}


function changeSelectedPage(id)
{
 var selectedPage = document.getElementById("Pager1_HiddenPageSelected");
    if (selectedPage == null) return;
    selectedPage.value = id;
}
function changePageNumber(page)
{
    var pageNumber = document.getElementById("HiddenSelectedPage");
    if (pageNumber == null) return;
    pageNumber.value = page;
}
function nextPage()
{
    var selectedPage = document.getElementById("Pager1_HiddenPageSelected");
    if (selectedPage == null) return;
    selectedPage.value = (selectedPage.value * 1) + 1;
    
}
function previousPage()
{
    var selectedPage = document.getElementById("Pager1_HiddenPageSelected");
    if (selectedPage == null) return;
    selectedPage.value = (selectedPage.value * 1) - 1;
    
}
function goToPage(pageNumber)
{
    var selectedPage = document.getElementById("Pager1_HiddenPageSelected");
    if (selectedPage == null) return;
    selectedPage.value = pageNumber;
    
}

//functions sets the selected listItem to active and the other lists to
//their default style
function setToActive(id)
{
    var ctrl;
    ctrl = document.getElementById(id);
    if (ctrl != null) {

    ctrl.className = 'active';
    }
    if(id == 'trained')
    {
        document.getElementById("feeling").className = "";
        document.getElementById("thoughts").className = "";
    }
    else if (id == 'feeling')
    {
        document.getElementById("trained").className = "";
        document.getElementById("thoughts").className = "";
    }
    else
    {
        document.getElementById("trained").className = "";
        document.getElementById("feeling").className = "";
    }
}


function addFileUpload() {

  var uploadArea = document.getElementById("upload_area");
  if (!uploadArea)
  return;
                                                  
      if (!addFileUpload.lastAssignedId)
        addFileUpload.lastAssignedId = 100;
     if(addFileUpload.lastAssignedId < 104)
     {
        var newLine = document.createElement("br");
        uploadArea.appendChild(newLine);
        var newUpload = document.createElement("input");
        newUpload.type = "file";
        newUpload.size = "64";
        newUpload.setAttribute("id", "FileAdd" + addFileUpload.lastAssignedId);
        newUpload.setAttribute("name", "FileAdd" + addFileUpload.lastAssignedId);
        uploadArea.appendChild(newUpload);
        addFileUpload.lastAssignedId++;
     }
     else
     {
      return;
     }
 }
 function MM_openBrWindow(theURL,winName,features) { //v2.0
		  popup = window.open(theURL,winName,features);
		  popup.focus();
		  }
		  
//****************************** //
//******** Water Marking ******* //

$(document).ready(function() {
    //variables that store watermark values for each controls
    
    var watermarkDate = "dd/mm/yyyy";
    var watermarkDiaryDate = "dd/mm/yyyy";
    var watermarkUpdateEntry = "your thoughts (maximum 250 characters)";
    var watermarkAuxiUpdateEntry = "your thoughts (maximum 250 characters)"
    if ($("#UpdateActivity_ActivityDate").val() == "") {
       $("#UpdateActivity_ActivityDate").val(watermarkDate);
    }
    $("#UpdateActivity_ActivityDate").focus(function() {
        if (this.value == watermarkDate) {
            this.value = "";
            removeWatermarkStyleFromActivityDate();
        }
        //when the contorl loss focus check whether there has been an entry
        //in no entry made by the user assign the watermark to the control
    }).blur(function() {
        if (this.value == "") {
            this.value = watermarkDate;
            addWatermarkStyleToActivityDate()
        }
    });
    
 
    if ($("#UpdateActivity_DiaryDate").val() == "") {
       $("#UpdateActivity_DiaryDate").val(watermarkDiaryDate);
    }
    $("#UpdateActivity_DiaryDate").focus(function() {
        if (this.value == watermarkDiaryDate) {
            this.value = "";
            removeWatermarkStyleFromDiaryDate();
        }
        //when the contorl loss focus check whether their has been an entry
        //in no entry made by the user assign the watermark to the control
    }).blur(function() {
        if (this.value == "") {
            this.value = watermarkDiaryDate;
            addWatermarkStyleToDiaryDate()
        }
    });
    
     if ($("#UpdateActivity_UpdateEntry").val() == "") {
       $("#UpdateActivity_UpdateEntry").val(watermarkUpdateEntry);
    }
    $("#UpdateActivity_UpdateEntry").focus(function() {
        if (this.value == watermarkUpdateEntry) {
            this.value = "";
            removeWatermarkStyleFromUpdateEntry()
        }
        //when the contorl loss focus check whether their has been an entry
        //in no entry made by the user assign the watermark to the control
    }).blur(function() {
        if (this.value == "") {
            this.value = watermarkUpdateEntry;
            addWatermarkStyleToUpdateEntry()
        }
    });
    
    if ($("#AllAuxilliaryControls_AuxilliaryUpdate_UpdateEntry").val() == "") {
       $("#AllAuxilliaryControls_AuxilliaryUpdate_UpdateEntry").val(watermarkAuxiUpdateEntry);
    }
    $("#AllAuxilliaryControls_AuxilliaryUpdate_UpdateEntry").focus(function() {
        if (this.value == watermarkAuxiUpdateEntry) {
            this.value = "";
            removeWatermarkStyleFromAuxiUpdateEntry()
        }
        //when the contorl loss focus check whether their has been an entry
        //in no entry made by the user assign the watermark to the control
    }).blur(function() {
        if (this.value == "") {
            this.value = watermarkAuxiUpdateEntry;
            addWatermarkStyleToAuxiUpdateEntry()
        }
    });
    
    
    
});

function removeWatermarkStyleFromActivityDate()
{
    var ctrl = document.getElementById("UpdateActivity_ActivityDate");
    if (ctrl == null)return;
    ctrl.className ="date-pick dp-applied";
}
function addWatermarkStyleToActivityDate()
{
    var ctrl = document.getElementById("UpdateActivity_ActivityDate");
    if (ctrl == null)return;
    ctrl.className ="date-pick dp-applied watermark";
}
function removeWatermarkStyleFromDiaryDate()
{
    var ctrl = document.getElementById("UpdateActivity_DiaryDate");
    if (ctrl == null)return;
    ctrl.className ="date-pick dp-applied";
}
function addWatermarkStyleToDiaryDate()
{
    var ctrl = document.getElementById("UpdateActivity_DiaryDate");
    if (ctrl == null)return;
    ctrl.className ="date-pick dp-applied watermark";
}
function removeWatermarkStyleFromUpdateEntry()
{
    var ctrl = document.getElementById("UpdateActivity_UpdateEntry");
    if (ctrl == null)return;
    ctrl.className ="expanding";
}
function addWatermarkStyleToUpdateEntry()
{
    var ctrl = document.getElementById("UpdateActivity_UpdateEntry");
    if (ctrl == null)return;
    ctrl.className ="expanding watermark";
}
function removeWatermarkStyleFromAuxiUpdateEntry()
{
    var ctrl = document.getElementById("AllAuxilliaryControls_AuxilliaryUpdate_UpdateEntry");
    if (ctrl == null)return;
    ctrl.className ="expanding";
}
function addWatermarkStyleToAuxiUpdateEntry()
{
    var ctrl = document.getElementById("AllAuxilliaryControls_AuxilliaryUpdate_UpdateEntry");
    if (ctrl == null)return;
    ctrl.className ="expanding watermark";
}
function removeDiary(id)
		{
		    var inputRemove;
		    inputRemove= document.getElementById('RemoveDiaryId');
		    if (inputRemove == null)
		    {return;}
		    else
		    {
		        inputRemove.value= id;
		    }
		}
		
function confirmDiaryRemove(id)
        {
            var agree=confirm("This action will remove the selected diary including related comments. Are you sure you want to continue?");
            if (agree)
            {
                var splitId = id.split("_");
                var dId = splitId[1];
                removeDiary(dId);
	            return true ;
	            }
            else
            {
	            return false ;
            }
        }
        function removePartner(id) {
            var inputRemove;
            inputRemove = document.getElementById('RemovePartnerId');
            if (inputRemove == null)
            { return; }
            else {
                inputRemove.value = id;
            }
        }

        function confirmPartnerRemove(id) {
            var agree = confirm("This action will remove the selected partner from your partners list. Are you sure you want to continue?");
            if (agree) {
                var splitId = id.split("_");
                var pId = splitId[1];
                removePartner(pId);
                return true;
            }
            else {
                return false;
            }
        }

        function toggleOption() {

            var optStatus = document.getElementById("UpdateActivity_optionstatus");
            if (optStatus.value == "0") {

                optStatus.value = "1";
                hideDiv('optionAdd');
                showDiv('optionCollapse');
                showDiv('optionalpanel')

            }
            else if (optStatus.value == "1") {
                optStatus.value = "0";
                hideDiv('optionCollapse');
                hideDiv('optionalpanel')
                showDiv('optionAdd');
            }
        }
