<!-- Original:  Sandeep Tamhankar (stamhankar@hotmail.com) -->
<!-- Web Site:  http://207.20.242.93 -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function IsValidTime(timeStr) {
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.

var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

var matchArray = timeStr.match(timePat);
if (matchArray == null) {
//alert("Time is not in a valid format.");
return "Time is not in a valid format.";
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];

if (second=="") { second = null; }
if (ampm=="") { ampm = null }

if (hour < 0  || hour > 23) {
//alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
return "Hour must be between 1 and 12. (or 0 and 23 for military time)";
}
if (hour <= 12 && ampm == null) {
if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
//alert("You must specify AM or PM.");
return "You must specify AM or PM.";
   }
}
if  (hour > 12 && ampm != null) {
//alert("You can't specify AM or PM for military time.");
return "You can't specify AM or PM for military time.";
}
if (minute<0 || minute > 59) {
//alert ("Minute must be between 0 and 59.");
return "Minute must be between 0 and 59.";
}
if (second != null && (second < 0 || second > 59)) {
//alert ("Second must be between 0 and 59.");
return "Second must be between 0 and 59.";
}
return 'valid';
}

function dateDiff(first_date,second_date) {
date1 = new Date();
date2 = new Date();
diff  = new Date();

//if (isValidDate(dateform.firstdate.value) && isValidTime(dateform.firsttime.value)) { // Validates first date 
date1temp = new Date(first_date);
date1.setTime(date1temp.getTime());
//}


//if (isValidDate(dateform.seconddate.value) && isValidTime(dateform.secondtime.value)) { // Validates second date 
date2temp = new Date(second_date);
date2.setTime(date2temp.getTime());
//}


// sets difference date to difference of first date and second date
// diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

diff.setTime((date2.getTime() - date1.getTime()));

timediff = diff.getTime();

if(timediff < 0 )
{   
    return -1;    // if pickup and return date is same and pickup time is greater than return time on that same date. 
}

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60)); 
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60)); 
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000); 
timediff -= secs * 1000;

//dateform.difference.value = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";

//return false; // form should never submit, returns false

if(weeks >= 1)
   temp=weeks * 7;
else
   temp=0;   

if(hours > 0 || mins > 0 || secs > 0)
   return (days+temp)+1;
else
   return (days+temp);
   
}

//  End -->

