//JavaScritp file picker.js contains routines for quick date selection for new event in Calendar
//Please Send RFC and bug reports to aklimenko@blackboard.com

var picker;
function showPicker(pname,locale) {

  if ( locale == null || locale == '' )
  {
  	locale = 'en_US';
  }
  var html = "/webapps/blackboard/monthn.jsp?loc="+locale;
  var yyyy = eval("window.document.forms[0]."+pname+"_yyyy");
  var mm   = eval("window.document.forms[0]."+pname+"_mm");
  if ((typeof(yyyy) == 'undefined')) {
    yyyy = eval("window.document.forms[1]."+pname+"_yyyy");
    mm   = eval("window.document.forms[1]."+pname+"_mm");
  }
  yyyy = (yyyy.selectedIndex == -1) ? (new Date()).getFullYear() - 20 : yyyy.options[yyyy.selectedIndex].value
                                    ? yyyy.options[yyyy.selectedIndex].value : (new Date()).getFullYear();
  mm   = (mm.selectedIndex == -1)   ? (new Date()).getMonth()+1 : mm.options[mm.selectedIndex].value
                                    ? mm.options[mm.selectedIndex].value : (new Date()).getMonth()+1;

  if (typeof(window.document.forms[0].pickdate) == 'undefined') {
    if (window.document.forms[1].pickdate.length > 1) {
      window.document.forms[1].pickdate[0].value = yyyy+"-"+mm+"-01";
      window.document.forms[1].pickname[0].value = pname;
    } else {
      window.document.forms[1].pickdate.value = yyyy+"-"+mm+"-01";
      window.document.forms[1].pickname.value = pname;
    }
  } else {
    if (window.document.forms[0].pickdate.length > 1) {
      window.document.forms[0].pickdate[0].value = yyyy+"-"+mm+"-01";
      window.document.forms[0].pickname[0].value = pname;
    } else {
      window.document.forms[0].pickdate.value = yyyy+"-"+mm+"-01";
      window.document.forms[0].pickname.value = pname;
    }
  }

  var height = CALENDAR_RESOURCES.getString("picker.height");
  if (!height || !parseInt(height)) { height = '220'; }
  var width = CALENDAR_RESOURCES.getString("picker.width");
  if (!width || !parseInt(width)) { width = '220'; }

  var strFeatures =
        "minimize=1,resizable=1,dependent=0,menubar=0,directories=0,location=0,status=0,scrollbars=0,toolbar=0"
        + ",left=" + (screen.width - 300)
        + ",top=120"
        + ",height=" + height
        + ",width=" + width
        + ",screenY=80"
        + ",screenX=" + (screen.width - 300);

    if (!self.picker) {picker=window.open(html,"newwin",strFeatures);}
    else if (self.picker.closed) {picker=window.open(html,"newwin",strFeatures);}
    else {self.picker.close(); picker=window.open(html,"newwin",strFeatures);}
}


function getTargetDate(shift, locale) {
  var strDate;

  if (typeof(window.opener.document.forms[0].pickdate) == 'undefined') {
    if (window.opener.document.forms[1].pickdate.length > 1) {
      strDate = window.opener.document.forms[1].pickdate[0].value;
    } else {
      strDate = window.opener.document.forms[1].pickdate.value;
    }
  } else {
    if (window.opener.document.forms[0].pickdate.length > 1) {
      strDate = window.opener.document.forms[0].pickdate[0].value;
    } else {
      strDate = window.opener.document.forms[0].pickdate.value;
    }
  }

  var arrDate = new Array();
  arrDate = strDate.split(/-/);
  arrDate[0] = parseInt(arrDate[0],10);
  arrDate[1] = parseInt(arrDate[1],10);
  dat = new Date(arrDate[0],(arrDate[1]-1+shift),1);

  if(shift != 0){
    var mon = dat.getMonth()+1;
    mon = (mon<10)?"0"+mon:mon;
    var year = dat.getFullYear();
    if (typeof(window.opener.document.forms[0].pickdate) == 'undefined') {
      if (window.opener.document.forms[1].pickdate.length > 1) {
        window.opener.document.forms[1].pickdate[0].value = year+"-"+mon+"-"+"01";
      } else {
        window.opener.document.forms[1].pickdate.value = year+"-"+mon+"-"+"01";
      }
    } else {
      if (window.opener.document.forms[0].pickdate.length > 1) {
        window.opener.document.forms[0].pickdate[0].value = year+"-"+mon+"-"+"01";
      } else {
        window.opener.document.forms[0].pickdate.value = year+"-"+mon+"-"+"01";
      }
    }
    window.location ="/webapps/blackboard/monthn.jsp?loc=" + locale;
  }
  return dat;
}

