<!--
	// Sunrise-sunset calculators, for computing azimuth or date of sunsets and sunrises
	// All javascript property of geomancy.org (Copyright 2008)

	// initialize days per month array - starting with March, ending with February
	var daysPerMonth = new Array(31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28);

	function deg2rad(degree){
		var rads = (degree/180) * Math.PI;
		
		return rads;
	}

	function rad2deg(rads){
		var degree = (rads/Math.PI) * 180;
		
		return degree;
	}
	
	function dateToDays(){
		var Y = document.initForm.year.value;
		var eqDate = document.initForm.eqDate.value;
		var theMonth = document.azForm.month.value;
		var theDate = document.azForm.date.value;
		theDate++;
		theDate--;
		
		// date of interest isn't in March
		if (theMonth != 0) {
			totalDays = daysPerMonth[0] - eqDate;
			for (i=1; i < theMonth; i++) {
				totalDays += daysPerMonth[i];
			}
			totalDays = totalDays + theDate;
		}
		// date of interest is in March
		else {
			if (theDate >= eqDate) {
				totalDays = theDate - eqDate;
			}
			else {
				marchDays = daysPerMonth[0] - eqDate;
				totalDays = marchDays + 334 + theDate + isLeap();				
			}
		}
		// add half a day, so rounding doesn't push the result into another day
		totalDays += .5;
		
		return totalDays;
	}
	
	function isLeap() {
	
		var Y = document.initForm.year.value;
			
		var r = Y % 4;
		var leap = 0;	
				
		if (r == 0) {
			r2 = Y % 100;
			if (r2 == 0) {
				r3 = Y % 400;
				if (r3 == 0) {
					leap = 1;
				}
			}
			else { leap = 1; }
		}
		return leap;
	}
	
	function daysToDate(days){
		var eqDate = document.initForm.eqDate.value;
		// seems to work, that the fallEqDate is one day before the eqDate...should verify
		var fallEqDate = eqDate - 1;
		var marchDays = 31 - eqDate;
		var theMonth = ""; var theMonth2 = ""; var theDate = ""; var theDate2 = ""; 
		var theResult = "Sorry...error in the calculation";
		eqDate++; eqDate--;
		days++; days--;
			
		if (days > 0) {
			if (days < marchDays) {
				theMonth = "March"; 
				theDate = eqDate + days;
			}
			else {
				aprilDays = marchDays + 30;
				if (days < aprilDays) { 
					theMonth = "April";
					theDate = days - marchDays;
					diffDays = (30-theDate) + 52;
					if (diffDays <= 71)
						{ theMonth2 = "August"; theDate2 = diffDays - 40; }
					else
						{ theMonth2 = "September"; theDate2 = diffDays - 71; }	
				}
				else {
					mayDays = aprilDays + 31;
					if (days < mayDays) { 
						theMonth = "May"; 
						theDate = days - aprilDays;
						diffDays = (31-theDate)+21;
						if (diffDays <= 41)
							{ theMonth2 = "July"; theDate2 = diffDays - 9; }
						else
							{ theMonth2 = "August"; theDate2 = diffDays - 40; }
					}
					else {
						juneDays = mayDays + 30;
						if (days < juneDays) { 
							theMonth = "June";
							theDate = days - mayDays;
							diffDays = 21 - theDate;
							if (diffDays < 10)
								{ theMonth2 = "June"; theDate2 = 21 + diffDays; }
							else
								{ theMonth2 = "July"; theDate2 = diffDays - 9; }}
						}
					}
			}
		}
		else if (days < 0) { 
			
			// default values, for now...until all the date logic is done
			theResult = "after Sept "+ fallEqDate + ", before Mar " + eqDate; theDate = "";
			
			posdays = Math.abs(days);
			septDays = 30 - fallEqDate;
			
			if (posdays < septDays) {
				theMonth = "September";
				theDate = fallEqDate + posdays;
				theMonth2 = "March";
				theDate2 = eqDate - posdays;		
			}
			else {
				octDays = septDays + 31;
				if (posdays < octDays) { 
					theMonth = "October";
					theDate = posdays - septDays;
					if (posdays < eqDate) 
						{ theMonth2 = "March"; theDate2 = eqDate - posdays; }
					else 
						{ theMonth2 = "February"; theDate2 = 28 + isLeap() - (posdays - eqDate); }
				}
				else {
					novDays = octDays + 30;
					if (posdays < novDays) { 
						theMonth = "November"; 
						theDate = posdays - octDays;
						if (posdays > (eqDate + (28 + isLeap()) ) )
							{ theMonth2 = "January"; theDate2 = 31 - (posdays - eqDate - (28+isLeap())); }
						else
							{ theMonth2 = "February"; theDate2 = 28 + isLeap() - (posdays - eqDate); }
					}
					else {
						decDays = novDays + 31;
						if (posdays < decDays) { 
							theMonth = "December";
							theDate = posdays - novDays;
							if (posdays > (eqDate + (28 + isLeap()) + 31) )
								{ theMonth2 = "December"; theDate2 = 31 - (posdays - (eqDate + (28 + isLeap()) + 31)); }
							else
								{ theMonth2 = "January"; theDate2 = 31 - (posdays - eqDate - (28+isLeap())); }
						}
					}
				}
			}
			
			
		}
		
		else if ( isNaN(days) ) { theResult = "Adjust azimuth by .1" }
		
		if (theDate2) {
			theDate = Math.floor(theDate / 1);
			theDate2 = Math.floor(theDate2 / 1);
			theResult = theMonth + " " + theDate + " OR " + theMonth2 + " " +theDate2;
			}
		else if (theDate) {
			theDate = Math.floor(theDate / 1);
			theResult = theMonth + " " + theDate;
		}
		
		
		// for error shooting, the line commented out below will give the raw variable days as the result
		/* theResult = days; */
		
		document.dayForm.result.value = theResult;
		
		
		return true;
	}
	
	function decOfAngles(){
		
		var lat = document.dayForm.lat.value;
		var horz = document.dayForm.horz.value;
		var az = document.dayForm.az.value;
		
		var rlat = deg2rad(lat);
		var rhorz = deg2rad(horz);
		var raz = deg2rad(az);
		
		var rdec = Math.asin ( ( Math.sin(rlat) * Math.sin(rhorz) ) + ( Math.cos(rlat) * Math.cos(rhorz) * Math.cos(raz) ));
		
		var dec = rad2deg(rdec);
		document.dayForm.declination.value = dec;
		
		return rdec;
		
	}
	
	function day(){
	
		var rprec = deg2rad(23.45);
		
		
		var rday = ( Math.asin(decOfAngles()/rprec) * 1.0146 );
		
		var day = rad2deg(rday);
		
		if (Math.abs(decOfAngles()) > 0.4102)
		{
			document.dayForm.result.value = 'Impossible Azimuth...please re-enter.';
			document.dayForm.declination.value = "";
		}
		
		else if ((document.dayForm.lat.value > 90) || (document.dayForm.lat.value < 0) || (document.dayForm.horz.value > 90) || (document.dayForm.horz.value < 0) || (document.initForm.year.value <= 0)) { 
		document.dayForm.result.value = "Values out of range - please re-enter"; 
		document.dayForm.declination.value ="";
		}
		
		else { daysToDate(day); }
		
		return true;
	}
	
	function decOfDay(){
		
		var t = dateToDays();
		var rprec = deg2rad(23.45);
		var rday = deg2rad(0.9856 * t);
		
		var rdec = rprec * Math.sin(rday);
		
		var dec = rad2deg(rdec);
		document.azForm.declination.value = dec;
		
		return rdec;
		
	}
	
	function azimuth(){
		
		var lat = document.azForm.lat.value;
		var horz = document.azForm.horz.value;
		
		var rlat = deg2rad(lat);
		var rhorz = deg2rad(horz);
		
		var raz = Math.acos ( ( Math.sin(decOfDay()) - (Math.sin(rlat) * Math.sin(rhorz) ))/ ( Math.cos(rlat) * Math.cos(rhorz) ) );
		
		var az = rad2deg(raz);
		
		az = Math.round(az*100)/100;
		set_az = 270 - (az-90);
		
		document.azForm.result.value = az;
		document.azForm.set_result.value = set_az;
		
		if (isLeap()){
			daysPerMonth[11] = 29;
		}
		else { daysPerMonth[11] = 28; }
		
		if ((document.azForm.date.value > daysPerMonth[document.azForm.month.value]) || (document.azForm.date.value < 1)) { 
		document.azForm.result.value = "Impossible Date...please re-enter."; 
		document.azForm.set_result.value = "";
		document.azForm.declination.value ="";
		}
		
		if ((document.azForm.lat.value > 90) || (document.azForm.lat.value < 0) || (document.azForm.horz.value > 90) || (document.azForm.horz.value < 0) || (document.initForm.year.value <= 0)) { 
		document.azForm.result.value = "Values out of range - please re-enter"; 
		document.azForm.set_result.value = "";
		document.azForm.declination.value ="";
		}
		
		
		return true;
			
	}
	
function radio_selected(radiobutton) {
 for (i = 0; i < radiobutton.length; i++) {
  if (radiobutton[i].checked) {
   return true;
  }
 }
 return false;
}

function check_form(page) {
if ( (page == 1) && (!radio_selected(document.prontosurvey.q1)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q2)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q3)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q4)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q5)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q6)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q7)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q8)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q9)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q10)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q11)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q12)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q13)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q14)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q15)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q16)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q17)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q18)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q19)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 1) && (!radio_selected(document.prontosurvey.q20)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}
if ( (page == 2) && (!radio_selected(document.prontosurvey.q21)) ) {
 alert('One or more mandatory fields are empty');
 return false;
}

}