﻿$(document).ready(function(){
  
    $(".btnUpDateLogIn").hover(
    function() { $(this).attr('src',baseUrl + 'Images/PersonalArea/UpDateDetailOn.gif'); },
    function() { $(this).attr('src',baseUrl + 'Images/PersonalArea/UpDateDetail.gif');}
    );
    $(".deleteChild").hover(
    function() { $(this).attr('src',baseUrl + 'Images/Albums/btnYesOn.gif'); },
    function() { $(this).attr('src',baseUrl + 'Images/Albums/btnYes.gif');}
    );
    
    $(".FavoriteItemTitleText").hover(
    function() { $(this).addClass('FavoriteItemTitleTextHover'); },
    function() { $(this).removeClass('FavoriteItemTitleTextHover');}
    );
    $(".btnCreateNewGroup").hover(
    function() { $(this).addClass('btnCreateNewGroupOn'); },
    function() { $(this).removeClass('btnCreateNewGroupOn');}
    );
    $(".btnDeleteGroup").hover(
    function() { $(this).addClass('btnDeleteGroupOn'); },
    function() { $(this).removeClass('btnDeleteGroupOn');}
    );    
});

function ChangeTab(pageToShow)
{
    window.location = pageToShow + ".aspx";
}
function showHideContent(titleId) //(MY FAVORITES PAGE)
{
    // HIDE ALL OPEN CATEGORIES
    if($(".FavoriteCategoryContent:visible").get(0) != null) 
        {
            $(".FavoriteCategoryContent:visible").slideUp('slow');
            $(".FavoriteCategoryContent:visible").prev().removeClass("On");
        }

    // HIDE SELECTED CONTENT IF IS SHOWN
    if($("#Favorite" + titleId + "Content:visible").get(0) != null)
        {
            $("#Favorite" + titleId + "Content").slideUp('slow');            
            $("#"+titleId).removeClass("On");
        }
    else  // SHOW SELECTED CONTENT IF IS HIDDEN
        {
            if($("#Favorite" + titleId + "Content").html().length > 35)//for some strange reason FireFox gives an empty div.html.length value of 30 ! ! !
            {   //happens only if the category contains items.
                $("#"+titleId).addClass("On");
                $("#Favorite" + titleId + "Content").slideDown('slow');
            }
        }
}

function deleteFromPersonalArea (userId, itemId, itemType)
{
   //alert("USER ID= " + userId + ", ITEM ID= " + itemId + ", ITEM TYPE= " + itemType);
   
   $.post(baseUrl + "PersonalArea/Handlers/FavoriteItemsHandler.ashx",
   { 
       userId:userId,
       itemId:itemId,
       itemType:itemType,
       action:"Delete"
   },
   function(data)
   {
       if(data == "Delete OK")
       {
           //alert("נמחק בהצלחה");
       }
   });

   $("#" + itemId + itemType).slideUp('slow');
}
function OpenWinDelete(ChildId)
{
     $('.childToDelete').val(ChildId);
     $('#winAreYouSure').show();
}

function sendLinkToFriend(link,id)
{    
   $.post(baseUrl + "PersonalArea/Handlers/sendLinkUrl.ashx",
      {
         linkUrl: link
      },
      function(data){
        $('.WindowSendLink').html('');
        $('#' + id).html(data);
        $('#SendLink').show();
      }
    );
}

//CONTACT MEN (contactMen.aspx)//
function paShowMyBookAddress(classDiv,classButton)
{
    if($('.' + classDiv).css('display') == 'none')
    {
        $('.' + classDiv).show('slow');
        $('.' + classButton).addClass( classButton + 'On');
    }
    else
    {
        $('.' + classDiv).hide('slow');
        $('.' + classButton).removeClass(classButton + 'On');
    }
    $('.AddNewContactMan').hide();
    $('.AddNewGroupWin').hide();
}
    //GROUPS ACTION
