var formNames = new Array('firstName',
                          'lastName',
                          'addressLine1',
                          'listAddressLine1',
                          'billAddressLine1',
                          'shipAddressLine1',
                          'addressLine2',
                          'listAddressLine2',
                          'billAddressLine2',
                          'shipAddressLine2',
                          'city',
                          'listCity',
                          'billCity',
                          'shipCity',
                          'USState',
                          'listUSState',
                          'billUSState',
                          'shipUSState',
                          'otherState',
                          'listOtherState',
                          'billOtherState',
                          'shipOtherState',
                          'country',
                          'listCountrySel',
                          'billCountrySel',
                          'shipCountrySel',
                          'otherCountry',
                          'listOtherCountry',
                          'billOtherCountry',
                          'shipOtherCountry',
                          'zip',
                          'USZip',
                          'listUSZip',
                          'billUSZip',
                          'shipUSZip',
                          'otherZip',
                          'listOtherZip',
                          'billOtherZip',
                          'shipOtherZip',
                          'email',
                          'loginName',
                          'email2',
                          'cardType',
                          'cardNumber',
                          'expMonth',
                          'expYear',
                          'nameOnCard',
                          'phone',
                          'phoneType',
                          'fax',
                          'password',
                          'passwordAgain',
                          'oldPassword',
                          'newPass1',
                          'newPass2',
                          'title',
                          'jobTitle',
                          'jobDescription',
                          'qualification',
                          'contactInformation',
                          'department',
                          'organization',
                          'orgType',
                          'orgTypeCd',
                          'orgDesType',
                          'orgName',
                          'orgDescription',
                          'discplCode2',
                          'disciplineCode',
                          'disciplineCd',
                          'disciplineCode0',
                          'comments',
                          'webComment',
                          'web_addr',
                          'minYearsReqd',
                          'experienceReq',
                          'confRegType',
                          'newsSubscrFlag',
                          'keywords',
                          'names',
                          'genericParameter0',
                          'genericParameter1',
                          'genericParameter2',
                          'genericParameter3'
                         );

var formMessage = new Array('The first name contains invalid characters.\n',
                            'The last name contains invalid characters.\n',
                            'The first address contains invalid characters.\n',
                            'The first address contains invalid characters.\n',
                            'The first billing address contains invalid characters.\n',
                            'The first shipping address contains invalid characters.\n',
                            'The second address contains invalid characters.\n',
                            'The second address contains invalid characters.\n',
                            'The second billing address contains invalid characters.\n',
                            'The second shipping address contains invalid characters.\n',
                            'The city contains invalid characters.\n',
                            'The city contains invalid characters.\n',
                            'The billing city contains invalid characters.\n',
                            'The shipping city contains invalid characters.\n',
                            'Please select a U.S. state.\n',
                            'Please select a U.S. state.\n',
                            'Please select a U.S. billing state.\n',
                            'Please select a U.S. shipping state.\n',
                            'The non-U.S. state contains invalid characters.\n',
                            'The non-U.S. state contains invalid characters.\n',
                            'The non-U.S. billing state contains invalid characters.\n',
                            'The non-U.S. shipping state contains invalid characters.\n',
                            'Please select a country.\n',
                            'Please select a country.\n',
                            'Please select a billing country.\n',
                            'Please select a shipping country.\n',
                            'Please select a country.\n',
                            'Please select a country.\n',
                            'Please select a billing country.\n',
                            'Please select a shipping country.\n',
                            'The zip code contains invalid characters.\n',
                            'The zip code contains invalid characters.\n',
                            'The zip code contains invalid characters.\n',
                            'The billing zip code contains invalid characters.\n',
                            'The shipping zip code contains invalid characters.\n',
                            'The postal code contains invalid characters.\n',
                            'The postal code contains invalid characters.\n',
                            'The billing postal code contains invalid characters.\n',
                            'The shipping postal code contains invalid characters.\n',
                            'The email address is invalid.\n',
                            'The email address is empty or invalid.\n',
                            'The email address is invalid.\n',
                            'Please select a credit card type.\n',
                            'The credit card number is invalid.\n',
                            'The expiration month is invalid.\n',
                            'The expiration year is invalid.\n',
                            'The name on the credit card contain invalid characters.\n',
                            'The phone number contains invalid characters.\n',
                            'Please select a phone type.\n',
                            'The fax number contains invalid characters.\n',
                            "The password is empty or contains invalid characters.\nValid password characters include letters, numbers, and ,./?';:\|=+-_)(*&^%$#@!~` ;\n",
                            'The second password entry is empty or does not match the first password entered.\n',
                            "The old password is empty or contains invalid characters.\nValid password characters include letters, numbers, and ,./?';:\|=+-_)(*&^%$#@!~` ;\n",
                            "The new password is empty or contains invalid characters.\nValid password characters include letters, numbers, and ,./?';:\|=+-_)(*&^%$#@!~` ;\n",
                            'The second password entry is empty or does not match the first password entered.\n',
                            'The title contains invalid characters.\n',
                            'The job title contains invalid characters.\n',
                            'The job description contains invalid characters.\n',
                            'The qualifications contain invalid characters.\n',
                            'The contact information contains invalid characters.\n',
                            'The department contains invalid characters.\n',
                            'The organization contains invalid characters.\n',
                            'Please select a classification for your organization.\n',
                            'Please select a classification for your organization.\n',
                            'Please select the organization name or description.\n',
                            'The organization name contains invalid characters.\n',
                            'The organization description contains invalid characters.\n',
                            'Please select one or more disciplines.\n',
                            'Please select one or more disciplines.\n',
                            'Please select one or more disciplines.\n',
                            'Please select one or more disciplines.\n',
                            'The comments contain invalid characters.\n',
                            'The comments contain invalid characters.\n',
                            'The URL contains invalid characters.\n',
                            'Please select the experience required.\n',
                            'Please select the experience required.\n',
                            'Please select a type of registration.\n',
                            'Please select the type of news subscription.\n',
                            'The word or phrase is empty or contains invalid characters.\n',
                            'The name is empty or contains invalid characters.\n',
                            'Please select a membership type.\n',
                            'The comments contain invalid characters.\n',
                            'genericParameter2 not valid.\n',
                            'genericParameter3 not valid.\n'
                           );