function dispDate(date,format){
  var month = date.getMonth();
  var year = date.getFullYear();
  var day = date.getDay();

  // TODO: need more flexibility -- externalize numbers etc.
  var args = new Array(
    year,                    // 2004
    month,                   // 3
    date.getDate(),          // 21
    getShortMonthName(month),// Mar
    getLongMonthName(month), // March
    getShortDayName(day),    // Sun
    getLongDayName(day)      // Sunday
  );

  document.write(CALENDAR_RESOURCES.getFormattedString("format." + format, args));
}

function getFirstDayOfWeek() {
  var strCount = LOCALE_SETTINGS.getString("LOCALE_SETTINGS.FIRST_DAY_OF_WEEK.03207");
  if (strCount.length == 1) { // better defensiveness needed here
    return parseInt(strCount,10);
  }
  return 0;
}

function lastDay(dat){
  var lastDay = 28;
  var mon = dat.getMonth();
  var year= dat.getFullYear();

  if ( (mon < 8 && (mon % 2 == 0 || mon == 7)) || (mon >= 8 && mon % 2 == 1)){
    lastDay = 31;
  }
  else if (mon == 1){
    var nextMonth = (new Date(year,mon,lastDay+1)).getMonth();
    if (nextMonth == 1){lastDay++};
  }
  else {
    lastDay = 30;
  }
  return lastDay;
}

/*
      if ($first_wd_of_mo < $start_wk) {
        $day = (7 * $w) + ($d + 1) - ( 7 - ($start_wk - $first_wd_of_mo));
      } else {
        $day = (7 * $w) + ($d + 1) - ($first_wd_of_mo - $start_wk);
      }

*/
function firstDow(dat){
  var sWeek = getFirstDayOfWeek();
  var curDay = dat.getDay();
  var firstDayOffset;
  if (curDay - sWeek < 0) {
    firstDayOffset = 7 - ( sWeek - curDay );
  } else {
    firstDayOffset = curDay - sWeek ;
  }
  return firstDayOffset;
}

function setDate(dat){
  var curDate = getTargetDate(0);
  var setYear = curDate.getFullYear();
  var setMonth= curDate.getMonth();
  var setDate = dat;
  var fname;
  var str;
  if (typeof(window.opener.document.forms[0].pickname) == 'undefined') {
    if (window.opener.document.forms[1].pickname.length > 1) {
      fname = window.opener.document.forms[1].pickname[0].value;
    } else {
      fname = window.opener.document.forms[1].pickname.value;
    }
    str = "window.opener.document.forms[1]."+fname;
  } else {
    if (window.opener.document.forms[0].pickname.length > 1) {
      fname = window.opener.document.forms[0].pickname[0].value;
    } else {
      fname = window.opener.document.forms[0].pickname.value;
    }
    str = "window.opener.document.forms[0]."+fname;
  }

  var month = eval(str+"_mm");
  var date  = eval(str+"_dd");
  var year  = eval(str+"_yyyy");
  month.selectedIndex = setMonth;

  for (var i=0;i<year.length;i++){
    if  (year.options[i].value == setYear){
      year.selectedIndex = i;
    }
  }
  if (year.selectedIndex < 0){ year.selectedIndex = 0;};
  eval("window.opener."+fname+"_normalizeDate (month,date,year)");
  date.selectedIndex = setDate-1;

  eval("window.opener."+fname+"_updateHidden(month)");
  self.window.close();
}

function zeroPad(n){
  n = parseInt(n,10);
  return (n<10)?'0'+n : n;
}

function getStrings(prefix, range) {
  var array = new Array(range.length);
  for (var i = 0; i < range.length; i++) {
    array[i] = CALENDAR_RESOURCES.getString(prefix + range[i]);
  }
  return array;
}

var dayNameSuffixes = new Array("sun", "mon", "tue", "wed", "thu", "fri", "sat");

var shortDayNames = getStrings("day.short.", dayNameSuffixes);
function getShortDayName(day) {
  return shortDayNames[day];
}

var longDayNames = getStrings("day.long.", dayNameSuffixes);
function getLongDayName(day) {
  return longDayNames[day];
}

var monthNameSuffixes = new Array("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");

var shortMonthNames = getStrings("month.short.", monthNameSuffixes);
function getShortMonthName(month) {
  return shortMonthNames[month];
}

var longMonthNames = getStrings("month.long.", monthNameSuffixes);
function getLongMonthName(month) {
  return longMonthNames[month];
}
