function isValidDate(dateStr) {
	// Date validation function courtesty of 
	// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
	   alert(dateStr + " Date is not in a valid format.")
	   return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
	  alert("Month must be between 1 and 12.");
	  return false;
	}
	if (day < 1 || day > 31) {
	  alert("Day must be between 1 and 31.");
	  return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	  alert("Month "+month+" doesn't have 31 days!")
	  return false;
	}
	if (month == 2) { // check for february 29th
	  var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	  if (day>29 || (day==29 && !isleap)) {
		alert("February " + year + " doesn't have " + day + " days!");
		return false;
	  }
	}
	return true;
}

function isValidTime(timeStr) {
	// Time validation function courtesty of 
	// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

	// 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 false;
	}
	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 false;
	}
	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 false;
	   }
	}
	if  (hour > 12 && ampm != null) {
	  alert("You can't specify AM or PM for military time.");
	  return false;
	}
	if (minute < 0 || minute > 59) {
	  alert ("Minute must be between 0 and 59.");
	  return false;
	}
	if (second != null && (second < 0 || second > 59)) {
	  alert ("Second must be between 0 and 59.");
	  return false;
	}
	return true;
}

/*function dateDiff(date_1,time_1,date_2,time_2){
  if(time_1.length == 5) time_1 += ':00';
  
  if(isValidDate(date_1)){  	
    var date1 = new Date(date_1+' '+time_1);
  }else return false;
  
  if(date_2 == undefined && time_2 == undefined){
  	var tmpDate = new Date();
  	var format = tmpDate.getDate()+'/'+(tmpDate.getMonth()+1)+'/'+tmpDate.getFullYear()+' '+tmpDate.getHours()+':'+tmpDate.getMinutes()+':00';
  	//alert(format);
  	var date2 = new Date(format);  	
  }else{
  	if(time_2.length == 5) time_2 += ':00';
  	if(isValidDate(date_2)){
  	  var date2 = new Date(date_2+' '+time_2);
    }else return false;
  }
  var diff = new Date();
  diff.setTime(date2.getTime() - date1.getTime());
  return diff.getTime();
}*/
function dateDiff(date_1,time_1,date_2,time_2){
  if(isValidDate(date_1)){  
  	var s = date_1.split('/');	
  	var t = time_1.split(':');
    var date1 = new Date(s[2],s[1],s[0],t[0],t[1],0,0);
  }else return false;
  
  if(date_2 == undefined && time_2 == undefined){
  	var date2 = new Date(); 
  	date2.setMonth(date2.getMonth() + 1); 	
  	//alert(date2.getDate()+'/'+date2.getMonth()+'/'+date2.getFullYear());
  }else{
  	if(isValidDate(date_2)){
  	  var s = date_2.split('/');	
  	  var t = time_2.split(':');
      var date2 = new Date(s[2],s[1],s[0],t[0],t[1],0,0);
    }else return false;
  }
  return (date1.getTime() - date2.getTime());
}

function convertDate(timediff){
	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;
	
	return [weeks,days,hours,mins,secs];
}
