/* 	datevalidation.js created by I.C. Causton 2000-10-21  	*/
/*															*/
/* 	Purpose: Provide functionality to display			 	*/
/*	appropriately and validate submitted dates				*/
/* 	the navigational areas within Clifford Chance Education */
/*	Education 											*/

//  Create arrays to hold date information
var aNames     		= new setArrayCreated('January','February','March','April','May','June','July','August',
	'September','October','November','December');
var aDays      		= new setArrayCreated(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var aDaysValue	= new setArrayCreated("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15",
	"16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
var aDow       		= new setArrayCreated('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

// Check using the form object whether the to and from date fields are valid - on their own and 
// compared to each other 
function setDateValidated(oFormObject) 
{
    var startday 	= oFormObject.fromDay.options[oFormObject.fromDay.selectedIndex].value;
    var startmonth 	= oFormObject.fromMonth.options[oFormObject.fromMonth.selectedIndex].value;
    var startyear 	= oFormObject.fromYear.options[oFormObject.fromYear.selectedIndex].text;
    var endday 		= oFormObject.toDay.options[oFormObject.toDay.selectedIndex].value;
    var endmonth 	= oFormObject.toMonth.options[oFormObject.toMonth.selectedIndex].value;
    var endyear 	= oFormObject.toYear.options[oFormObject.toYear.selectedIndex].text;

    unvalidstartdate 	= startday + '/' + startmonth + '/' + startyear;
    unvalidenddate 		= endday + '/' + endmonth + '/' + endyear;

    var startdate 	= new Date(startyear-0,startmonth-1,startday-0);
    var enddate 	= new Date(endyear-0,endmonth-1,endday-0);

    var validstartdate 	= padout(startdate.getDate()) + '/' + padout(startdate.getMonth()+1) + '/' + y2k(startdate.getYear())
    var validenddate 	= padout(enddate.getDate()) + '/' + padout(enddate.getMonth()+1) + '/' + y2k(enddate.getYear())
    
    if (unvalidstartdate != validstartdate) 
	{
        alert('Publication Date: ' + oFormObject.fromDay.options[oFormObject.fromDay.selectedIndex].text +
                         ' ' + oFormObject.fromMonth.options[oFormObject.fromMonth.selectedIndex].text +
                         ' ' + oFormObject.fromYear.options[oFormObject.fromYear.selectedIndex].text + ' is invalid');
        return false;
    }

    if (unvalidenddate != validenddate) 
	{
        alert('Review Date: ' + oFormObject.endday.options[oFormObject.toDay.selectedIndex].text +
                       ' ' + oFormObject.endmonth.options[oFormObject.toMonth.selectedIndex].text +
                       ' ' + oFormObject.endyear.options[oFormObject.toYear.selectedIndex].text + ' is invalid');
        return false;
    }

    starttime 	= Date.UTC(y2k(startdate.getYear()),startdate.getMonth(),startdate.getDate(),0,0,0);
    endtime 	= Date.UTC(y2k(enddate.getYear()),enddate.getMonth(),enddate.getDate(),0,0,0);
    
    if (starttime < endtime) 
	{
        // valid
    }
    else 
	{
        alert("'Publication Date' has to be before 'Review Date'");
        return false
    }
    
    currentdate = new Date();
    currenttime = Date.UTC(y2k(currentdate.getYear()),currentdate.getMonth(),currentdate.getDate(),0,0,0);
    
    if (endtime <= currenttime) 
	{
        // valid
    }
    else 
	{
        alert("Future dates are invalid");
        return false;
    }
    
    return true;
}

// Expand the day number if it is a singular digit
function padout(number) 
{ 
	return (number < 10) ? '0' + number : number; 
}

// Deal with millenium discrepancies
function y2k(number) 
{ 
	return (number < 1000) ? number + 1900 : number; 
}

// Set the value of hidden input fields in order to concatenate the date before submitting it
function setDates(oDateField1, oDateField2, sPath)
{
	var dFromDate	= null;
	var dToDate		= null;
	//var sPath		= parent.content.document.data;

	dFromDate			= new String(sPath.fromDay.value + "-" + sPath.fromMonth.value + "-" + sPath.fromYear.value);
	oDateField1.value	= dFromDate;

	if(oDateField2 != null)
	{
		dToDate	= new String(sPath.toDay.value + "-" + sPath.toMonth.value + "-" + sPath.toYear.value);
		oDateField2.value	= dToDate;
	}
}


//Added by NPS. 
// This will set the date for SHO to NHS start date! Required in 
// the GP Registrar Request Form. Set the value of hidden input fields in order 
// to concatenate the date before submitting it
function set_GP_SHO_dates(oDateField1, oDateField2, oDateField3, oDateField4, sPath)
{
	var dFromDate	= null;
	var dFromDate2  = null; 
    var dFromDate3  = null; 
	var dFromDate4  = null; 
    
    //var sPath		= parent.content.document.data;

	dFromDate			= new String(sPath.sho_nhs_start_day.value + "-" + sPath.sho_nhs_start_month.value + "-" + sPath.sho_nhs_start_year.value);
	oDateField1.value	= dFromDate;
	
	if(oDateField2 != null)
	{
	    dFromDate2			= new String(sPath.sho_nhs_end_day.value + "-" + sPath.sho_nhs_end_month.value + "-" + sPath.sho_nhs_end_year.value);
	    oDateField2.value	= dFromDate2;
	}
    
    if(oDateField3 != null)
	{
	    dFromDate3			= new String(sPath.gp_start_day.value + "-" + sPath.gp_start_month.value + "-" + sPath.gp_start_year.value);
	    oDateField3.value	= dFromDate3;
	}
	
	if(oDateField4 != null)
	{
	    dFromDate4			= new String(sPath.gp_end_day.value + "-" + sPath.gp_end_month.value + "-" + sPath.gp_end_year.value);
	    oDateField4.value	= dFromDate4;
	}

}

//Added by NPS. 
// This will set the date for Start and End of Change of working circumstances, in the 
// change of working circumstances form. Set the value of hidden input fields in order 
// to concatenate the date before submitting it
function set_start_end_dates(oDateField1, oDateField2, sPath)
{
	var dFromDate	= null;
	var dFromDate2  = null; 
    //var sPath		= parent.content.document.data;

	if(oDateField1 != null)
	{
	    dFromDate			= new String(sPath.start_day.value + "-" + sPath.start_month.value + "-" + sPath.start_year.value);
	    oDateField1.value	= dFromDate;
	}	
	if(oDateField2 != null)
	{
	    dFromDate2			= new String(sPath.end_day.value + "-" + sPath.end_month.value + "-" + sPath.end_year.value);
	    oDateField2.value	= dFromDate2;
	}
}


// Generic function to create and return an array object
function setArrayCreated() 
{
    for (i = 0; i < setArrayCreated.arguments.length; i++)
	{
        this[i] = setArrayCreated.arguments[i];
	}
}

// Function called from onChange event handler in order to display the correct number of days 
// in a chosen month
function changeMonth(oDaySelect, oYearSelect, iMonth) 
{
	/*var iNumberOfDays			= aDays[iMonth];
	var sText					= 1;
	oDaySelect.options.length	= 0;
	
	// Extra control statement to deal with leap year
	if((iMonth == 1) && (oYearSelect.value % 4 == 0))
	{
		iNumberOfDays	= iNumberOfDays + 1;
	}
	
	for(var i = 0; i < iNumberOfDays; i++)
	{
		
		if(i == 0)
		{
			oDaySelect.options[i]	= new Option(sText,aDaysValue[i],1,1);	
		}
		else 
		{
			sText					=	sText + 1; 
			oDaySelect.options[i]	= new Option(sText,aDaysValue[i]);
		}
	}*/
}


// Function to ensure that the number of days is correct if the year is changed when 
// the month is set to Feburary 
function setYearChanged(oYearSelect, iMonth)
{
	if(iMonth	== 1)
	{
		var oDaySelect	= null;
		
		if(oYearSelect.name.indexOf("from") != -1)
		{
			oDaySelect	= document.forms[0].fromDay;
		}
		else
		{
			oDaySelect	= document.forms[0].toDay;
		}
		
		changeMonth(oDaySelect, oYearSelect, iMonth);
	}
}

//	checks if the date given is in the past
function isDatePassed(dd, mm, yy) {
	var nowDate = new Date();
	nowDate.setMinutes(0);
	nowDate.setHours(0);
	nowDate.setSeconds(0);
	
	var d = selectValue(dd);
	var m = selectValue(mm);
	var y = selectValue(yy);
	
	if (d=='*' || m=='*' || y=='*') return true;
	
	//	get the date entered
	var thenDate = new Date();
	with (thenDate) {
		setSeconds(0);
		setMinutes(0);
		setHours(0);
		setYear(y);
		setMonth(m-1, d);
	}
	return thenDate.valueOf()<nowDate.valueOf();
}
