// Updates the url to which the form needs to be submitted.
function reviewChanges(){

	//Make sure school-name, address, contact person and e-mail are not empty
	if(document.sch_register.sch_name.value.length ==0){
		alert('Enter the School/Parish name');
		document.sch_register.sch_name.focus();
		return false;
	}
	if(document.sch_register.sch_street.value.length ==0){
		alert('Enter Street address for the School/Parish');
		document.sch_register.sch_street.focus();
		return false;
	}
	if(document.sch_register.sch_city.value.length ==0){
		alert('Enter City for the School/Parish');
		document.sch_register.sch_city.focus();
		return false;
	}
	if(document.sch_register.sch_state.value.length ==0){
		alert('Enter State for the School/Parish');
		document.sch_register.sch_state.focus();
		return false;
	}else if(document.sch_register.sch_state.value.length > 2){
		alert('Enter only two letters for State');
		document.sch_register.sch_state.focus();
		return false;
	}
	if(document.sch_register.sch_zip.value.length ==0){
		alert('Enter Zip code for the School/Parish');
		document.sch_register.sch_zip.focus();
		return false;
	}else if(!validateNumber(document.sch_register.sch_zip.value)){
		alert('School/Parish Zipcode not valid.Enter only numbers');
		document.sch_register.sch_zip.focus();
		return false;
	}
	if(document.sch_register.first_name.value.length ==0){
		alert('Enter First Name for School/Parish contact person');
		document.sch_register.first_name.focus();
		return false;	
	}
	if(document.sch_register.last_name.value.length ==0){
		alert('Enter Last Name for School/Parish contact person');
		document.sch_register.last_name.focus();
		return false;	
	}
	if(document.sch_register.contact_email.value.length ==0){
		alert('Enter Contact E-mail for School/Parish');
		document.sch_register.contact_email.focus();
		return false;	
	}else if(!validateEmail(document.sch_register.contact_email.value)){
		alert('School/Parish contact E-mail not valid');
		document.sch_register.contact_email.focus();
		return false;
	}
	if(document.sch_register.sch_no_stu.value.length ==0){
		alert('Enter number of test takers for School/Parish');
		document.sch_register.contact_email.focus();
		return false;	
	}else if(!validateNumber(document.sch_register.sch_no_stu.value)){
		alert('School/Parish student count not valid');
		document.sch_register.sch_no_stu.focus();
		return false;
	}
	if(document.sch_register.sch_ts_month.value == '-'){
		alert('Enter Survey start month');
		document.sch_register.sch_ts_month.focus();
		return false;	
	}
	if(document.sch_register.sch_ts_date.value == '-'){
		alert('Enter Survey start date');
		document.sch_register.sch_ts_date.focus();
		return false;	
	}
	if(document.sch_register.sch_ts_year.value == '-'){
		alert('Enter Survey start year');
		document.sch_register.sch_ts_year.focus();
		return false;	
	}
	if(document.sch_register.sch_te_month.value == '-'){
		alert('Enter Survey end month');
		document.sch_register.sch_te_month.focus();
		return false;	
	}
	if(document.sch_register.sch_te_date.value == '-'){
		alert('Enter Survey end date');
		document.sch_register.sch_te_date.focus();
		return false;	
	}
	if(document.sch_register.sch_te_year.value == '-'){
		alert('Enter Survey end year');
		document.sch_register.sch_te_year.focus();
		return false;	
	}
	//alert('validating dates');
	if(!validateDates(parseInt(document.sch_register.sch_ts_month.value),
					  parseInt(document.sch_register.sch_ts_date.value),
					  parseInt(document.sch_register.sch_ts_year.value),
					  parseInt(document.sch_register.sch_te_month.value),
  					  parseInt(document.sch_register.sch_te_date.value),
					  parseInt(document.sch_register.sch_te_year.value))){
		return false;
	}
	//alert('validated dates');
	getDioceseName();
	getProgramName();
	document.sch_register.action="review_changes_sch.jsp";
	return true;
}

//Validate e-mail address
function validateEmail(email){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(filter.test(email)){
		//alert('matched email');
		return true;
	}else{
		//alert('not matched email');
		return false;
	}
}

//validate numeric fields
function validateNumber(num){
	var charpos = num.search("[^0-9]");
	if(charpos >= 0){
		return false;
	}else{
		return true;
	}
}
//Get the name of the diocese
function getDioceseName(){
	var selectBox = document.forms[0].diocode;
	document.sch_register.dio_name.value= selectBox.options[selectBox.selectedIndex].text;
	//alert(document.sch_register.dio_name.value);
	return true;
}

