var RoomSwitcher = {}

// Windows
var WindowsPath     = 'C:\\Documents and Settings\\All Users\\Documents\\DYMO Label\\Label Files\\';
var WindowsShippingLabel = 'Shipping (30323, 30573).LWL';
var WindowsAddressLabel  = 'Address (30252, 30320, 30572).LWL';
var WindowsMailLabel     = 'Mail_Label.LWL';

// Mac
var MacPath     = '/Applications/DYMO Label/Label Files/';
var MacShippingLabel = 'Shipping (30323, 30573).DLF';
var MacAddressLabel  = 'Address (30252, 30320, 30572).DLF';
var MacMailLabel     = 'Mail_Label.DLF';

RoomSwitcher = {
    showLoading: function(id) {
        Element.show('progress-indicator-'+id);
    },

    loadingComplete: function(id,form) {
        Element.hide('progress-indicator-'+id);
    }
}

function setInitialFocus() {
    var f = document.getElementsByClassName("first");
    if(f[0]) f[0].focus();
}

function PrintLabels(id,label_format,single_label_only) {

    var childLabel = '';
    $A(label_format.split(',')).each(function(label) {
      var field = null;
      if(label == 'room') {
        field = $A($('person_'+id+'_room_id').getElementsByTagName('option')).find( function(o) { return o.selected;} );
      } else {
        field = $(label+id);
      }
      if(field) {
        var field_value = (field.innerHTML) ? field.innerHTML : field.value;
        if(field_value) {
          childLabel += "\n"+field_value.unescapeHTML();
        }
      }
    });

    var securityCode = $('security_code'+id).innerHTML;
    var room = $A($('person_'+id+'_room_id').getElementsByTagName('option')).find( function(o) { return o.selected;} );
    var room_name = room.innerHTML;
    var first_name = $('first_name'+id).innerHTML;
    var parentLabel = 'Parent Ticket:\n'+first_name+'-'+securityCode+'\nRoom:'+room_name;

    if(!securityCode || securityCode == '') {
      alert('Not checked in.');
      return false;
    }

    // To "debug", uncomment these.
    // alert(childLabel);
    // alert(parentLabel);

    if (navigator.appName == "Microsoft Internet Explorer") {
        // for IE        
        var DymoAddIn, DymoLabel;        
        DymoAddIn = new ActiveXObject('DYMO.DymoAddIn');        
        DymoLabel = new ActiveXObject('DYMO.DymoLabels');        

        if (DymoAddIn.Open(WindowsPath+WindowsShippingLabel)) {
        // for IE        
           DymoLabel.SetAddress(1, childLabel);
           DymoAddIn.Print2(1, true, 1);
        }
        if (DymoAddIn.Open(WindowsPath+WindowsAddressLabel) && (single_label_only == 'false')) {
        // for IE        
           DymoLabel.SetAddress(1, parentLabel);
           DymoAddIn.Print2(1, true, 0);
        }

    } else if((navigator.appVersion.indexOf("Safari") >= 0) && (navigator.appVersion.indexOf("Macintosh") >= 0)) {

        // try to get a reference to the existing plugin window
        newWindow = window.open('','plugin_window');
        // if the plugin window hasn't been loaded(i.e. it's the very first time)
        if(newWindow.document.title != 'Plugin') {
          newWindow = window.open('/plugin.html','plugin_window'); // set dimensions, etc.
        }
        newWindow.opener.focus(); // don't change focus from current page/window

        if (single_label_only == 'false') {
          newWindow.document.getElementById("DYMOLabelPlugin").Open(MacPath+MacAddressLabel);
          newWindow.document.getElementById("DYMOLabelPlugin").GetLabelImage();
          newWindow.document.getElementById("DYMOLabelPlugin").SetObjectData('Address',parentLabel);
          newWindow.document.getElementById("DYMOLabelPlugin").Print(1, 'left', 0);
        }

        newWindow.document.getElementById("DYMOLabelPlugin").Open(MacPath+MacShippingLabel);
        newWindow.document.getElementById("DYMOLabelPlugin").GetLabelImage();
        newWindow.document.getElementById("DYMOLabelPlugin").SetObjectData('Address',childLabel);
        newWindow.document.getElementById("DYMOLabelPlugin").Print(1, 'right', 0);

    } else if (navigator.appName == "Netscape") {

        if ((navigator.appVersion.indexOf("Macintosh")) >= 0 && (navigator.appVersion.indexOf('Safari') < 0)) {
            alert("To print labels on the Mac, please use Safari.");
            return;
        }

        try
        {
          netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

          // create and use the nsDymoAddIn object
          var DymoAddInCID = "@dymo.com/sdk/nsDymoAddIn;1";
          DymoAddIn = Components.classes[DymoAddInCID].createInstance();
          DymoAddIn = DymoAddIn.QueryInterface(Components.interfaces.nsIDymoAddIn2);
          // create and use the nsDymoLabels object
          var DymoLabelsCID = "@dymo.com/sdk/nsDymoLabels;1";
          DymoLabels = Components.classes[DymoLabelsCID].createInstance();
          DymoLabels = DymoLabels.QueryInterface(Components.interfaces.nsIDymoLabels);

          if (DymoAddIn.Open(WindowsPath+WindowsShippingLabel)) {
              DymoLabels.SetAddress(1, childLabel);
              DymoAddIn.Print2(1, true, 1);
          } 

          if (DymoAddIn.Open(WindowsPath+WindowsAddressLabel) && (single_label_only == 'false')) {
            DymoLabels.SetAddress(1, parentLabel);
            DymoAddIn.Print2(1, true, 0);
          }
        }
        catch (err)
        {
          alert(err);
          return;
        }
    }
}

