/******************************************/
/* (C)Copyright : Pegaxis Technologies    */
/* Author	: Simon Longley	          */
/* Created	: 24/08/2006		  */
/******************************************/

function validate()
{
	iFirstError 	= -1;
	sErrors		= "The following must be completed before you can continue:\n\n";
	
	for(i=0;i<aMandatoryFields.length;i++)
	{	
		if(window.document.data[aMandatoryFields[i][0]].value=="")
		{
			if(iFirstError == -1)
				iFirstError = i;
			sErrors+= " * "+aMandatoryFields[i][1]+"\n";
		}
	}
	if(iFirstError != -1)
	{
		alert(sErrors);
		window.document.data[aMandatoryFields[iFirstError][0]].focus();
		return(iFirstError);
	}
	else
		return(-1);
}

function validateForm()
{
	// Check the form and submit it if there are no mandatory fields missing.

	if(validate() == -1)
		window.document.data.submit();
}

function checkDateBoundary(dStart,dEnd)
{
	// Check That dEnd is AFTER dStart

	sDate = new String(dStart);

	if(sDate.indexOf("/") == -1)
		return("Incorrect separator specified");

	aDateParts	=	sDate.split('/');

	sDay1 	= aDateParts[0];
	sMonth1	= aDateParts[1];
	sYear1	= aDateParts[2];

	sDate2 = new String(dEnd);

	if(sDate2.indexOf("/") == -1)
		return("Incorrect separator specified");

	aDateParts	=	sDate2.split('/');

	sDay2 	= aDateParts[0];
	sMonth2 = aDateParts[1];
	sYear2	= aDateParts[2];
	
	// Check the years / months then days

	if(sYear2 < sYear1)
		return(-1);
	if(sYear2 == sYear1 && sMonth2 < sMonth1)
		return(-1);
	if(sYear2 == sYear1 && sMonth2 == sMonth1 && sDay2 < sDay1)
		return(-1);
	
	return(0);
}


function validateDate(oObject)
{
	// Ensure that the date specified in the string is valid.

	aDaysInMonth = new Array("0","31","28","31","30","31","30","31","31","30","31","30","31");

	sDate = new String(oObject.value);

	if(sDate.indexOf("/") == -1)
		return("Incorrect separator specified");

	aDateParts	=	sDate.split('/');

	sDay 	= aDateParts[0];
	sMonth 	= aDateParts[1];
	sYear	= aDateParts[2];

	if(isNaN(parseInt(sDay)))
		return("The day value must be a number");

	if(sDay.length > 1)
	{
		if(isNaN(parseInt(sDay.substring(2,1))))
			return("The day value must be a number");
	}

	if(sMonth.length > 1 && sMonth.substring(0,1) == '0')
	{
		sMonth = sMonth.substring(1,2);
	}

	if(isNaN(parseInt(sMonth)))
		return("The month value must be a number");

	if(isNaN(parseInt(sYear)))
		return("The year value must be a number");

	if((sYear % 4) == 0 || (sYear / 100) == 0)
		aDaysInMonth[2] = 29;

	if(sYear.length != 4)
		return("Date must be in yyyy format");

	if(parseInt(sYear) < 1900)
		return("The year specified is too early, please enter a later date");

	if(parseInt(sMonth) > 12)
		return("Invalid month specified");

	if(parseInt(sDay) > 31)
		return("Invalid day specified");

	if(parseInt(sDay) > parseInt(aDaysInMonth[parseInt(sMonth)]))
		return("Invalid day of the month specified");

	return(null);		
}

function checkDate(oObject)
{
	
	sRet = validateDate(oObject);

	if(sRet == null)
		return;

	alert(sRet);
	oObject.focus();
}

function validateTime(oObject)
{
	// Ensure that the time specified in the object will be Ok.

	sTime	= new String(oObject.value);

	if(sTime.indexOf(":") == -1)
		return("The Time Requires A Minutes Seperator (:)");
		
	sHours	= sTime.substring(0,sTime.indexOf(":"));
	sMins	= sTime.substring(sTime.indexOf(":") +1,sTime.length);

	if(sHours.length == 1)
		sHours = "0"+sHours;

	if(sMins.length == 1)
		sMins = "0"+sMins;

	iHours	= parseInt(sHours);
	iMins	= parseInt(sMins);

	if(isNaN(sHours))
		return("The hours value must be a number");
	if(isNaN(iMins))
		return("The Minutes value must be a number");

	if(iHours < 00 || iHours > 23)
		return("Invalid hour specified");

	if(iMins < 00 || iMins > 59)
		return("Invalid minute value specified");

	oObject.value	= sHours+":"+sMins;
	return(null);	
}

function checkTime(oObject)
{
	
	sRet = validateTime(oObject);

	if(sRet == null)
		return;

	richDialog(sRet);
	oObject.focus();
}
