var validNameChars = lowercaseLetters + "'.- ";
var validLoginNameChars = lowercaseLetters + digits + "_-"

function isFirstSelected(select)
{
  return (select.selectedIndex == 0);
}

function validatePersInfo(form)
{
  if (isFirstSelected(form.optTitle_ID))
  {
    alert( "Please select your Title in the \"title\" field." );
    form.optTitle_ID.focus();       
    return false;
  }
  else if (isWhitespace(form.txtFirstName.value))
  {
    alert( "Please enter your First Name in the \"first name\" field.");
    form.txtFirstName.focus();    
    return false;
  }
  else if (!isCharsInBag(form.txtFirstName.value.toLowerCase(), validNameChars))
  {
    alert( "Please enter only letter characters in the \"first name\" field.");
    form.txtFirstName.focus();    
    return false;
  }  
  else if (isWhitespace(form.txtLastName.value))
  {
    alert( "Please enter your Last Name in the \"last name\" field.");
    form.txtLastName.focus();    
    return false;
  }
  else if (!isCharsInBag(form.txtLastName.value.toLowerCase(), validNameChars))
  {
    alert( "Please enter only letter characters in the \"last name\" field.");
    form.txtLastName.focus();    
    return false;
  } 
  return true;
}

function validateEmail(form)
{
  if (!isEmail(form.txtEmail.value))
  {
    alert( "Please enter a valid email address in the \"e-mail\" field.");
    form.txtEmail.focus();    
    return false;
  } 
  return true;
}

function validateBirthDate(form)
{
  if (!isInteger(form.txtBirthDateYear.value) ||
    (parseInt(form.txtBirthDateYear.value) < 1900) || (parseInt(form.txtBirthDateYear.value) > 1995))
  {
    alert( "Please enter a valid Year in the \"birth date\" field.");
    form.txtBirthDateYear.focus();    
    return false;
  } 
  else if (!isDate(form.txtBirthDateYear.value, form.optBirthDateMonth_ID.options[form.optBirthDateMonth_ID.selectedIndex].value, form.optBirthDateDay.options[form.optBirthDateDay.selectedIndex].value))
  {
    alert( "Please enter a valid Date in the \"birth date\" field.");
    form.txtBirthDateDay.focus();    
    return false;
  } 
  else if ( 
    ((isFirstSelected(form.optBirthDateMonth_ID) || isFirstSelected(form.optBirthDateDay)) && !isWhitespace(form.txtBirthDateYear.value)) ||
    (isFirstSelected(form.optBirthDateMonth_ID) && !isFirstSelected(form.optBirthDateDay)) || 
    (!isFirstSelected(form.optBirthDateMonth_ID) && isFirstSelected(form.optBirthDateDay)) )
  {
    alert( "Please enter a valid Birthday in the \"birth date\" field.");
    form.txtBirthDateYear.focus();    
    return false;
  } 
  return true;
}

function validateLoginName(form)
{
  if (isWhitespace(form.txtLoginName.value) ||
    !isCharsInBag(form.txtLoginName.value.toLowerCase(), validLoginNameChars) ||
    (form.txtLoginName.value.length < 4) || (form.txtLoginName.value.length > 12))
  {
    alert( "Please enter from 4 to 12 characters long Login Name in the \"login name\" field.");
    form.txtLoginName.focus();    
    return false;
  } 
  return true;
}

function validatePassword(form)
{
  if (isWhitespace(form.txtPassword.value) || (form.txtPassword.value.length < 4) || (form.txtPassword.value.length > 12))
  {
    alert( "Please enter from 4 to 12 characters long Password in the \"password\" field.");
    form.txtPassword.focus();    
    return false;
  }    
  else if (form.txtPassword.value != form.txtRePassword.value)
  {
    alert( "Your passwords do not match. Please make sure you enter your password identically.");
    form.txtPassword.value = "";
    form.txtRePassword.value = "";
    form.txtPassword.focus();    
    return false;
  }    
  return true;
}

function validateAccPassword(form)
{
  if (!isWhitespace(form.txtPassword.value) || !isWhitespace(form.txtRePassword.value))
  {
    return validatePassword(form);
  }    
  return true;
}

function validatePhones(form)
{
  if (!isCharsInBag(form.txtHomePhone.value, validWorldPhoneChars))
  {
    alert( "Please enter numbers or the characters ()-+ in the \"home phone\" field.");
    form.txtHomePhone.focus();    
    return false;
  }  
  else if (!isCharsInBag(form.txtWorkPhone.value, validWorldPhoneChars))
  {
    alert( "Please enter numbers or the characters ()-+ in the \"work phone\" field.");
    form.txtWorkPhone.focus();    
    return false;
  }  
  else if (!isCharsInBag(form.txtMobilePhone.value, validWorldPhoneChars))
  {
    alert( "Please enter numbers or the characters ()-+ in the \"mobile\" field.");
    form.txtMobilePhone.focus();    
    return false;
  }  
  return true;
}

