function validDate (field)
{
  validPattern = /^\d{1,2}\/\d{1,2}\/\d{4}/;
  
  if (field == undefined)
    return false
  else if (!validPattern.test(field.value))
    return false
  else
  {
    var arDate = field.value.split("/");
    
    month = arDate[0];
    day = arDate[1];
    year = arDate[2];
    
    if ((month <= 0) || (month > 12))
      return false;
      
    days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    
    if (year % 4 == 0)
      days[1] = 29;
      
    return ((day >= 1) && (day <= days[month - 1]));
  }
}

function requiredColors(commentsForm)
{
  var myDocument = document;
  
  switch (commentsForm.CategoryID.value)
  {
    case ("0")  : commentsForm.TheaterNumber.disabled = true;
                  commentsForm.FilmID.disabled = true;
                  commentsForm.VisitDate.disabled = true;
                  commentsForm.FirstName.disabled = true;
                  commentsForm.LastName.disabled = true;
                  commentsForm.Address1.disabled = true;
                  commentsForm.Address2.disabled = true;
                  commentsForm.City.disabled = true;
                  commentsForm.State.disabled = true;
                  commentsForm.ZIPCode.disabled = true;
                  commentsForm.Phone.disabled = true;
                  commentsForm.EMailAddress.disabled = true;
                  commentsForm.Comment.disabled = true;
                  commentsForm.Action.disabled = true;
                  
                  myDocument.getElementById("lblTheater").style.color = "gray";
                  myDocument.getElementById("lblMovie").style.color = "gray";
                  myDocument.getElementById("lblShowDate").style.color = "gray";
                  myDocument.getElementById("lblFirstName").style.color = "gray";
                  myDocument.getElementById("lblLastName").style.color = "gray";
                  myDocument.getElementById("lblAddress1").style.color = "gray";
                  myDocument.getElementById("lblAddress2").style.color = "gray";
                  myDocument.getElementById("lblCity").style.color = "gray";
                  myDocument.getElementById("lblState").style.color = "gray";
                  myDocument.getElementById("lblZIPCode").style.color = "gray";
                  myDocument.getElementById("lblPhone").style.color = "gray";
                  myDocument.getElementById("lblEMailAddress").style.color = "gray";
                  myDocument.getElementById("lblComment").style.color = "gray";
                  
                  break;
                  
    case ("2")  : enableComponents(commentsForm);
                  commentsForm.TheaterNumber.disabled = false;
                  commentsForm.TheaterNumber.focus();
                  myDocument.getElementById("lblTheater").style.color = "red";

                  commentsForm.FilmID.disabled = true;
                  myDocument.getElementById("lblMovie").style.color = "gray";
                  commentsForm.VisitDate.disabled = true;
                  myDocument.getElementById("lblShowDate").style.color = "gray";
                 
                  commentsForm.FilmID.value = 0;
                  commentsForm.VisitDate.value = "";
                  
                  break;
                  
    case ("3")  :
    case ("12") : enableComponents(commentsForm);
                  commentsForm.TheaterNumber.disabled = false;
                  commentsForm.FilmID.disabled = false;
                  commentsForm.VisitDate.disabled = false;
                  commentsForm.TheaterNumber.focus();

                  myDocument.getElementById("lblTheater").style.color = "red";
                  myDocument.getElementById("lblMovie").style.color = "red";
                  myDocument.getElementById("lblShowDate").style.color = "red";
                  
                  break;

    case ("8")  : location.href = "Content.aspx?PageName=MediaFilmInquiries&PageTitle=Media Inquiries - Films&AboutButtons=1";
                  break;
    
    case ("9")  : location.href = "Content.aspx?PageName=MediaTheaterInquiries&PageTitle=Media Inquiries - Theatres&AboutButtons=1";
                  break;
    
 //   case ("10") : location.href = "Content.aspx?PageName=InTheaterAdvertising&PageTitle=In Theatre Advertising&AboutButtons=1";
 //                 break;
    
    case ("11") : location.href = "Content.aspx?PageName=DonationRequests&PageTitle=Donation Requests&AboutButtons=1";
                  break;
                
    default     : enableComponents(commentsForm);
                  commentsForm.TheaterNumber.disabled = true;
                  commentsForm.FilmID.disabled = true;
                  commentsForm.VisitDate.disabled = true;
                 
                  myDocument.getElementById("lblTheater").style.color = "gray";
                  myDocument.getElementById("lblMovie").style.color = "gray";
                  myDocument.getElementById("lblShowDate").style.color = "gray";

                  commentsForm.TheaterNumber.value = 0;
                  commentsForm.FilmID.value = 0;
                  commentsForm.VisitDate.value = "";
                
                  commentsForm.FirstName.focus();
                  commentsForm.FirstName.select();
  }
}

