
function incrementToday(myOffset)
{//Function to Get today's date and add myOffset to it. Returns a date string in UK Format.
myDateObj = new Date ; //Create date object
myDateObj.setUTCMilliseconds( myDateObj.getUTCMilliseconds() + (86400000 * myOffset) ) ;  //Decrement by exactly one day (86400000 milliseconds).
myDate =  myDateObj.getUTCDate();  //Get the date (0-31) from myDateObj
myMonth =  myDateObj.getUTCMonth() + 1 ;   //Get the month  from myDateObj. We increment because JS months go 0-11 !!!
myYear  =  myDateObj.getUTCFullYear(); //Get the year  from myDateObj

// Convert to strings, so that we can add a leading 0 if needed.
myDate=myDate.toString() ;
myMonth=myMonth.toString() ;

if ( myDate.length <2 ) {myDate = '0'+ myDate}
if ( myMonth.length <2 ) {myMonth = '0'+ myMonth}

myUKDateString = myDate + '-' + myMonth + '-' + myYear; 
return (myUKDateString);
}








function offsetDate(myDate,myOffset)
{
//Function to take a UK short format date and add myOffset (in days) to it. Returns a date string in UK Format.
//Offsets can be negative.

//Extract items from UK date string and convert to floating integers.
if (Day   =   myDate.substr(0,2)) {} else {err = true; }
if (Month =   myDate.substr(3,2)) {} else {err = true; }
if (Year  =   myDate.substr(6,4)) {} else {err = true; }

Year=parseFloat(Year);
Day=parseFloat(Day);
Month=parseFloat(Month);

//Create new date object to help manipulate date, and populate date object with components of UK String.
myDateObj = new Date() 
myDateObj.setUTCDate(Day)
myDateObj.setUTCMonth(Month-1)
myDateObj.setUTCFullYear(Year)

//Set the UTC of the date object to itself plus our offset. Store components of updated date object.
myDateObj.setUTCMilliseconds( myDateObj.getUTCMilliseconds() +  (86400000 * myOffset) ) ;
myDay =  myDateObj.getUTCDate();  //Get the date (0-31) from myDateObj
myMonth =  myDateObj.getUTCMonth() + 1 ;   //Get the month  from myDateObj. We increment because JS months go 0-11 !!!
myYear  =  myDateObj.getUTCFullYear(); //Get the year  from myDateObj


// Convert to strings, so that we can add a leading 0 if needed.
myDay=myDay.toString() ;
myMonth=myMonth.toString() ;

if ( myDay.length <2 ) {myDay = '0'+ myDay}
if ( myMonth.length <2 ) {myMonth = '0'+ myMonth}

//Construct an updated UK string and return.
myUKDateString = myDay + '-' + myMonth + '-' + myYear; 
return (myUKDateString);
}






















function checkDate(myDate)
{
err = false;  /* Reset error flag before searching for errors*/


/* Extract date elements from the UK formatted string.
   If this operation fails for any element, exit the function and return false.*/

if (Day   =   myDate.substr(0,2)) {} else {err = true; }
if (Month =   myDate.substr(3,2)) {} else {err = true; }
if (Year  =   myDate.substr(6,4)) {} else {err = true; }	 


/* Convert date elements into integers using parseFloat - note that parseInt breaks down at 8!*/
Year=parseFloat(Year);
Day=parseFloat(Day);
Month=parseFloat(Month);


/* Validation of Year*/
if ((Year < 1) || (Year > 2999)) { err = true; }  /* Error if Year not between 0001 and 3000* /

/* Validation of Month*/
if ((Month < 1) || (Month > 12)) { err = true; }  /* Error if Month not 1-12* /

/* Validation of Day*/
if (Day < 1) { err = true; }  /* Error if Day less than one * /


/* Validation of Day in Months affected by leap Year 
   February cannot have 28 or 29 Days in a leap Year  */
if ((Year % 4 == 0) || (Year % 100 == 0) || (Year % 400 == 0)) { leap = 1; } else { leap = 0; }
if ((Month == 2) && (leap == 1) && (Day > 29)) {  err = true; }
if ((Month == 2) && (leap != 1) && (Day > 28)) {  err = true; }


/* Validation of Day in Months not affected by leap year
	Odd numbered Months have 31 Days, even numbered Months have 30 Days, apart from February which is validated above*/
if ((Day > 31) && ((Month == "01") || (Month == "03") || (Month == "05") 
|| (Month == "07") || (Month == "08") || (Month == "10") || (Month == "12")))
	{ err = true; }
if ((Day > 30) && ((Month == "04") || (Month == "06") || (Month == "09") || (Month == "11"))) 
	{  err = true; }


	
if (err == false) {valid = true } else {valid = false }; /*Function returns false if the date is invalid. */
return (valid);

} /* End of Function */

	
function UKDateToUTC(myDate)  /*  Converts a UK date string dd/mm/yyyy to UTC milliseconds */ 
{ 
if (Day   =   myDate.substr(0,2)) {} else {err = true; }
if (Month =   myDate.substr(3,2)) {} else {err = true; }
if (Year  =   myDate.substr(6,4)) {} else {err = true; }

Year=parseFloat(Year);
Day=parseFloat(Day);
Month=parseFloat(Month);

//Create new date object to store this date and populate the object with components of the UK date string. 
myDateObj = new Date() 
myDateObj.setUTCDate(Day)
myDateObj.setUTCMonth(Month-1)
myDateObj.setUTCFullYear(Year)

//Tease out the components just as a check. Not really needed.
myDay =  myDateObj.getUTCDate();  //Get the date (0-31) from myDateObj
myMonth =  myDateObj.getUTCMonth() + 1 ;   //Get the month  from myDateObj. We increment because JS months go 0-11 !!!
myYear  =  myDateObj.getUTCFullYear(); //Get the year  from myDateObj

myDate=myDateObj.valueOf()  //Tease out the final value in milliseconds.
return (myDate) ;
}  /* End of function */











function calc_status_timeout()
{
//Void Function to calculate status timeouts for SO_add, SO_update, PO_add, PO_update
//This code relies on webform controls in the pages listed above.
if (document.mainForm.status_timeout_selector.selectedIndex ==  0)
{
	status_timeoutDIV.innerHTML=offsetDate(document.mainForm.UKToday.value,document.mainForm.Xdays.value)
	document.mainForm.status_timeout.value=status_timeoutDIV.innerHTML
}


if (document.mainForm.status_timeout_selector.selectedIndex == 1)
{
	if (checkDate(document.mainForm.travel_date.value)) {
	status_timeoutDIV.innerHTML=offsetDate(document.mainForm.travel_date.value,(document.mainForm.Xdays.value * -1) )
	document.mainForm.status_timeout.value=status_timeoutDIV.innerHTML }
		else {status_timeoutDIV.innerHTML='The travel date is not valid.'}
}

if (document.mainForm.status_timeout_selector.selectedIndex == 2)
{
	if (checkDate(document.mainForm.travel_date.value)) {
	status_timeoutDIV.innerHTML=offsetDate(document.mainForm.travel_date.value,(document.mainForm.Xdays.value) )
	document.mainForm.status_timeout.value=status_timeoutDIV.innerHTML }
		else {status_timeoutDIV.innerHTML='The travel date is not valid.'}
}

if (document.mainForm.status_timeout_selector.selectedIndex == 3)
{
	if (checkDate(document.mainForm.return_date.value)) {
	status_timeoutDIV.innerHTML=offsetDate(document.mainForm.return_date.value,(document.mainForm.Xdays.value) ) }
		else {status_timeoutDIV.innerHTML='The return date is not valid.'}
}

}
