
//-------------------------------------------------------------------
// checkValidEmail(strEmail)
//   checks for valid email 
//-------------------------------------------------------------------
function checkValidEmail(strEmail) 
{
   if( _isValidEmail(strEmail) == false )
   {
      alert( "Enter valid email" )
      return false;
   }
   return true;
}

function _isValidEmail(emailStr) 
{
   if (emailStr.length == 0) 
   {
       return false;
   }
   var emailPat=/^(.+)@(.+)$/;
   var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
   var validChars="\[^\\s" + specialChars + "\]";
   var quotedUser="(\"[^\"]*\")";
   var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
   var atom=validChars + '+';
   var word="(" + atom + "|" + quotedUser + ")";
   var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
   var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
   var matchArray=emailStr.match(emailPat);
   if (matchArray == null) 
   {
       return false;
   }
   var user=matchArray[1];
   var domain=matchArray[2];
   if (user.match(userPat) == null) 
   {
       return false;
   }
   var IPArray = domain.match(ipDomainPat);
   if (IPArray != null) 
   {
      for (var i = 1; i <= 4; i++) 
      {
         if (IPArray[i] > 255) 
         {
             return false;
         }
      }
      return true;
   }
   var domainArray=domain.match(domainPat);
   if (domainArray == null)    
   {
       return false;
   }
   var atomPat=new RegExp(atom,"g");
   var domArr=domain.match(atomPat);
   var len=domArr.length;
   if ((domArr[domArr.length-1].length < 2) ||
       (domArr[domArr.length-1].length > 3)) 
   {
       return false;
   }
   if (len < 2) 
   {
       return false;
   }
   return true;
}

//-------------------------------------------------------------------
// checkNumeric(strNumeric)
//   checks for valid number 
//-------------------------------------------------------------------
function checkNumeric(strNumber,strErrorMessage)
{
   var errorMessage = "Enter Valid Number" ;
   if(typeof(strErrorMessage)!="undefined" ) 
      errorMessage = strErrorMessage ;
   if( _isNumeric(strNumber) == false )
   {
      alert( errorMessage )
      return false;
   }
   return true;
}

function _isNumeric(strNumber)
{
   return isNumeric(strNumber) ;
}

//-------------------------------------------------------------------
// checkMatch(str, myRegExp, strErrorMessage)
//   checks for valid str matching expression 
//-------------------------------------------------------------------

function _match(str, myRegExp)
{
   var re = new RegExp(myRegExp) ;
 //alert( "_match="+ (str.match(re ) != null) ) ;
   return str.match(re ) != null ;
}

function checkMatch(str, myRegExp, strErrorMessage)
{
   var errorMessage = "Invalid Characters Found" ;
   if(typeof(strErrorMessage)!="undefined" ) 
      errorMessage = strErrorMessage ;
   if( _match(str, myRegExp) == false )
   {
      alert( errorMessage )
      return false;
   }
   return true;
}


