
/**
@brief A Javascript for validating request forms
@author Aniko T. Valko, Keymodule Ltd. UK
*/


/**
@brief Checks if all the required fields in the request
form have been filled in properly by the user
*/
function validateRequestForm()
{
  var ok_user_details = validateUserDetails() ;

  //alert("User details: " + ok_user_details) ;

  if (ok_user_details == undefined) ok_user_details = true ;

  var ok_product_details = validateProductDetails() ;

  //alert("Product details: " + ok_product_details) ;

  if (ok_product_details == undefined) ok_product_details = true ;

  return ok_user_details &&
    ok_product_details ;
}


/* Functions to validate user details ---------------------- */

/**
@brief Checks if the user details fields have been
given properly by the user
*/
function validateUserDetails()
{
  var ok_first_name   = validateFirstName() ;
  var ok_family_name  = validateFamilyName() ;
  var ok_job_title    = validateJobTitle() ;
  var ok_organization = validateOrganization() ;
  var ok_email        = validateEmail() ;
  var ok_phone        = validatePhone() ;

  return ok_first_name &&
    ok_family_name &&
    ok_job_title &&
    ok_organization &&
    ok_email &&
    ok_phone ;
}

/**
@brief Checks if the 'First name' field has been
filled in properly by the user
*/
function validateFirstName()
{
  if (!isMandatoryFieldById ("FirstName")) return true ;

  return isFieldFilled ("FirstName") ;
}

/**
@brief Checks if the 'Family name' field has been
filled in properly by the user
*/
function validateFamilyName()
{
  if (!isMandatoryFieldById ("FamilyName")) return true ;

  return isFieldFilled ("FamilyName") ;
}

/**
@brief Checks if the 'Job title' field has been
filled in properly by the user
*/
function validateJobTitle()
{
  if (!isMandatoryFieldById ("JobTitle")) return true ;

  return isFieldFilled ("JobTitle") ;
}

/**
@brief Checks if the 'Organization' field has been
filled in properly by the user
*/
function validateOrganization()
{
  if (!isMandatoryFieldById ("Organization")) return true ;

  return isFieldFilled ("Organization") ;
}

/**
@brief Checks if the 'E-mail' field has been
filled in properly by the user
*/
function validateEmail()
{
  if (!isMandatoryFieldById ("Email")) return true ;

  if (!isFieldFilled ("Email"))
  {
    return false ;
  }

  if (!isEmailCorrect("Email"))
  {
    return false ;
  }

  return isEmailOfficial("Email") ;
}

/**
@brief Checks if the 'Phone' field has been
filled in properly by the user
*/
function validatePhone()
{
  if (!isMandatoryFieldById ("Phone")) return true ;

  return isFieldFilled ("Phone") ;
}


/* Functions to validate product details ---------------------- */

/**
@brief Checks if the product details fields have been
given properly by the user
*/
function validateProductDetails()
{
  var ok_product       = validateProduct() ;
  var ok_platform      = validatePlatform() ;
  var ok_whereheard    = validateWhereHeard() ;
  var ok_user_count    = validateUserCount() ;
  var ok_license_count = validateLicenseCount() ;

  return ok_product &&
    ok_platform &&
    ok_whereheard &&
    ok_user_count &&
    ok_license_count ;
}

/**
@brief Checks if the 'Product' field has been
filled in properly by the user
*/
function validateProduct()
{
  if (!isMandatoryFieldByName ("Product")) return true ;

  return isCheckboxGroupToggled ("Product") ;
}

/**
@brief Checks if the 'Platform' field has been
filled in properly by the user
*/
function validatePlatform()
{
  if (!isMandatoryFieldByName ("Platform")) return true ;

  if (!isCheckboxGroupToggled ("Platform"))
  {
    return false ;
  }

  return isOtherFilled ("Platform") ;
}

/**
@brief Checks if the 'WhereHeard' field has been
filled in properly by the user
*/
function validateWhereHeard()
{
  if (!isMandatoryFieldByName ("WhereHeard")) return true ;

  if (!isCheckboxGroupToggled ("WhereHeard"))
  {
    return false ;
  }

  return isOtherFilled ("WhereHeard") ;
}

/**
@brief Checks if the 'UserCount' field has been
filled in properly by the user
*/
function validateUserCount()
{
  if (!isMandatoryFieldById ("UserCount")) return true ;

  return !isUnspecifiedOptionSelected ("UserCount") ;
}

/**
@brief Checks if the 'LicenseCount' field has been
filled in properly by the user
*/
function validateLicenseCount()
{
  if (!isMandatoryFieldById ("LicenseCount")) return true ;

  return !isUnspecifiedOptionSelected ("LicenseCount") ;
}


/* Functions to check the fields --------------------------- */

/**
@brief Checks if the field specified by an id is mandatory
*/
function isMandatoryFieldById
  (vFieldId)
{
  var field = document.getElementById(vFieldId) ;

  if (field == null)
  {
    return false ;
  }

  return isMandatoryField (field) ;
}

/**
@brief Checks if the field specified by its name is mandatory
*/
function isMandatoryFieldByName
  (vFieldName)
{
  var fields = document.getElementsByName(vFieldName) ;

  if (fields.length == 0)
  {
    return false ;
  }

  return isMandatoryField (fields[0]) ;
}

/**
@brief Checks if the given field is mandatory
*/
function isMandatoryField
   (vField)
{
  var mandatory_value = vField.getAttribute ("mandatory") ;

  if (mandatory_value == null ||
      mandatory_value.length == 0)
  {
    return false ;
  }

  return mandatory_value == "yes" ;
}

