
/**************************************************************/
/**************************************************************/
/* Form specific functions for Business Jet Center            */
/*   - bjc.js contains general functions                      */
/**************************************************************/
/**************************************************************/

var focusset = false;

/**************************************************************/
/* set focus on the first form field                          */
/**************************************************************/
function setFirstFocus() {

  var theForm = document.forms[0]

  for(i=0; i<theForm.elements.length; i++){

     var theField = theForm.elements[i];

     // Skip over hidden, disabled and readonly fields (continue to next field)
     if(theField.type == 'hidden'  ||  theField.disabled == true  ||  theField.readOnly == true) {
        continue;    
     }
     else {
        // Stop looking when the first radio or checkbox is found (exit function without setting focus)
        if(theField.type == 'radio'  ||  theField.type == 'checkbox') {
           break;
        }
    
        // Set focus and exit the function when the first text, textarea or select-one field is found
        if(theField.type == 'text'  ||  theField.type == 'textarea'  ||  theField.type == 'select-one') {
           theField.focus();
           break;
        }
     }
  }

}

/**************************************************************/
/* Add a field to the required list                           */
/**************************************************************/
function addRequired(addId)
{
    if(document.getElementById('required').value.indexOf(addId) == -1) { 
        var curReq = document.getElementById('required').value;
        document.getElementById('required').value = curReq + ',' + addId;
    }
}

/**************************************************************/
/* Remove a field from the required list                      */
/**************************************************************/
function removeRequired(removeId)
{
    var remove = ',' + removeId;
    var newReq = document.getElementById('required').value.replace(remove,"");
    document.getElementById('required').value = newReq;
}

/**************************************************************/
/* Verify that required fields have data in them              */
/**************************************************************/
function checkRequired(field)
{

  if(!document.getElementById) { return true; }

  if(document.getElementById('required').value.indexOf(field.id) == -1) {
      return true;
  }

  if(field.type.toLowerCase() == 'radio') {
      checkRequiredRadio(field);
      return true;
  }
  
  var newClass = field.className;
  if(trim(field.value).length < 1) {
      errorOn(field);
      return false;
  }
  else { 
      errorOff(field);
      return true;
  }
  
  return true;
  
}

/**************************************************************/
/* Verify that required radio fields have data in them        */
/**************************************************************/
function checkRequiredRadio(name)
{
   var clicked = false;
   var radios = document.getElementsByName(name);
   for(i=0; i<radios.length; i++){
     if(radios[i].checked==true) {
        clicked = true;
     }
   }
   /** turn the error background on or off **/
   if(clicked) {
       for(i=0; i<radios.length; i++){
           errorOff(radios[i]);
       }
   }
   else {
       for(i=0; i<radios.length; i++){
           errorOn(radios[i]);
       }
   }

   return clicked;

}

/**************************************************************/
/* turn the error display on for a form field                 */
/**************************************************************/
function errorOn(field) 
{
   var newClass = field.className;
   if(newClass.indexOf("error") == -1) { 
       newClass += "error";
       field.className=newClass;
   }
}

/**************************************************************/
/* turn the error display off for a form field                */
/**************************************************************/
function errorOff(field)
{
   var newClass = field.className;
   if(newClass.indexOf("error") != -1) { 
       field.className = newClass.replace(/error/,'');
   }
}

/**************************************************************/
/* verify that the email address looks valid                  */
/**************************************************************/
function isEmail(string) {

    /* field is required so if it's blank we already have an error */
    if(trim(string).length < 1) {
        return true;
    }

    if (string.search(/^\w+((-\w+)|(\.\w+)|(_\w+)|(\+\w+))*\@[A-Za-z0-9]+((\.|-|_)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
        return true;
    }
    else {
        return false;
    }
}

/**************************************************************/
/* set focus on a form field                                  */
/**************************************************************/
function setfocus(field) {
     if(!focusset) {
        field.focus();
        focusset = true;
    }
}

/**************************************************************/
/* Check all of the required fields specified by the          */
/* hidden 'required' field on the form.                       */
/* Note:  Every required field needs an id (Netscape issue)   */
/**************************************************************/
function checkForm() {

  if(!document.getElementById){alert('no by id'); return;}

  if(!document.getElementById('required')){alert('no required'); return;}

  focusset      = false;
  var goodform  = true;
  var errormsg  = "The following blank fields are required:\n";
  var reqfields = document.getElementById('required').value.split(',');

  for(var i=0;i<reqfields.length;i++)
  {
    var fa=document.getElementsByName(reqfields[i]);  
    if(!fa){continue;}
    if(fa[0].type.toLowerCase() == 'radio') {
        var newClass = fa[0].className;
        var clicked  = false;
        for(i2=0; i2<fa.length; i2++){
          if(fa[i2].checked==true) {
             clicked = true;
          }
        }
        /** turn the error background on or off **/
        if(clicked) {
            for(i3=0; i3<fa.length; i3++){
                errorOff(fa[i3]);
            }
        }
        else {
            goodform = false;
            for(i3=0; i3<fa.length; i3++){
                errorOn(fa[i3]);
            }
            errormsg += " > " + fa[0].name.replace(/_/g," ") + "\n";
        }
    }
    else {
        var f=document.getElementById(reqfields[i]);
        if(!f){continue;}
        if(trim(f.value).length < 1) {
            errorOn(f);
            goodform = false;
            errormsg += " > " + f.name.replace(/_/g," ") + "\n";
            setfocus(f);
        }
        else { 
            errorOff(f);
        }
     }
  }

  if(goodform) {
      errormsg = "";
  }
    
  if (document.getElementById('Email')) {
      if (!isEmail(document.getElementById('Email').value)) {
          goodform = false;
          errormsg += "\nPlease Check Email Address Format\n";
          setfocus(document.getElementById('Email'));
          errorOn(document.getElementById('Email'));
      }
  }

  if (document.getElementById('Confirmation_Email')) {
      if (!isEmail(document.getElementById('Confirmation_Email').value)) {
          goodform = false;
          errormsg += "\nPlease Check Confirmation Email Address Format\n";
          setfocus(document.getElementById('Confirmation_Email'));
          errorOn(document.getElementById('Confirmation_Email'));
      }
  }

  if(!goodform) {
      alert(errormsg);
  }

  return goodform;
      
}


/**************************************************************/
/* Utility function to display form info in an alert box      */
/**************************************************************/
function formAlert() {

// This Function is currently messed up but could be fixed

var theForm = document.forms[0]
   var alertText = ""

//  for(i=0; i<theForm.elements.length; i++){
   for(i=0; i<40; i++){

      var theField = theForm.elements[i];
//      alert('type=' + theField.type);
      if(theField.type != 'hidden'  &&  theField.disabled != true) {
      //  alert('field' + i + ' not hidden or disabled');
      } else {
        alert('field' + i + ' IS hidden or disabled*****');
      }

      if(theField.type == 'text' || 'select-one' || 'textarea') { theField.focus(); }
      
   alertText += "Name: " + theForm.elements[i].name + "   Element Type: " + theForm.elements[i].type + "\n"

      if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
      alertText += "Element Value: " + theForm.elements[i].value + "\n"
      }
      else if(theForm.elements[i].type == "checkbox"){
      alertText += "Element Checked? " + theForm.elements[i].checked + "\n"
      }
      else if(theForm.elements[i].type == "select-one"){
      alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n"
      }
   }
   alert(alertText)

 //   document.forms[0].elements[0].focus();
}

