function GetSelectedPackages(){
    myform = document.forms['packages'];
    intCount = myform.elements.length;

    strpackages="";

    for (x=1;x<=intCount;x++)
    {
       if (myform.elements[x-1].checked == true){
          bolSubmit=true;
          strpackages=strpackages+","+myform.elements[x-1].value;
       }
    }
    return strpackages;
}

function AsyncSelectPackage(){
    $('packagebuttons').innerHTML = "<img src='images/spacer.gif' id='packagebuttonwaiting' />";
    //Get list of buttons for selected invoices

    $('packagebuttonwaiting').src = pic1.src;

    strpackages=GetSelectedPackages();

    if(strpackages==""){
         $('packagebuttons').innerHTML = "<img src='images/spacer.gif' id='packagebuttonwaiting' />";
         return;
    }

    //Set the last list of invoices to global 'lastSelectedInvoiceList'
    //when we get back from async process compare with this last list
    //if not match then continue showing waiting and do not show buttons
    //this will prevent any lag issues with previous clicks returning bogus
    //button list that do not apply to the selected invoices.
    lastSelectedPackageList = strpackages;

    var opt = {
         method: 'post',
         postBody: '&packages='+strpackages+'&action=3044&fuse=3',
         onSuccess: onSelectPackageSuccess,
         on404: on404,
         onFailure: onFailure
    }
    new Ajax.Request('index.php', opt);
}

function onSelectPackageSuccess(t) {
    //in the event that multiple checks were performed
    //we only want to disable the waiting status on the
    //last one selected
    var packagesselected = t.responseText.split('|')[0];
    var strButtons = t.responseText.split('|')[1];

    if(packagesselected!=lastSelectedPackageList) return;

    if(packagesselected==0){
        $('packagebuttons').innerHTML = "<img src='images/spacer.gif' id='packagebuttonwaiting' />";
    }else{
        $('packagebuttons').innerHTML = strButtons;
    }
}

//General function to shoot out the async function performed on packages
function PerformAsyncActionOnPackages(args,successFunction){

    var opt = {
         method: 'post',
         postBody: args,
         onSuccess: successFunction,
         on404: on404,
         onFailure: onFailure
    }
    new Ajax.Request('index.php', opt);
}

function DisablePackageButtons(){
    elements = document.getElementsByName('options');
    for (i=0;i<elements.length;i++){
        var tempobj=elements[i]
        tempobj.disabled=true;
    }
}

function EnablePackageButons(){
    elements = document.getElementsByName('options');
    for (i=0;i<elements.length;i++){
        var tempobj=elements[i]
        tempobj.disabled=false;
    }
}

function SuspendPackages(){
    strpackages=GetSelectedPackages();
    if (strpackages=="") return;

    setStatus("Working...");
    DisablePackageButtons();

    var options = '&packages='+strpackages+'&action=3046&fuse=3';
    if (document.forms['options'].elements['serverplugin'] != null && document.forms['options'].elements['serverplugin'].checked == true){
        options = options+'&serverplugin=1';
    }
    var opt = {
         method: 'post',
         postBody: options,
         onSuccess: onSuspendPackagesSuccess,
         on404: on404,
         onFailure: onFailure
    }
    new Ajax.Request('index.php', opt);
}

function onSuspendPackagesSuccess(t){
    unsetStatus("Packages suspended successfully", 2000);
    var response = t.responseText.split('|');
    var packages = response[0].split(',');
    var messages = response[1].replace(/(,)/g, '<br>\n');
    $('errMessages').innerHTML = '<font class="red"><b>'+messages+'</b></font>';
    for(x=1;x<packages.length;x++){
        $('tdPackageStatus'+packages[x]).innerHTML=lang('Suspended');
    }
    AsyncSelectPackage();g
    EnablePackageButtons();
}

function UnSuspendPackages(){
    strpackages=GetSelectedPackages();
    if (strpackages=="") return;

    setStatus("Working...");
    DisablePackageButtons();

    var options = '&packages='+strpackages+'&action=3047&fuse=3';
    if (document.forms['options'].elements['serverplugin'] != null && document.forms['options'].elements['serverplugin'].checked == true){
        options = options+'&serverplugin=1';
    }
    var opt = {
         method: 'post',
         postBody: options,
         onSuccess: onUnSuspendPackagesSuccess,
         on404: on404,
         onFailure: onFailure
    }
    new Ajax.Request('index.php', opt);
}

function onUnSuspendPackagesSuccess(t){
    unsetStatus("Packages unsuspended successfully", 1000);
    var response = t.responseText.split('|');
    var packages = response[0].split(',');
    var messages = response[1].replace(/(,)/g, '<br>\n');
    $('errMessages').innerHTML = '<font class="red"><b>'+messages+'</b></font>';
    for(x=1;x<packages.length;x++){
        $('tdPackageStatus'+packages[x]).innerHTML=lang('Active');
    }

    AsyncSelectPackage();
    EnablePackageButtons();
}


