function isValidFormMobileNo(mobile_no) {
  if (mobile_no.value != "") {
    if (mobile_no.value.length != 8) {
      alert ('Invalid mobile no. Please try again.');
      mobile_no.focus();
      return false;
    }
    if (isInt(mobile_no) == false || (/^[9]{3}[\d]+$/.test(mobile_no.value))) {
      alert ('Invalid mobile no. Please try again.');
      mobile_no.focus();
      return false;
    }
  }
  return true;
}

function isValidFormPhoneNo(phone_no) {
  if (phone_no.value != "") {
    if (phone_no.value.length != 8) {
      alert ('Invalid phone no. Please try again.')
      phone_no.focus();
      return false;
    }
    if (isInt(phone_no) == false || (/^[9]{3}[\d]+$/.test(phone_no.value))) {
      alert ('Invalid phone no. Please try again.');
      phone_no.focus();
      return false;
    }
  }
  return true;
}

function isValidFormRegUsername(username) { // for new member
  username.value = trim(username.value);
  if (!isFilled(username.value)) {
    alert('Login Name must be filled in.');
    username.focus();
    return false;
  }
  if(!(/^[\d\w\_\-]{6,10}$/.test(username.value))) {
    alert('Login Name should be within 6 to 10 alphanumeric characters. Please amend.');
    username.focus();
    return false;
  }
  return true;
}

function isValidFormOldUsername(username) { // for old member
  username.value = trim(username.value);
  if (!isFilled(username.value)) {
    alert('Login Name must be filled in.');
    username.focus();
    return false;
  }
  if (username.value.length > 10) {
    alert('Login Name should be within 6 to 10 alphanumeric characters. Please amend.');
    username.focus();
    return false;
  }
  return true;
}

function isValidFormRegPassword(pass, pass2) { // for new member
  if (!isFilled(pass.value)) {
    alert('Password must be filled in');
    pass.focus();
    return false;
  }
  if(!(/^[\d\w\_\-]{6,10}$/.test(pass.value))) {
    alert('Password should be within 6 to 10 alphanumeric characters. Please amend.');
    pass.focus();
    return false;
  }
  if (pass.value != pass2.value) {
    alert('Password and Re-enter password are not identical. Please amend.');
    pass2.focus();
    return false;
  }
  return true;
}

function isValidFormOldPassword(pass) { // for old member
  if (!isFilled(pass.value)) {
    alert('Password must be filled in');
    pass.focus();
    return false;
  }
  if (pass.value.length > 10) {
    alert('Password should be within 6 to 10 alphanumeric characters. Please amend.');
    pass.focus();
    return false;
  }
  return true;
}

function isValidFormLastName(lname) {
  if (!isFilled(lname.value)) {
    alert('Surname must be filled in. ');
    lname.focus();
    return false;
  }
  if (lname.value.indexOf("'") != -1) {
    alert("No ' within Surname. Please amend.");
    lname.focus();
    return false;
  }
  if (lname.value.getBytes() > 30) {
    alert('Surname must be no longer than 30 characters, please amend.');
    lname.focus();
    return false;
  }
  return true;
}

function isValidFormFirstName(fname) {
  if (!isFilled(fname.value)) {
    alert('Given name must be filled in');
    fname.focus();
    return false;
  }
  if (fname.value.indexOf("'") != -1) {
    alert("No ' within Given name. Please amend.");
    fname.focus();
    return false;
  }
  if (fname.value.getBytes() > 30) {
    alert('Given name must be no longer than 30 characters, please amend.');
    fname.focus();
    return false;
  }
  return true;
}

function isValidFormAddress(addr1, addr2, addr3) {
  if (addr1.value.indexOf("'") != -1) {
    alert("No ' within Address. Please amend.");
    addr1.focus();
    return false;
  }
  if (addr2.value.indexOf("'") != -1) {
    alert("No ' within Address. Please amend.");
    addr2.focus();
    return false;
  }
  if (addr3.value.indexOf("'") != -1) {
    alert("No ' within Address. Please amend.");
    addr3.focus();
    return false;
  }
  if (addr1.value.getBytes() > 30) {
    alert('Address (Line 1) must be no longer than 30 characters, please amend.');
    addr1.focus();
    return false;
  }
  if (addr2.value.getBytes() > 30) {
    alert('Address (Line 2) must be no longer than 30 characters, please amend.');
    addr2.focus();
    return false;
  }
  if (addr3.value.getBytes() > 30) {
    alert('Address (Line 3) must be no longer than 30 characters, please amend.');
    addr3.focus();
    return false;
  }
  return true;
}