function getErrorMessage(formElementName) {
   var k;
   for(k=0;k<formNames.length;k++) {
     if (formElementName == formNames[k]) {
      var m = formMessage[k];
      return m; 
     }
   }
   return 'No message for '+formElementName + '\n';
}

function isNumberc(num,extra) {
  var s = "0123456789"+extra;
  var len = s.length;
  var number = num;
  for(a=0;a<number.length;a++) {
   var isnum = 1;
   for(y=0;y<len;y++) {
    if(s.charAt(y)==number.charAt(a)) isnum=0;
   }
   if (isnum==1) return false;
  }
 return true;
}

function isAlphac(str,extra) {
  var s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+extra;
  var len = s.length;
  var text = str;
  for(a=0;a<text.length;a++) {
   var isnum = 1;
   for(y=0;y<len;y++) {
    if(s.charAt(y)==text.charAt(a)) isnum=0;
   }
   if (isnum==1) return false;
  }
 return true;
}

function isTextc(str,disallow) {
  for (var i=0; i < str.length; i++) {
    if(disallow.indexOf(str.charAt(i)) != -1) {
      return false;
    }
  }
  return true;
}

function trim(trimString) {
  var startPosition = 0;
  for (var i = 0; i < trimString.length; i++) {
    if (trimString.charAt(i) != " ") {
      startPosition = i;
      break;
    }
  }
  var endPosition = -1;
  for (var i = (trimString.length - 1); i >= 0; i--) {
    if (trimString.charAt(i) != " ") {
      endPosition = i;
      break;
    }
  }
  endPosition++;
  trimString = trimString.substr(startPosition, (endPosition - startPosition));
  return trimString;
}

//
//isValidEmail validate an email address
//

function isValidEmail(email)
{
    var emailOk  = true;
    var atSymbol = email.indexOf('@');
    var period   = email.lastIndexOf('.');
    var space    = email.indexOf(' ');
    var length   = email.length - 1;  // Array is from 0 to length-1

    if ((atSymbol < 1) ||                  // '@' cannot be in first position
        (period <= atSymbol+1) ||             // Must be atleast one valid char btwn '@' and '.'
        (period == length ) ||             // Must be atleast one valid char after '.'
        (space  != -1))                    // No empty spaces permitted
       {
          emailOk = false;
       }
    return emailOk;
}

//
//getCheckedRadioButton returns the value of the selected radio button
//
function getCheckedRadioButton(buttons) {
  var i;
  for (i = 0; i < buttons.length; i++) {
    if (buttons[i].checked == "1") {
      return buttons[i].value;
    }
  }
}

//
//getCheckedCheckboxes returns the values of the selected check boxes
//
function getCheckedCheckboxes(boxes,values) {
  var i;
  for (i = 0; i < boxes.length; i++) {
    if (boxes[i].checked == "1") {
      values[values.length] = boxes[i].value;
    }
  }
}

//
//getListboxSelection returns the value of the selection in a single select
//  list box
//

function getListboxSelection(list) {
  return list[list.selectedIndex].value;
}


function dateCheck(w_date) {

  /*

  11162000 tjm

  Does some rudimentary checks on a date

  Rules:
      must be in the format of mm/dd/yyyy
      1 <= w_month <= 12
      1 <= w_days  <= Appropriate # of days for that month/year combo
      year>=1000
      
  */

  var w_ok=true;
  var w_d=w_date.toString();

  if (w_d.length!=10) w_ok=false;
  if (!allDigitsDate(w_d)) w_ok=false;

  // Extract values
  var w_year=parseInt(w_date.substring(6,10), 10);
  var w_days=parseInt(w_date.substring(3,5), 10)
  var w_month=parseInt(w_date.substring(0,2), 10);

  if (w_month<1 || w_month>12) w_ok=false;
  if (w_days<1 || w_days>getDaysInMonth(w_month,w_year) ) w_ok=false;

  if (w_year<1000)   // 4 digit years
     w_ok=false;
     
  return w_ok;

}

function getDaysInMonth(month,year)  {

    var days;
    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
        month==10 || month==12)  days=31;
    else if (month==4 || month==6 || month==9 || month==11) days=30;
    else if (month==2)  {
        if (isLeapYear(year)) {
            days=29;
        }
        else {
            days=28;
        }
    }
    return (days);
}

function isLeapYear (Year) {

    // If it's not a century and it's divisible by 4, it's a leap year
    // Centuries must be divisible by 400

    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
        return (true);
    }
    else {
        return (false);
    }
}

function allDigitsDate(w_str) {

   // Returns true if a string is all 0..9

   w_str=String(w_str);
   for (var i=0;i < w_str.length;i++) {
      var w_ch=w_str.substring(i,i + 1);
      if ((w_ch < '0' || w_ch > '9') &&  w_ch!='/')
         return false;
   }

   return true;
}