//  else if (!isCharsInBag(form.txtFax.value, validWorldPhoneChars))
//  {
//    alert( "Please enter numbers or the characters ()-+ in the \"fax\" field.");
//    form.txtFax.focus();    
//    return false;
//  }  

function validatePhone(form)
{
  if (!isCharsInBag(form.txtPhone.value, validWorldPhoneChars))
  {
    alert( "Please enter numbers or the characters ()-+ in the \"phone\" field.");
    form.txtPhone.focus();    
    return false;
  }  
  return true;
}

function validateAddress(form)
{
  if (isWhitespace(form.txtAddress.value))
  {
    alert( "Please enter your Address in the \"address\" field.");
    form.txtAddress.focus();    
    return false;
  }
  else if (isWhitespace(form.txtCity.value))
  {
    alert( "Please enter your City in the \"city\" field.");
    form.txtCity.focus();    
    return false;
  }
  else if (isWhitespace(form.txtZipCode.value))
  {
    alert( "Please enter your Zip Code in the \"zip code\" field.");
    form.txtZipCode.focus();    
    return false;
  }
  return true;
}

function validateAddress2(form)
{
  if (!isWhitespace(form.txtAddress.value)  ||
    !isWhitespace(form.txtCity.value) ||
    !isWhitespace(form.txtZipCode.value) ||
    !isWhitespace(form.txtRegion.value))
  {
    return validateAddress(form);
  }
  return true;
}

function removeConfirmation(form)
{
  if (confirm("Are you sure you want to remove the below address?" + 
    "\n\n" + form.hdnMsgRemoveAddress.value.replace(/<br>/gi, "\n")))
  {
    form.hdnRemoveAddress.value = "Y";
    form.submit();
  } 
}

function validateRegistration(form)
{
  if (validatePersInfo(form) &&
    validateEmail(form) &&
    validateLoginName(form) &&
    validatePassword(form))
  {
    return true;
  } 
  return false;
}

function validateAccountPersInfo(form)
{
  if (validatePersInfo(form) &&
    validateBirthDate(form))
  {
    return true;
  } 
  return false;
}

function validateAccountAccInfo(form)
{
  if (validateLoginName(form) &&
    validateEmail(form) &&
    validateAccPassword(form))
  {
    return true;
  } 
  return false;
}

function validateWomanRegistration(form)
{
  if (validatePersInfo(form) &&
    validateEmail(form) &&
    validatePhone(form) &&
    validateAddress2(form))
  {
    return true;
  } 
  return false;
}

function validateCorporateRegistration(form)
{
  if (validatePersInfo(form) &&
    validatePhone(form) &&
    validateEmail(form) &&
    validateLoginName(form) &&
    validatePassword(form))
  {
    return true;
  } 
  return false;
}

function validateAppointment(form)
{
  if ((isWhitespace(form.txtName.value)) || (!isCharsInBag(form.txtName.value.toLowerCase(), lowercaseLetters + ".- ")))
  {
    alert( "Please enter only letter characters in the \"Name\" field.");
    form.txtName.focus();    
    return false;
  } 
  else if (!isEmail(form.txtEmail.value))
  {
    alert( "Please enter a valid Email address in the \"Email\" field.");
    form.txtEmail.focus();    
    return false;
  }   
  else if ((isWhitespace(form.txtPhone.value)) || (!isCharsInBag(form.txtPhone.value, validWorldPhoneChars)))
  {
    alert( "Please enter numbers or the characters ()-+ in the \"Phone\" field.");
    form.txtPhone.focus();    
    return false;
  }  
  else if ((isWhitespace(form.txtResidence.value)))
  {
    alert( "Please enter your place of residence in the \"Place of residence\" field.");
    form.txtResidence.focus();    
    return false;
  }
  else if ((isWhitespace(form.txtDate.value)))
  {
    alert( "Please enter a valid date into \"Preferred date\" field.");
    form.txtDate.focus();    
    return false;
  }
  else if ((isWhitespace(form.txtTime.value)))
  {
    alert( "Please enter a valid time into \"Preferred time\" field.");
    form.txtTime.focus();    
    return false;
  }
  return true;
}

function validateWorkEmail(form)
{
  if (!isWhitespace(form.txtWorkEmail.value))
    if (!isEmail(form.txtWorkEmail.value))
    {
      alert( "Please enter a valid email address in the \"work e-mail\" field.");
      form.txtWorkEmail.focus();    
      return false;
    } 
  return true;
}

function validateNewAccountPersInfo(form)
{
  if (validatePersInfo(form) &&
    validateBirthDate(form) &&
    validateEmail(form) &&
    validatePhones(form) &&
    validateAddress(form) &&
    validateWorkEmail(form))
  {
    return true;
  } 
  return false;
}