// Check string for presence of invalid characters.
//   Current valid characters are: A-Z , 0-9 , ' , - , _ , # , @ , & " ;
//   May consider: ()
//   Maybe: * % ! ? _
//   Perhaps: { } [ ] < > $
//   Probably not: , . " ' / \ | ^ ~ ` = + ; :
//
//   There may be different versions of this method where either the undefined is different or passed in.
//
function checkValidInputText( str, strErrorMessage )
{
   var errorMessage = "Valid Characters are A-Z , a-z , 0-9 , ' , - , _ , # , @ , & " ;
   var errorMessageNote = "If you think your text is valid you may have a hidden character." +
                          "\n" +
                          "Please clear the text field and retype your text.";
   if(typeof(strErrorMessage)!="undefined" ) 
      errorMessage = strErrorMessage +"\n"+ errorMessage +"\n"+ errorMessageNote ;
   myRegExp=/^[a-zA-Z0-9\_\-\#\'\@\&]*$/ ;
   return checkMatch( str, myRegExp, errorMessage ) ;
}

function checkValidSimpleText( str, strErrorMessage )
{
   if( isBlank( str ) )
   {
      if(typeof(strErrorMessage)!="undefined" ) 
         alert( strErrorMessage + " cannot be blank" ) ;  
      else
         alert( "text cannot be blank" ) ;  
      return false ;
   }

   var errorMessage = "Valid Characters are A-Z , a-z , 0-9  " ;
   var errorMessageNote = "If you think your text is valid you may have a hidden character." +
                          "\n" +
                          "Please clear the text field and retype your text.";
   if(typeof(strErrorMessage)!="undefined" ) 
      errorMessage = strErrorMessage +"\n"+ errorMessage +"\n"+ errorMessageNote ; 
   myRegExp=/^[a-zA-Z0-9 ]*$/ ;
   return checkMatch( str, myRegExp, errorMessage ) ;
}

//-------------------------------------------------------------------
// checkInteger(strInteger)
//   checks for valid number 
//-------------------------------------------------------------------
function checkInteger(strInteger,strErrorMessage)
{
   var errorMessage = "Enter Valid Number" ;
   if(typeof(strErrorMessage)!="undefined" ) 
      errorMessage = strErrorMessage ;
   if( isInteger(strInteger) == false )
   {
      alert( errorMessage )
      return false;
   }
   return true;
}

//-------------------------------------------------------------------
// checkIsGreater(strIntegerGreater, strIntegerLesser)
//   checks for first parameter greater than second parameter
//   can be float or integer
//-------------------------------------------------------------------
function checkIsGreater(strIntegerGreater, strIntegerLesser, strErrorMessage)
{
   var errorMessage = strIntegerGreater+" must be greater than "+strIntegerLesser ;
   if(typeof(strErrorMessage)!="undefined" ) 
      errorMessage = strErrorMessage ;

   var greater = parseFloat(strIntegerGreater)
   var lesser = parseFloat(strIntegerLesser)

   if( greater <= lesser )
   {
      alert( errorMessage )
      return false;
   }
   return true;
}

//-------------------------------------------------------------------
// dateRangeCheck(szDate)
//   checks that end is before start  
//-------------------------------------------------------------------
function dateRangeCheck( szStartDate, szEndDate )
{
//alert( "start="+szStartDate+" end="+szEndDate ) ;
  var d = compareDates( szStartDate,"MM-dd-yyyy",szEndDate,"MM-dd-yyyy" ) ;
  if( d == 1 ) 
  {
      alert( "End Date cannot be before Start Date" ) ; return false ;
  }
  return true ;
}

//-------------------------------------------------------------------
// checkDate(strDate)
//   checks valid date 
//-------------------------------------------------------------------
function checkDate(strDate)
{
   if (strDate.length==0)
   {
      alert("The date must not be blank. The format should be : mm-dd-yyyy")
      return false
   }
   var daysInMonth = _daysArray(12)
   var pos1=strDate.indexOf(hyphen)
   var pos2=strDate.indexOf(hyphen,pos1+1)
   var strMonth=strDate.substring(0,pos1)
   var strDay=strDate.substring(pos1+1,pos2)
   var strYear=strDate.substring(pos2+1)
   strYr=strYear
   if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
   if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
   for (var i = 1; i <= 3; i++) 
   {
      if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
   }
   month=parseInt(strMonth)
   day=parseInt(strDay)
   year=parseInt(strYr)
   if (pos1==-1 || pos2==-1)
   {
      alert("The date format should be : mm-dd-yyyy")
      return false
   }
   if (strMonth.length<1 || month<1 || month>12)
   {
      alert("Please enter a valid month")
      return false
   }
   if (strDay.length<1 || day<1 || day>31 || (month==2 && day>_daysInFebruary(year)) || day > daysInMonth[month]){
      alert("Please enter a valid day")
      return false
   }
   if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
   {
      alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
      return false
   }
   if (strDate.indexOf(hyphen,pos2+1)!=-1 || isInteger(_stripCharsInBag(strDate, hyphen))==false)
   {
      alert("Please enter a valid date")
      return false
   }
   return true
}

//--
//-- used for date check 
//-- 
var hyphen= "-";
var colon= ":";
var minYear=1000;
var maxYear=2100;


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 );
}

