//////////////////////////////////////////////////////////////////////
// icsEvent.js
//////////////////////////////////////////////////////////////////////

function isRequiredOKLocal(thisForm) {
// Return true if all of the fields have permissible values.
// Raise an alert with corrective action otherwise.
  theResult = true;
  if (selectValue(thisForm.startMonth) == "---"
      || selectValue(thisForm.endMonth) == "---"
      || selectValue(thisForm.startDayOfMonth) == "--"
      || selectValue(thisForm.endDayOfMonth) == "--") {
    theResult = false;
    alert("Please enter both the start date and end date and resubmit.")
    }
  else {
    var yyyy = parseInt(selectValue(thisForm.startYear), 10);
    var mmm = selectValue(thisForm.startMonth);
    var dd = parseInt(selectValue(thisForm.startDayOfMonth), 10);
    if (! isMonthLengthOK(yyyy, mmm, dd))
      theResult = false;
    yyyy = parseInt(selectValue(thisForm.endYear), 10);
    mmm = selectValue(thisForm.endMonth);
    dd = parseInt(selectValue(thisForm.endDayOfMonth), 10);
    if (! isMonthLengthOK(yyyy, mmm, dd))
      theResult = false;
    }
  return theResult;
}

function entry(thisForm) {
  var space = " ";
  var endOfLine = "\r\n";
  var theResult = "";
  var i = 0;
  var thisDate = eventDates(thisForm);
  var thisCity = textValue(thisForm.city);
  var thisRegion = "(" + selectValue(thisForm.region) + ")";
  var thisEvent = textValue(thisForm.eventTitle);
  var thisVenueName = textValue(thisForm.venueName);
  var thisVenueAddress = textValue(thisForm.venueAddress);
  var thisTimes = sentence("", textValue(thisForm.times));
  var thisGroup = sentence("Sponsored by", textValue(thisForm.group));
  var thisTeacher = sentence("Teachers are", textValue(thisForm.teacher));
  var thisMusic = sentence("Music by", textValue(thisForm.music));
  var thisDress = sentence(textValue(thisForm.dress), " attire");
  var thisRefreshment = sentence("Refreshments:", textValue(thisForm.refreshments));
  var thisInformation = textValue(thisForm.extraInformation);
  var thisPrice = sentence("", price(thisForm));
  var thisContact = sentence("Contact", contacts(thisForm));
  var thisWebpage = textValue(thisForm.webPage);
  if (thisVenueName) {
    thisVenueName = " at " + thisVenueName;
  }
  if (thisVenueAddress) {
    thisVenueAddress = ", " + thisVenueAddress;
  }
  theResult = thisDate + endOfLine
            + thisCity + space
            + thisRegion + space
            + thisEvent
            + thisVenueName
            + thisVenueAddress
            + ". "
            + thisTimes
            + thisGroup
            + thisTeacher
            + thisMusic
            + thisDress
            + thisRefreshment
            + thisPrice
            + thisInformation
            + thisContact
            + thisWebpage;
  //alert(theResult);
  return theResult;
}

function sentence(str, text) {
  var theResult = "";
  if (text != "")
    theResult = str + " " + text + ". ";
  return theResult;
}

function price(thisForm) {
  var theResult = "";
  var i = 0;
  var priceUS = textValue(thisForm.priceUS);
  var priceCA = textValue(thisForm.priceCA);
  if (priceCA != "")
    this[i++] = "Cdn$" + priceCA;
  if (priceUS != "")
    this[i++] = "US$" + priceUS;
  if (0 < i) theResult = this[0];
  if (1 < i) theResult += " " + this[1];
  return theResult;
}

function makeArray()    {
  for (i = 0; i<makeArray.arguments.length; i++)
    this[i] = makeArray.arguments[i];
  this.length = makeArray.arguments.length;
  return this;
 }

var daysOfWeek = new makeArray('Sun',
                               'Mon',
                               'Tue',
                               'Wed',
                               'Thu',
                               'Fri',
                               'Sat');

function isLeapYear(yyyy) {
// Return true if yyyy is a leap year.
  if (yyyy % 400 == 0) return true;
  if (yyyy % 100 == 0) return false;
  if (yyyy % 4 == 0) return true;
  return false;
  }

