function calculateDate(value)
{
if (document.form.fromDate.value != null && value != null)
{
var date = getDate(document.form.fromDate.value);
date.setDate(date.getDate() + parseInt(value)-1);
var curr_date = date.getDate();
var curr_month = date.getMonth()+1;
var curr_year = date.getFullYear();
var dateString = formatString(curr_date) + "-" + formatString(curr_month) + "-" + curr_year;
document.form.toDate.value= dateString;
}
}
function calculateValidDays()
{
if (document.form.fromDate.value != null && document.form.toDate.value != null)
{
var fromDate = getDate(document.form.fromDate.value);
var toDate = getDate(document.form.toDate.value);
var date = new Date();
date.setTime(Math.abs(fromDate.getTime() - toDate.getTime()));
days = Math.floor(date.getTime() / (1000 * 60 * 60 * 24));
document.form.days.value= days+1;
}
else if(document.form.toDate.value != null && document.form.days.value != "")
{
var date = getDate(document.form.toDate.value);
date.setDate(date.getDate() - parseInt(document.form.days.value));
var curr_date = date.getDate();
var curr_month = date.getMonth()+1;
var curr_year = date.getFullYear();
var dateString = formatString(curr_date) + "-" + formatString(curr_month) + "-" + curr_year;
document.form.fromDate.value= dateString;
}
else if(document.form.fromDate.value != null && document.form.days.value != "")
{
var date = getDate(document.form.fromDate.value);
date.setDate(date.getDate() + parseInt(document.form.days.value)+1);
var curr_date = date.getDate();
var curr_month = date.getMonth()+1;
var curr_year = date.getFullYear();
var dateString = formatString(curr_date) + "-" + formatString(curr_month) + "-" + curr_year;
document.form.toDate.value= dateString;
}
}
function formatString(value)
{
return ("00" + value).substring(("00" + value).length-2, ("00" + value).length);
}
function getDate(dateValue)
{
var m_arrDate = dateValue.split("-");
var m_DAY = m_arrDate[0];
var m_MONTH = m_arrDate[1]-1;
var m_YEAR = m_arrDate[2];
if(m_YEAR.length > 4)
{
return new Date();
}
return new Date(m_YEAR, m_MONTH, m_DAY);
}
function reloadPeriod()
{
document.form.action = "annual_leave_application_form.php?from="+document.form.fromDate.value+"&to="+document.form.toDate.value+"&reason="+document.form.reason.value;
document.form.submit();
}
var dtCh= "-";
var minYear=1900;
var maxYear=2200;
function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
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;
}
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;
}
function isDate(dtStr){
var daysInMonth = DaysArray(12);
var pos1=dtStr.indexOf(dtCh);
var pos2=dtStr.indexOf(dtCh,pos1+1);
var strDay=dtStr.substring(0,pos1);
var strMonth=dtStr.substring(pos1+1,pos2);
var strYear =dtStr.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);
}
year=parseInt(strYr);
month=parseInt(strMonth);
day=parseInt(strDay);
if (pos1==-1 || pos2==-1){
alert("1Please insert valid date in the format :dd-mm-YYYY (eg: 01-01-2009).");
return false;
}
if (strMonth.length<1 || month<1 || month>12){
alert("2Please insert valid date in the format :dd-mm-YYYY (eg: 01-01-2009).");
return false;
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
alert("3Please insert valid date in the format :dd-mm-YYYY (eg: 01-01-2009).");
return false;
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
alert("Please insert valid date in the format :dd-mm-YYYY (eg: 01-01-2009).");
return false;
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
alert("4Please insert valid date in the format :dd-mm-YYYY (eg: 01-01-2009).");
return false;
}
return true;
}
function check_form(){
var dt_fromDate=document.form.fromDate;
var dt_toDate=document.form.toDate;
var strFromDate = form.fromDate.value;
var dayPartFromDate = parseInt(strFromDate.substring(0,2),10);
var monPartFromDate = parseInt(strFromDate.substring(3,5),10);
var yearPartFromDate = parseInt(strFromDate.substring(6,10),10);
var dtFrom = new Date(yearPartFromDate, monPartFromDate-1, dayPartFromDate);
var strToDate = form.toDate.value;
var dayPartToDate = parseInt(strToDate.substring(0,2),10);
var monPartToDate = parseInt(strToDate.substring(3,5),10);
var yearPartToDate = parseInt(strToDate.substring(6,10),10);
var dtTo = new Date(yearPartToDate, monPartToDate-1, dayPartToDate);
if ( form.fromDate.value == "")
{
alert("Please fill in all the fields marked with *");
document.form.fromDate.focus();
return false;
}
else if ( form.toDate.value == "")
{
alert("Please fill in all the fields marked with *");
document.form.toDate.focus();
return false;
}
else if (isDate(dt_fromDate.value)==false){
dt_fromDate.focus();
return false;
}
else if (isDate(dt_toDate.value)==false){
dt_toDate.focus();
return false;
}
else if(dtTo < dtFrom){
alert("Invalid date range. 'To' date should not be earlier than 'From' date");
return false;
}
return true;
}
sitie_aniem 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.