
//============================================
// testCheckbox
//============================================

function testCheckbox (rcbInput,rstrErrMsg){
 
    var intSelections = 0;
    var inputLength = rcbInput.length;

    if (inputLength == undefined) {
        if(rcbInput.checked) {
            intSelections = 1
        } 
    } else {
        for (i = 0; i < inputLength; i++) {
            if (rcbInput[i].checked){ 
              intSelections = intSelections + 1; 
            }
        }
    }
    if (intSelections < 1 ) {
       alert (rstrErrMsg);
       return (false);
    } else {
       return (true);
    }
}

function getCheckedValue(rcbInput) {
    if (!rcbInput) {
        return "";
    }
    var inputLength = rcbInput.length;
    if (inputLength == undefined) {
        if(rcbInput.checked) {
            return rcbInput.value;
        } else {
          return "";
        }
    }
    for(var i = 0; i < inputLength; i++) {
      if(rcbInput[i].checked) {
        return rcbInput[i].value;
      }
    }
    return "";
}


//============================================
// testDate
//============================================
function testDate(field1) {
   var fLength = field1.value.length;                          // length of field1 in characters.
   var divider_values = new Array ('-','.','/',' ',':','_',','); // array to hold date seperators. 
   var array_elements = 7;                                     // number of divider_values.
   var day1 = new String(null);                                // day value holder
   var month1 = new String(null);                              // month value holder
   var year1 = new String(null);                               // year value holder
   var divider1 = null;                                        // divider holder
   var outdate1 = null;                                        // formatted date to return
   var counter1 = 0;                                           // counter for divider looping 
   var divider_holder = new Array ('0','0','0');               // array for positions of dividers
   var s = String(field1.value);                               // supplied date value variable

// If field is empty do nothing
   if ( fLength == 0 ) {
      return true;
   }
   
// Deal with today or now
   if ( field1.value.toUpperCase() == 'NOW' || field1.value.toUpperCase() == 'TODAY' ) {
      var newDate1 = new Date();
      if (navigator.appName == "Netscape") {
         var myYear1 = newDate1.getYear() + 1900;
         }
      else {
         var myYear1 =newDate1.getYear();
      }
      var myMonth1 = newDate1.getMonth()+1;
      var myDay1 = newDate1.getDate();
      field1.value = myDay1 + "/" + myMonth1 + "/" + myYear1;
      fLength = field1.value.length;                            //re-evaluate string length.
      s = String(field1.value)                                  //re-evaluate string value.
   }

// Check the date is the required length
   if ( fLength != 0 && (fLength < 5 || fLength > 11) ) {
      invalid_date(field1);
      return false;
   }

// Find position and type of divider in the date
   for ( var i=0; i<3; i++ ) {
      for ( var x=0; x<array_elements; x++ ) {
         if ( s.indexOf(divider_values[x], counter1) != -1 ) {
            divider1 = divider_values[x];
            divider_holder[i] = s.indexOf(divider_values[x], counter1);
            counter1 = divider_holder[i] + 1;
            break;
         }
      }
   }

// if element 2 is not 0 then more than 2 dividers have been found so date is invalid.
   if ( divider_holder[2] != 0 ) {
      invalid_date(field1);
      return false;
   }

// Process dates with no dividers
   if ( divider_holder[0] == 0 && divider_holder[1] == 0 ) { 
      if ( fLength == 6 ) {                                       //ddmmyy
         day1 = field1.value.substring(0,2);
         month1 = field1.value.substring(2,4);
         	year1 = field1.value.substring(4,6);
         	if ( (year1 = convert_year(year1)) == false ) {
            invalid_date(field1);
            return false;
          }
      }
      else if ( fLength == 7 ) {                                  //ddmmmyy
         day1 = field1.value.substring(0,2);
         month1 = field1.value.substring(2,3);
         year1 = field1.value.substring(5,7);
         if ( (month1 = convert_month(month1)) == false ) {
            invalid_date(field1);
            return false; 
         }
         if ( (year1 = convert_year(year1)) == false ) {
            invalid_date(field1);
            return false; 
         }
      }
      else if ( fLength == 8 ) {                                 //ddmmyyyy
         day1 = field1.value.substring(0,2);
         month1 = field1.value.substring(2,2);
         year1 = field1.value.substring(4,8);
      }
      else if ( fLength == 9 ) {                                 //ddmmmyyyy
         day1 = field1.value.substring(0,2);
         month1 = field1.value.substring(2,3);
         year1 = field1.value.substring(5,9);
         if ( (month1 = convert_month(month1)) == false ) {
            invalid_date(field1);
            return false; 
         }
      }

      //.. if invalid .. display an error message
      if ( (outdate1 = validate_date(day1,month1,year1)) == false ) {
         invalid_date(field1);
         return false;
      }
 
      //.. all OK .. so insert reformatted date
      field1.value = outdate1;
      return true; 
}
		
// Process dates with two dividers	
   if ( divider_holder[0] != 0 && divider_holder[1] != 0 ) {
      day1 = field1.value.substring(0, divider_holder[0]);
      month1 = field1.value.substring(divider_holder[0] + 1, divider_holder[1]);
      year1 = field1.value.substring(divider_holder[1] + 1, field1.value.length);
   }

// Check day and year are numeric
   if ( isNaN(day1) && isNaN(year1) ) { 
      invalid_date(field1);
      return false;
   }

// Make d day dd
   if ( day1.length == 1 ) {
      day1 = '0' + day1;
   }

// Make m month mm
   if ( month1.length == 1 ) {
      month1 = '0' + month1; 
   }

// Make y year to yy
   if ( year1.length == 1 ) {
      year1 = '0' + year1; 
   }

// Make yy year yyyy
   if ( year1.length == 2 ) {
      if ( (year1 = convert_year(year1)) == false ) {
 	      invalid_date(field1);
  	      return false;
      }
   }

// Convert alpha month to nn
   if ( month1.length == 3 || month1.length == 4 ) {
      if ( (month1 = convert_month(month1)) == false) {
         invalid_date(field1);
         return false;
      }
   }

// Check if all date components are correct length
   if ( (day1.length == 2 || month1.length == 2 || year1.length == 4) == false) {
      invalid_date(field1);
      return false;
   }

// If invalid .. display an error message
   if ( (outdate1 = validate_date(day1, month1, year1)) == false ) {
      invalid_date(field1);
      return false;
   }

// All OK .. so insert reformatted date
   field1.value = outdate1;
   return true;
}

