//Displays all posted forms
function showFormPostings(offset, minSize) {
  clearAndShowLoading();
  
  new Ajax.Request('/platform/templates/crm/ajax/getFormPostings.xml?__toolbar=0&crmOffset='+offset+'&minSize='+minSize, {
    onSuccess: function(transport) {
      showData(transport.responseText);
    }
  });
  
}

//Displays all contacts
function showContacts(offset) {
  clearAndShowLoading();
  
  new Ajax.Request('/platform/templates/crm/ajax/getContacts.xml?__toolbar=0&offset='+offset, {
    onSuccess: function(transport) {
      showData(transport.responseText);
    }
  });
  
}

//Show a specific posted form and its containing name value pairs
function showFormPost(id, offset) {
  clearAndShowLoading();
  new Ajax.Request('/platform/templates/crm/ajax/getFormPosting.xml?__toolbar=0&postID='+id+'&crmOffset='+offset, {
    onSuccess: function(transport) {
      showData(transport.responseText);
    }
  });
}

function showFormPostInline(id, el) {
  new Ajax.Request('/platform/templates/crm/ajax/getFormPosting.xml?__toolbar=0&postID='+id+'&crmOffset=0&inline=1&el='+el, {
    onSuccess: function(transport) {
      $('crmTD'+el).innerHTML = transport.responseText;
      $('crmTR'+el).toggle();
      Effect.BlindDown('crmTD'+el, { duration: 0.5 });
    }
  });
}

//Shows the view where to create a new contact
function showCreateContact(id, offset) {
  clearAndShowLoading();
  new Ajax.Request('/platform/templates/crm/ajax/showCreateContact.xml?__toolbar=0&postID='+id+'&crmOffset='+offset, {
    onSuccess: function(transport) {
      showData(transport.responseText);
    }
  });
}

//Creates the new contact and displays contact view
function createContact() {
  clearAndShowLoading();
  new Ajax.Request('/platform/templates/crm/ajax/createContact.xml', {
    parameters : $('createContactForm').serialize(), 
    onSuccess: function(transport) {
      showData(transport.responseText);
    }
  });
}

function updateContact() {

  clearAndShowLoading();
  new Ajax.Request('/platform/templates/crm/ajax/updateContact.xml', {
    parameters : $('crmContactUpdateForm').serialize(), 
    onSuccess: function(transport) {
      showData(transport.responseText);
    }
  });
}

//Saves a note for a contact and displays the contact
function createNote(id) {

  $('noteError_'+id).hide();

  //Note cant be empty
  if($('crmNote_'+id).value.blank()) {
    $('noteError_'+id).show();
    return false;
  }


  //clearAndShowLoading();
  new Ajax.Request('/platform/templates/crm/ajax/createNote.xml', {
    parameters : $('crmNoteForm_'+id).serialize(), 
    onSuccess: function(transport) {
    
        var ourDate = new Date();
        var ourMonth =   ourDate.getMonth()+1;
        if (ourMonth<10) ourMonth="0"+ourMonth;
        var ourDay = ourDate.getDate();
        if (ourDay<10) ourDay="0"+ourDay;
        var ourHours = ourDate.getHours();
        if (ourHours<10) ourHours="0"+ourHours;
        var ourMinutes = ourDate.getMinutes();
        if (ourMinutes<10) ourMinutes="0"+ourMinutes;
        var ourSeconds = ourDate.getSeconds();
        if (ourSeconds<10) ourSeconds="0"+ourSeconds;
        var tmpEl = new Element('div');
        var noteContent = $('crmNote_'+id).value.replace(new RegExp("[\n\r]","g"),"<br />");
        tmpEl.insert("<span>"+ourDate.getFullYear()+"-"+ourMonth+"-"+ourDay+" "+ourHours+":"+ourMinutes+":"+ourSeconds+" by "+$('crmNoteForm_'+id).name.value+"</span><div class='crmNote'>"+noteContent+"</div>");
        $('crmNotes_'+id).insert({ top: tmpEl });
        if($('noNotes_'+id)) $('noNotes_'+id).hide();
        $('crmNote_'+id).value = '';
    }
  });
  return false;
}


