var position;

function setSrcAndStatus( imgName, URL, statTxt)
{
  document.images[imgName].src = URL;
  window.status = statTxt;
}

function popPrint(URL)
{
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=675,height=600,left=120,top=175');");
}

function popContact(URL)
{
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=800,height=800,left=120,top=175');");
}

function verifyNewsletterForm()
{
  form = document.newsletterForm;
  if ((form.elements["name"].value == "") || (form.elements["email"].value == ""))
  {
    alert("You must enter both your name and your email address.");
    return false;
  }
  else
  {
    email = form.elements["email"];
    x = email.value.split("@");
    if (x.length != 2)
    {
      email.value = "";
      email.focus();
      return false;
    }
    else if (x[0].indexOf(".") != -1)
    {
      email.value = "";
      email.focus();
      return false;
    }
    else if (x[1].indexOf(".") == -1)
    {
      email.value = "";
      email.focus();
      return false;
    }
    else
    {
      form.submit();
    }
  }
}

function resetNewsletterForm()
{
  form = document.newsletterForm;
  form.reset();
}

function checkrequired(which)
{
  var pass = true;

  if (document.images)
  {
    for (i = 0; i < which.length; i++)
    {
      var tempobj = which.elements[i];

      if (tempobj.name.substring(0,8) == "required")
      {
        if (((tempobj.type == "text" || tempobj.type == "textarea") && tempobj.value == '') ||
            (tempobj.type.toString().charAt(0) == "s" && tempobj.selectedIndex == -1))
        {
          pass = false;
          break;
        }
      }
    }
  }
  if (!pass)
  {
    alert("One or more of the required elements are not completed. Please complete them, then submit again!");
    return false;
  }
  else
  {
    return true;
  }
}

function countChoices(obj)
{
  max = 3; // max. number allowed at a time
  // add more checkboxes here
  theForm = obj.form;
  count = 0;
  count += ((theForm.elements["BetterLiving"].checked == true) ? 1 : 0) +
           ((theForm.elements["ItsElementary"].checked == true) ? 1 : 0) +
           ((theForm.elements["ReadingDirections"].checked == true) ? 1 : 0) +
           ((theForm.elements["PrimaryStudents"].checked == true) ? 1 : 0) +
           ((theForm.elements["Garfield"].checked == true) ? 1 : 0) +
           ((theForm.elements["Science"].checked == true) ? 1 : 0) +
           ((theForm.elements["Math"].checked == true) ? 1 : 0) +
           ((theForm.elements["Seasons"].checked == true) ? 1 : 0) +
           ((theForm.elements["BlackHistory"].checked == true) ? 1 : 0) +
           ((theForm.elements["Hispanic"].checked == true) ? 1 : 0) +
           ((theForm.elements["ESL"].checked == true) ? 1 : 0) +
           ((theForm.elements["SpecialEd"].checked == true) ? 1 : 0) +
           ((theForm.elements["AtRisk"].checked == true) ? 1 : 0) +
           ((theForm.elements["LifeSkills"].checked == true) ? 1 : 0) +
           ((theForm.elements["Journalism"].checked == true) ? 1 : 0) +
           ((theForm.elements["Comics"].checked == true) ? 1 : 0) +
           ((theForm.elements["SportsSidelines"].checked == true) ? 1 : 0) +
           ((theForm.elements["Library"].checked == true) ? 1 : 0) +
           ((theForm.elements["Fitness"].checked == true) ? 1 : 0) +
           ((theForm.elements["Sheroes"].checked == true) ? 1 : 0) +
           ((theForm.elements["WriteConnection"].checked == true) ? 1 : 0);
  // If you have more checkboxes you must also add them here
  // add more  (box_ ? 1 : 0)  's separated by '+'

  if (count > max)
  {
    alert("You are only allowed to check off 3 FREE study units. \nUncheck your selection to choose a another one.");
    obj.checked = false;
  }
}