function isValidFormEmail(email) {
  if (!isValidEmail(email.value)) {
    alert('Email address is invalid. Please amend.');
    email.focus();
    return false;
  }
  if (email.value.length > 60) {
    alert('Maximun length of email address is 60 characters. Please amend.');
    email.focus();
    return false;
  }
  return true;
}

function isValidFormHKID(id_passport, hkid_prefix) {
  return true;
  if (!isFilled(id_passport.value)) {
    alert('Please enter your HKID Card No.');
    hkid_prefix.focus();
    return false;
  }
  if (!isHKID(id_passport.value)) {
    alert('Sorry! The HKID Card No. you provided is invalid. Please try again.');
    hkid_prefix.focus();
    return false;
  }
  return true;
}

function isValidFormPinNo(pin_no) {
  if (!isFilled(pin_no.value)) {
    alert('Please enter your Octopus Card No.');
    pin_no.focus();
    return false;
  }
  if (/^[0-9]{1,10}$/.test(pin_no.value) == false) {
    alert('Octopus Card No. must be numerals (0-9). Please amend.');
    pin_no.focus();
    return false;
  }
  if (isCheckDigit(pin_no.value) == false) {
    alert('Octopus Card No. is incorrect. Please amend.');
    pin_no.focus();
    return false;
  }
  return true;
}

function isValidFormShareholderCode(shareholder_code) {
  if (shareholder_code.value.indexOf("'") != -1) {
    alert("No ' within Shareholder Bonus Points Code. Please amend.");
    shareholder_code.focus();
    return false;
  }
  return true;
}

function isValidFormRefUsername(ref_username) { // for MGM
  ref_username.value = trim(ref_username.value);
  if (ref_username.value.length > 10) {
    alert('Referee\'s User Name should be within 6 to 10 alphanumeric characters. Please amend.');
    ref_username.focus();
    return false;
  }
  return true;
}

function isValidFormTripInfo(mf) {
  if (mf.TRIP_TYPE[0].checked) {
    if (mf.HVYR_START_STATION.options[mf.HVYR_START_STATION.options.selectedIndex].value == 0) {
      alert('Please enter your last MTR trip information.');
      mf.HVYR_START_STATION.focus();
      return false;
    }
    if (mf.HVYR_END_STATION.options[mf.HVYR_END_STATION.options.selectedIndex].value == 0) {
      alert('Please enter your last MTR trip information.');
      mf.HVYR_END_STATION.focus();
      return false;
    }
  }
  else if (mf.TRIP_TYPE[1].checked) {
    if (mf.LGTR_START_STATION.options[mf.LGTR_START_STATION.options.selectedIndex].value == 0) {
      alert('Please enter your last MTR trip information.');
      mf.LGTR_START_STATION.focus();
      return false;
    }
    if (mf.LGTR_END_STATION.options[mf.LGTR_END_STATION.options.selectedIndex].value == 0) {
      alert('Please enter your last MTR trip information.');
      mf.LGTR_END_STATION.focus();
      return false;
    }
  }
  else if (mf.TRIP_TYPE[2].checked) {
    if (mf.BUS_ROUTE.options[mf.BUS_ROUTE.options.selectedIndex].value == 0) {
      alert('Please enter your bus route.');
      mf.BUS_ROUTE.focus();
      return false;
    }
  }
  else if (!mf.TRIP_TYPE[2].checked && !mf.TRIP_TYPE[1].checked && !mf.TRIP_TYPE[0].checked) {
    alert('Please enter your last MTR trip information or bus route.');
    mf.HVYR_START_STATION.focus();
    return false;
  }
  return true;
}

