// shows new div when submit is clicked
function showProgressBar() {
    if (document.getElementById('displayInProgress') != null) {
       document.getElementById('displayInProgress').style.top = document.body.scrollTop + 150;
       document.getElementById('displayInProgress').style.visibility = 'visible';
    }
}

function disableButtons() {
    if (document.getElementById('submitform') != null) {
       document.getElementById('submitform').value = "Please Wait.....";
       document.getElementById('submitform').disabled = true;
    }

    if (document.getElementById('resetform') != null) {
       document.getElementById('resetform').disabled = true;
    }

    showProgressBar();
}

// focus on the first text element
function focus1() {
    var inputs = document.getElementsByTagName('input');
    for (var k=0; k<inputs.length; k++)
    {
        if (inputs[k].type == 'text')
        {
                inputs[k].focus();
                return true;
        }
    }
}
// Used on signup to show which template is selected
function select_tem(template)
{
    var id = "ras";
    window.location.href="http://devntpa.netopia.com/ntpa/details.html?template=" + template + "&resell=" + id;	
}

// Shows the instructions for putting a nxg template on a page
function select(id)
{
        window.location.href="/ntpa/latest_support/location.html?template=" + id
}

// validation code used on Lookup Account
function check_status(){
  if (document.forms[0].office_status.checked)
     document.forms[0].office_status.value = "ACTCLS";
}

function set_status(){
  document.forms[0].office_status.value = "ACTCLS";
}

function validate(){
  check_status();
  var m = /^\*/;
  if (document.forms[0].key_value.value.match(m) != null)
  {
    alert("Invalid Entry");
    document.forms[0].key_value.focus();
    return false;
  }
  else if (document.forms[0].key_value.value == "")
  {
    alert('You must enter a "Search For" value');
    document.forms[0].key_value.focus();
    return false;
  }
  else
  {
    for(var i = 0; i < document.forms[0].key_fld.length; i++) {
      if (document.forms[0].key_fld[i].checked)
        document.forms[0].key_fld.value = document.forms[0].key_fld[i].value;
    }
    for(var i = 0; i < 3; i++) {
      if (document.forms[0].sort_by[i].checked)
        document.forms[0].sort_by.value = document.forms[0].sort_by[i].value;
    }
    for(var i = 0; i < 2; i++) {
      if (document.forms[0].match_type[i].checked)
        document.forms[0].match_type.value = document.forms[0].match_type[i].value;
    }
    disableButtons();
    return true;
  }
}

// Validate Fields on History view page
function hist_validate() {
  // Account Name is mandatory
  if (document.getElementById('member_name').value == "") {
     alert("You must enter a value for Account Name");
     document.getElementById('member_name').focus();

     return false;
  }

  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
  
  // Start and End dates must be mm/dd/yy if entered.
  if (document.getElementById('start_date').value != "") {
     var matchArray = document.getElementById('start_date').value.match(datePat); // is the format ok?
     if (matchArray == null) {
        alert("Start Date is not in a valid format. It should be in the format MM/DD/YY")
        document.getElementById('start_date').value="";
        document.getElementById('start_date').focus();
        return false;
     }
  }

  if (document.getElementById('end_date').value != "") {
     var matchArray = document.getElementById('end_date').value.match(datePat); // is the format ok?
     if (matchArray == null) {
        alert("End Date is not in a valid format. It should be in the format MM/DD/YY")
        document.getElementById('end_date').value="";
        document.getElementById('end_date').focus();
        return false;
     }
  }

  disableButtons();
  return true;  // date is valid
}
 
// Used on update member record
function validate_memberupdate_step2(){

  var valid;
  var myForm = document.forms[0];

  valid = lengthValidater(myForm.email, "Email", 0);
  if (!(valid)) {
     return false;
  }
  valid = lengthValidater(myForm.first, "First Name", 0);
  if (!(valid)) {
     return false;
  }
  valid = lengthValidater(myForm.last, "Last Name", 0);
  if (!(valid)) {
     return false;
  }
  valid = lengthValidater(myForm.phone, "Phone", 9);
  if (!(valid)) {
     return false;
  }
 
  myForm.submit();
  return true;
}

function lengthValidater(formElement, elementName, elLength)
{
   if (formElement.value.length <= elLength)
   {
      alert("The " + elementName + " must have at least " + (elLength + 1) + " character(s). Please fix the value.");
      formElement.focus();
      return false;
   }
   else
   {
      return true;
   }
}
