/******
 11/03/06 - removed One-Way option for charter quote in function setUpTravel
 ******/

/**************************************************************/
/**************************************************************/
/* Form specific functions for Business Jet                   */
/*   - bj.js contains general functions                       */
/**************************************************************/
/**************************************************************/

ns4 = document.layers;
ie4 = document.all;
nn6 = document.getElementById && !document.all;
focusset = false;

/** These variables are used to set the 'required' and 'ignore' hidden fields. **/
var roundTripReq = "Departing_From,Departing_Date,Arriving_At,Return_Date,";
var roundTripOpt = "Departure_Time,Return_Time,";
var oneWayReq    = "Departing_From,Departing_Date,Arriving_At,";
var oneWayOpt    = "Departure_Time,";
var multiCityReq = "From1,To1,Date1,From2,To2,Date2,From3,To3,Date3,";
var multiCityOpt = "Time1,Time2,Time3,From4,To4,Date4,Time4,";

/**************************************************************/
/* preload the form field images (in case fields hidden)      */
/**************************************************************/
function preloadFields() {

 if (document.images) {
   img1  = new Image();
   img1.src = '/images/blueroundlong.jpg';
   img2  = new Image();
   img2.src = '/images/blueroundmed.jpg';
   img3  = new Image();
   img3.src = '/images/blueroundmed2.jpg';
   img4  = new Image();
   img4.src = '/images/blueroundshort.jpg';
   img5  = new Image();
   img5.src = '/images/blueroundxshort.jpg';
   img6  = new Image();
   img6.src = '/images/bluetextarea.jpg';
 }

}

/**************************************************************/
/* 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;
        }
     }
  }

}

/**************************************************************/
/* Insert fixed attachment for form background images.        */
/* This is needed for IE browser (maybe others?) to prevent   */
/* the form field background from scrolling.                  */
/**************************************************************/
/* Call this function in the HEAD of all form pages.          */
/**************************************************************/
function insertFixed() {

   if(navigator.appName == "Microsoft Internet Explorer") {
       document.write("<STYLE>input.textlong {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>input.textlongerror {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>input.textmed {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>input.textmed2 {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>input.textmed2error {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>input.textmederror {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>input.textshort {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>input.textshorterror {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>input.textxshort {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>input.textxshorterror {background-attachment:fixed;}</STYLE>");
       document.write("<STYLE>textarea {background-attachment:fixed;}</STYLE>");
   }
}

/**************************************************************/
/* Reset the quote form and then reset the dynamic fields.    */
/**************************************************************/
function resetQuoteForm()
{
    document.quoteform.reset();
    setUpTravel();
    setFirstFocus();
}
    
/**************************************************************/
/* Set up the appropriate from/to/date/time fields based on   */
/* the Type of Travel field (this is needed for Refresh)      */
/**************************************************************/
function setUpTravel()
{
    if(document.quoteform.Travel_Type[0].checked) {
        setUpRoundTrip();
    }
    else {
 // REMOVED ONE-WAY FOR NOW (was type[1])
 //       if(document.quoteform.Travel_Type[1].checked) {
 //           setUpOneWay();
 //       }
 //       else {
            if(document.quoteform.Travel_Type[1].checked) {
                setUpMulti();
            }
 //       }    
    }

    /* check HowLearned and set up Specify as needed */
    checkHow(document.getElementById('How_Learned'),true); 
}
        
/**************************************************************/
/* Set up the from/to/date/time fields for Round-Trip travel. */
/* Also set up 'required' and 'ignore' hidden form fields.    */
/**************************************************************/
function setUpRoundTrip()
{
    document.getElementById('rtow').style.display = "block";
    document.getElementById('ReturnDate').style.visibility = "visible";
    document.getElementById('ReturnTime').style.visibility = "visible";
    document.getElementById('mc').style.display = "none";
    
    /* Set the "required" field value for Round-Trip */
    var addReq = "Departing_From,Departing_Date,Arriving_At,Return_Date,";
    var coreReq = document.getElementById('coreRequired').value;
    document.getElementById('required').value = roundTripReq + coreReq;
    document.getElementById('ignore').value = multiCityReq + multiCityOpt;
    setSpecifyReq();
}