function actionGroupFunction()
{
    var userId = $.cookie('Huggies');
    if(userId == null)
    {
        alert("לפני ביצוע באנשי קשר יש להרשם לאתר");
        return 0;
    }
    userId = userId.replace("UserId=","");
    if($('#statusAction').val() == 'edit')
        EditGroup($('#groupId').val());
    else
        if($('#statusAction').val() == 'add')
            AddNewGroup(userId);
        else
            deleteGroup(userId);
}
function AddNewGroup(userId)
{    
    $.post(baseUrl + "PersonalArea/Handlers/GroupAction.ashx",
     { 
       name: $('#txtNameGroup').val(),
       userId:userId,
       status:"add"
     },
     function(data)
     {       
       if(data != "Error")
       {
           var i=2;
           $('.MainPaMyGroups').each(function(){
                i++;
           });
           name = $('#txtNameGroup').val();
           groupId = data;
           $('.AddNewGroupWin').slideUp('slow');
           var html = '';
           html += "<div  class='MainPaMyGroups' id='Line_"+i+"'>";
           html +=  getGroupTableHtml(i,name,groupId);
           html +="</div>";
           html +="<input type='hidden' value='"+groupId+"' id='groupId_"+i+"'/>"
           if(i == 2)
                $('.paGroupLine').html(html);
           else
                $('.paGroupLine').append(html);
           alert("הוספת קבוצה חדשה בהצלחה");
       }
       else
           alert("!!! הוספת קבוצה חדשה נכשלה");
     });
}
function EditGroup(groupId)
{    
    $.post(baseUrl + "PersonalArea/Handlers/GroupAction.ashx",
     { 
       name: $('#txtNameGroup').val(),
       groupId:groupId,
       status:"edit"
     },
     function(data)
     {       
       if(data != "Error")
       {
           i = $('#index').val();
           name =  $('#txtNameGroup').val();
           groupId = data;
           $('.AddNewGroupWin').slideUp('slow');
           var html = getGroupTableHtml(i,name,groupId);
           $('#Line_'+i).html('');
           $('#Line_'+i).html(html);
           alert("עריכת קבוצה בוצעה בהצלחה");
       }
       else
           alert("!!! עריכת קבוצה נכשל");
     });
}
function openWinActionGroup(status,i,groupId)
{
    $('#statusAction').val(status);
    $('#index').val(i);
    
    if(status == 'edit')
    {
        $('.ActionGroup').show();
        $('#txtNameGroup').val($('#name_' + i).html());        
        $('#groupId').val(groupId);
        $.post(baseUrl + "PersonalArea/Handlers/ShowContactMenByGroup.ashx",
        {
          groupId:groupId,
          status:"showAddress"
        },
        function(data)
        {
          if(data != "Error")
          {
               $('.GroupsAddressLine').html(data);
          }
          else
               alert(data);
        });
    }
    else//ADD NEW GROUP
    {
        $('.GroupsAddressLine').html('');
        $('#txtNameGroup').val('');
        $('.ActionGroup').hide();        
    }
    $('.AddNewGroupWin').show();
}
function deleteGroup(userId)
{    
    var delGroupsIds ="";
    cooseGroups = false; // CHACK IF COOSE ADDRESSES
    $('.MainPaMyGroups INPUT:checked').each(function(){
        var i = $(this).attr("id").split("_G")[1];
        delGroupsIds+= ";" + $('#groupId_' +i).val();
        cooseGroups = true;
    });

    if(!cooseGroups){
        alert("לא נבחרו קבוצות למחיקה");
        return;
    }
    else
        var resultAnswer =  window.confirm('? האם למחוק קבוצות מסומנות');
    if(!resultAnswer)   return;
    $.post(baseUrl + "PersonalArea/Handlers/DeleteGroups.ashx",
     {
       name:'',
       status:'delete',
       delGroupsIds:delGroupsIds,
       userId:userId
     },
     function(data)
     {       
       if(data != "Error")
       {
           $('.paGroupLine').html(data);
           alert("..קבוצות נמחקו בהצלחה");
       }
       else
           alert("!!! מחיקת קבוצות נכשלה");
    });
}
    //ADDRESSES ACTION