function enableComponents(commentsForm)
{
  var myDocument = document;
  
  commentsForm.FirstName.disabled = false;
  commentsForm.LastName.disabled = false;
  commentsForm.Address1.disabled = false;
  commentsForm.Address2.disabled = false;
  commentsForm.City.disabled = false;
  commentsForm.State.disabled = false;
  commentsForm.ZIPCode.disabled = false;
  commentsForm.Phone.disabled = false;
  commentsForm.EMailAddress.disabled = false;
  commentsForm.Comment.disabled = false;
  commentsForm.Action.disabled = false;

  myDocument.getElementById("lblFirstName").style.color = "red";
  myDocument.getElementById("lblLastName").style.color = "red";
  myDocument.getElementById("lblAddress1").style.color = "red";
  myDocument.getElementById("lblAddress2").style.color = "black";
  myDocument.getElementById("lblCity").style.color = "red";
  myDocument.getElementById("lblState").style.color = "red";
  myDocument.getElementById("lblZIPCode").style.color = "red";
  myDocument.getElementById("lblPhone").style.color = "red";
  myDocument.getElementById("lblEMailAddress").style.color = "red";
  myDocument.getElementById("lblComment").style.color = "red";
}

function validateForm(commentsForm)
{
  var regZIPCode = new RegExp("^[0-9]{5}");
  var regShowDate = new RegExp("^([0-1]{1}[0-2]{1}|[1-9]{1})\/([0-3]{1}[0-9]{1}|[1-9]{1})\/[0-9]{4}", "i");
  var regEMail = new RegExp("^[A-Z0-9\-\_\.]+\@[A-Z0-9\-\_]+\.[A-Z0-9\-\_]{1,}", "i");

  switch (commentsForm.CategoryID.value)
  {
    case ("0")  : {
                    alert("Please select a Regarding category.");
                    commentsForm.CategoryID.focus();
                    return false;
                  }
                  
    case ("3")  :
    case ("12") : if (commentsForm.FilmID.value == "0")
                  {
                    alert("Please select a movie.");
                    commentsForm.FilmID.focus();
                    return false;
                  }
                  
                  if (!validDate(commentsForm.VisitDate))
                  {
                    alert("Please enter the show date and time in the format \"mm/dd/yyyy\"");
                    commentsForm.VisitDate.focus();
                    commentsForm.VisitDate.select();
                    return false;
                  }
                  
    case ("2")  : if (commentsForm.TheaterNumber.value == "0")
                  {
                    alert("Please select a theatre.");
                    commentsForm.TheaterNumber.focus();
                    return false;
                  }
                
                
  }
  
  if (commentsForm.FirstName.value == "")
  {
    alert("Please enter your first name.");
    commentsForm.FirstName.focus();
    commentsForm.FirstName.select();
    return false;
  }
  
  if (commentsForm.LastName.value == "")
  {
    alert("Please enter your last name.");
    commentsForm.LastName.focus();
    commentsForm.LastName.select();
    return false;
  }
  
  if (commentsForm.Address1.value == "")
  {
    alert("Please enter your street address.");
    commentsForm.Address1.focus();
    commentsForm.Address1.select();
    return false;
  }
  
  if (commentsForm.City.value == "")
  {
    alert("Please enter your city.");
    commentsForm.City.focus();
    commentsForm.City.select();
    return false;
  }
  
  if (!regZIPCode.test(commentsForm.ZIPCode.value))
  {
    alert("Please enter either your ZIP Code.");
    commentsForm.ZIPCode.focus();
    commentsForm.ZIPCode.select();
    return false;
  }
  
  if (commentsForm.Phone.value == "")
  {
    alert("Please enter your phone number.");
    commentsForm.Phone.focus();
    commentsForm.Phone.select();
    return false;
  }
  
  if (!regEMail.test(commentsForm.EMailAddress.value))
  {
    alert("\"" + commentsForm.EMailAddress.value + "\" is not a valid e-mail address.");
    commentsForm.EMailAddress.focus();
    commentsForm.EMailAddress.select();
    return false;
  }
  
  if (commentsForm.Comment.value == "")
  {
    alert("Please enter your comments.");
    commentsForm.Comment.focus();
    commentsForm.Comment.select();
    return false;
  }
  
  return true;
}

function selectState(commentsForm)
{
  var theaterLocation = commentsForm.TheaterNumber.options[commentsForm.TheaterNumber.selectedIndex].text;
  var iIndex = theaterLocation.indexOf(",")
  var stateCode = theaterLocation.substring(iIndex + 2, iIndex + 4);
  
  for (iIndex = 0; iIndex < commentsForm.State.length; iIndex++)
  {
    if (commentsForm.State.options[iIndex].value == stateCode)
    {
      commentsForm.State.selectedIndex = iIndex;
      break;
    }
  }
}
