function isblank(s) {
var i1;
  for(i1 = 0; i1 < s.length; i1++) {
    c = s.charAt(i1);
    if((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true;
}

function IsStringNull(str, val) {
  if(str == null || str.length == 0 || str == val || isblank(str))
    return true;
  else
    return false;
}


/* return float number or -100000 if error */
function checkfloat(checkString){
  checkString = checkString.replace(/,/g, ""); // remove all <,>
  var x=parseFloat(checkString);
  var retv;
  if (!isNaN(x)) {
    retv=x;
  }
  else{
    retv = -100000;
  }
   return retv;
}


function CheckYear(yr, minyear, maxyear, msg) {
var y;
  if(!IsStringNull(yr, "")) {
     y = parseInt(yr);
     if(y < minyear || y > maxyear || isNaN(y)) {
        alert(msg + " Year must be 4 digits number between " + minyear + " and " + maxyear + ".");
        return false;
     }
  }
  return true;
}

function checkDate(month, day, year) {
  if((month==2 || month==4 || month==6 || month==9 || month==11) && day>30) {
    return false;
  }
  var leap = 0;
  var l1=year%4;
  var l2=year%100;
  var l3=year%400;
  if((l1==0 && l2>0) || (l3==0)) leap=1;
  if(month==2 && day>28+leap) {
    return false;
  }
  return true;
}

function checkSSn(ssn) {
  var ssnPattern=/\d{9}/;
  if(ssn.length != 9) return false;
  if (ssn.search(ssnPattern) != 0) return false;
  return true;
}

function check4SSn(ssn) {
  //alert("ssn="+ssn);
  var ssnPattern=/\d{4}/;
  if(ssn.length != 4) return false;
  if (ssn.search(ssnPattern) != 0) return false;
  return true;
}

function checkPIN(pin) {
  var pinPattern=/\d{6}/;
  if(pin.length != 6) return false;
  if (pin.search(pinPattern) != 0) return false;
  return true;
}


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function getMinFromMidnight(timeStr) {
  var timePat = /^(\d{1,2}):(\d{2})(\s?(AM|am|PM|pm))$/;
  var timeArray = timeStr.match(timePat);
  if(timeArray == null) return -1;
  var hour = parseInt(parseFloat(timeArray[1]));
  var minute = parseInt(parseFloat(timeArray[2]));
  var ampm = timeArray[4];
  ampm = ampm.toUpperCase();
  if(hour < 12 && ampm == "PM") hour = hour + 12;
  return hour*60 + minute;
}


function compareTime(fromTimeStr, toTimeStr) {
  // Checks if time is in HH:MM AM/PM format.
  // The seconds and AM/PM are optional.

  var timePat = /^(\d{1,2}):(\d{2})(\s?(AM|am|PM|pm))$/;

  var fromArray = fromTimeStr.match(timePat);
  var toArray = toTimeStr.match(timePat);

  if (fromArray == null || toArray == null) {
    alert("The time format must be hh:mm AM/PM.");
    return false;
  }

  fromHour = parseInt(parseFloat(fromArray[1]));
  toHour = parseInt(parseFloat(toArray[1]));
  //alert("fromHour = " + fromHour + "  toHour = " + toHour);
  if (fromHour < 0 || fromHour > 12 || toHour < 0 || toHour > 12) {
    alert("Hour must be between 0 and 12.");
    return false;
  }
  
  fromMinute = fromArray[2];
  toMinute = toArray[2];
  //alert("fromMinute = " + parseInt(fromMinute) + "  toMinute = " + toMinute);
  if (fromMinute < 0 || fromMinute > 59 ||
      toMinute < 0 || toMinute > 59) {
    alert("Minute must be between 0 and 59.");
    return false;
  }
  
  fromAmpm = fromArray[4];
  fromAmpm = fromAmpm.toUpperCase();
  toAmpm = toArray[4];
  toAmpm = toAmpm.toUpperCase();
  //alert("fromAmpm = " + fromAmpm + "  toAmpm = " + toAmpm);
  
  if (fromAmpm == "AM" && toAmpm == "PM") {
    return true;
  }
  if (fromAmpm == "PM" && toAmpm == "AM") {
    alert("From Time must be earlier than To Time.");
    return false;
  }
  if (fromAmpm == "AM" && toAmpm == "AM") {
    if (fromHour > toHour) {
      alert("From Time must be earlier than To Time.");
      return false;
    }
    else if (fromHour == toHour) {
      if (fromMinute > toMinute) {
        alert("From Time must be earlier than To Time.");
        return false;      
      }
    }  
  }
  if (fromAmpm == "PM" && toAmpm == "PM") {
    if (fromHour == 12 && toHour == 12) {
      //alert("fromMinute = " + fromMinute + "  toMinute = " + toMinute);
      if (fromMinute > toMinute) {
        alert("From Time must be earlier than To Time.");
        return false;      
      }
    }
    else if (fromHour == 12 && toHour != 12) {
      return true;
    }
	else if (fromHour != 12 && toHour == 12) {
        alert("From Time must be earlier than To Time.");
        return false;      
	}
  
    if (fromHour > toHour) {
      alert("From Time must be earlier than To Time.");
      return false;
    }
    else if (fromHour == toHour) {
      if (fromMinute > toMinute) {
        alert("From Time must be earlier than To Time.");
        return false;      
      }
    }  
  }
  
  return true;
}

