/**
  Validate reservation form
  -check to see that all required text fields have a non empty string in them
  -verify phone number has the correct amount of digits if country = united states.
  -verify state selected if country == australia, canada, or US
  -check credit card info to make sure that the proper amount of digits are in each one
  -verify that the expiration date is in the future
  
  
  
  If validate checks passed, submit form.
  
  If not, highlight fields that need to be corrected and display correction notice
  
  repeat until passed

*/


var validation = {};

function init(){
  validation.bf = document.forms[0];
  validation.errorCount = 0;
  validation.errorMessages = new Array();
  validation.alertStr = 'There were a few problems with your submission.  Please correct the noted fields in red and resubmit.  Thank You!\n\n';
}

function validate(){
  init();
  resetRed();
  cleanData();
  if(requiredFilled()){
    if(emailCheck()){
      validation.submit();
      return true;
    }
  }
  else{
    emailCheck();
  }
  for(var i = 0; i < validation.errorMessages.length; i++){
    validation.alertStr += '\t' + (i + 1) + '. ' + validation.errorMessages[i];
  }
  window.alert(validation.alertStr);
  return false;
}

function setRed(e){
  e.setAttribute('class', e.getAttribute('class') + ' formError');
   //e.setAttribute('style', 'background-color: #ffaaaa;');
}

function resetRed(){
  for(var i = 0; i < validation.bf.elements.length; i++){
    if(validation.bf.elements[i].type == 'text'){
      validation.bf.elements[i].setAttribute('class', validation.bf.elements[i].getAttribute('class').replace(/formError/, ''));
    }
    //if(validation.bf.elements[i].type == 'text') validation.bf.elements[i].setAttribute('style', 'background-color: #ffffff;');
  }
}

function cleanData(){
  /*Iterate through all text inputs and textareas and trim all strings.
    Remove all dashes from phone numbers */
    for(var i = 0; i < validation.bf.elements.length; i++){
      var e = validation.bf.elements[i];
      if(e.type == 'text' || e.type == 'textarea'){
        e.value = trim(e.value);
      }
    }
    
  return true;
}

function requiredFilled(){
    var filled = true;
    for(var i = 0; i < validation.bf.elements.length - 1; i++){
      var e = validation.bf.elements[i];
        if(e.value.length < 1){
          filled = false;
          setRed(e);
          //get parent's sibling's first child's text value
          validation.errorMessages[validation.errorCount] = e.previousSibling.previousSibling.textContent + ' must be filled in.\n';
          validation.errorCount++;
        }
    }
    
    return filled;
}

function emailCheck(){
  if(validation.bf.email.value == validation.bf.confirmEmail.value){
    var validEmail = /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}?\b/;
    if(validEmail.test(validation.bf.email.value)){
   // window.alert('valid email');
      return true;
    }
    else{
      validation.errorMessages[validation.errorCount] = 'Please double check your email address.  It is currently not in an understandable format.\n';
      validation.errorCount++;
    }
  }
  else{
    validation.errorMessages[validation.errorCount] = 'Please make sure that your email address is typed exactly the same in both fields.\n';
    validation.errorCount++;
  }
  setRed(validation.bf.email);
  setRed(validation.bf.confirmEmail);
  return false;
}


function trim(s){
	return rtrim(ltrim(s));
}

function ltrim(s){
	var l=0;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	return s.substring(l, s.length);
}

function rtrim(s){
	var r=s.length -1;
	while(r > 0 && s[r] == ' ')
	{	r-=1;	}
	return s.substring(0, r+1);
}