function Help() {
  var leftLocation = window.screen.width-720;
  var page = '../help/index.htm';
  HelpWin = window.open(page, 'HelpWin', 'width=700,height=500,toolbar=yes,location=no,menubar=no,scrollbars=no,resizable=yes,top=30,left='+leftLocation)
  setTimeout('HelpWin.focus()', 50);
}

function SelectDate(evt, input) {
  offsetY = 200;
  if(evt.x) {
    x = evt.x;
    y = evt.y-offsetY;
  }
  if (evt.PageX) {
    x = evt.PageX;
    y = evt.PageY-offsetY;
  }
  SD = window.open('pickdate.asp?input='+input, 'SD', 'width=220,height=250,toolbar=no,location=no,menubar=no,scrollbars=no,resizable=no,top='+y+',left='+x)
  setTimeout('SD.focus()', 50);
}

function PopUp(helpPage) {
  var leftLocation = window.screen.width-600;
  PopUpWin1 = window.open(helpPage, 'PopUpWin1', 'width=550,height=400,toolbar=yes,location=no,menubar=no,scrollbars=yes,resizable=yes,top=150,left='+leftLocation)
  setTimeout('PopUpWin1.focus()', 50);
}

function NewPopUp(Page, top) {
  var leftLocation = window.screen.width-600;
  PopUpWin2 = window.open(Page, 'PopUpWin2', 'width=550,height=600,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes,top=100,left='+leftLocation)
  setTimeout('PopUpWin2.focus()', 50);
}

function PopAPT(Page, top) {
  var leftLocation = window.screen.width-600;
  PopUpWin2 = window.open('../' + Page, 'PopUpWin2', 'width=550,height=600,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes,top=100,left='+leftLocation)
  setTimeout('PopUpWin2.focus()', 50);
}

function ClearIt(form) {
  var m = 0;
  for (var i=0; i<form.length; i++) {
    // clear all input text box, set all value to empty
    if (form.elements[i].type == 'text')
      form.elements[i].value = ''; 
    // reset radio button, see explanation below
    else if (form.elements[i].type == 'radio') {
      // this script won't work if you have two set of
      // radio button in your form
      // find the first rodio button
      // set its first element to true and
      // all other elements to false
      // if there are more than one set of radio button
      // then this code will set the first element of
      // second set of radio button to false too
      // which is not what it should be
      m++;
      if (m == 1) // first element, set to true
        form.elements[i].checked = true;
      else       // not a first one, set to false
        form.elements[i].checked = false;
    }
    // clear all dropdown box
    // reset to first item
    else if (form.elements[i].type == 'select-one')
      form.elements[i].selectedIndex = 0;
  }
  form.submit();
}

function getFront(mainStr, searchStr) {
  foundOffset = mainStr.indexOf(searchStr);
  if (foundOffset == -1)
    return null;
  return mainStr.substring(0,foundOffset);
}

function getEnd(mainStr, searchStr) {
  foundOffset = mainStr.indexOf(searchStr);
  if (foundOffset == -1)
    return null;
  return mainStr.substring(foundOffset+searchStr.length, mainStr.length);
}

function replaceString(mainStr, searchStr, replaceStr) {
  var front = getFront(mainStr, searchStr);
  var end = getEnd(mainStr, searchStr);
  if (front != null && end != null)
    return front + replaceStr + end;
  return null;
}

function isDate(inputStr) {
  while (inputStr.indexOf("-") != -1) {
    // both - and / are ok
    // replace - with /
    inputStr = replaceString(inputStr, "-", "/");
  }
  var delim1 = inputStr.indexOf("/");
  var delim2 = inputStr.lastIndexOf("/");
  if (delim1 == delim2)
    return false;
  else {
    var mm = parseInt(inputStr.substring(0,delim1), 10);
    var dd = parseInt(inputStr.substring(delim1+1,delim2), 10);
    var yy = parseInt(inputStr.substring(delim2+1,inputStr.length), 10);
    if(isNaN(mm)||isNaN(dd)||isNaN(yy))
      return false;
    else if(mm < 1 || mm > 12 || dd < 1 || dd > 31 || yy < 1900 || yy > 2500)
      return false;
    else
      return true;
  }
}

