//-------------------------------------------------------------------------
// Validation.js - validation functions for the PRS application form
//-------------------------------------------------------------------------

//
// *** NOTE: Validation is limited, since the data gets reviewed and 
//           entered into the PRS by NLM admin.
//

//
// Main validation function
//

function validateApplication(parms)
{
  var adminLabel="Administrator";
  if (parms.indivAccount && parms.indivAccount.value == "true")
  {
    adminLabel="Investigator";
  }

  // Check terms and conditions (Accept) radio button must be checked
  if (! parms.Accept[0].checked)
  {
    alert("Terms and conditions must be accepted, for the application to be processed.");
    return false;
  }

  // NOTE: Navigator cannot handle parms.OrgType.value, so we have to do it the hard way...
  var orgType;
  for (var i=0; i<parms.OrgType.options.length; i++)
  {
    if (parms.OrgType.options[i].selected)
      orgType = parms.OrgType.options[i];
  }
  if (orgType.value.search("--")>=0)
  {
    alert("Organization Type must be selected.");
    return false;
  }

  if (isEmpty(parms.Country, "Country"))
    return false;

  if (isEmpty(parms.RegAuthName, "Regulatory Authority"))
    return false;

  if (isEmpty(parms.RegAuthAddress, "Regulatory Authority Address"))
    return false;

  if (isEmpty(parms.OrgName, "Organization Name"))
    return false;

  if (isEmpty(parms.OrgAddress, "Organization Address"))
    return false;

  if (isEmpty(parms.OfficialRep, "Official Representative"))
    return false;

  if (isEmpty(parms.OffRepPhone, "Official Representative Phone"))
    return false;
  if (!isValidPhoneNumber(parms.OffRepPhone))
    return false;

  if (isEmpty(parms.OffRepEmail, "Official Representative Email"))
    return false;
  if (!isValidEmail(parms.OffRepEmail))
    return false;

  if (isEmpty(parms.AdminName, adminLabel+" Name"))
    return false;

  if (isEmpty(parms.AdminPhone, adminLabel+" Phone"))
    return false;
  if (!isValidPhoneNumber(parms.AdminPhone))
    return false;

  if (isEmpty(parms.AdminEmail, adminLabel+" Email Address"))
    return false;
  if (!isValidEmail(parms.AdminEmail))
    return false;

  // passed all checks
  return true;
}


//
// Contact request form validation function
//

function validateContactRequest(parms)
{
  // Check terms and conditions (Accept) radio button must be checked
  if (! parms.Accept[0].checked)
  {
    alert("Terms and conditions must be accepted, for the application to be processed.");
    return false;
  }

  if (isEmpty(parms.OrgName, "Organization"))
    return false;

  if (isEmpty(parms.Name, "Requestor Name"))
    return false;

  if (isEmpty(parms.Dept, "Requestor Department/Group"))
    return false;

  if (isEmpty(parms.Phone, "Requestor Phone Number"))
    return false;
  if (!isValidPhoneNumber(parms.Phone))
    return false;

  if (isEmpty(parms.Email, "Requestor Email Address"))
    return false;
  if (!isValidEmail(parms.Email))
    return false;

  // if we reach here, all tests passed
  return true;
}


//
// Utility functions
//

function isEmpty(object, name)
{
    var value = object.value;
    for (var i = 0; i < value.length; i ++)
    {
        var c = value.charAt(i);
        if( (c != ' ') && (c != '\n') && (c != '\t') )
            return false;
    }
    // must be empty...
    object.focus();
    object.select();
    alert(name + " must be entered.");
    return true;
}

function isValidPhoneNumber(object)
{
  // May have foreign numbers some day, so format could vary widely.
  //   - require min of 9 digits [Nepal has 6 digit numbers, plus 3 digit country code...]

  if (object.value.length<9)
  {
    object.focus();
    object.select();
    alert("Please enter a valid phone number, including area code.");
    return false;
  }
  return true;
}


// Following functions courtesy of Commerce One, with tweaks...

function isValidEmail(objField)
/************************************************************
*  Purpose:  Determines if field contains a correctly formatted 
*    e-mail address. Pop up alert box if not valid.
*  Inputs:   objField - HTML form field
*  Outputs:  True if e-mail address is valid.  False, otherwise.
************************************************************/ 
{
    var blnValid  = true;

    // Drop out of function if field has no value
    if (objField.value.length==0) return blnValid;

    blnValid = isValidEmailValue(objField.value, true);

    if (blnValid == false)
    {
        alert('Please enter a valid e-mail address.');
        objField.focus();
        objField.select();
    }

    return blnValid;
}

function isValidEmailValue(strValue, blnRequired)
/************************************************************
*  Purpose:  Determines if field contains a correctly formatted 
*    e-mail address.  [Not perfect, but pretty good...]
*  Inputs:   Value of email
*  Outputs:  True if e-mail address is valid.  False, otherwise.
************************************************************/ 
{
    // Drop out of function if field has no value
    if (strValue.length==0) return !blnRequired;
    
    var intAtSym    = strValue.indexOf('@');
    var intPeriod   = strValue.lastIndexOf('.');
    var intSpace    = strValue.indexOf(' ');
    var intSingleQuote    = strValue.indexOf('\'');
    var intDoubleQuote    = strValue.indexOf('\"');
    var intLength   = strValue.length - 1;   // Array is from 0 to length-1
    
    if ((intAtSym < 1) ||                     // '@' cannot be in first position
        (intPeriod <= intAtSym+1) ||          // Must be atleast one valid char btwn '@' and '.'
        (intPeriod == intLength ) ||          // Must be atleast one valid char after '.'
        // ***** (intSingleQuote  != -1) ||            // No embedded quotes
        (intDoubleQuote  != -1) ||            
        (intSpace  != -1))                    // No internal spaces permitted
    {  
        return false;
    }

    return true;
}