function _daysArray(n) 
{
   for (var i = 1; i <= n; i++) 
   {
      this[i] = 31
      if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
      if (i==2) {this[i] = 29}
   } 
   return this
}

//-------------------------------------------------------------------
// checkTime()
//   checks valid time 
//-------------------------------------------------------------------
function check12Time(strTime)
{
   var colonCount = 0;
   var hasMeridian = false;
   for (var i=0; i<strTime.length; i++) 
   {
      var ch = strTime.substring(i, i+1);
      if ( (ch < '0') || (ch > '9') ) {
         if ( (ch != ':') && (ch != ' ') && (ch != 'a') && (ch != 'A') && (ch != 'p') && (ch != 'P') && (ch != 'm') && (ch != 'M')) {
            alert( 'invalid char' ) ;
            return false;
         }
      }
      if (ch == ':') { colonCount++; }
      if ( (ch == 'p') || (ch == 'P') || (ch == 'a') || (ch == 'A') ) { hasMeridian = true; }
   }
   if ( colonCount != 1 || hasMeridian == false ) 
   { 
       alert("The time format should be : hh:mm AM/PM")
       return false; 
   }
   var hh = strTime.substring(0, strTime.indexOf(":"));
   if ( (parseFloat(hh) < 1) || (parseFloat(hh) > 12) ) 
   {   
      alert("Hours must be between 1-12. The time format should be : hh:mm AM/PM")
      return false; 
   }
   var mm = strTime.substring(strTime.indexOf(":")+1, strTime.length);
   if ( (parseFloat(mm) < 0) || (parseFloat(mm) > 59) ) 
   { 
      alert("Minutes must be between 0-59. The time format should be : hh:mm AM/PM")
      return false; 
   }
   return true;
}

//-- temporary passthru till 12 hour method is online
function checkTime(strTime)
{
  return check24Time(strTime) ;
}

//-------------------------------------------------------------------
// check24Time()
//   checks valid time 
//-------------------------------------------------------------------
function check24Time(strTime)
{
   var pos1=strTime.indexOf(colon)
   var pos2=strTime.length
   var strHour=strTime.substring(0,pos1)
   var strMinute=strTime.substring(pos1+1,pos2)
   //alert(" pos1="+pos1+" pos2="+pos2+" HH:mm="+strHour+":"+strMinute)
   if (strHour.charAt(0)=="0" && strHour.length>1) strHour=strHour.substring(1)
   if (strMinute.charAt(0)=="0" && strMinute.length>1) strMinute=strMinute.substring(1)
   hour=parseInt(strHour)
   minute=parseInt(strMinute)
        
   if (pos1==-1)
   {
      alert("The time format should be : HH:mm")
      return false
   }
   if ( hour == 24 ) hour=0
   if (strHour.length<1 || hour<0 || hour>23)
   {
      alert("Please enter a valid hour(00-23)")
      return false
   }
   if (strMinute.length<1 || minute<0 || minute>59)
   {
      alert("Please enter a valid minute(00-59)")
      return false
   }
   if ( isInteger(_stripCharsInBag(strTime, colon) )==false)
   {
      alert("Please enter a valid time : HH:mm")
      return false
   }
   return true 
}

//-------------------------------------------------------------------
// checkRange()
//   checks valid range 
//-------------------------------------------------------------------
function checkInRange(strStartNum, strEndNum, strTargetNum, strFieldName)
{
   startNum = parseInt(strStartNum) ;
   endNum = parseInt(strEndNum) ;
   targetNum = parseInt(strTargetNum) ;
   if( strTargetNum.length==0 || isInteger(strTargetNum)==false || targetNum<startNum || targetNum>endNum )
   {
      alert( "Enter "+strFieldName+" between "+strStartNum+" and "+strEndNum )
      return false;
   }
   return true;
}

function _stripCharsInBag(s, bag)
{
    var i;
    var returnString = "";
    //-- Search through string's characters one by one.
    //-- If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

//-------------------------------------------------------------------
// isBlank(value)
//   Returns true if value only contains spaces
//-------------------------------------------------------------------
function isBlank(val)
{
  if(val==null){ return true; }
  for(var i=0;i<val.length;i++) 
  {
    if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
  }
  return true;
}
