/**************************************************************************************************
 *
 * COMPANY:    KENROB & Associates, Inc.
 *
 * FILENAME:   edct.js
 * PURPOSE:    Javascript functions used by EDCT Lookup application.
 * MODIFICATION HISTORY:
 *   
 * 08/04/2004     Ram Pasala     Created
 * 03/16/2006     Ram Pasala     Modified to validate new EDCT parameters.
 * 07/19/2006     Jeff Carroll   Added function to open window for Aircraft Type Definition window.
 *
 **************************************************************************************************/
// CHECK STRING - ENSURE ALL CHARACTERS ARE ALPHA OR NUMERIC ONLY
//  (NO SPECIAL CHARACTERS)

function isAlphaNumeric(checkString, theElementName)
{
//   var filter =  /^((\d)|(\s)|[a-zA-Z-_+=.,;()'])+$/;
   var filter =  /^((\d)|[a-zA-Z])+$/;
   var msg="";

   if ( checkString != "" ) {
   	checkString = trimAll(checkString);
      if (filter.test(checkString)) {  
         return true;
      }
      else 
      {
         msg ="The "+theElementName+" field contains illegal character(s). Only alphanumeric characters  are allowed.\n";
         msg+="\nPlease re-enter the "+theElementName+" value.";
//       alert(msg);
      return false;
      }
   }
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
   {
      sString = sString.substring(1, sString.length);
   }
   while (sString.substring(sString.length-1, sString.length) == ' ')
   {
      sString = sString.substring(0,sString.length-1);
   }
   return sString;
}


function isAlphaNumeric_old(theElement, theElementName)
{
  var s = theElement.value;
  var filter=/^[a-zA-Z0-9]{1,}$/;
  if (s.length == 0 ) return true;
  if (filter.test(s))  
       return true;
  else  
         //alert("Please enter a valid alphanumeric value in the "+theElementName+" field!\nThe offending character(s) will be removed." );
    theElement.value=toAlphanumeric(theElement.value);
    theElement.focus(); 
    return false;
}

function toAlphanumeric(checkString)
{
    var newString = "";    // REVISED/CORRECTED STRING
    var count = 0;         // COUNTER FOR LOOPING THROUGH STRING

    // LOOP THROUGH STRING CHARACTER BY CHARACTER
    for (var i = 0; i < checkString.length; i++) {
        var ch = checkString.substring(i, i+1);

        // ENSURE CHARACTER IS AN ALPHA OR NUMERIC CHARACTER
        if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z") ||
            (ch >= "0" && ch <= "9")) {
            newString += ch;
        }
    }

    if (checkString != newString) {
        return newString;
    }
    return checkString;
}


/*This function handles only the MM/DD/YYYY format.
 *It also checks for Leap Year
 */
function isValidDate(theElement, theElementName)
{
    var DayArray =new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    var MonthArray = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
    var thisYear = null;
    var thisMon = null;
    var thisDay = null;
    var today = null;
    var inpDate = theElement.value;
    if (inpDate.length == 0 ) return false;
    thisMon = inpDate.substr(0,2);
    thisDay = inpDate.substr(3,2);
    thisYear = inpDate.substr(6,4);
    var filter=/^[01]{1}[0-9]{1}\/[0-3]{1}[0-9]{1}\/[0-9]{4}$/;
    if (! filter.test(inpDate))
      {   
          //alert("The " + theElementName + " field either contains invalid numbers or is not in MM/DD/YYYY format!");
          theElement.focus();
          return false;
      }
    /* Check Valid Month */
    var filter=/01|02|03|04|05|06|07|08|09|10|11|12/ ;
    if (! filter.test(thisMon))
      {
         //alert("Please enter the correct month in the "+theElementName+" field!");
         theElement.focus();
         return false;
      }
    /* Check For Leap Year */
    N=Number(thisYear);
    if ( ( N%4==0 && N%100 !=0 ) || ( N%400==0 ) )
      {
        DayArray[1]=29;
      }
    /* Check for valid days for month */
    for(var ctr=0; ctr<=11; ctr++)
      {
      if (MonthArray[ctr]==thisMon)
        {
           if (thisDay<= DayArray[ctr] && thisDay >0 )
              return true;
           else
           {
              //alert("Please enter a valid day in the " + theElementName+" field!");
              theElement.focus();
              return false;
           }
        }
    }
}

// Check if a given string contains only white space
function isblank(s) {
   for(var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
   }
   return true;
}


function findItem( f, fieldName, argName, argValue )
{
  var arg = argName;
  var arg2 = argValue;
  if ( (arg == null) || (arg == "" ) )
    arg = "p_arg_names";
  if ( (arg2 == null) || (arg2 == "") )
    arg2 = "p_arg_values";
  for(var i = 0; i < f.length - 1; i++) {
    if ( (f.elements[i].name != null) && 
         (f.elements[i].name == arg) &&
         (f.elements[i].value == fieldName) ) { 
      for(var j = i+1; j < f.length; j++) {
        if ( (f.elements[j].name == arg2) &&
             (f.elements[j].value != "ARRAYSTART") &&
             (f.elements[j].value != "ARRAYEND") )
           return (j);
      }
    }
  }
  return -1;
}