function addField(name) {

 var tmpEl = new Element('div');
 tmpEl.insert("<input name='email' /><br />");
 $(name).insert({ bottom: tmpEl });

}

function showContact(id, offset) {
  clearAndShowLoading();
  new Ajax.Request('/platform/templates/crm/ajax/showContact.xml?__toolbar=0&contactID='+id+'&crmOffset='+offset, {
    onSuccess: function(transport) {
      showData(transport.responseText);
    }
  });
}

function showContactInline(id, el) {
  new Ajax.Request('/platform/templates/crm/ajax/showContact.xml?__toolbar=0&contactID='+id+'&crmOffset=0&inline=1&el='+el, {
    onSuccess: function(transport) {
      $('crmTD'+el).innerHTML = transport.responseText;
      $('crmTR'+el).toggle();
      Effect.BlindDown('crmTD'+el, { duration: 0.5 });
    }
  });
}

function showEditContact(contactID) {
  new Ajax.Request('/platform/templates/crm/ajax/showEditContact.xml?__toolbar=0&contactID='+contactID, {
    onSuccess: function(transport) {
    showData(transport.responseText);
    }
  });

}

//Swaps first and last name
function swapFirstAndLastname() {
  var ln = $('crmLastname').value;
  $('crmLastname').value = $('crmFirstname').value;
  $('crmFirstname').value = ln;
}

// 1. Hide all content in layer (good if we want to show again if error occured)
// 2. Show loading animation
// 3. Show layer
function clearAndShowLoading() {
  $('adminDynamicContent').hide();
  $('adminAjaxLoading').show();
  $('adminDynamicContentContainer').show();
}

// 1. Insert HTML in layer
// 2. Hide loading animation
// 3. Show content in layer
function showData(html) {

  if($('contents-spot-viewer')) { $('contents-spot-viewer').hide(); }
     
  $('adminDynamicContent').innerHTML = html;
  $('adminAjaxLoading').hide();
  $('adminDynamicContent').show();
  $('adminMenu').scrollTo();
}

//Closes all layers for the CRM and shows flash if disabled (flash cant be shown due to layer problems)
function closeCRM() {

$('adminDynamicContentContainer').hide();
if($('contents-spot-viewer')) { $('contents-spot-viewer').show(); }

}

function searchContacts() {

  clearAndShowLoading();

  new Ajax.Request('/platform/templates/crm/ajax/getContacts.xml', {
    parameters : $('crmSearchContactsForm').serialize(), 
    onSuccess: function(transport) {
      showData(transport.responseText);
    }
  });
}

//Mark the post id as deleted so it won't show up in listing
function removeFormPost(id, offset, minSize) {

  if(!confirm('Remove post?')) {
    return false;
  }

  new Ajax.Request('/platform/templates/crm/ajax/removeFormPost.xml?__toolbar=0&postID='+id+'&offset='+offset+'&minSize='+minSize, {
    onSuccess: function(transport) {
    showData(transport.responseText);
    }
  });
}

//Removes a contact
function removeContact(contactID) {
  
  if(!confirm('Remove contact?')) {
    return false;
  }
  
  new Ajax.Request('/platform/templates/crm/ajax/removeContact.xml?__toolbar=0&contactID='+contactID, {
    onSuccess: function(transport) {
    showContacts(0);
    }
  });
}


function sortAndOrderBy(column) {
  //We should swith asc and desc
  if($('crmOrderBy').value==column) {
    if($('crmAscDesc').value=='ASC') { $('crmAscDesc').value='DESC'; } else {$('crmAscDesc').value='ASC';}
  } else {
    $('crmAscDesc').value='ASC';
  }

  $('crmOrderBy').value=column;
  searchContacts();

}