
//
// Javascript stored in just the root frame.
//
<!-- <xsl:template name="contextJavascript"> -->

<!-- alert('LoadingContextJavascript!'); -->
var jmain = self;
var date_error = '0';
<!-- Note that a local install might want to change these -->
var gDefaultPopupWidth='550';
var gDefaultPopupHeight='650';
<!-- Define a global variable to store platform and browser info; leave them initially undefined -->
var gPlatform = '';
var gNN = '';
var gIE = '';

<!-- the value of global info is a bunch of var= statements -->
<!-- these globals are picked up in template javascript_vars since they use xsl calls -->

  var window_list = new Array();

  function setClientInfo() {
    var s = navigator.userAgent; 

   <!-- alert('setting up client info'); -->
    if (s == '') 
      gIE = 3; 
    else if (s.indexOf('MSIE') != -1) 
      gIE = parseFloat(s.substring(s.indexOf('MSIE') + 5, s.length)); 
    else if (s.indexOf('Mozilla') != -1) 
      gNN = parseFloat(s.substring(s.indexOf('Mozilla') + 8, s.length)); 

    if ( s.indexOf('Win') != -1 ) 
      gPlatform = 'Win32'; 
  }

  function accept_window(a_window)
  {
    if (a_window)
    {
      window_list[window_list.length] = a_window;
    }
  }

  function close_child_windows()
  {
    for ( x = 0; x < window_list.length; x++)
    {
      var win = window_list[x];

      if (win && !win.closed)
      {
        win.close_all();
      }
    }
  }

  function close_all()
  {
    close_child_windows();
    jmain.close();
  }

  function reloadOpener()
  {
    var parent_f = jmain.opener.frames[1].document.forms[0];
    parent_f.elements['Calid_add'].click();
    close();
  }

  function setDate(newdate)
  {
<!--    alert('New Date = '+newdate); -->
    gDate = newdate;
  }

  function setView(newview)
  {
    gOldView = gCurView;
    gCurView = newview.split(":")[0];
  }

  function padZero(n) {
    if (n.length == 1) {
      return '0' + n;
    }
    else {
      return n;
    }
  }

  function checkNumberStringInput(number)
  {
    for (var i=0; i < number.length; i++)
    {
        var ch=number.charAt(i);
        if ( (ch < '0') || (ch > '9') ) {
           return -1;
        }
    }
    return parseInt(number, 10);
  }

  function getDateFromForm(this_form)
  {
    var thedate;

    if (this_form != null)
    {
      if ((checkNumberStringInput(this_form['dd']) != -1) && (checkNumberStringInput(this_form['mm']) != -1) && (checkNumberStringInput(this_form['ccyy']) != -1)){
        var day = padZero(this_form['dd'].value);
        var month = padZero(this_form['mm'].value);
        var year = this_form['ccyy'].value;
        var hour;
        if (this_form['hr'])
          hour = this_form['hr'].value;
        else
          hour = null;

        if ((hour == null) || (checkNumberStringInput(hour) == -1)) {
          thedate = year + month + day + 'T000000';
        }
        else {
          var minute;
	  if ((this_form['mn']) && (checkNumberStringInput(this_form['mn']) != -1))
            minute = this_form['mn'].value;
          else
            minute = '00';        

          var ampm = this_form['pm'] ? this_form['pm'].selectedIndex : null;
          if (ampm != null) {
            if ( ampm == 1 && hour != '12') {
              hour = parseInt(hour) + 12; 
            }
            if ( ampm == 0 && hour == '12') {
              hour = '00'; 
            }
          }
        
          thedate = year + month + day 
                  + 'T' + padZero(hour) + padZero(minute) + '00';
        }
      }
      else {
        thedate = gDate;
      }
    }
    else {
      thedate = gDate;
    }

    return (thedate);
    
  }

  function newViewCommand(newView, newdate, fullreload, navto_item, navto_name,modtime, prevViewFlag)

  {
    if (!navto_item) navto_item = "calid";
    if (!navto_name) navto_name = "";

<!-- if the date hasn't changed, try to optimize -->

<!-- but provide a way to avoid the optimization -->
    if(fullreload != 'force') {
      if (gCurView.indexOf("overview") == 0  ||
    	gCurView.indexOf("dayview") == 0  ||
        gCurView.indexOf("weekview") == 0  ||
        gCurView.indexOf("monthview") == 0 ||
        gCurView.indexOf("yearview") == 0  ||
        gCurView.indexOf("groupview") == 0
       )
        {
           if (newView.indexOf("overview") == 0  ||
            newView.indexOf("dayview") == 0  ||
            newView.indexOf("weekview") == 0  ||
            newView.indexOf("monthview") == 0 ||
            newView.indexOf("yearview") == 0  ||
            newView.indexOf("groupview") == 0
            )
             {
              fullreload = 'false';
             }
        }
    } else {
        fullreload = 'true';
    }
    
    if (fullreload == 'true')
    {
      var x;

      if ((x = newView.indexOf(':')) >= 0)
      {
        newView = newView.substring(0, x);
      }
    }

<!-- make sure the newdate is a valid date (1970 <x < 2037) -->
      if(newdate){
        yearString = newdate.substring(0,4);
<!-- javascript does auto convert of string to num in a lessthan comparison -->
        if (yearString < 1970) {
            alert(alert_min_year);
            newdate='';
            return;
        }
        if (yearString > 2036) {
            alert(alert_max_year);
            newdate='';
            return;
        }
      }

<!-- Maintain the tzid if it's in the ARG list -->
    var tzidArray = jmain.location.search.match(/tzid=([^&]*)/);
    var tzid = '';
    if (tzidArray && tzidArray.length > 0) {
      tzid = tzidArray[1];
    }
      

<!-- use the newdate if given, otherwise use the current global date -->
    var urlstring = jmain.gCommandBaseURL +
        '/command.shtml?view=' + escape(newView) +
       '&id=' + escape(gID) + 
       '&crc=' + escape(gCRC) + 
       '&date=' +
       (newdate != '' ? escape(newdate) : escape(gDate)) + 
       (prevViewFlag == 'true' ? '&prevView=' + escape(gOldView) : '') +
       (navto_name != '' ? '&'+ navto_item + '=' + escape(navto_name) : '') +
       (tzid != '' ? '&tzid=' + tzid : '') +
       "&security=1" +
       (modtime != '0' ? '&'+ 'modtime' + '=' + escape(modtime) : '');

    var randomnumber=Math.floor(Math.random()*1000);

    var newurlstring = jmain.gCommandBaseURL +
        '/command.shtml?view=' + escape(newView) +
       '&id=' + escape(gID) +
       '&crc=' + escape(gCRC) +
       '&date=' +
       (newdate != '' ? escape(newdate) : escape(gDate)) +
       '&prevView=' + escape(gOldView) +
       (navto_name != '' ? '&'+ navto_item + '=' + escape(navto_name) : '') +
       (modtime != '0' ? '&'+ 'modtime' + '=' + escape(modtime) : '') +
       '&random=' + escape(randomnumber) +
       '&payload=' + escape(urlstring) +
       (tzid != '' ? '&tzid=' + tzid : '') +
       "&security=1";

<!--  alert(urlstring); -->

    if (newView == 'logout')
    {
      urlstring    += "&url=/?action=logout";
      newurlstring += "&url=/?action=logout";
      jmain.close_child_windows();
    }

    if(fullreload == 'true')
    {
      if (modtime != '0')
      {
        jmain.location.replace(newurlstring);
      }
      else
      {
        jmain.location.replace(urlstring);
      }
    } else
    {
      if (modtime != '0')
      {
        frames[1].document.location.href = newurlstring;
      }
      else
      {
        frames[1].document.location.href = urlstring;
      }
    }

    if(newdate != '')
    {
      setDate(newdate);
    }
    setView(newView);
  }

  function newViewGroupCommand(newView, newdate, fullreload, navto_group)
  {

    if (!navto_group) navto_group = "";
    if (fullreload == 'true')
    {
      var x;

      if ((x = newView.indexOf(':')) >= 0)
      {
        newView = newView.substring(0, x);
      }
    }

<!-- Maintain the tzid if it's in the ARG list -->
    var tzidArray = jmain.location.search.match(/tzid=([^&]*)/);
    var tzid = '';
    if (tzidArray && tzidArray.length > 0) {
      tzid = tzidArray[1];
    }
    
<!-- use the newdate if given, otherwise use the current global date -->
    var urlstring = jmain.gCommandBaseURL +
        '/command.shtml?view=' + escape(newView) +
       '&id=' + escape(gID) + 
       '&crc=' + escape(gCRC) + 
       '&date=' +
       (newdate != '' ? escape(newdate) : escape(gDate)) + 
       '&prevView=' + escape(gOldView) + 
       (navto_group != '' ? '&group='+ escape(navto_group) : '') + 
       (navto_group != '' ? '&isAllGroup=false' : '') +
       (tzid != '' ? '&tzid=' + tzid : '') +
       "&security=1";


<!--  alert(urlstring); -->

    if(fullreload == 'true') {
      jmain.location.replace(urlstring);
    } else {
      frames[1].document.location.href = urlstring;
    }

    if(newdate != '')
    {
      setDate(newdate);
    }
    setView(newView);
  }

  function newPopupCommand(pView, pDate, pArgs, newArgs, wNum, wH, wW, fW)
  {
    var thedate;

    var x = pView.indexOf('print');
    if (x >= 0)
    {
      pView = gCurView + ':print';
    }

    if(gPlatform == '') {
      setClientInfo();
    }
    
    if(pDate != '')
    {
      if (pDate == 'from_form') {
        thedate = getDateFromForm(parent.frames[1].document.forms[0]);
      } else if (pDate.indexOf('from_js') >= 0) {
<!--    pick up the needed time information if it's there -->
        var t=pDate.indexOf('T');
        var gT = gDate.indexOf('T');
        if((t > 0) && (gT > 0)) {
          thedate = (gDate.slice(0,gT+1)) + (pDate.slice(t+1));
        } else {
          thedate = gDate;
        }
      }
      else {
        thedate = pDate;
      }
    }
    else {
      thedate = gDate;
    }
<!-- newArgs should include their &'s -->
    if(newArgs == 'extraargs')
    {
      newArgs = '';
    }

<!-- Maintain the tzid if it's in the ARG list -->
    var tzidArray = jmain.location.search.match(/tzid=([^&]*)/);
    var tzid = '';
    if (tzidArray && tzidArray.length > 0) {
      tzid = tzidArray[1];
    }


<!-- note that thedate could be blank but this still works -->
    var urlstring = jmain.gCommandBaseURL + '/command.shtml?view=' +
                    escape(pView) +
                    '&id=' + escape(gID) +
		    '&crc=' + escape(gCRC) + 
                    '&date=' + escape(thedate) +
                    (tzid != '' ? '&tzid=' + tzid : '') +
                    '&prevView=' + escape(gCurView) +
                    pArgs + newArgs +
                    '&security=1';
    
    var wname = escape(gID) + wNum;
<!-- For now - to allow customized width and height -->
<!-- if the w or h are 0 or not set, use the gDefaultPopupWidth and gDefaultPopupHeight from this file -->
    if((wH == '') || (wH == '0'))  {
      wH = gDefaultPopupHeight;
    }
    if((wW == '') || (wW == '0')) {
      wW = gDefaultPopupWidth;
    }
    var wfeatures = 'height=' + wH + ',width=' + wW; 

<!-- add resizeable corners if we're IE or Netscape 6 or greater -->
    if(gIE || gNN > 5)
      wfeatures += ',resizable';

<!-- if a full window is desired, add chrome  -->
    if(fW == 'true')
      wfeatures += ',scrollbars,menubar,toolbar,locationbar';
    var w = window.open(urlstring, wname, wfeatures, 'false');
    jmain.accept_window(w);
  }

  function setRRule(newRule)
  {
    var w = opener.frames[1].document.forms[0];

    w['e_existingRRule'].value=newRule;
    w['e_rruleChanged'].value='true';
    
     if (w['recurrence_enabled'] != null) {
      w['recurrence_enabled'].checked=true;
    }

    w['i_tabswitch'].value=1;
    w.submit();
  }

  function icsNormalize(a) {
    var b = '';

    for (i = 0; i < a.length; i++) {
      var c = a.substring(i, i + 1);
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
        b += '~';
      } else {
        b += c;
      }
    }

    return b;
  }

  function NewEventGroupInvite(options, selectedIndex, selectionList)
  {
    if ( options[selectedIndex].value.length == 0 )
    {
      selectionList.selectedIndex = 0;
      selectedIndex = 0;
      return false;
    } else {

      jmain.frames[1].window.document.forms[0].mass_invite.value = icsNormalize(options[selectedIndex].value);

      jmain.frames[1].document.form.submit();
      return true;
    }
  } 

  function CreateGroupQuickAdd(options, selectedIndex, selectionList)
  {
    if ( options[selectedIndex].value.length == 0 )
    {
      selectionList.selectedIndex = 0;
      selectedIndex = 0;
      return false;
    } else {
      var fr = parent.frames[1];
      var add_form  = fr.document.forms[0];

      <!-- addCalendars operation expects the calendar list as a,b,c,d,  -->
      add_form.newCalAddr.value = icsNormalize(options[selectedIndex].value) + ',';

      if (add_form['myaction'])
      {
        add_form['myaction'].value = 'addCalendars';
        void add_form.submit();
      }
    }
  }

  function GroupSelect(options, selectedIndex, selectionList)
  {
    if (options[selectedIndex].value.length == 0)
    {
      selectionList.selectedIndex = 0;
      selectedIndex = 0;
    }

    var prevView = jmain.gCurView;
    var id = jmain.gID;
    var date = jmain.gDate;
<!-- Maintain the tzid if it's in the ARG list -->
    var tzidArray = jmain.location.search.match(/tzid=([^&]*)/);
    var tzid = '';
    if (tzidArray && tzidArray.length > 0) {
      tzid = tzidArray[1];
    }



    var splitString = options[selectedIndex].value.split(';');

<!--      alert(splitString[0]); -->
<!--    alert(splitString[1]); -->

    var group = '';
    if (splitString[0].substring(0, 6) == 'group-')
    {
      group = '&group=' + icsNormalize(splitString[0].substring(6, splitString[0].length + 1));
    }

    var calid = '';
    if (splitString[0].substring(0, 4) == 'cal-')
    {
      calid = '&calid=' + icsNormalize(splitString[0].substring(4, splitString[0].length + 1));
    }

 
    var randomnumber=Math.floor(Math.random()*1000);

    var payload;
    if (splitString[1] != '')
    {
      payload =
        jmain.gCommandBaseURL +
        '/command.shtml?view=' + prevView +
        '&id=' + id +
        '&crc=' + gCRC +
        '&date=' + date +
        (tzid != '' ? '&tzid=' + tzid : '') +
        group +
        calid +
        '&isAllGroup=' + (selectedIndex == 1 ? 'true' : 'false') +
        '&security=1' +
        '&modtime=' + splitString[1];
    }

    jmain.document.location.href =
        jmain.gCommandBaseURL +
        '/command.shtml?view=' + prevView +
        '&id=' + id +
        '&crc=' + gCRC +
        '&date=' + date +
        (splitString[1] != '' ? '&modtime=' + splitString[1] : '') +
        group +
        calid +
        (tzid != '' ? '&tzid=' + tzid : '') +
        '&isAllGroup=' + (selectedIndex == 1 ? 'true' : 'false') +
        (splitString[1] != '' ? '&random=' + randomnumber : '') +
        (splitString[1] != '' ? '&payload=' + escape(payload) : '') +
        '&security=1';

    return true;
  }

  function delEventTaskCommand(type, calid, cal, name, view, uid)
  {
    if ( type == 'new_event') {
      if (!confirm(confirm_event_delete))
        return;	
    } else {
      if (!confirm(confirm_task_delete))
        return;	
    }

<!-- Maintain the tzid if it's in the ARG list -->
    var tzidArray = jmain.location.search.match(/tzid=([^&]*)/);
    var tzid = '';
    if (tzidArray && tzidArray.length > 0) {
      tzid = tzidArray[1];
    }

    var urlstring = jmain.gCommandBaseURL +
        '/command.shtml?type=' + type +
       '&shortcut=' + 'true' +
       '&myaction=' + 'delete' +
       '&e_uid=' + escape(uid) +
       '&id=' + escape(gID) +
       '&crc=' + escape(gCRC) +
       '&date=' + gDate +
       '&prevView=' + escape(view) +
       (tzid != '' ? '&tzid=' + tzid : '') +
       (name != '' ? '&'+ cal + '=' + escape(name) : '') +
       (cal != 'calid' ? '&calid=' + escape(calid) : '') +
       '&security=1';

    jmain.location.replace(urlstring);

  }

  function MoveCursorOnFocus(dialog, options, selectedIndex, selectionList)
  {
    if(gPlatform == '') {
      setClientInfo();
    }
    <!-- alert('dialog is ' + dialog); -->
    if (gNN < 5 && (gPlatform != 'Win32')) {
      if ((options[selectedIndex].value)  && (options[selectedIndex].value.length == 0))
      {
        selectionList.selectedIndex = 0;
        selectedIndex = 0;
        return false;
      } else {
	if (dialog == 'NewEvent') {
         jmain.frames[1].window.document.forms[0].invitee.focus();
        } else if (dialog == 'NewGroup') {
         jmain.frames[1].window.document.forms[0].member.focus();
        } else if (dialog == 'Export') {
         jmain.frames[2].window.document.forms[0].ms_day.focus();
	}
        return ;
      }
    }
  }


