///////////////////////////////////////////////////////////////////////////////
// Available functions.
///////////////////////////////////////////////////////////////////////////////
//
// isValidDate(textBoxObject day, textBoxObject month, textBoxObject year, canBeNull, (Optional)customErrorMessage);
//
///////////////////////////////////////////////////////////////////////////////

var alertsOn = true;

function setAlerts(val){
   alertsOn = val;
}

function sendAlert(message, customMessage){
   if(alertsOn){
      if(customMessage != null){
         alert(customMessage);
      }
      else{
         alert(message);
      }
   }
}

///////////////////////////////////////////////////////////////////////////////
// Date validation functions.
///////////////////////////////////////////////////////////////////////////////
   function isValidDate(dayObj, monthObj, yearObj, canBeNull, customMessage){
      if(canBeNull && dayObj.value.length == 0  && monthObj.value.length == 0 && yearObj.value.length == 0){
         return true;
      }
      // zero in the first array value is because we index by month. January is 1 so we index value 1 in the array.
      var daysInMonth = [0,31,0,31,30,31,30,31,31,30,31,30,31];
      daysInMonth[2] = daysInFebruary(yearObj.value);

      // We need to check the month before the day to make sure the day is valid for that month.
      if(!isValidMonth(monthObj.value)){
         sendAlert("Please enter a valid month.", customMessage);
         monthObj.focus();
         return false;
      }
      if(!isValidDay(dayObj.value, daysInMonth[parseInt(monthObj.value,10)])){
         sendAlert("Please enter a valid day.", customMessage);
         dayObj.focus();
         return false;
      }
      if(!isValidYear(yearObj.value)){
         sendAlert("Please enter a valid year.", customMessage);
         yearObj.focus();
         return false;
      }

      return true;
   }

   function isValidDate2(dayObj, monthObj, yearObj, canBeNull, canBePast, customMessage){
      if(canBeNull && dayObj.value.length == 0  && monthObj.value.length == 0 && yearObj.value.length == 0){
         return true;
      }
      // zero in the first array value is because we index by month. January is 1 so we index value 1 in the array.
      var daysInMonth = [0,31,0,31,30,31,30,31,31,30,31,30,31];
      daysInMonth[2] = daysInFebruary(yearObj.value);

      // We need to check the month before the day to make sure the day is valid for that month.
      if(!isValidMonth(monthObj.value)){
         sendAlert("Please enter a valid month.", customMessage);
         monthObj.focus();
         return false;
      }
      if(!isValidDay(dayObj.value, daysInMonth[parseInt(monthObj.value)])){
         sendAlert("Please enter a valid day.", customMessage);
         dayObj.focus();
         return false;
      }
      if(!isValidYear(yearObj.value)){
         sendAlert("Please enter a valid year.", customMessage);
         yearObj.focus();
         return false;
      }

      if(!canBePast){
         var todayDate = new Date();
         var dateToCheck = new Date();
         dateToCheck.setYear(yearObj.value);
         dateToCheck.setMonth(monthObj.value-1);
         dateToCheck.setDate(dayObj.value);
         var checkDate = dateToCheck.getTime();
         if(dateToCheck < todayDate){
            sendAlert("Date cannot be in the past. Please enter a future date.", customMessage);
            return false;
         }
      }

      return true;
   }

   function isValidYear(year){
      if(!isPosInteger(year)){
         return false;
      }
      if(year.length != 4){
         return false;
      }
      return true;
   }

   function isValidDay(day, numDaysInMonth){
      if(!isPosInteger(day)){
         return false;
      }
      if(!valueBetween(day, 1, numDaysInMonth)){
         return false;
      }
      return true;
   }

   function isValidMonth(month){
      if(!isPosInteger(month)){
         return false;
      }
      if(!valueBetween(month, 1, 12)){
         return false;
      }
      return true;
   }

   function daysInFebruary (year){
      // February has 29 days in any year evenly divisible by four,
       // EXCEPT for centurial years which are not also divisible by 400.
       return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
   }
