//
// Maintenance History
//
// 07/08/2004 (LaSalle) - Added openHerbalGram function.
// 09/01/2003 (LaSalle) - Moved function asc() from Registration.js.
//
function SearchSubmitCheck() {
  var FormOK = false;
  if (QuickSearchForm.criteria.value != "") {
    FormOK = true;
  }
  if (FormOK == false) {
    alert("Please fill in the search field.");
    QuickSearchForm.criteria.focus();
    return false;
  } else {
    return true;
  }
}

function datetime() {
  var now = new Date();
  var nowAMPM;
  var arrMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  var arrDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
  var nowHours = now.getHours();
  var nowMinutes = now.getMinutes();
  
  if (nowHours > 11) {
    nowAMPM = "PM";
  } else {
    nowAMPM = "AM";
  }
  if (nowHours == 0) {
    nowHours = 12;
  }
  if (nowHours > 12) {
    nowHours = nowHours - 12;
  }
  if (nowMinutes < 10) {
    nowMinutes = "0" + nowMinutes;
  }
  var result = ""
  result += arrDays[now.getDay()] + ", "
  result += arrMonths[now.getMonth()] + " ";
  result += now.getDate() + ", ";
  result += now.getFullYear() + " ";
  //result += nowHours + ":";
  //result += nowMinutes + " ";
  //result += nowAMPM;
  return result;
}

function openNewWin(strPage,intWidth,intHeight) {
//
// Opens a new window.
//
  var winWidth = (screen.width - intWidth)/2;
  var winHeight = (screen.height - intHeight)/2;
    
  parent.window.open(strPage, 'EmersonPopUp', 'scrollbars,resizable,top=' + winHeight + ',left=' + winWidth + ',width=' + intWidth + ',height=' + intHeight);
}

function IsDateValidOld(form,element) {
  //variable that is used to match the separator.
  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

  //variable to store form value
  var Temp = form.elements[element];
  var strDate = Temp.value;
    
  // is the format ok?
  var matchArray = strDate.match(datePat);
  if (matchArray == null) {
    alert(form.elements[element].name + " Date is invalid!")
    form.elements[element].select();
      return false;
  }
  month = matchArray[1]; // parse date into variables
  day = matchArray[3];
  year = matchArray[4];
  // check month range
  if (month < 1 || month > 12) {
    alert(form.elements[element].name + " Month must be between 1 and 12!");
    form.elements[element].select();
    return false;
  }
  if (day < 1 || day > 31) {
    alert(form.elements[element].name + " Day must be between 1 and 31!");
    form.elements[element].select();
    return false;
  }
  // check for months 4,6,9,11. they can't have 31 days.
  if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
    alert("Month " + month + " doesn't have 31 days!");
    form.elements[element].select();
    return false;
  }
  // check for february 29th / leap year check
  if (month == 2) {
    var isLeap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day == 29 && !isLeap)) {
      alert("February " + year + " doesn't have " + day + " days!");
      form.elements[element].select();
      return false;
    }
  }
/*
  if (year.length < 4) {
    alert(form.elements[element].name + " Year must be in YYYY format!");
    form.elements[element].select();
    return false;
  }
*/
  return true;
}

function isDateValid(dteValue) {
  // variable that is used to match the separator.
  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

  // variable to store form value
  //var Temp = form.elements[element];
  //var strDate = Temp.value;
    
  // is the format ok?
  var matchArray = dteValue.match(datePat);
  if (matchArray == null) {
    return false;
  }
  month = matchArray[1]; // parse date into variables
  day = matchArray[3];
  year = matchArray[4];
  
  // check month range
  if (month < 1 || month > 12) {
    return false;
  }
  if (day < 1 || day > 31) {
    return false;
  }
  // check for months 4,6,9,11. they can't have 31 days.
  if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
    return false;
  }
  // check for february 29th / leap year check
  if (month == 2) {
    var isLeap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day == 29 && !isLeap)) {
      return false;
    }
  }
/*
  if (year.length < 4) {
    return false;
  }
*/
  return true;
}

function asc(achar) {
  var csstr;
  csstr = ' !"#$%&\'()*+,-./0123456789:;<=>?@'
  csstr += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  csstr += '[\]^_`abcdefghijklmnopqrstuvwzyx'
  csstr += '{|}~'
    
  for (i = 0; i < csstr.length; i++) {
    if (achar == csstr.substring(i,i+1)) {
      break;
    }
  }
  return i + 32;
}

function setFocus() {
  var allForms = document.forms;
  var found = false;
  
  for (var i = 0; i <= allForms.length && !found; i++) {
    var currentForm = allForms[i];
    if (currentForm) {
      var fields = currentForm.all;
    
      for (var j = 0; j <= fields.length && !found; j++) {
        var field = fields.item(j);
        if (((field.type == "text") || 
            (field.type == "textarea") || 
            (field.type == "select-one") || 
            (field.type == "password")) && (field.disabled != true)) {
          field.focus();
          found = true;
        }
      }
    }
  }
}

//
//
//
function openHerbalGram() {
  parent.window.open('http://www.herbalgram.org/emersonecologics/herbalgram/default.asp', 'HerbalGram', 'scrollbars,resizable,width=800,height=500');
}