function GoSearch(form) {
  if (form.valid_date) { // test to see if valid_date is one of the form element
    if (form.valid_date.value.length != 0 && !isDate(form.valid_date.value)) {
      alert("The Date entry is not in an acceptable format.\nYou can enter date in the following format: mm/dd/yyyy, or mm-dd-yyyy.\nMonths must be entered between the range of 01 and 12.\nDays must be entered between the range of 01 and a maximum of 31.\nYears must be entered between the range of 1900 and 2500.");
      form.valid_date.focus();
      form.valid_date.select();
      return false;
    }
  }
  if (form.date_from) { // test to see if valid_date is one of the form element
    if (form.date_from.value.length != 0 && !isDate(form.date_from.value)) {
      alert("The Date entry is not in an acceptable format.\nYou can enter date in the following format: mm/dd/yyyy, or mm-dd-yyyy.\nMonths must be entered between the range of 01 and 12.\nDays must be entered between the range of 01 and a maximum of 31.\nYears must be entered between the range of 1900 and 2500.");
      form.date_from.focus();
      form.date_from.select();
      return false;
    }
  }
  if (form.date_to) { // test to see if date_to is one of the form element
    if (form.date_to.value.length != 0 && !isDate(form.date_to.value)) {
      alert("The Date entry is not in an acceptable format.\nYou can enter date in the following format: mm/dd/yyyy, or mm-dd-yyyy.\nMonths must be entered between the range of 01 and 12.\nDays must be entered between the range of 01 and a maximum of 31.\nYears must be entered between the range of 1900 and 2500.");
      form.date_to.focus();
      form.date_to.select();
      return false;
    }
  }
  if (form.apc_rate1) { // test to see if apc_rate if one of the form element
    if (isNaN(form.apc_rate1.value)) {
      alert("The APC Rate entry must be a number.");
      form.apc_rate1.focus();
      form.apc_rate1.select();
      return false;
    }
    else if (isNaN(form.apc_rate2.value)) {
      alert("The APC Rate entry must be a number.");
      form.apc_rate2.focus();
      form.apc_rate2.select();
      return false;
    }
  }
  if (form.apc && form.apc.value != "") // if apc length < 5, left fill with 0's
  {
    while (form.apc.value.length < 5)
      form.apc.value = "0" + form.apc.value;
  }
  if (form.revcode && form.revcode.value != "") // if revcode length < 4, left fill with 0's
  {
  	var idx = (event.srcElement != null && event.srcElement.id == "revcodehcpcs_search") ? 3 : 4;
    while (form.revcode.value.length < idx)
      form.revcode.value = "0" + form.revcode.value;	
  }
  if (form.edit && form.edit.value != "") // if edit length < 3, left fill with 0's
  {
    while (form.edit.value.length < 3)
      form.edit.value = "0" + form.edit.value;	
  }
  for (var i=1; i<6; i++)
  {
    if (document.getElementById("apc"+i) && document.getElementById("apc"+i).value != "")
      while (document.getElementById("apc"+i).value.length < 5)	
        document.getElementById("apc"+i).value = "0" + document.getElementById("apc"+i).value; 
  }
  // SQL Server does not single quote
  // and HTML does not like double quote
  // so we don't accept both of them in our search string
  for (var i=0; i<form.length; i++) {
    if (form.elements[i].type == 'text' && (form.elements[i].value.indexOf("'") != -1 || form.elements[i].value.indexOf("\"") != -1)) {
      alert("Please do not include single quote(') or double quote (\") in your query string, thanks.");
      form.elements[i].focus();
      form.elements[i].select();
      return false;
    }
  }
  // if it comes down to here, everything is ok, submit the form
  //form.submit();
  // in order to distinguish whether it is a search submit
  // or a full_desc toggle, we add a hidden field.
  form.curpage.value = 1;
  return true;
}