function isValidFormTripDate(ltrip_yr, ltrip_mn, ltrip_dy) {
  var ltrip_yr_value = ltrip_yr.options[ltrip_yr.options.selectedIndex].value;
  var ltrip_mn_value = ltrip_mn.options[ltrip_mn.options.selectedIndex].value - 1;
  var ltrip_dy_value = ltrip_dy.options[ltrip_dy.options.selectedIndex].value;
  var enterdate = new Date(ltrip_yr_value, ltrip_mn_value, ltrip_dy_value);
  var rightnow = new Date();
  
  if (getDateString(enterdate) - getDateString(rightnow) > 0) {
    alert('Invalid date of last MTR trip entered. Please amend.');
    ltrip_yr.focus();
    return false;
  }
  return true;
}

function isValidFormDateofBirth(bdate_yr, bdate_mm, bdate_dy) {
//  var bdate_yr_value = bdate_yr.options[bdate_yr.options.selectedIndex].value;
  var bdate_yr_value = 1980;
  var bdate_mm_value = bdate_mm.options[bdate_mm.options.selectedIndex].value - 1;
  var bdate_dy_value = bdate_dy.options[bdate_dy.options.selectedIndex].value;
  var enterdate = new Date(bdate_yr_value, bdate_mm_value, bdate_dy_value);
  var rightnow = new Date();
  
  if (getDateString(enterdate) - getDateString(rightnow) > 0) {
    alert('Please enter your valid date of birth.');
    bdate_yr.focus();
    return false;
  }
  return true;
}

function isValidSStation(mf, max_count) {
  var sel;
  var count = 0;
  
  sel = mf.elements['S_STATION[]'];
  for (var i=0; i<sel.length; i++) {
    if (sel.options[i].selected) {
      count++;
    }
  }

  if (count > max_count) {
    return false;
  } else {
    return true;
  }
}

function validate_pin_update(mf, old_pin_no) {
  if (!isValidFormPinNo(mf.pin_no)) { return false; } //** check Octopus card no.
  if (!isValidFormTripInfo(mf)) { return false; }     //** check last MTR trip
  if (!isValidFormTripDate(mf.ltrip_Year, mf.ltrip_Month, mf.ltrip_Day)) { return false; }    //** check last MTR date
  return true;
}

function validate_clean_update(mf) {
  if (!isValidFormMobileNo(mf.mobile_no)) { return false; }     //** check mobile no
  if (!isValidFormEmail(mf.email)) { return false; }            //** check email
//  if (!isValidFormDateofBirth(mf.bdate_Year, mf.bdate_Month, mf.bdate_Day)) { return false; } //** check date of birth
  if (!isValidFormDateofBirth(1980, mf.bdate_Month, mf.bdate_Day)) { return false; } //** check date of birth
  return true;
}

