/*
  index.js
*/ 


//--- CloseNotFound ---
function CloseNotFound() {
  var objNF  = document.getElementById('not_found_message');
  objNF.style.display = 'none';
}


//--- CountryChange ---
function CountryChange() {
  var frmEntry = document.getElementById('cf_form');
  var intIndex = frmEntry.country.options.selectedIndex;
  var strCountry = frmEntry.country.options[intIndex].value;
  var strCountryCode;
  var objStatePrompt = document.getElementById('state_prompt');
  
  strCountryCode = Trim(strCountry.substring(0,2));
  if (strCountryCode == "US") {
    frmEntry.state_text.style.display = "none";
    frmEntry.state_text.value = "";
    frmEntry.state_select.style.display = "inline";
    objStatePrompt.innerHTML = "State*:";
  }
  else {
    frmEntry.state_select.style.display = "none";
    frmEntry.state_select.options.selectedIndex = 0;
    frmEntry.state_text.style.display = "inline";
    objStatePrompt.innerHTML = "State:";
  }
}


//--- FormNext ---
function FormNext() {
  var frmEntry;
  var strContactName;
  var strErrorMsg = '';
  
  frmForm = document.getElementById('cf_form');
  strErrorMsg = ValidateContactName(frmForm, strErrorMsg);
  strErrorMsg = ValidateState(frmForm, strErrorMsg);
  strErrorMsg = ValidateEmail(frmForm, strErrorMsg);
  strErrorMsg = ValidatePhone(frmForm, strErrorMsg);
  if (strErrorMsg.length > 0) {
    strErrorMsg = "Error:\n\n" + strErrorMsg;
    alert(strErrorMsg);
  }
  else {
    frmForm.submit();
  }
}


//--- onLoadInit ---
function onLoadInit() {
  // call a country change event to make sure state-input type is correct
  CountryChange();
}


//--- ValidateContactName ---
function ValidateContactName(frmForm, strErrorMsg) {
  strContactName = Trim(frmForm.full_name.value);
  if (strContactName.length == 0) {
    strErrorMsg += " - Contact Name required. \n";
  }
  return strErrorMsg;
}


//--- ValidateEmail ---
function ValidateEmail(frmForm, strErrorMsg) {
  strEmail = Trim(frmForm.email.value);
  if(strEmail.length == 0) {
    strErrorMsg += " - Email required. \n";
  }
  return strErrorMsg;
}


//--- ValidatePhone ---
function ValidatePhone(frmForm, strErrorMsg) {
  strPhone = Trim(frmForm.phone.value);
  if(strPhone.length == 0) {
    strErrorMsg += " - Phone required. \n";
  }
  return strErrorMsg;
}


//--- ValidateState ---
function ValidateState(frmForm, strErrorMsg) {
  var intIndex = frmForm.country.selectedIndex;
  var strCountry = frmForm.country.options[intIndex].value;
  var strCountryCode = Trim(strCountry.substring(0,2));
  var strState;
  
  // state is only required for US
  if (strCountryCode == "US") {
    if (frmForm.state_select.options.selectedIndex == 0) {
      strErrorMsg += " - State required. \n";
    }
  }
  
  return strErrorMsg;
}