function isNull( field , fieldName) {
  selected = 0;
  fieldIsNull = 0;
  if ( field.type == "text" ||
       field.type == "password" ||
       field.type == "textarea" ) {
    if ( field.value == "" )
      fieldIsNull = 1;
  } else if ( field.type == "select-one" ) {
      if ( field.options[field.selectedIndex].value == "%null%")
        fieldIsNull = 1;
  } else if ( field.type == "select-multiple" ) {
      fieldIsNull = 1;
      for ( i = 0; i < field.length; i++ )
        if ( field.options[i].selected )
          fieldIsNull = 0;
  } else if ( field.type == "undefined" ||
              field.type == "checkbox"  ||
              field.type == "radio" ) {
      fieldIsNull = 1;
      for ( i = 0; i < field.length; i++ )
        if ( field[i].checked )
          fieldIsNull = 0;
  }
  if ( fieldIsNull ) {
      if ( isNull.arguments.length  == 1 ) 
         alert( " Value cannot be null." );
      else  
         alert( fieldName + " Value cannot be null." );
      if ( field.type == "text" ||
           field.type == "textarea"  ||
           field.type == "password"  ||
           field.type == "select-one"  ||
           field.type == "select-multiple" )
        field.focus();
     return false;
  }
  
  return true;
}

function FieldUpperCase(field)
{
   var strUpper;
   if ( field.value != "" ){
      strUpper = field.value.toUpperCase();
      field.value = strUpper;
   }
   return true;
}

function checkAll(){
  var idx;
  
  idx = 0;
  
  if (!isNull( document.FORM_EDCT.elements[idx], "Call Sign *")) {
    return false;
   }
  
  if (!isAlphaNumeric( document.FORM_EDCT.elements[idx], "Call Sign *")) {
    return false;
   }

  idx = 1;
  
  if (!isNull( document.FORM_EDCT.elements[idx], "Departure *")) {
    return false;
   }
  
  if (!isAlphaNumeric( document.FORM_EDCT.elements[idx], "Departure *")) {
    return false;
   }

  idx = 2;
  
  if (!isNull( document.FORM_EDCT.elements[idx], "Arrival *")) {
    return false;
   }
  
  if (!isAlphaNumeric( document.FORM_EDCT.elements[idx], "Arrival *")) {
    return false;
   }
   
   idx = 3;

  if (!isNull( document.FORM_EDCT.elements[idx], "Date")) {
    return false;
   }
   
   if (!isValidDate( document.FORM_EDCT.elements[idx], "Date")) {
     return false;
   }

  idx = 4;
  
  if (!isNull( document.FORM_EDCT.elements[idx], "Aircraft type *")) {
    return false;
   }
  
  if (!isAlphaNumeric( document.FORM_EDCT.elements[idx], "Aircraft type *")) {
    return false;
   }
   
  return true;
}


function validateEdct(form) {
   var text;
   var empty_fields = "";
   var errors = "";
   
// Loop through the elements of the form, looking for all text and textarea elements that dont
// have an "optional" property defined. Then, check for fields that are empty and make a list of them.
// Also, if any of these elements have a "min" or a "max" property defined, then verify that they are
// numbers and that they are in the right range. Put together error messages for fields that are wrong.
// alert("validate form");

   for(var i = 0; i < form.length - 2; i++) {
      var e = form.elements[i];

   // first check if the field is empty
      if ((e.value == null) || (e.value == "") || isblank(e.value)) {
         empty_fields += "\n        " + e.title;
         continue;
      }

      if(e.name == "callsign") {
         if (isAlphaNumeric(e.value, e.name) == false) {
            errors += "- The " + e.title + " field contains one or more illegal characters. Only alphanumeric characters are allowed.\n";
            e.focus();
         }
      }

      if(e.name == "dept") {
         if (isAlphaNumeric(e.value, e.name) == false) {
            errors += "- The " + e.title + " field contains one or more illegal characters. Only alphanumeric characters are allowed.\n";
            e.focus();
         }
      }

      if(e.name == "arr") {
         if (isAlphaNumeric(e.value, e.name) == false) {
            errors += "- The " + e.title + " field contains one or more illegal characters. Only alphanumeric characters are allowed.\n";
            e.focus();
         }
      }

      if(e.name == "actype") {
         if (isAlphaNumeric(e.value, e.name) == false) {
            errors += "- The " + e.title + " field contains one or more illegal characters. Only alphanumeric characters are allowed.\n";
            e.focus();
         }
      }

   }

   
   if (!empty_fields && !errors) {
      return true;
   }


   text  = "___________________________________________________________________________\n\n";
   text += "The form has not been submitted because of the following error(s).\n";
   text += "Please correct these error(s) and re-submit the form.\n"   
   text += "___________________________________________________________________________\n\n";

   if (empty_fields) {
      text += "- The following required field(s) are empty:"
         + empty_fields + "\n";
   if (errors) text += "\n";
   }
   text += errors;
   alert(text);
   return false;
}

// Open a window to the page with details about all the various Aircraft Types.
function aircraftTypeDetails() {
   var goto_page = "/edct/jsp/ac_types.jsp";
   acTypeWindow=window.open('','window3',  'toolbar=no,scrollbars=yes, resizable=yes, width=800, height=600');
   acTypeWindow.location.href=goto_page;
   acTypeWindow.focus();
}