///////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
// Primary Validation Functions
///////////////////////////////////////////////////////////////////////////////

   //////////////////////////////////////////////////////////////////
   //////////////////////  Validate Names /////////////////////////

   // Good values: alpha, space, hyphen, apostrophe
   function isValidName(textBoxObj, canBeNull, message, customMessage){
      isValid = true
      if(!canBeNull && textBoxObj.value.length == 0){
         isValid = false
      }
      isValidNameReg = /[^A-Za-z'\s-]/;
      if(isValidNameReg.test(textBoxObj.value)){
         isValid = false
      }
      if(!isValid){
         sendAlert(message, customMessage);
         textBoxObj.focus();
      }
      return isValid;
   }

   function isValidFullName(textBoxObj, canBeNull, customMessage){
      return isValidName(textBoxObj, canBeNull, "Please enter a valid name.", customMessage);
   }

   function isValidFirstName(textBoxObj, canBeNull, customMessage){
      return isValidName(textBoxObj, canBeNull, "Please enter a valid first name.", customMessage);
   }

   function isValidLastName(textBoxObj, canBeNull, customMessage){
      return isValidName(textBoxObj, canBeNull, "Please enter a valid last name.", customMessage);
   }
   //////////////////////  End Validate Names   //////////////////////
   //////////////////////////////////////////////////////////////////

   //////////////////////////////////////////////////////////////////
   //////////////////   Validate PhoneNumber //////////////////////
   function isValidPhoneNum(areaCodeObj, prefixObj, suffixObj, canBeNull, customMessage){
      if(canBeNull && areaCodeObj.value.length == 0 && prefixObj.value.length == 0 && suffixObj.value.length == 0){
         return true;
      }
      if(!isValidAreaCode(areaCodeObj.value)){
         sendAlert("Please enter a valid area code.", customMessage);
         areaCodeObj.focus();
         return false;
      }
      if(!isValidPhonePrefix(prefixObj.value)){
         sendAlert("Please enter a valid phone prefix.", customMessage);
         prefixObj.focus();
         return false;
      }
      if(!isValidPhonesuffix(suffixObj.value)){
         sendAlert("Please enter a valid phone suffix.", customMessage);
         suffixObj.focus();
         return false;
      }
      return true;
   }

   function isValidAreaCode(areaCode){
      if(areaCode.length != 3){
         return false;
      }
      validAreaCode = /[2-9]\d\d/;
      return validAreaCode.test(areaCode);
   }

   function isValidPhonePrefix(prefix){
      if(prefix.length != 3){
         return false;
      }
      validPrefix = /[2-9]\d\d/;
      return validPrefix.test(prefix);
   }

   function isValidPhonesuffix(suffix){
      if(suffix.length != 4){
         return false;
      }
      validSuffix = /\d\d\d\d/;
      return validSuffix.test(suffix);
   }
   ////////////////  End Validate PhoneNumber /////////////////////
   //////////////////////////////////////////////////////////////////

   function isValidEmail(textBoxObj, canBeNull, customMessage){
      if(canBeNull && textBoxObj.value.length == 0){
         return true;
      }
      validEmail = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
      if(!validEmail.test( textBoxObj.value )){
         sendAlert("Please enter a valid Email address.", customMessage)
         textBoxObj.focus();
         return false;
      }
      return true;
   }

   // Currently this function test for 2 alpha characters. Thats it.
   function isValidState(textBoxObj, canBeNull, customMessage){
      if(canBeNull && textBoxObj.value.length == 0){
         return true;
      }
      isValidStateChars = /[a-zA-Z][a-zA-Z]/;
      if(!isValidStateChars.test( textBoxObj.value ) || textBoxObj.value.length > 2){
         sendAlert("Please enter a valid State.", customMessage);
         textBoxObj.focus();
         return false;
      }
      return true;
   }

   function isValidCity(textBoxObj, canBeNull, customMessage){
      if(!canBeNull && textBoxObj.value.length == 0){
         sendAlert("Please enter a valid city.", customMessage);
         textBoxObj.focus();
         return false;
      }
      isNotAlphaChars = /[^a-zA-Z\s]/;
      if(isNotAlphaChars.test( textBoxObj.value )){
         sendAlert("Please enter a valid city.", customMessage);
         textBoxObj.focus();
         return false;
      }
      return true;
   }

   // 5 numeric digits
   function isValidZipCode(textBoxObj, canBeNull, customMessage){
      if(!canBeNull){
         if(textBoxObj.value.length == 0){
            sendAlert("Please enter a valid Zip Code.", customMessage);
            textBoxObj.focus();
            return false;
         }
      }
      if(!isPosInteger(textBoxObj.value) || textBoxObj.value.length != 5){
         sendAlert("Please enter a valid Zip Code.", customMessage);
         textBoxObj.focus();
         return false;
      }
      return true;
   }

////////////////// Cost Center   ///////////////////////////////
   function isValidCostCenter(textBoxObj, canBeNull, customMessage){
      if(!canBeNull){
         if(textBoxObj.value.length == 0){
            sendAlert("Please enter a valid Cost Center number.", customMessage);
            textBoxObj.focus();
            return false;
         }
      }
      if(!isPosInteger(textBoxObj.value) || textBoxObj.value.length != 6){
         sendAlert("Please enter a six digits for the Cost Center.", customMessage);
         textBoxObj.focus();
         return false;
      }
      return true;
   }
///////////////////////////////////////////////////////////////////////////////
//////////////////   Validate Time Minutes   ///////////////////////////////

   function isValidTimeHour(testValue){
      if(testValue.value.length == 0){
         sendAlert("Please enter a valid Hour.");
         return false;
      }
      if(!isPosInteger(testValue.value)){
         sendAlert("Please enter a valid Hour.");
         return false;
      }

      if(testValue.value < 1 || testValue.value > 12 ){
         sendAlert("Please enter a valid Hour.");
         return false;
      }
      return true;
   }
   function isValidTimeMinute(testValue, customMessage){
      isMin = /[0-5]\d/;
      if(!isMin.test( testValue.value )){
         sendAlert("Please enter valid minute.");
         return false;
      }
      return true;
   }
///////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
// Cedars functions
///////////////////////////////////////////////////////////////////////////////

   // Four or Five numeric digits.
   function isValidPhysicianID(textBoxObj, canBeNull, customMessage){
      if(canBeNull && textBoxObj.value.length == 0){
         return true;
      }
      if(!isPosInteger(textBoxObj.value) || textBoxObj.value.length >5 || textBoxObj.value.length <4){
         sendAlert("Please enter a valid Physician ID.", customMessage)
         textBoxObj.focus();
         return false;
      }
      return true;
   }

   // "CA" + 1 alpha + space + 6 numeric digits. (Fill with leading zeros to reach 6 numeric.)
   function isValidCaStateLicenseNumber(testValue, canBeNull, customMessage){
      if(canBeNull && testValue.length == 0){
         return true;
      }
      isStateLicense = /CA[a-zA-Z]\s\d\d\d\d\d\d/;
      if(!isStateLicense.test( testValue ) || testValue.length > 10){
         sendAlert("Please enter a valid state license number.", customMessage);
         return false;
      }
      return true;
   }

   // "CA" + 1 alpha + space + 6 numeric digits. (Fill with leading zeros to reach 6 numeric.)
   function isValidDEALicenseNumber(testValue, canBeNull, customMessage){
      if(canBeNull && testValue.length == 0){
         return true;
      }
      isDEALicense = /[a-zA-Z][a-zA-Z]\d\d\d\d\d\d\d/;
      if(!isDEALicense.test( testValue ) || testValue.length > 9){
         sendAlert("Please enter a valid DEA license number.", customMessage);
         return false;
      }
      return true;
   }
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// General functions
///////////////////////////////////////////////////////////////////////////////
   function isPosInteger(testValue){
      isInt = /\D/;
      return !isInt.test( testValue );
   }

   function valueBetween(num, Min, Max){
      if(num < Min || num > Max){
         return false;
      }
      return true;
   }

   function validateSelectObj(obj, customMessage){
      if(obj.selectedIndex == 0){
         sendAlert("Please select an option.", customMessage);
         obj.focus();
         return false;
      }
      return true
   }

   function validateRadioSelection(obj, customMessage){
      hasSelection = false;
      for(i=0; i<obj.length; i++){
         if(obj[i].checked == true){
            hasSelection = true;
         }
      }


      if(!hasSelection){
         sendAlert("Please select an option.", customMessage);
         //obj.focus();
      }
      return hasSelection;
   }

   function validateCheckboxSelection(obj, customMessage){
      hasSelection = false;
      if(obj.checked == true){
        hasSelection = true;
      }


      if(!hasSelection){
         sendAlert("Please select an option.", customMessage);
         //obj.focus();
      }
      return hasSelection;
   }

   function isNotNull(textBoxObj, customMessage){
      if(textBoxObj.value.length == 0){
         sendAlert("Please enter a value.", customMessage);
         textBoxObj.focus();
         return false;
      }
      return true
   }

///////////////////////////////////////////////////////////////////////////////