/**************************************************************/
/* Set up the from/to/date/time fields for One-Way travel.    */
/* Also set up 'required' and 'ignore' hidden form fields.    */
/**************************************************************/
function setUpOneWay()
{
    document.getElementById('rtow').style.display = "block";
    document.getElementById('ReturnDate').style.visibility = "hidden";
    document.getElementById('ReturnTime').style.visibility = "hidden";
    document.getElementById('mc').style.display = "none";
    
    /* Set the "required" field value for One-Way */
    var coreReq = document.getElementById('coreRequired').value;
    document.getElementById('required').value = oneWayReq + coreReq;
    document.getElementById('ignore').value = multiCityReq + multiCityOpt + 'Return_Date';
    setSpecifyReq();
}

/**************************************************************/
/* Set up the from/to/date/time fields for Multi-City travel. */
/* Also set up 'required' and 'ignore' hidden form fields.    */
/**************************************************************/
function setUpMulti()
{
    document.getElementById('mc').style.display = "block";
    document.getElementById('rtow').style.display = "none";
    
    /* Set the "required" field value for Multi-City */
    var coreReq = document.getElementById('coreRequired').value;
    document.getElementById('required').value = multiCityReq + coreReq;
    document.getElementById('ignore').value = roundTripReq + roundTripOpt;
    setSpecifyReq();
}

/**************************************************************/
/* Specify is required if How_Learned is friend, pub or other */
/**************************************************************/
function setSpecifyReq()
{
    if(document.getElementById('How_Learned').value    == 'Friend'
       || document.getElementById('How_Learned').value == 'Publication'
       || document.getElementById('How_Learned').value == 'Other') {
       if(document.getElementById('required').value.indexOf("Specify") == -1) { 
           var curReq = document.getElementById('required').value;
           document.getElementById('required').value = curReq + ',Specify';
       }
    }
    else {
       var newReq = document.getElementById('required').value.replace(",Specify","");
       document.getElementById('required').value = newReq;
    }
}

/**************************************************************/
/* 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;
}

/**************************************************************/
/* Load the From of Leg2 with the To of Leg1                  */
/**************************************************************/
function loadNext(number)
{
    var to = 'To'+number;
    var nextFrom = 'From'+(number+1);
    if(trim(document.getElementById(nextFrom).value).length < 1) { 
        document.getElementById(nextFrom).value = document.getElementById(to).value;
    }
}

/**************************************************************/
/* 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/,'');
   }
}

/**************************************************************/
/* show 'Specify' under certain conditions                    */
/* "load" means we came from onLoad setup - don't set focus.  */
/**************************************************************/
function checkHow(field,load) {
    if(field.value == 'Friend'
       || field.value == 'Publication'
       || field.value == 'Other') {
       document.getElementById('Specify').style.visibility = "visible";
       document.getElementById('specLbl').style.visibility = "visible";
       if(!load) {
           document.getElementById('Specify').focus();
       }
       setSpecifyReq();
    }
    else {
       document.getElementById('Specify').value = "";
       document.getElementById('Specify').style.visibility = "hidden";
       document.getElementById('specLbl').style.visibility = "hidden";
       if(!load) {
           document.getElementById('Comments').focus();
       }
       setSpecifyReq();
    }
}

/**************************************************************/
/* 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\n";
          setfocus(document.getElementById('Email'));
          errorOn(document.getElementById('Email'));
      }
  }

  if(!goodform) {
      alert(errormsg);
  }

  return goodform;
      
}


/**************************************************************/
/* Read all the fields and edit the ones flagged as emails    */
/* Note: this function is used by both contactsMaint.php and  */
/*       deptMaint.php.  If changes are made to the function  */
/*       please test both pages (might need to clone this).   */
/**************************************************************/
function checkContactMaintForm(inForm) {

  var e = inForm.elements;
  var val;
  var errorCnt = 0;
  var lineName;
  var lineField;
  focusset     = false;

  for (var i=0; i < e.length; i++) {
      /* skip over <fieldset> which causes problems */
      if(typeof(e[i].type) == 'undefined') {
      }
      else {
          val = e[i].value;
          if(val.indexOf("Email_Link") > -1) {
              lineName  = val.replace(/Email_Link/,'Line');
              lineField = document.getElementById(lineName);
              if(lineField.value.length < 1) {
                  e[i].checked = false;
                  errorOff(lineField);
                  continue;
              }
              if(!e[i].checked) {
                  errorOff(lineField);
                  continue;
              }
              if(isEmail(lineField.value)) {
                  errorOff(lineField);
              }
              else {
                  setfocus(lineField);
                  errorOn(lineField);
                  errorCnt++;
              }
          }
      }    
   }

   if(errorCnt > 0) {
       alert('Invalid Email Address Found (' + errorCnt + ').  Please try again. ');
       return false;
   }
   else {
       return true;
   }

}

