//CUSTOM FIELD VALIDATION
function v_HHonors(){
  // make sure we have a vuser field
  if(el.vuser != null){
    // check to make sure there's something in there
    if(el.vuser.value == ""){
      alert('Please enter your Hilton HHonors account number.');
      return false;
    }
    // we need to do some math here so make sure we have an integer (see spec for more details on formula)
    var intInput = parseInt(el.vuser.value,10);
    // split the number into the first 8 bytes (digits) and the last byte
    var intBase = Math.floor(intInput/10);
    var intCheck = ((intBase - (Math.floor((intBase / 9)) * 9)) + 1);
    if(intCheck != (intInput % 10)){
      alert('The HHonors account number you entered is invalid.\nPlease check it and try again.');
      return false;
    }
  }
  return true;
}

function v_MileagePlus()
{
  if(el.vuser != null){
    // check to make sure there's something in there
    if(el.vuser.value == ""){
      alert('Please enter your Mileage Plus account number.');
      return false;
    }
    // we need to do some math here so make sure we have an integer (see spec for more details on formula)
    var intInput = parseInt(el.vuser.value,10);
    // split the number into the first 10 digits and the last check digit
    var intBase = Math.floor(intInput/10);
    var intCheck = 0;
    for (var i=0; i<10; i++){
      var intFactor = (i % 6) + 2;
      intCheck += ((intBase % 10) * intFactor);
      intBase = Math.floor(intBase/10);
    }
    
    intCheck = intCheck % 11;
    intCheck = ((intCheck == 0) || (intCheck == 1) ? 0 : (11 - intCheck));

    if(intCheck != (intInput % 10)){
      alert('The MileagePlus account number you entered is invalid.\nPlease check it and try again.');
      return false;
    }
  }
  return true;
}

//**********************************************************

function customValidation() {
  if(el.vuserName != null){
    // See if we have any custom validation for the current vuser attribute
    switch(el.vuserName.value){  //vuserName.value should contain the attribute's name (not display name)
      case "HHonors":
        if(!v_HHonors()) return false;
        break;
      case "MileagePlus":
        if(!v_MileagePlus()) return false;
        break;
      default:
        break;
    }
  }
	
	// validate custom barnes and noble promo
	if(document.getElementById('bnpromo') != null){
		var promoCode = document.getElementById('bnpromo').value;
		if (promoCode.toUpperCase() != "EPUB459"){
			alert('You have entered an invalid promotion code.\nPlease check it and try again.');
			return false;
		}
	}
  return true;
}