/******************** convert_month *******************************
 Purpose:  Converts mmm or mmmm month to mm

 Parms:   monthIn .............. Month to be converted

*******************************************************************/
function convert_month(monthIn) {

   var month_values = new Array ("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

   monthIn = monthIn.toUpperCase(); 
   if ( monthIn.length == 3 ) {
      for ( var i=0; i<12; i++ ) {
         if ( monthIn == month_values[i] ) {
            monthIn = i + 1;
            if ( i != 9 && i != 10 && i != 11 ) {
                monthIn = '0' + monthIn;
            }
            return monthIn;
         }
       }
   }
   else if ( monthIn.length == 4 && monthIn == 'JUNE') {
      monthIn = '06';
      return monthIn;
   }
   else if ( monthIn.length == 4 && monthIn == 'JULY') {
      monthIn = '07';
      return monthIn;
   }
   else if ( monthIn.length == 4 && monthIn == 'SEPT') {
      monthIn = '09';
      return monthIn;
   }
   else {
      return false;
   } 
}

/********************* invalid_date *******************************
 Purpose:  Produces an Error Message if date is invalid
           - Resets focus on invalid field
           - Reselects invalid field for editing

 Parms:   inField .............. Field that is invalid

*******************************************************************/
function invalid_date(inField) {
   alert("The value " + inField.value + " is not a vaild date.\n\r" + 
      "Please enter date in the format dd-mmm-yyyy");
   inField.focus();
   inField.select();
   return true 
}

/******************** validate_date *******************************
 Purpose:  Validates date
           - Checks day is valid for month (including leap years)
           - Checks month is valid 01->12

 Parms:   day2 ................. Day of date to be validated 
          month2 ............... Month of date to be validated
          year2 ................ Year of date to be validated

*******************************************************************/

function validate_date(day2, month2, year2) {  
   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 inpDate = day2 + month2 + year2; 
   var filter=/^[0-9]{2}[0-9]{2}[0-9]{4}$/;  
   var months_mmm = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

//Check ddmmyyyy date supplied
   if (! filter.test(inpDate)) { 
     return false;  
   }

// Check Valid Month  
   filter=/01|02|03|04|05|06|07|08|09|10|11|12/ ; 
   if (! filter.test(month2)) { 
     return false; 
   } 

// Check For Leap Year  
   var N = Number(year2); 
   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]==month2) { 
         if (day2<= DayArray[ctr] && day2 >0 ) {
//          inpDate = month2 + '/' + day2 + '/' + year2; 
            inpDate = day2 + "-" + months_mmm[ctr] + "-" + year2;
            return inpDate;
         }  
         else { 
            return false; 
         }  
      }  
   }
}

