/*******************************************************
*	Tesco Week Routines
*
*	Description:	Allows several variations of a date to be entered
*					and validates it into a string with the standard
*					format of DD Mon CCYY
*
*	Author:			Nick Hindle
*
*	Created:		11 Jan 2001
*
*	Notes:			The parameter 'newdatefield' should be an HTML text object
*					as the formatted string is placed back into it and the string
*					is highlighted
*
*	Version History:
*
*	Date			Comment
*	----			-------
*	11 Jan 2001		First created
*
*******************************************************/

function formatdate(newdatefield)
{
	var months="JanFebMarAprMayJunJulAugSepOctNovDec";
	var monthdays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var day="",mon="",yy="";
	var p=0;
	var delims="\\/- ";
	var newdate = newdatefield.value;
	var validdate=true;
	var tm="",hh="",mm="";
		
	if (!newdate||newdate=="") return true;
	if (newdate!="today")
	{
	  //while (newdate.substring(p,p+1)>='0'&&newdate.substring(p,p+1)<='9')
	  while (delims.indexOf(newdate.substring(p,p+1),0)==-1)
		if (day.length==0&&newdate.substring(p,1+p)=='0')
			p++;
		else
			day+=newdate.substring(p,1+p++);
 	  while (newdate.substring(++p,p+1)==' ');
	  while (delims.indexOf(newdate.substring(p,p+1),0)==-1)
  		if (mon.length==0&&newdate.substring(p,1+p)=='0')
			p++;
		else
			mon+=newdate.substring(p,1+p++);
	  if (isNaN(0+mon))
	    {	mon=mon.substring(0,3).toLowerCase();
			mon = mon.charAt(0).toUpperCase() + mon.substring(1,20);
			if (months.indexOf(mon,0)==-1) mon=""; }
	  while (newdate.substring(++p,p+1)==' ');
	  //while (newdate.substring(p,p+1)>='0'&&newdate.substring(p,p+1)<='9')
	  while (delims.indexOf(newdate.substring(p,p+1),0)==-1)
   		if (yy.length==0&&newdate.substring(p,1+p)=='0')
			p++;
		else
			yy+=newdate.substring(p,1+p++);
  	  if (yy.length==1) yy="0"+yy;
	  if (yy.length==2) yy="20"+yy;
	  while (newdate.substring(++p,p+1)==' ');
	  while (p<newdate.length&&newdate.substring(p,p+1)!=' ')
	  	tm+=newdate.substring(p,1+p++);
			  
	  //if (0+day<1||0+mon<1||mon>12||0+yy<1900)
	  if (parseInt(day)<1||parseInt(mon)<1||parseInt(mon)>12||parseInt(yy)<1900)  
		validdate=false;
	}
	else 
	{
		d = new Date();
		d.getTime();
		day = d.getDate();
		mon = d.getMonth()+1;
		yy = d.getYear();
	}
	if (tm!="")	{
		p=0;
		while (tm.substring(p,p+1)>='0'&&tm.substring(p,p+1)<='9')
			hh+=tm.substring(p,1+p++);
		if (tm.substring(p,1+p)==':')
		{
			p++;
			while (tm.substring(p,p+1)>='0'&&tm.substring(p,p+1)<='9')
				mm+=tm.substring(p,1+p++);
		}
		if ((tm.indexOf("P")!=-1||tm.indexOf("p")!=-1)&&parseInt(hh)<=12)
			hh=parseInt(hh)+12;
		if (hh==24) hh=0;
		while (mm.length<2)
			mm="0"+mm;	
		tm=hh+":"+mm;
		if (parseInt(hh)<0||parseInt(hh)>23||parseInt(mm)<0||parseInt(mm)>59)
			validdate=false;
	}
	
	if (parseInt(day)<1||parseInt(mon)<1||parseInt(mon)>12||parseInt(yy)<1900||parseInt(yy)>2999) 
		validdate=false;

	p=(mon-1)*3;
	if (isNaN(p))
		if (day>monthdays[months.indexOf(mon,0)/3])
			validdate=false;
	else
		if (day>monthdays[0+mon-1])
			validdate=false;
			
	if (!validdate)
		{ alert("This is an invalid date"); newdatefield.focus(); newdatefield.select(); return false; }
		
	if (isNaN(p))
		newdatefield.value=day+' '+mon+' '+yy+' '+tm;
	else
		newdatefield.value=day+' '+months.substring(p,p+3)+' '+yy+' '+tm;
		
	return true;
}

