function checkdate(objName) {
	var datefield = objName;
	if (chkdate(objName) == false) {
		datefield.select();
		alert("This date is invalid.  Please try again.");
		datefield.focus();
		return false;
	}
	else {
		return true;
   }
}

function correctDate(objName) {
	var datefield = objName;
	if (validDate(objName) == false) {
		datefield.select();
		alert("This date is invalid.  Please try again.");
		datefield.focus();
		return false;
	}
	else {
		return true;
   }
}

function chkdate(objName) {
	//var strDatestyle = "US"; //United States date style
	var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intDay;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "JAN";
	strMonthArray[1] = "FEB";
	strMonthArray[2] = "MAR";
	strMonthArray[3] = "APR";
	strMonthArray[4] = "MAY";
	strMonthArray[5] = "JUN";
	strMonthArray[6] = "JUL";
	strMonthArray[7] = "AUG";
	strMonthArray[8] = "SEP";
	strMonthArray[9] = "OCT";
	strMonthArray[10] = "NOV";
	strMonthArray[11] = "DEC";
	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	if ((strDay == null) || (strMonth == null) || (strYear == null)) {
		err = 11;
		return false;
	}
	

	if (strYear.length == 2) {
		strYear = "20" + strYear;
	}

	if (strYear.length == 1) {
		strYear = "200" + strYear;
	}

	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intDay = parseInt(strDay, 10);
	if (isNaN(intDay)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30 || intDay < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intDay < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intDay > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intDay > 28) {
				err = 10;
				return false;
			}
		}
	}
	if (intDay < 10) {
		strDay = "0" + intDay;
	}	
	//if (strDatestyle == "US") {
	//	datefield.value = strMonthArray[intMonth-1] + "-" + strDay + "-" + strYear;
	//}
	//else {
	//	datefield.value = strDay + "-" + strMonthArray[intMonth-1] + "-" + strYear;
	//}
	return true;
}

function validDate(objName) {
	//var strDatestyle = "US"; //United States date style
	var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intDay;
	var intMonth;
	var intYear;
	var int2DigitYear;  // SRH Added 23/09/2005
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "JAN";
	strMonthArray[1] = "FEB";
	strMonthArray[2] = "MAR";
	strMonthArray[3] = "APR";
	strMonthArray[4] = "MAY";
	strMonthArray[5] = "JUN";
	strMonthArray[6] = "JUL";
	strMonthArray[7] = "AUG";
	strMonthArray[8] = "SEP";
	strMonthArray[9] = "OCT";
	strMonthArray[10] = "NOV";
	strMonthArray[11] = "DEC";
	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	if ((strDay == null) || (strMonth == null) || (strYear == null)) {
		err = 11;
		return false;
	}
	
	int2DigitYear = parseInt(strYear, 10); // SRH Added 23/09/2005

	if (strYear.length == 2) {
		if (int2DigitYear < 50) {  // SRH Added 23/09/2005
			strYear = "20" + strYear;
		} else {
			strYear = "19" + strYear; // SRH Added 23/09/2005
		}
		
	}
	
	if (strYear.length == 1) {
		if (int2DigitYear < 50) { // SRH Added 23/09/2005
			strYear = "200" + strYear;
		} else {
			strYear = "190" + strYear; // SRH Added 23/09/2005
		}
	}
	
	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intDay = parseInt(strDay, 10);
	if (isNaN(intDay)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30 || intDay < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intDay < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intDay > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intDay > 28) {
				err = 10;
				return false;
			}
		}
	}
	if (intDay < 10) {
		strDay = "0" + intDay;
	}	
	if (strDatestyle == "US") {
		datefield.value = strMonthArray[intMonth-1] + "-" + strDay + "-" + strYear;
	}
	else {
		datefield.value = strDay + "-" + strMonthArray[intMonth-1] + "-" + strYear;
	}
	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) {
			return true;
		}
	}
	else {
		if ((intYear % 4) == 0) {
			return true;
		}
	}
	return false;
}

// R12794 Added 
function getOracleYearInteger(oracleDate) {
  var year = 0;

  if (oracleDate.length = 11) {
    year = oracleDate.substr(7,4);    
  }
    
  return year;
}

// R12794 Added 
function getOracleMonthInteger(oracleDate) {
  var month = 0;
  var monthStr = '';

  var intMonthArray = new Array(12);
    intMonthArray['JAN'] = 0;
    intMonthArray['FEB'] = 1;
    intMonthArray['MAR'] = 2;
    intMonthArray['APR'] = 3;
    intMonthArray['MAY'] = 4;
    intMonthArray['JUN'] = 5;
    intMonthArray['JUL'] = 6;
    intMonthArray['AUG'] = 7;
    intMonthArray['SEP'] = 8;
    intMonthArray['OCT'] = 9;
    intMonthArray['NOV'] = 10;
    intMonthArray['DEC'] = 11;

  // Will = 11 if 'DD-MON-YYYY' format
  if (oracleDate.length = 11) {

    monthStr = oracleDate.substr(3,3);    
    month = intMonthArray[monthStr];   
  }
  
  return month;
}

// R12794 Added 
function getOracleDayInteger(oracleDate) {
  var day = 0;

  if (oracleDate.length = 11) {
    day = oracleDate.substr(0,2);    
  }
    
  return day;
}

// R12794 Added 
// Returns the number of days between now and the specified date
// Returns a -ve number if the date is in the past and a +ve number
// if the date is in the future
function daysBetween(day,month,year) {
  var currentDate = new Date();
  var currentTime = currentDate.getTime();
  var enteredTime = Date.UTC(year, month, day);
  
  var betweenTime = (enteredTime - currentTime);
  
  return Math.round(betweenTime / DAY);
} 

// R12794 Added 
// Returns the number of months between now and the specified date
// Returns a -ve number if the date is in the past and a +ve number
// if the date is in the future
function monthsBetween(day,month,year) {
  var currentDate  = new Date();
  var currentDay   = currentDate.getDate();
  var currentMonth = currentDate.getMonth();
  var currentYear  = currentDate.getFullYear();

  var totalMonths = 0;
  
  var numberOfYears  = currentYear  - year;
  var numberOfMonths = currentMonth - month;

  // In the past 
  if (numberOfYears > 0) {
  
    if (numberOfMonths > 0) {
        totalMonths = -1 * ((numberOfYears * 12) + numberOfMonths);
    }
    else {
        totalMonths = -1 * ((numberOfYears * 12) + numberOfMonths);    
    }  
    // Need to make a correction so only deal with EXACT months
    if (day > currentDay) {
        totalMonths = totalMonths + 1;
    }
  }
  // In the future
  else {

    if (numberOfMonths > 0) {
        totalMonths = Math.abs(numberOfYears * 12) - numberOfMonths;
    }
    else {
        totalMonths = Math.abs(numberOfYears * 12) - numberOfMonths;    
    }
    // Need to make a correction so only deal with EXACT months
    if (day < currentDay) {
        totalMonths = totalMonths - 1;
    }
  }
    
  return totalMonths;
} 