function PrintAddressLabels(first_name, last_name, address1, city, state, zip) {

    if (navigator.appName == "Microsoft Internet Explorer") {
        // for IE        
        var DymoAddIn, DymoLabel;        
        DymoAddIn = new ActiveXObject('DYMO.DymoAddIn');        
        DymoLabel = new ActiveXObject('DYMO.DymoLabels');        

        if (DymoAddIn.Open(WindowsPath+WindowsMailLabel)) {
           // for IE        
           DymoLabel.SetAddress(1, first_name+' '+last_name+'\n'+address1+'\n'+city+', '+state+' '+zip);
           DymoAddIn.Print2(1, true, 0);
        } else {
            alert('Please use the Dymo Label Software to create an address label named Mail_Label.LWL and save it. Then try printing a label again. For details instructions click on home and find the FAQ.');
        }
    }

    if((navigator.appVersion.indexOf('Safari') >= 0) && (navigator.appVersion.indexOf('Macintosh') >= 0)) {
        // try to get a reference to the existing plugin window
        newWindow = window.open('','plugin_window');
        // if the plugin window hasn't been loaded(i.e. it's the very first time)
  
        if(newWindow.document.title != 'Plugin') {
          newWindow = window.open('/plugin.html','plugin_window'); // set dimensions, etc.
        }
  
        newWindow.opener.focus(); // don't change focus from current page/window
  
        var address = first_name+' '+last_name+'\n'+address1+'\n'+city+', '+state+' '+zip;

        newWindow.document.getElementById("DYMOLabelPlugin").Open(MacPath+MacMailLabel);
        newWindow.document.getElementById("DYMOLabelPlugin").SetObjectData('Address', address);
        newWindow.document.getElementById("DYMOLabelPlugin").Print(1, 'left', 0);

        return;
    }

    // For FF
    if (navigator.appName == "Netscape") {
        if ((navigator.appVersion.indexOf("Macintosh")) >= 0 && (navigator.appVersion.indexOf('Safari') < 0)) {
            alert("To print labels on the Mac, please use Safari.");
            return;
        }
        
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

        // create and use the nsDymoAddIn object
        var DymoAddInCID = "@dymo.com/sdk/nsDymoAddIn;1";
        DymoAddIn = Components.classes[DymoAddInCID].createInstance();
        DymoAddIn = DymoAddIn.QueryInterface(Components.interfaces.nsIDymoAddIn2);


        // create and use the nsDymoLabels object
        var DymoLabelsCID = "@dymo.com/sdk/nsDymoLabels;1";
        DymoLabels = Components.classes[DymoLabelsCID].createInstance();
        DymoLabels = DymoLabels.QueryInterface(Components.interfaces.nsIDymoLabels);

        if (DymoAddIn.Open(WindowsPath+WindowsMailLabel)) {
            DymoLabels.SetAddress(1, first_name+' '+last_name+'\n'+address1+'\n'+city+', '+state+' '+zip);
            DymoAddIn.Print2(1, true, 0);
        } 
    }
}
function ApplyBirthdayStripes(oArgs,opts) {
  var table = opts['table'];
  var nBirthdayColumn = opts['birthdayColumnIndex'];
  var allRows = this._elTbody.rows;
  var nStartIndex = 0;
  var nEndIndex = allRows.length;
  var today = new Date();
  var past = new Date().setDate(today.getDate() - 7);
  var future = new Date().setDate(today.getDate() + 7 );

  for(var i=nStartIndex; i<nEndIndex; i++) {
      var thisRow = table.getTrEl(allRows[i]);
      var thisCell = table.getTdEl(thisRow.cells[nBirthdayColumn]);
      var birthday = new Date(Date.parse(thisCell.innerHTML));
      birthday.setFullYear(today.getFullYear());
      var birthdayClass = null;
      if((birthday.getMonth()==today.getMonth()) && (birthday.getDate() == today.getDate())) {
          birthdayClass = 'birthday_today';
      }else if((birthday >= past) && (birthday < today)) {
          birthdayClass = 'birthday_past';
      }else if((birthday > today) && (birthday <= future)) {
          birthdayClass = 'birthday_future';
      }

      if(birthdayClass != null) {
          // remove all the old/existing CSS classes
          [
          YAHOO.widget.DataTable.CLASS_ODD,
          YAHOO.widget.DataTable.CLASS_EVEN,
          YAHOO.widget.DataTable.CLASS_FIRST,
          YAHOO.widget.DataTable.CLASS_LAST,
          'birthday',
          'birthday_past',
          'birthday_future'
          ].each(function(cssClass) {
            YAHOO.util.Dom.removeClass(thisRow,cssClass);
          });
          YAHOO.util.Dom.addClass(thisRow, birthdayClass);
      }
  }
};