function isMonthLengthOK(yyyy, mmm, dd) {
// Return true if the day number is valid within the month.
  if (mmm == 'Feb') {
    if (isLeapYear(yyyy) && dd > 29) {
      alert("Feb of " + yyyy + " has only 29 days.")
      return false
    } else if (dd > 28) {
      alert("Feb of " + yyyy + " has only 28 days.")
      return false
      }
    }
  else if ((mmm == 'Apr' || mmm == 'Jun' || mmm == 'Sep' || mmm == 'Nov') && dd > 30) {
    alert(mmm + " has only 30 days.")
    return false
  } else if (dd > 31) {
    alert(mmm + " has only 31 days.")
    return false
    }
  return true
  }

function eventDates(thisForm) {
  var space = " ";
  var startDate = "";
  var endDate = "";
  var theResult = "";
  
  var startYear = selectValue(thisForm.startYear);
  var startMonth = selectValue(thisForm.startMonth);
  var startDayOfMonth = selectValue(thisForm.startDayOfMonth);

  var endYear = selectValue(thisForm.endYear);
  var endMonth = selectValue(thisForm.endMonth);
  if (endMonth == thisForm.endMonth.options[0].text) endMonth = startMonth;
  var endDayOfMonth = selectValue(thisForm.endDayOfMonth);
  if (endDayOfMonth == thisForm.endDayOfMonth.options[0].text) endDayOfMonth = startDayOfMonth;

  var startDay = daysOfWeek[(new Date(startMonth + " " + startDayOfMonth + ", " + startYear)).getDay()];
  var endDay = daysOfWeek[(new Date(endMonth + " " + endDayOfMonth + ", " + endYear)).getDay()];

  if (startYear == endYear) {
    if (startMonth == endMonth) {
      if (startDayOfMonth == endDayOfMonth) { // Dates are equal
        endDate = endMonth + space + endDayOfMonth + space + endYear;
      }
      else { // Only day of month differs
        startDate = startMonth + space + startDayOfMonth;
        endDate = endDayOfMonth + space + endYear;
      }
    }
    else { // Months differ
      startDate = startMonth + space + startDayOfMonth;
      endDate = endMonth + space + endDayOfMonth + space + endYear;
    }
  }
  else { // Years differ
    startDate = startMonth + space + startDayOfMonth + space + startYear;
    endDate = endMonth + space + endDayOfMonth + space + endYear;
  }
  
  if (startDate != "")
    theResult = startDay + "-" + endDay + ", " + startDate + "-" + endDate;
  else
    theResult = endDay + ", " + endDate;

  return theResult;
}

function contact1(thisForm) {
// Return a string representing the first contact.
// Requires the TEXT fields:
//     contact1Name,
//     contact1AreaCode, contact1PhoneNumber,
//     contact1FaxAreaCode, contact1FaxNumber,
//     contact1Email
  theResult = "";
  theResult = textValue(thisForm.contact1Name);
  if (textValue(thisForm.contact1PhoneNumber) != "")
    theResult += " "
      + textValue(thisForm.contact1PhoneAreaCode)
      + "-"
      + textValue(thisForm.contact1PhoneNumber);
  if (textValue(thisForm.contact1FaxNumber) != "")
    theResult += " "
      + textValue(thisForm.contact1FaxAreaCode)
      + "-"
      + textValue(thisForm.contact1FaxNumber)
      + "(fax)";
  if (textValue(thisForm.contact1Email) != "")
    theResult += " "
      + textValue(thisForm.contact1Email);
  return theResult;
}
function contact2(thisForm) {
// Return a string representing the first contact.
// Requires the TEXT fields:
//     contact2Name,
//     contact2AreaCode, contact2PhoneNumber,
//     contact2FaxAreaCode, contact2FaxNumber,
//     contact2Email
  theResult = "";
  theResult = textValue(thisForm.contact2Name);
  if (textValue(thisForm.contact2PhoneNumber) != "")
    theResult += " "
      + textValue(thisForm.contact2PhoneAreaCode)
      + "-"
      + textValue(thisForm.contact2PhoneNumber);
  if (textValue(thisForm.contact2FaxNumber) != "")
    theResult += " "
      + textValue(thisForm.contact2FaxAreaCode)
      + "-"
      + textValue(thisForm.contact2FaxNumber)
      + "(fax)";
  if (textValue(thisForm.contact2Email) != "")
    theResult += " "
      + textValue(thisForm.contact2Email);
  return theResult;
}
function contacts(thisForm) {
// Return a string representing the (two) contact(s)
  var theResult = contact1(thisForm);
  var secondContact = contact2(thisForm);
  if (secondContact != "")
    theResult += " or " + secondContact;
  return theResult; 
}
