﻿/// Validate page input
/// Check each input on page one by one, if validation fails on one input, popup a message box 
/// with detailed error message and then highlight the input control
function ValidatorOnSubmit()
{

  // --------------------
  // Host Association
  // --------------------  
  if (isEmpty("txt_club_name"))
  {
    alert("Please specify the host association.");
    document.getElementById("txt_club_name").focus();
    return false;
  }
  
  // --------------------  
  // Affiliate
  // -------------------- 
  var dp_league_id = document.getElementById("dp_league_id");
  if (dp_league_id.value == -1)
  {
    alert("Please select an affiliate.");
    document.getElementById("dp_league_id").focus();
    return false;
  }  
  
  // -------------------- 
  // Contact Person:
  // -------------------- 
  //if (isEmpty("txt_Chairperson"))
  //{
  //  alert("Please enter the name of the contact person.");
  //  document.getElementById("txt_Chairperson").focus();
  //  return false;
  //}
  // --------------------
  // Contact firstname
  // --------------------
  if (isEmpty("txt_ContactPerson_first"))
  {
    alert("Please enter the first name of the contact person.");
    document.getElementById("txt_ContactPerson_first").focus();
    return false;
  }  
  // --------------------
  // Contact lastname
  // --------------------
  if (isEmpty("txt_ContactPerson_last"))
  {
    alert("Please enter the last name of the contact person.");
    document.getElementById("txt_ContactPerson_last").focus();
    return false;
  }

  // -----------------------------
  // Email
  // -----------------------------
  if (!ValidateEmail("txt_Email", ""))
  {
    return false;
  }
  
  // -----------------------------
  // Address
  // -----------------------------
  if (!ValidateAddress("txt_StreetNo", "txt_Street", "txt_City", "txt_Zip", ""))
  {
    return false;
  }

  // -----------------------------------
  // Resident Phone number
  // -----------------------------------
  if (!ValidatePhone("txtphone1area_phone_office", "txtphone1part1_phone_office", "txtphone1part2_phone_office", "the resident"))
  {
    return false;
  }

  // -----------------------------------
  // Business Phone number
  // -----------------------------------
  if (!ValidatePhone("txtphone1area_bus_phone_office", "txtphone1part1_bus_phone_office", "txtphone1part2_bus_phone_office", "the business"))
  {
    return false;
  }

  // -----------------------------------
  // Aggree check
  // -----------------------------------
  //var chkAgreed = document.getElementById("cklist_Agreed_0");
  //if (!chkAgreed.checked)
  //{
  //  alert("You must agree the terms before going forward.");
  //  chkAgreed.focus();
  //  return false;
  //}
      
  return true;
}



/// **********************************
/// Validate address info
/// **********************************
function ValidateAddress(streetNoElementId, streetElementId, cityElementId, postalElementId, person)
{
  // Street No.
  if (isEmpty(streetNoElementId))
  {
    alert("Please specify a street number" + person + ".");
    document.getElementById(streetNoElementId).focus();
    return false;
  }
  // Street address
  if (isEmpty(streetElementId))
  {
    alert("Please specify detailed street" + person + ".");
    document.getElementById(streetElementId).focus();
    return false;
  }
  // City
  if (isEmpty(cityElementId))
  {
    alert("Please specify the city" + person + ".");
    document.getElementById(cityElementId).focus();
    return false;
  }    
  // Postal Code
  if (isEmpty(postalElementId))
  {
    alert("Please specify the postal code" + person + ".");
    document.getElementById(postalElementId).focus();
    return false;
  }    
  // Is a valid postal code
  var postalElement = document.getElementById(postalElementId);
  if (!isValidZipCode(postalElement.value) && !isValidPostalCode(postalElement.value))
  {
    alert("Postal code" + person + " is not valid.");
    postalElement.focus();
    return false;
  }
  
  return true;  
}



/// *******************************
/// Validate phone number 
/// *******************************
function ValidatePhone(areaElementId, part1ElementId, part2ElementId, person)
{
  // Area code is required and should be at least 3 digits
  var phoneArea = document.getElementById(areaElementId);
  if (isEmpty(areaElementId))
  {
    alert("Please provide area code of " + person + " phone number.");
    phoneArea.focus();
    return false; 
  }
  if (phoneArea.value.length < 3)
  {
    alert("Area code of " + person + " phone number should have at least 3 digits.");
    phoneArea.focus();
    return false; 
  }
  
  // Phone number part 1 is required and should have 3 digits
  var phonePart1 = document.getElementById(part1ElementId);
  if (isEmpty(part1ElementId))
  {
    alert("Please provide part 1 of " + person + " phone number.");
    phonePart1.focus();
    return false; 
  }
  if (phonePart1.value.length < 3)
  {
    alert("Part 1 of " + person + " phone number should have 3 digits.");
    phonePart1.focus();
    return false; 
  }
  
  // Phone number part 2 is required and should have at least 3 digits
  var phonePart2 = document.getElementById(part2ElementId);
  if (isEmpty(part2ElementId))
  {
    alert("Please provide part 2 of " + person + " phone number.");
    phonePart2.focus();
    return false; 
  }
  if (phonePart2.value.length < 3)
  {
    alert("Part 2 of " + person + " phone number should have at least 3 digits.");
    phonePart2.focus();
    return false; 
  }
  return true; 
}


/// **********************************
/// Validate email address
/// **********************************
function ValidateEmail(emailElementId, person)
{
  // Email is required
  if (isEmpty(emailElementId))
  {
    alert("Please provide an Email address" + person + ".");
    document.getElementById(emailElementId).focus();
    return false;
  }
  // Is a valid email address
  var emailElement = document.getElementById(emailElementId);
  if (!isValidEmailAddress(emailElement.value))
  {
    alert("Email address" + person + "is not valid.");
    emailElement.focus();
    return false;
  }
  
  return true; 
}
