/*
	Function is used to check if the year is a leap year
*/
function IsLeapYear(theyear) 
{
	if (theyear % 100 == 0) 
	{
    	if (theyear % 400 == 0) 
	  	{ 
	  		return true; 
	  	}
	}
    else 
  	{
    	if ((theyear % 4) == 0) 
	 	{ 
	 		return true; 
	 	}
     }

	return false;
}

/*
	Function is used to check if a series of select boxes used to create a date
	contain a valid date (i.e. not February 30)
*/
function CheckDate(theform,thefield,themessage)
{
	var theday_field_name=thefield+"_day";
	var themonth_field_name=thefield+"_month";
	var theyear_field_name=thefield+"_year";
	var theday=0;
	var themonth=0;
	var theyear=0;
	
	theday=theform[theday_field_name][theform[theday_field_name].selectedIndex].value;
	themonth=theform[themonth_field_name][theform[themonth_field_name].selectedIndex].value;
	theyear=theform[theyear_field_name][theform[theyear_field_name].selectedIndex].value;
	
	if (themonth ==  2) 
	{
		if (theday > 28)
		{
			if (IsLeapYear(theyear))
			{
				if (theday > 29)
				{
					alert("February only has 29 days in a leap year.");
					theform[theday_field_name].focus();
					return false;
				}
			}
			else
			{
				alert("February only has 28 days.");	
				theform[theday_field_name].focus();
				return false;
			}
		}
	}
	if ((themonth == 4) || (themonth == 6) || (themonth == 9) || (themonth == 11))
	{
		if (theday > 30)
		{
			alert("Please pick 30 or less days for the " + themessage + ".");
			theform[theday_field_name].focus();
			return false;
		}
	}

return true;
}

function nostartdate(checkbox,theform,themonth,theday,theyear) {
	
	if (checkbox.checked) {
		theform.jobstartdate_day.options[0].selected ='true';
		theform.jobstartdate_month.options[0].selected ='true';
		theform.jobstartdate_year.options[0].selected ='true';
	} else {
		for (var x = 0; x <= 12; x++){
			if (theform.jobstartdate_month.options[x].value == themonth) {				
				theform.jobstartdate_month.options[x].selected = true;
				break;
			}
		}
		for (var x = 0; x <= 31; x++){
			if (theform.jobstartdate_day.options[x].value == theday) {
				theform.jobstartdate_day.options[x].selected = true;
				break;
			}
		}
		for (var x = 0; x <= 2; x++){
			if (theform.jobstartdate_year.options[x].value == theyear) {				
				theform.jobstartdate_year.options[x].selected = true;
				break;
			}
		}
	}
}

function noclosingdate(checkbox,theform,themonth,theday,theyear) {
	
	if (checkbox.checked) {
		theform.jobclosingdate_day.options[0].selected ='true';
		theform.jobclosingdate_month.options[0].selected ='true';
		theform.jobclosingdate_year.options[0].selected ='true';
	} else {
		for (var x = 0; x <= 12; x++){
			if (theform.jobclosingdate_month.options[x].value == themonth) {				
				theform.jobclosingdate_month.options[x].selected = true;
				break;
			}
		}
		for (var x = 0; x <= 31; x++){
			if (theform.jobclosingdate_day.options[x].value == theday) {
				theform.jobclosingdate_day.options[x].selected = true;
				break;
			}
		}
		for (var x = 0; x <= 2; x++){
			if (theform.jobclosingdate_year.options[x].value == theyear) {				
				theform.jobclosingdate_year.options[x].selected = true;
				break;
			}
		}
	}
}


function Check60Days(theform,currentmonth,currentday,currentyear,startdatemodified) {
	var end = new Date();
	var sixtydays = new Date();
	var message = '';
	
	end.setFullYear(theform.jobclosingdate_year.value,theform.jobclosingdate_month.value-1,theform.jobclosingdate_day.value);
	end.setDate(end.getDate());
	
	if (theform.jobnostartdate.checked) {	
		sixtydays.setFullYear(currentyear,currentmonth-1,currentday);
		message = "from now."; 
	} else {
		sixtydays.setFullYear(theform.jobstartdate_year.value,theform.jobstartdate_month.value-1,theform.jobstartdate_day.value);		
		message = "from the job start date."; 
	}
	
	sixtydays.setDate(sixtydays.getDate()+60);
	
	if ((end>sixtydays) || startdatemodified == 1) {
		if (startdatemodified != 1) {
			alert ("The closing date must be less than or equal to 60 days "+message);
		}
		for (var x = 0; x <= 11; x++){
			if (theform.jobclosingdate_month.options[x].value == sixtydays.getMonth()+1) {						
				theform.jobclosingdate_month.options[x].selected = true;
				break;
			}
		}
		for (var x = 0; x <= 30; x++){
			if (theform.jobclosingdate_day.options[x].value ==  sixtydays.getDate()) {
				theform.jobclosingdate_day.options[x].selected = true;
				break;
			}
		}
		for (var x = 0; x <= 1; x++){
			if (theform.jobclosingdate_year.options[x].value ==  sixtydays.getYear()) {				
				theform.jobclosingdate_year.options[x].selected = true;
				break;
			}
		}
	}
	
}


