﻿/*=================================
  Link Editing
=================================*/
function deleteLink_Callback(resp){
  // Get the index of the returned id
  var linkID = parseInt(resp.responseText);
  
  // Remove the Link ID from the Array
  if (! isNaN(linkID)){
    for (var i = 0; i < links.length; i++){
      if (links[i].fldLinkID == linkID){
        links.splice(i, 1);
      }
    }
  }
  
  // Redraw the links
  setLinks();
}

function deleteLink_Delete(e){
  // Get the sender
  if (! e) e = window.event;
  var sender = e.srcElement ? e.srcElement : e.target;
  var link = links[sender.linkIndex];

  // Prompt the user to confirm
  if (! confirm('Are you sure you want to delete the link \'' +
  link.fldTitle + '\'?')){
    setLinkOnClick(null);
    return false;
  }

  // Set the Callback
  ajax.setReadyStateChanged(deleteLink_Callback);
  ajax.send('GET', '/edit/links.aspx?delete=' + currentGroup +
    '&linkID=' + link.fldLinkID, true, null);
  
  return false;
}

function editLink_Cancel(sender){
  // Hide the Group Edit form
  formContainer_Hide(document.getElementById('editLinkContainer'));

  // Unset the onclick for all the links
  setLinkOnClick(null);
}

function editLink_Edit(e){
  // Get the sender
  if (! e) e = window.event;
  var sender = e.srcElement ? e.srcElement : e.target;
  var link = links[sender.linkIndex];
  
  // Fill the form
  var frm = document.getElementById('editLink');
  frm.linkID.value = link.fldLinkID;
  frm.address.value = link.fldURL;
  frm.title.value = link.fldTitle;
  frm.desc.value = link.fldDescription;
  frm.oldGroup.value = currentGroup;
  frm.group.value = currentGroup;
  selGroup_OnChange(frm.group);
  frm.column.selectedIndex = link.fldColumn;
  frm.newWin.checked = (link.fldAttributes & 2);

  // Set the Title & Show the form
  document.getElementById('editLinkTitle').innerHTML = 'Edit Link';
  formContainer_Show(document.getElementById('editLinkContainer'));
  
  return false;
}

function editLink_Callback(resp){
  // Response returns new group data
  getData_Callback(resp);

  // Hide the Group Edit form
  formContainer_Hide(document.getElementById('editLinkContainer'));
}
 
function editLink_OnSubmit(sender){
  // Get the Form
  var frm = document.getElementById('editLink');
  
  // Validate the form
  if (frm.address.value.length == 0){ // Address
    alert('Address must be given');
    frm.address.focus();
    return false;
  }else if (frm.title.value.length == 0){ // Title
    alert('Title must be given');
    frm.title.focus();
    return false;
  }else if (frm.group.value == ''){ // Group
    alert('Group must be given');
    frm.group.focus();
    return false;
  }else if (frm.column.value == ''){ // Column
    alert('Column must be given');
    frm.column.focus();
    return false;
  }
  
  // Get the data
  var data = 'address=' + escape(frm.address.value) +
    '&title=' + escape(frm.title.value) +
    '&desc=' + escape(frm.desc.value) +
    '&group=' + escape(frm.group.value) +
    '&column=' + escape(frm.column.value) +
    (frm.newWin.checked ? '&newWin=on' : '') +
    '&linkID=' + escape(frm.linkID.value);
  if (frm.group.value == currentGroup || frm.oldGroup.value == currentGroup){
    data += '&get=' + currentGroup;
  }
    
  // Set the Callback
  ajax.setReadyStateChanged(editLink_Callback);
  ajax.send('POST', '/edit/links.aspx', true, data);
  
  return false;
}

function optAddLink_OnClick(e){
  // Reset the form
  var frm = document.getElementById('editLink');
  frm.linkID.value = '';
  frm.address.value = '';
  frm.title.value = '';
  frm.desc.value = '';
  frm.oldGroup.value = '';
  frm.group.value = currentGroup;
  selGroup_OnChange(frm.group);
  frm.newWin.checked = true;

  // Set the Group Title & Show the form
  document.getElementById('editLinkTitle').innerHTML = 'Add New Link';
  formContainer_Show(document.getElementById('editLinkContainer'));
}

function optDeleteLink_OnClick(e){
  // Check if this is a another user's group
  if (! isOwnGroup(currentGroup)){
    alert('Cannot delete links from another User\'s Group');
    return;
  }

  // Set the onclick for all the links
  setLinkOnClick(deleteLink_Delete, 'Delete');
}

function optEditLink_OnClick(sender){
  // Check if this is a another user's group
  if (! isOwnGroup(currentGroup)){
    alert('Cannot edit links in another User\'s Group');
    return;
  }

  // Set the onclick for all the links
  setLinkOnClick(editLink_Edit, 'Edit');
}

function setLinkOnClick(func, action){
  // Set the onclick for all the links
  var anchors = document.getElementById('links').getElementsByTagName('a');
  for (var i = 0; i < anchors.length; i++){
    anchors[i].onclick = func;
    
    if (func){
      anchors[i].title = action + ' this link';
      anchors[i].onmouseover = function(e){this.style.backgroundColor = '#FF0000';};
      anchors[i].onmouseout = function(e){this.style.backgroundColor = '#FFFFFF';};
    }else{
      anchors[i].title = links[anchors[i].linkIndex].fldDescription;
      anchors[i].onmouseover = null;
      anchors[i].onmouseout = null;
    }
  }
}