function SendPackageWelcomeEmail(){
    strpackages=GetSelectedPackages();
    if (strpackages=="") return;

    setStatus("Working...");
    DisablePackageButtons();

    var options = '&packages='+strpackages+'&action=3048&fuse=3';
    var opt = {
         method: 'post',
         postBody: options,
         onSuccess: onSendPackageWelcomeEmailSuccess,
         on404: on404,
         onFailure: onFailure
    }
    new Ajax.Request('index.php', opt);
}

function onSendPackageWelcomeEmailSuccess(t){
    unsetStatus("Welcome emails sent", 2000);
    $('errMessages').innerHTML = '<font class="red"><b>'+t.responseText+'</b></font>';
}

function ActivatePackages(){
    strpackages=GetSelectedPackages();
    if (strpackages=="") return;

    setStatus("Working...");
    DisablePackageButtons();

    var options = '&packages='+strpackages+'&action=3049&fuse=3';
    if (document.forms['options'].elements['serverplugin'] != null && document.forms['options'].elements['serverplugin'].checked == true){
        options = options+'&serverplugin=1';
    }
    if (document.forms['options'].elements['registrarplugin'] != null && document.forms['options'].elements['registrarplugin'].checked == true){
        options = options+'&registrarplugin=1';
    }
    if (document.forms['options'].elements['sendwelcomeemail'].checked == true){
        options = options+'&sendwelcomeemail=1';
    }
    var opt = {
         method: 'post',
         postBody: options,
         onSuccess: onActivatePackagesSuccess,
         on404: on404,
         onFailure: onFailure
    }
    new Ajax.Request('index.php', opt);
}

function onActivatePackagesSuccess(t){
    unsetStatus("Packages activated successfully", 2000);
    var response = t.responseText.split('|');
    var packages = response[0].split(',');
    var messages = response[1].replace(/(,)/g, '<br>\n');
    $('errMessages').innerHTML = '<font class="red"><b>'+messages+'</b></font>';
    for(x=1;x<packages.length;x++){
        $('tdPackageStatus'+packages[x]).innerHTML=lang('Active');
    }
    AsyncSelectPackage();
    EnablePackageButtons();
}

function DeletePackages(){
    strpackages=GetSelectedPackages();
    if (strpackages=="") return;

    setStatus("Working...");
    DisablePackageButtons();

    var options = '&packages='+strpackages+'&action=3045&fuse=3';
    if (document.forms['options'].elements['serverplugin'] != null && document.forms['options'].elements['serverplugin'].checked == true){
        options = options+'&serverplugin=1';
    }
    var opt = {
         method: 'post',
         postBody: options,
         onSuccess: onDeletePackagesSuccess,
         on404: on404,
         onFailure: onFailure
    }
    new Ajax.Request('index.php', opt);
}

function onDeletePackagesSuccess(t){
    unsetStatus("Packages deleted successfully", 2000);
    var response = t.responseText.split('|');
    var packages = response[0].split(',');
    var messages = response[1].replace(/(,)/g, '<br>\n');
    $('errMessages').innerHTML = '<font class="red"><b>'+messages+'</b></font>';
    for(x=1;x<packages.length;x++){
        document.forms['packages'].elements['p'+packages[x]].checked=false;
        $('tblPackages').deleteRow($('trPackage'+packages[x]).rowIndex);
    }
    AsyncSelectPackage();
    EnablePackageButtons();
}

function CancelPackages(){
    strpackages=GetSelectedPackages();
    if (strpackages=="") return;

    setStatus("Working...");
    DisablePackageButtons();

    var options = '&packages='+strpackages+'&action=3050&fuse=3';
    if (document.forms['options'].elements['serverplugin'] != null && document.forms['options'].elements['serverplugin'].checked == true){
        options = options+'&serverplugin=1';
    }
    var opt = {
         method: 'post',
         postBody: options,
         onSuccess: onCancelPackagesSuccess,
         on404: on404,
         onFailure: onFailure
    }
    new Ajax.Request('index.php', opt);
}

function onCancelPackagesSuccess(t){
    unsetStatus("Packages cancelled successfully", 2000);
    var response = t.responseText.split('|');
    var packages = response[0].split(',');
    var messages = response[1].replace(/(,)/g, '<br>\n');
    $('errMessages').innerHTML = '<font class="red"><b>'+messages+'</b></font>';
    for(x=1;x<packages.length;x++){
        $('tdPackageStatus'+packages[x]).innerHTML=lang('Cancelled');
    }
    AsyncSelectPackage();
    EnablePackageButtons();
}