
function checkDateFromControl(strControl){
	var strDate = document.getElementById(strControl).value ;
	
	if (strDate.length>0){
		if(IsValidDate(strDate).length == 0){
			alert('Invalid date format.');
			document.getElementById(strControl).focus();
		}
	}	
}

function monthtonum(strMonth){
/*********************************************************************************
* Function:		monthtonum - Converts a long month to a number
*				
* Inputs:		strMonth - longmonth like those in array
*
* Outputs:		month as a number e.g. 3 for March
**********************************************************************************/
var MonthArray = Array('January','February','March','April','May','June','July','August','September','October','November','December');
								
	if (strMonth.length > 2) {
		for(i=0;i<12;i++){
			if(strMonth==MonthArray[i]){
			strMonth = i+1
			}	
		}
	}
							
	return strMonth;
}

function FixInt(txtText)
/*********************************************************************************
* Function:		FixInt - Remove trailing 0s due to problem with parseInt('08')
*				
* Inputs:		txtText - String to strip the 0 from
*
* Outputs:		Trimmed string
**********************************************************************************/
{
	var strText = new String(txtText);
	var aryText = strText.split('.');
	strText = aryText[0];
	var iLen = strText.length;
	var iStart = 'NaN';

	if (iLen > 0)
	{
		for (i=0;i<iLen;i++)
		{
			if (strText.charAt(i) != '0')
			{
				iStart = i;
				break;
			}
		}
		
		if (iStart == 'NaN')
		{
			iStart = (iLen - 1);
		}

		return strText.substr(iStart);
	}
	else
	{
		return '';
	}
}


function IsValidDate(strDateValue){
/*******************************************************************************
* Function:		IsValidDate - Checks if the date is valid and formats it if so	
*
* Inputs:		strDateValue - String that represents a date
*
* Outputs:		"" - invalid  OR "dd/mm/yyyy" - valid
*******************************************************************************/
	
	var iLeapDays;
	var	SplitArray = new Array();
	
	var arrSplitCharacters = new Array(3) ;
	arrSplitCharacters[0]="/"; 
	arrSplitCharacters[1]="-"; 
	arrSplitCharacters[2]=".";

	for(var n=0;n<arrSplitCharacters.length;n++){
		if (InStr(strDateValue,arrSplitCharacters[n]) > -1){
			//alert('hit')
			SplitArray = strDateValue.split(arrSplitCharacters[n]);
		}
	}
	
	//alert(SplitArray[0])
	//alert(SplitArray[1])
	//alert(SplitArray[2])
		
	//var	SplitArray = strDateValue.split("/");
	
/****************************************************
*	Count there are 3 items
****************************************************/
	if (SplitArray.length != 3)
	{
		return '';
	}
	
/****************************************************
* split the string
****************************************************/

	var	strDay = SplitArray[0];
	var strMonth = SplitArray[1];
	var strYear = SplitArray[2];
	
/****************************************************
* Store the number of days per month
****************************************************/
	var DayArray = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		
/****************************************************
* Check if any are empty or NOT numeric
****************************************************/
	
	if ((strDay.length == 0) ||
			(strMonth.length == 0) ||
			(strYear.length == 0))
	{
		return '';
	}
	
	if ((isNaN(parseInt(FixInt(strDay))) != false) ||
			(isNaN(parseInt(FixInt(strMonth))) != false) ||
			(isNaN(parseInt(FixInt(strYear))) != false))
	{
		return '';
	}
	else
	{
		var iDay = parseInt(FixInt(strDay));
		var iMonth = parseInt(FixInt(strMonth));
		var iYear	 = parseInt(FixInt(strYear));		
	}

/****************************************************
* Check the month is within the boundry
****************************************************/
	if ((iMonth < 1) || (iMonth > 12))
	{
		return '';
	}

/****************************************************
* Check the day is not negative 
****************************************************/
	if (iDay < 1)
	{
		return '';
	}

/****************************************************
* 	Check the Year is not than 4 digits 
****************************************************/
	
//	if ((iYear < 1000) || (iYear > 9999))
//	{
//		return '';
//	}
	
	var strYear = '';
	var strYear = iYear+'';
	

	if ((strYear.length == 3) || (strYear.length > 4))
	{
		return '';
	}


/****************************************************
* Check the number of days is correct for each month 
* except for Feb
****************************************************/
	if (iMonth != 2)
	{
		if (iDay > DayArray[iMonth - 1])
		{
			return '';
		}	
	}


/****************************************************
* Checking Feb for the number of days depending on 
* the year (Look for the Leap) - % is MOD
****************************************************/
	if (iMonth == 2)
	{
		iLeapDays=DaysInFeb(iYear);
		
		if (iDay > iLeapDays)
		{
			return '';
		}
	}

/****************************************************
* Everything is good so output in the correct format
****************************************************/

	// Use this if you want those leading zeros
	//return FormatDate(iDay + '/' + iMonth + '/' + iYear);	
	
	//Please no leading zeros
	return (iDay + '/' + iMonth + '/' + iYear);	

}


function FormatDate(strDate)
{
/***********************************************************************************
* Function:		FormatDate 	
*
* Inputs:		string with a correct date
*
* Outputs:		string in format dd/mm/yyyy
************************************************************************************/
	
	var	SplitArray = strDate.split("/");
	var strDay;
	var strMonth;
	var strYear;

/****************************************************
*	if the day is single figure add 0
****************************************************/

	if (SplitArray[0].length == 1)
	{
		strDay = '0' + SplitArray[0];
	}
	else
	{
		strDay = SplitArray[0];
	}

/****************************************************
*	if the month is single figure add 0
****************************************************/
	if (SplitArray[1].length == 1)
	{
		strMonth = '0' + SplitArray[1];
	}
	else
	{
		strMonth = SplitArray[1];
	} 

	return strDay + '/' + strMonth + '/' + SplitArray[2];
}


function DaysInFeb(iYear)
/************************************************************************************
* Function:		DaysInFeb
*
* Inputs:		Year in format yyyy 
*
* Outputs:		days in feb e.g 28
*************************************************************************************/
{
	var iLeapDays;

		if ((iYear % 4) == 0)
		{
			if ((iYear % 100) == 0)
			{
				if ((iYear % 400) == 0)
				{
					iLeapDays = 29;
				}
				else
				{
					iLeapDays = 28;
				}
			}
			else
			{
				iLeapDays = 29;
			}
		}
		else
		{
			iLeapDays = 28;
		}
		
	return iLeapDays
		
}

function RemovePadding(iNum)
/************************************************************************************
* Function:		RemovePadding
*
* Inputs:		Number (with leading zeros)
*
* Outputs:		Same Number (without leading zeros)
*************************************************************************************/
{
	if ((iNum.length == 2) && (iNum.slice(0,1) == '0'))
		{
			iNum = iNum.slice(1,2);
		}
	return iNum;
}



function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}


      function Mid(str, start, len)
        /***
                IN: str - the string we are LEFTing
                    start - our string's starting position (0 based!!)
                    len - how many characters from start we want to get

                RETVAL: The substring from start to start+len
        ***/
        {
                // Make sure start and len are within proper bounds
                if (start < 0 || len < 0) return "";

                var iEnd, iLen = String(str).length;
                if (start + len > iLen)
                        iEnd = iLen;
                else
                        iEnd = start + len;

                return String(str).substring(start,iEnd);
        }


        function Len(str)
        /***
                IN: str - the string whose length we are interested in

                RETVAL: The number of characters in the string
        ***/
        {  return String(str).length;  }