/**
@brief Checks if the given field has been filled in by the user
*/
function isFieldFilled
  (vFieldId)
{
  var field = document.getElementById(vFieldId) ;
  var error_message_field =
    document.getElementById("ErrorMessage" + vFieldId) ;

  if (!isEmpty(field))
  {
    hideErrorMessage(error_message_field) ;
    return true ;
  }

  displayErrorMessage
    (error_message_field, "Please fill in this field") ;
  return false ;
}

/**
@brief Checks if the Other text area is proprly filled in if
the 'Other' checkbox is toggled
*/
function isOtherFilled
  (vFieldName)
{
  if (!isLastCheckboxToggled (vFieldName))
    return true ;

  var text_area = document.getElementById(vFieldName + "Other") ;

  var error_message_field =
    document.getElementById("ErrorMessage" + vFieldName) ;

  if (!isEmpty(text_area))
  {
    hideErrorMessage(error_message_field) ;
    return true ;
  }

  displayErrorMessage
    (error_message_field, "Please specify your other option") ;
  return false ;
}

/**
@brief Checks if the given email address has the correct form
*/
function isEmailCorrect
   (vFieldId)
{
  var field = document.getElementById(vFieldId) ;
  var error_message_field =
    document.getElementById("ErrorMessage" + vFieldId) ;

  var email_pattern = /^(?:\w+\.?)*\w+@(?:\w+\.?)*\w+$/;

  if (email_pattern.test(field.value)) //getValue(field)))
  {
    hideErrorMessage(error_message_field) ;
    return true ;
  }

  displayErrorMessage
    (error_message_field, "Please enter a valid e-mail address") ;
  return false ;
}

/**
@brief Checks if the given email address is not from a public domain
*/
function isEmailOfficial
   (vFieldId)
{
  var field = document.getElementById(vFieldId) ;
  var error_message_field =
    document.getElementById("ErrorMessage" + vFieldId) ;

  var domain_patterns =
    new Array
    ( /@126\.com/g ,
      /@aol\.com/g ,
      /@comcast\.com/g ,
      /@e-mail\.su/g ,
      /@gmail\.com/g ,
      /@hotmail\.com/g ,
      /@i\.ua/g ,
      /@libero\.it/g ,
      /@mail\.ru/g ,
      /@rambler\.ru/g ,
      /@yahoo\.com/g
     ) ;
  
  for (var i = 0 ; i < domain_patterns.length ; ++i)
  {
    if (domain_patterns[i].test(field.value))
    {
      displayErrorMessage
        (error_message_field, "Please enter an e-mail address issued at your institution") ;
      return false ;
    }
  }

  hideErrorMessage(error_message_field) ;
  return true ;
}

/**
@brief Checks if at least one checkbox is toggled in a checkbox group
*/
function isCheckboxGroupToggled
   (vCheckboxGroupName)
{
  var checkbox_group = document.getElementsByName(vCheckboxGroupName) ;

  var error_message_field =
    document.getElementById("ErrorMessage" + vCheckboxGroupName) ;

  for (var i = 0 ; i < checkbox_group.length ; ++i)
  {
    if (checkbox_group[i].checked == true) 
    {
      hideErrorMessage(error_message_field) ;
      return true ;
    }
  }

  displayErrorMessage
     (error_message_field, "Please check at least one item") ;
  return false ;
}

/**
@brief Checks if the last checkbox is toggled in a checkbox group
*/
function isLastCheckboxToggled
   (vCheckboxGroupName)
{
  var checkbox_group = document.getElementsByName(vCheckboxGroupName) ;

  if (checkbox_group.length == 0) return false ;

  var index = checkbox_group.length - 1 ;

  return checkbox_group[index].checked ;
}

/**
@brief Checks if the 'Unspecified' option is selected
*/
function isUnspecifiedOptionSelected
   (vComboBoxId)
{
  var combo_box = document.getElementById(vComboBoxId) ;

  var error_message_field =
    document.getElementById("ErrorMessage" + vComboBoxId) ;

  var unspecified_option = "Unspecified" ;

  if (combo_box.options.length == 0 ||
      combo_box.options[combo_box.selectedIndex].value != unspecified_option)
  {
    hideErrorMessage(error_message_field) ;
    return false ;
  }

  displayErrorMessage
     (error_message_field, "Please specify the value") ;
  return true ;
}



/* Functions to handle error messages ---------------------- */

/**
@brief Hides the given element representing an error message
*/
function hideErrorMessage
   (vElement)
{
  setClass(vElement, "hidden-error-message") ;
  setValue(vElement, "") ;
}

/**
@brief Hides the given element representing an error message
*/
function displayErrorMessage
  (vElement,
   vMessage)
{
  setClass(vElement, "displayed-error-message") ;
  setValue(vElement, vMessage) ;
  vElement.disabled = true ;

  //alert("ErrorMessElement: " + vElement.id + "\nclassName: " + vElement.className) ;
}


/* Utility functions --------------------------------------- */

/**
@brief Sets the element's value
*/
function setValue
   (vElement,
    vValue)
{
  vElement.value = vValue ;
}

/**
@brief Returns the element's value
*/
function getValue
   (vElement)
{
  return vElement.value ;
}

/**
@brief Checks if the element value's is empty
*/
function isEmpty
   (vElement)
{
  return vElement.value.length == 0 ;
}

/**
@brief Checks if the element's value is too short
*/
function tooShort
   (vElement,
    vMinLength)
{
  return vElement.value.length < vMinLength ;
}

/**
@brief Checks if the element's value is too long
*/
function tooLong
   (vElement,
    vMaxLength)
{
  return vElement.value.length < vMaxLength ;
}

/**
@brief Sets an element's class to the given class name
*/
function setClass
   (vElement,
    vClassName)
{
  if (vClassName.length == 0) return ;

  vElement.className = vClassName ;
}