function verifySearchForm()
{
  form = document.searchForm;
  msg = "Start date cannot be later then end date!";
  if (form.start_year.options[form.start_year.selectedIndex].value > form.end_year.options[form.end_year.selectedIndex].value)
  {
    alert(msg);
    return false;
  }
  else if (form.start_year.options[form.start_year.selectedIndex].value == form.end_year.options[form.end_year.selectedIndex].value)
  {
    if (form.start_mon.options[form.start_mon.selectedIndex].value > form.end_mon.options[form.end_mon.selectedIndex].value)
    {
      alert(msg);
      return false;
    }
    else if (form.start_mon.options[form.start_mon.selectedIndex].value == form.end_mon.options[form.end_mon.selectedIndex].value)
    {
      if (form.start_day.options[form.start_day.selectedIndex].value > form.end_day.options[form.end_day.selectedIndex].value)
      {
        alert(msg);
        return false;
      }
    }
  }
  form.submit();
}

function resetSearchForm()
{
  form = document.searchForm;
  form.reset();
}

function DistillNumber(tmpStr, strMask)
{
  strMask = Replace(strMask, '#', '');
  for (position = 0; position < strMask.length++; position++)
  {
    tmpStr = Replace(tmpStr, strMask.substring(position, (position + 1)), '');
  }
  return tmpStr;
}

function LenNumStr(strMask)
{
  tmpStr = strMask;
  for (position = 0; position < (strMask.length + 1); position++)
  {
    if (strMask.charAt(position) != '#')
    {
      tmpStr = Replace(tmpStr, strMask.charAt(position), '');
    }
  }
  return tmpStr.length;
}

function FormatNumber(key, textbox, strMask)
{
  numStr = DistillNumber(textbox.value, strMask);
  if (key == 9)
  {
    return true;
  }
  else if ((key == 8) && (numStr.length != 0))
  {
    numStr = numStr.substring(0, numStr.length - 1);
  }
  else if ((((key > 47) && (key < 58)) || ((key > 95) && (key < 106))) && (numStr.length < LenNumStr(strMask)))
  {
    numStr = numStr + String.fromCharCode(key);
  }

  var formattedStr='';
  for (position = 0; position < strMask.length; position++)
  {
    if (strMask.charAt(position) == '#')
    {
      if (numStr.length != 0)
      {
        formattedStr = formattedStr + numStr.charAt(0);
        numStr = numStr.substring(1, numStr.length);
      }
      else
      {
        formattedStr = formattedStr + "";
      }
    }
    else if (strMask.charAt(position) != '#')
    {
      formattedStr = formattedStr + strMask.charAt(position);
    }
//    tmpStr = Replace(tmpStr, strMask.substring(position, position + 1), '');
  }
  textbox.value = formattedStr;
  return false;
}

function Replace(fullString, text, by)
{
  // Replaces text with by in string
  var strLength = fullString.length;
  var txtLength = text.length;

  if ((strLength == 0) || (txtLength == 0))
  {
    return fullString;
  }

  var i = fullString.indexOf(text);
  if ((!i) && (text != fullString.substring(0, txtLength)))
  {
    return fullString;
  }

  if (i == -1)
  {
    return fullString;
  }

  var newstr = fullString.substring(0, i) + by;

  if ((i + txtLength) < strLength)
  {
    newstr += Replace(fullString.substring(i + txtLength, strLength), text, by);
  }

  return newstr;
}

function verifyEmail(email)
{
  if (email.value != "")
  {
    x = email.value.split("@");
    if (x.length != 2)
    {
      alert("This is not a valid email address.");
      email.value = "";
      email.focus();
      return false;
    }
    else if (x[0].indexOf(".") != -1)
    {
      alert("This is not a valid email address.");
      email.value = "";
      email.focus();
      return false;
    }
    else if (x[1].indexOf(".") == -1)
    {
      alert("This is not a valid email address.");
      email.value = "";
      email.focus();
      return false;
    }
  }
}

function verifyContactForm()
{
  form = document.contactForm;
  if ((form.elements["comments"].value == "") || (form.elements["sub_name"].value == "") ||
      (form.elements["sub_phone"].value == "") || (form.elements["sub_email"].value == ""))
  {
    alert("One or more of the required fields was not entered.");
    return false;
  }
  else
  {
    email = form.elements["sub_email"];
    x = email.value.split("@");
    if (x.length != 2)
    {
      email.value = "";
      email.focus();
      return false;
    }
    else if (x[0].indexOf(".") != -1)
    {
      email.value = "";
      email.focus();
      return false;
    }
    else if (x[1].indexOf(".") == -1)
    {
      email.value = "";
      email.focus();
      return false;
    }
    else
    {
      form.submit();
    }
  }
}

function resetContactForm()
{
  form = document.contactForm;
  form.reset();
}