/******************** convert_year *******************************
 Purpose:  converts yy years to yyyy
            - Uses a hinge date of 10 .. < 10 = 20yy .. => 10 = 19yy.

 Parms:   inYear ............... Year to be converted 		

*******************************************************************/
function convert_year(inYear) {
   if ( inYear < 10 ) {
      inYear = "20" + inYear;
      return inYear;
   }
   else if ( inYear >= 10 ){
      inYear = "19" + inYear;
      return inYear;
   }
   else {
      return false;
   } 
}
//============================================
// testDate1B42
//============================================
function testDate1B42 (rstrDate1, rstrDate2, rstrErrorMessage) {

// make sure that the dates are valid and pre-format them .. otherwise return
   if (!testDate(rstrDate1)) return;
   if (!testDate(rstrDate2)) return;

// create two strings from the converted dates in the format yyyymmdd
   var strDate1 = rstrDate1.value.substring(7,11) + convert_month(rstrDate1.value.substring(3,6)) +
         	         rstrDate1.value.substring(0,2);
   var strDate2 = rstrDate2.value.substring(7,11) + convert_month(rstrDate2.value.substring(3,6)) +
         	         rstrDate2.value.substring(0,2);

// now check to see if 1 is greater than 2
   if (strDate1 > strDate2){ 
      alert(rstrErrorMessage);
      return(false)
   }
  else
     return (true);
}

//============================================
// testNumeric
//============================================
function testNumeric(Ctrl,ErrorMessage) {
   var strString = Ctrl.value;
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   for (i = 0; i < strString.length && blnResult == true; i++) {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1) {
         blnResult = false;
         alert(ErrorMessage);
         Ctrl.focus();
         Ctrl.select();
      }
   }
   return blnResult;
}
//============================================
// testPullDown
//============================================
function testPullDown(Ctrl,ErrorMessage) {
   if (Ctrl.selectedIndex == 0){ 
      alert(ErrorMessage);
      Ctrl.focus();
      return(false)
   }
  else
     return (true);
}
//============================================
// testRequired
//============================================
function testRequired(Ctrl,ErrorMessage) {
   if (Ctrl.value == ""){ 
      alert(ErrorMessage);
      Ctrl.focus();
      Ctrl.select();
      return(false)
   }
  else
     return (true);
}

function testFieldLength(Ctrl,maxLength,FieldName) {
   if(Ctrl.value.length > maxLength) {
         alert(FieldName + " can not be longer than " + maxLength + " characters.");
         Ctrl.focus();
         Ctrl.select();
	     return(false);
   }
   return(true);
}

//============================================
// testSSN
//============================================
function testSSN(Ctrl, ErrorMessage) {
    var strSSN = Ctrl.value;
    var matchArr = strSSN.match(/^(\d{3})-?\d{2}-?\d{4}$/);
    var numDashes = strSSN.split('-').length - 1;
    if (matchArr == null || numDashes == 1) {
        alert(ErrorMessage);
        Ctrl.focus();
        Ctrl.select();
        return false;
    }
    if (numDashes == 0) {
       Ctrl.value = strSSN.charAt(0) + strSSN.charAt(1) + strSSN.charAt(2) + "-" + strSSN.charAt(3) +
                    strSSN.charAt(4) + "-" + strSSN.charAt(5) + strSSN.charAt(6) + strSSN.charAt(7) +
                    strSSN.charAt(8)
    }
    return true
}