function validate_form_data(mf, formtype) {
  //  Validation only for registration form
  if (formtype == 'REGISTER') {
    if (!isValidFormPinNo(mf.pin_no)) { return false; }         //** check Octopus card no.
    if (!isValidFormTripInfo(mf)) { return false; }             //** check last MTR trip
    if (!isValidFormTripDate(mf.ltrip_Year, mf.ltrip_Month, mf.ltrip_Day)) { return false; }  //** check last MTR date
    if (!isValidFormRegUsername(mf.username)) { return false; } //** check login name
  }

  if (formtype == 'REGISTER' || mf.pass.value != '') {
    if (!isValidFormRegPassword(mf.pass, mf.pass2)) { return false; } //** check password
  }

  if (!isValidFormEmail(mf.email)) { return false; }            //** check email
  if (!isValidFormLastName(mf.lname)) { return false; }         //** check Surname
  if (!isValidFormFirstName(mf.fname)) { return false; }        //** check Given name
//  if (!isValidFormHKID(mf.id_passport, mf.hkid_prefix)) { return false; } //** check HK ID Card

  //if (!isValidFormAddress(mf.addr1, mf.addr2, mf.addr3)) { return false; } //** check address

  if (!isValidFormMobileNo(mf.mobile_no)) { return false; }     //** check mobile no
  // if (!isValidFormPhoneNo(mf.phone_no)) { return false; }    //** check phone no
//  if (!isValidFormDateofBirth(mf.bdate_Year, mf.bdate_Month, mf.bdate_Day)) { return false; } //** check date of birth
  if (!isValidFormDateofBirth(1980, mf.bdate_Month, mf.bdate_Day)) { return false; } //** check date of birth
  
  //** check referral
  if (formtype == 'REGISTER' && typeof(mf.ref_username) != "undefined") {
    if (isFilled(mf.ref_username.value)) {
      if (!isValidFormRefUsername(mf.ref_username)) {
        return false;
      }
    }
  }

  //** check shareholder
  if (typeof(mf.shareholder_code) != "undefined") {
    if (isFilled(mf.shareholder_code.value)) {
      if (!isValidFormShareholderCode(mf.shareholder_code)) {
        return false;
      }
    }
  }

  if (typeof(mf.P_STATION) != "undefined") {
    if (mf.P_STATION.options[mf.P_STATION.options.selectedIndex].value == 0) {
      alert('Please select Your Station');
      mf.P_STATION.focus();
      return false;
    }
  }

  if (typeof(mf.S_STATION) != "undefined") {
    if (! isValidSStation(mf, 2)) {
      alert('Please select 2 Stations');
      mf.elements['S_STATION[]'].focus();
      return false;
    }
  }

  //** check read and understand of condition
  if(formtype != 'REGISTER' && !mf.read_understand.checked) {
    alert('Please click the check box to confirm you agree to observe and be bound by the rules, policies and terms and conditions.');
    mf.read_understand.focus();
    return false;
  }
  return true;
}

function validate_forget_password(mf) {
  var filled_info_count = 0;

  if (isFilled(mf.pin_no.value)) {
    if (!isValidFormPinNo(mf.pin_no)) { return false; }
    filled_info_count++;
  }

  if (isFilled(mf.username.value)) {
    if (!isValidFormOldUsername(mf.username)) { return false; }
    filled_info_count++;
  }

  if (filled_info_count < 2) {
    alert('Please provide Octopus Card No. and Login Name.');
    mf.pin_no.focus();
    return false;
  }
  return true;
}

function validate_delivery_data(mf) {
  //** check last name
  if (!isValidFormLastName(mf.delivery_last_name)) {
    return false;
  }

  //** check first name
  if (!isValidFormFirstName(mf.delivery_first_name)) {
    return false;
  }

  //** check address
  if (!isFilled(mf.delivery_address_1.value + mf.delivery_address_2.value + mf.delivery_address_3.value)) {
    alert('Address must be filled in');
    mf.delivery_address_1.focus();
    return false;
  }
  if (!isValidFormAddress(mf.delivery_address_1, mf.delivery_address_2, mf.delivery_address_3)) {
    return false;
  }

  //** check email
  if (!isValidFormEmail(mf.delivery_email)) {
    return false;
  }

  //** check phone no
  if (!isFilled(mf.delivery_phone_no.value)) {
    alert ('Phone no. must be filled in')
    mf.delivery_phone_no.focus();
    return false;
  }
  if (!isValidFormPhoneNo(mf.delivery_phone_no)) {
    return false;
  }
  return true;
}

function validate_login(mf) {
  if (!isFilled(mf.username.value)) {
    alert('Login Name must be filled in.');
    mf.username.focus();
    return false;
  }
  if (!isFilled(mf.password.value)) {
    alert('Password must be filled in.');
    mf.password.focus();
    return false;
  }
  /*
  if (!isValidFormOldUsername(mf.username) {
    return false;
  }
  if (!isValidFormOldPassword(mf.password) {
    return false;
  }
  */
  return true;
}

function login_submit(mf) {
  if (validate_login(mf) == true) {
    mf.login_username.value = mf.username.value;
    mf.login_password.value = mf.password.value;
    mf.username.value = '';
    mf.password.value = '';
    mf.dummy.value = 'login';
    mf.dummy.name  = 'action';
    mf.submit();
  }
}

function isValidMobileNo(mf) { // old function
  return isValidFormMobileNo(mf.mobile_no);
}

function isValidPhoneNo(mf) { // old function
  return isValidFormPhoneNo(mf.phone_no);
}