function paAddNewContactAddress(sender)
{    
    name = $('#txtNameMan').val();
    cellular = $('#txtCellular').val();
    emailFrom = $('#txtEmailAddress').val();
    
    var id = checkDetailsNewContactMan(name,emailFrom,cellular);
    if(id == false)  return 0;
    
     $.post(baseUrl + "Handlers/AddNewContactman.ashx",
     { 
       name: $('#txtNameMan').val(),
       cellular:$('#txtCellular').val(),
       email:$('#txtEmailAddress').val(),
       id:id
     },
     function(data)
     {       
       if(data != "Error")
       {
            addressId = data;
            var i=2;
            $('.MainPaMyAddress').each(function(){
                i++;
            });
            $('.AddNewContactMan').slideUp('slow');
            
            var html = '';
            html +="<div  class='MainPaMyAddress' id='line_"+i+"'>";
            html += getAddressTableHtml(i,name,cellular,emailFrom,addressId)    
            html +="</div>";
            
            if(i == 2)
                $('.paAddressLine').html(html);
            else
                $('.paAddressLine').append(html);
            
            html = getAllAddressesForOneGroup(i,name,cellular,emailFrom,addressId);
            if(i == 2)
                $('#AllAddressLine').html(html);
            else
                $('#AllAddressLine').append(html);
           
            $('.AddNewContactMan').slideUp('slow');
            alert("הוספת איש קשר חדש בהצלחה");
       }
       else
            alert("!!! הוספת איש קשר חדש נכשלה");
    });
}
function paDeleteContactMen()
{    
    var userId = $.cookie('Huggies');
    if(userId == null)
    {
        alert("לפני ביצוע  שינוי באנשי קשר יש להרשם לאתר");
        return 0;
    }
    userId = userId.replace("UserId=","");
    var idUsers;
    idUsers = checkBeforDeleteContactMen();
    if(idUsers == false)
        return;
    var resultAnswer =  window.confirm('? האם למחוק כתובות מסומנות');
    if(!resultAnswer)   return;
    $.post(baseUrl + "PersonalArea/Handlers/paDeleteContactMen.ashx",
     {        
       idUsers:idUsers,
       id:userId
     },
     function(data)
     {       
       if(data != "Error")
       {
           $('.paAddressLine').html(data);
           var html = '';
           $('.MainPaMyAddress').each(function(){
                var i = $(this).attr("id").split("_")[1];
                html += getAllAddressesForOneGroup(i,$('.MainName_'+i).html(),$('.MainCellular_'+i).html(),$('.MainEmail_'+i).html(),$('#addressId_' + i).val());
           });
           if(html == '')
                html= "<div class='NoContactMen'>אין אנשי קשר</div>";
           $('#AllAddressLine').html(html);
           alert("..כתובות נמחקו בהצלחה");
       }
       else
           alert("!!! מחיקת כתובת נכשלה");
    });
}
function editContactManAddress(userId,manId)
{
    name =  $('#txtNameMan').val();
    cellular =  $('#txtCellular').val();
    email =  $('#txtEmailAddress').val();
    
    var id = checkDetailsNewContactMan(name,email,cellular);
    if(id == false)  return 0;
    $.post(baseUrl + "Handlers/AddNewContactman.ashx",
     { 
       name: name,
       cellular:cellular,
       email:email,
       id:userId,
       status:"edit",
       manId:manId
     },
     function(data)
     {       
       if(data != "Error")
       {
           i = $('#index').val();
           name =  $('#txtNameMan').val();
           cellular =  $('#txtCellular').val();
           email =  $('#txtEmailAddress').val();
           addressId = data;
           $('.AddNewContactMan').slideUp('slow');
           var html = getAddressTableHtml(i,name,cellular,email,addressId);
           //Update Uindow "MyBookAllAddressWin" (contactMen.aspx)
           $('#Name_'+i).html(name);
           $('#Cellular_'+i).html(cellular);
           $('#Email_'+i).html(email);//
           
           $('#line_'+i).html('');
           $('#line_'+i).html(html);
           alert("עריכת קבוצה בוצעה בהצלחה");
       }
       else
           alert("!!! עריכת קבוצה נכשל");
     });
}
function editContactMan(index,name,sellular,email)
{
    $('#txtNameMan').val(name);
    $('#txtCellular').val(sellular);
    $('#txtEmailAddress').val(email);
    $('#currentAction').val('edit');
    $('.AddNewContactManImg').addClass('EditContactManImg');
    $('#index').val(index);
    $('.AddNewContactMan').show();
}
function addContactManAddress()
{
    $('#txtNameMan').val('');
    $('#txtCellular').val('');
    $('#txtEmailAddress').val('');
    $('#currentAction').val('new');
    $('.AddNewContactManImg').removeClass('EditContactManImg');
    $('.AddNewContactMan').show();
}
function ContactManAction()
{
    var userId = $.cookie('Huggies');
    if(userId == null)
    {
        alert("לפני ביצוע באנשי קשר יש להרשם לאתר");
        return 0;
    }
    userId = userId.replace("UserId=","");
    if($('#currentAction').val() == 'new')
        paAddNewContactAddress(userId);
    else
        editContactManAddress(userId,$('#addressId_' + $('#index').val()).val());
}
//CONTACT MEN BY GROUPID
function deleteContactMenByGroupId()
{
    var groupId = $('#groupId').val();
    var delGroupsAddressIds ="";
    cooseGroups = false; // CHACK IF COOSE ADDRESSES
    $('.GroupAddressDiv INPUT:checked').each(function(){
        var i = $(this).attr("id").split("_")[1];
        delGroupsAddressIds+= ";" + $('#groupAddressId_' +i).val();
        cooseGroups = true;
    });
    if(!cooseGroups){
        alert("לא נבחרו כתובות למחיקה");
        return;
    }
    else
        var resultAnswer =  window.confirm('? האם למחוק קבוצות מסומנות');
    if(!resultAnswer)   return;
    $.post(baseUrl + "PersonalArea/Handlers/DeleteContactMenByGroupId.ashx",
    {
       groupId:groupId,
       delGroupsAddressIds:delGroupsAddressIds
    },
    function(data)
    {
       if(data != "Error")
       {
           $('.GroupsAddressLine').html(data);
       }
       else
            alert(data);
    });
}
function DisplayContactsByGroupId()
{
    //EMPTY ALL CHACKBOX IN WINDOWS MYBOOKALLADDRESSWIN
    $("#MyBookAllAddressWin INPUT[@type=checkbox]").each(function(){
        $(this).attr("checked","");
    });
    //CHECKED ALL BY groupId
    $("#AddNewGroupWin INPUT[@type=checkbox]").each(function(){
        var GroupContact = $(this).attr("id");
        $("#MyBookAllAddressWin INPUT[@type=checkbox]").each(function(){
            //COMPARE BY EMAIL ADDRESS
            if($(this).parent().next().next().next().text() == $('#'+GroupContact).parent().next().next().next().text())
                $(this).attr("checked","checked");
        });
    });
    $('.MyBookAllAddressWin').show();
}
function addNewContactManByGroupId()
{
    var addaddressIds ="";
    var groupId = $('#groupId').val();
    cooseAddress = false; // CHACK IF COOSE ADDRESSES

    $(".AllAddressLine_address INPUT:checked").each(function(){
        var ind = $(this).attr("id").split("_")[1];
        addaddressIds += ";" + $('#addressGId_' +ind).val();
        cooseAddress = true;
    });

    if(!cooseAddress){
        alert("לא נבחרו כתובות להוספה");
        return;
    }
    $('#groupId').val(groupId);
    $.post(baseUrl + "PersonalArea/Handlers/ShowContactMenByGroup.ashx",
    {
        groupId:groupId,
        status:"addNew",
        addaddressIds :addaddressIds
    },
    function(data)
    {
       if(data != "Error")
       {
            var groupAddressIds = data.split(",");
            var j = 0;
            var html = '';
            $('.AllAddressLine_address INPUT:checked').each(function(){
                var i = $(this).attr("id").split("_")[1];
                html += getLineAddressByGroupId(i,$('#Name_'+i).html(),$('#Cellular_'+i).html(),$('#Email_'+i).html(),groupAddressIds[j]);
                j++;
            });
           $('.GroupsAddressLine').html(html);
           $('.MyBookAllAddressWin').hide();
       }
       else
            alert(data);
    });
}
function FillAllAddressLine()
{
    var html = '';
    var i = 1;
    $('.MainPaMyAddress').each(function(){
        html += getAllAddressesForOneGroup(i,$('.MainName_'+i).html(),$('.MainCellular_'+i).html(),$('.MainEmail_'+i).html(),$('#addressId_'+i).val());
        i++;
    });
    if(i == 1)
        html= "<div class='NoContactMen'>אין אנשי קשר</div>";
    $('#AllAddressLine').html(html);
}
function getGroupTableHtml(i,name,groupId)
{
    var html = '';
        html =
        "<div class='MyGroupCell' style='width:24px;'>"+
             "<input type='checkbox' id='Chk_G" + i + "'/>"+
        "</div>"+
        "<div class='MyGroupCell' id='name_"+ i +"' style='width:245px;'>"+ name +"</div>"+
        "<div class='MyGroupCell GroupCellEdit'>"+
            "<img src='../images/PersonalArea/btnEditGroup.gif' alt='' class='btnEditGroup' onclick='openWinActionGroup(\"edit\","+i+","+groupId+")' />"+
        "</div>";
    return html;
}
function getAddressTableHtml(i,name,cellular,email,addressId)
{
    var html = '';
    html =
    "<div class='MyBookCell' style='width:26px;'><input type='checkbox' id='chk_"+i+"'/></div>"+
    "<div class='MyBookCell MainName_"+i+"' style='width:160px;'>"+name+"</div>"+
    "<div class='MyBookCell MainCellular_"+i+"' style='width:169px;'>"+cellular+"</div>"+
    "<div class='MyBookCell paEmailCell'>"+
        "<div class='FRight MainEmail_"+i+"' style='width:190px;'>"+email+"</div>"+
        "<div class='FRight MT3 Pointer'><img src='../Images/PersonalArea/btnEdit.gif' alt='' onclick='editContactMan(" +i+ ",\"" +name+ "\",\"" +cellular+ "\",\"" +email+ "\")'/></div>"+
   "</div>"+
   "<input type='hidden' value='"+addressId+"' id='addressId_"+i+"'/>";
    return html;
}
function getAllAddressesForOneGroup(i,name,cellular,email,addressId)
{
    var html = '';
    html =
    "<div style='width:100%;' class='AllAddressLine_address'>"+
        "<div class='MyBookCell' style='width:26px;'><input type='checkbox' id='chkAG_"+i+"'/></div>"+
        "<div class='MyBookCell' id='Name_"+i+"' style='width:160px;'>"+name+"</div>"+
        "<div class='MyBookCell' id='Cellular_"+i+"' style='width:169px;'>"+cellular+"</div>"+
        "<div class='MyBookCell EmailCell' id='Email_"+i+"'>"+email+"</div>"+
        "<input type='hidden' value='"+addressId+"' id='addressGId_"+i+"'/>"+
    "</div>";   
    return html;
}
function getLineAddressByGroupId(i,name,cellular,email,addressId)
{
    var html = '';
    html =
    "<div class='GroupAddressDiv'>" +
        "<div class='MyBookCell' style='width:26px;'><input type='checkbox' id='chkAG_"+i+"' /></div>" +
        "<div class='MyBookCell name_"+i+"' style='width:160px;'>"+name+"</div>" +
        "<div class='MyBookCell Cellular_"+i+"' style='width:169px;'>"+cellular+"</div>" +
        "<div class='MyBookCell Email_"+i+" EmailCell'>"+email+"</div>" +
        "<input type='hidden' value='"+addressId+"' id='groupAddressId_"+i+"'/>" +
    "</div>"+
    "<div class='Breaker'></div>";
    return html;
}
//END --> CONTACT MEN (contactMen.aspx)//