//Get the name of the selected program
function getProgramName(){
	var selectBox = document.forms[0].sch_prog;
	document.sch_register.sch_program.value= selectBox.options[selectBox.selectedIndex].text;
	//alert(document.sch_register.sch_program.value);
	return true;
}

function validateDates(ts_mon,ts_date,ts_year,te_mon,te_date,te_year){
	var today = new Date();
	//alert('current Date is'+ today);
	var currentMonth = today.getMonth();
	//returns 0 for Jan so add 1.
	currentMonth = currentMonth + 1;
	//alert('current Month is'+ currentMonth);
	//alert('ts_month '+ document.sch_register.sch_ts_month.value);
	var currentDate = today.getDate();
	//alert('current Day is'+ currentDate);
	var currentYear = today.getFullYear();
	
	//Compare current date and start date
	if(ts_year == currentYear){
		if(ts_mon == currentMonth){
			if(ts_date < currentDate){
				alert('Survey start date should be today or later');
				document.sch_register.sch_ts_date.focus();
				return false;
			}
		}else if(ts_mon < currentMonth){
			alert('Survey start month should be greater or equal to the current month');	
			document.sch_register.sch_ts_month.focus();				
			return false;
		}
	}
	if(!validateDayOfTheMonth(ts_date,ts_mon,ts_year,1)){
		return false;
	}

	//Compare start and end dates
	if(te_year == ts_year){
		if(te_mon == ts_mon){
			if(te_date < ts_date){
				alert('Survey end date should be greater that start date');
				document.sch_register.sch_te_date.focus();
				return false;
			}
		}else if(te_mon < ts_mon){
			alert('Survey end month should be greater or equal to the test start month');					
			document.sch_register.sch_te_month.focus();
			return false;
		}
	}else if(te_year < ts_year){//Since we are listing only 5 years starting from current year, te_year can neverbe less than ts_year.
			alert('Survey end year should be greater or equal to test start year');					
			document.sch_register.sch_te_year.focus();
			return false;
	}
	
	if(!validateDayOfTheMonth(te_date,te_mon,te_year,2)){
		return false;
	}
	return true;
}
function validateDayOfTheMonth(date,mon,year,date_type){
	//Check for leap year if month is feb
	if(mon == 2){
		//alert('month is feb');
		//alert(year%4);
		if(year%4 == 0){
			//alert('year is leap');
			if(date <1 || date>29){
				if(date_type == 1){
						alert('Survey start date must be between 1-29 for the selected month');
						document.sch_register.sch_ts_date.focus();
						return false;
				}else if(date_type == 2){
						alert('Survey end date must be between 1-29 for the selected month');
						document.sch_register.sch_te_date.focus();
						return false;
				}
			}
		}else{
			//alert('year is not leap');
			if(date <1 || date>28){
				if(date_type == 1){
						alert('Survey start date must be between 1-28 for the selected month');
						document.sch_register.sch_ts_date.focus();
						return false;
				}else if(date_type == 2){
						alert('Survey end date must be between 1-28 for the selected month');
						document.sch_register.sch_te_date.focus();
						return false;
				}
			}
		
		}
	}else if(mon <= 7){
		if(mon%2 ==1){
			if(date<1 || date>31){
				if(date_type == 1){
						alert('Survey start date must be between 1-31 for the selected month');
						document.sch_register.sch_ts_date.focus();
						return false;
				}else if(date_type == 2){
						alert('Survey end date must be between 1-31 for the selected month');
						document.sch_register.sch_te_date.focus();
						return false;
				}
			}
		}else{
			if(date<1 || date>30){
				if(date_type == 1){
						alert('Survey start date must be between 1-30 for the selected month');
						document.sch_register.sch_ts_date.focus();
						return false;
				}else if(date_type == 2){
						alert('Survey end date must be between 1-30 for the selected month');
						document.sch_register.sch_te_date.focus();
						return false;
				}
			}			
		}
	}else if(mon > 7){
		if(mon%2 ==0){
			if(date<1 || date>31){
				if(date_type == 1){
						alert('Survey start date must be between 1-31 for the selected month');
						document.sch_register.sch_ts_date.focus();
						return false;
				}else if(date_type == 2){
						alert('Survey end date must be between 1-31 for the selected month');
						document.sch_register.sch_te_date.focus();
						return false;
				}
			}
		}else{
			if(date<1 || date>30){
				if(date_type == 1){
						alert('Survey start date must be between 1-30 for the selected month');
						document.sch_register.sch_ts_date.focus();
						return false;
				}else if(date_type == 2){
						alert('Survey end date must be between 1-30 for the selected month');
						document.sch_register.sch_te_date.focus();
						return false;
				}
			}			
		}
	}
	return true;
}
