/*	Application  : John Day River Boater Permits, Oregon
	Name         : js/permitappform.js
	Author       : Tim Carley 
	Created      : March 01, 2009
	Last Updated : 3/7/2011
	History      : 3/7/2011; changed calls to isdate() JS function
					3/2/2011; removed old code; debugging Google Chrome; 
					added more detailed error messgaes for alt leaders
					2/28/2011; added Email2 for verification
					2/10/2011; added checking of alt leaders to validateForm()
					2/9/2011; removed references to vendor site; added addAltLeader()
					5/13/2010; added support for DateofBirth
	Purpose		 : common Javascript functions used on the permit app form
*/
function sGo(input) {
	var submit = validateForm();
	if (submit == true)	{
		document.PSForm.wassub.value = 1 ;
		document.PSForm.submit();
	}
}
function CheckAccess(field){
	var nIndex=field.selectedIndex;
	var nAccessPoint=field.options[nIndex].value;
	var lcnhpt = 1 ;
	var slcttxt = "Launch Point" ;
	if (field.name == 'TakeoutPointID') {
		lcnhpt = 0 ;
		var slcttxt = "Take Out Point" ;
	}
	if (nAccessPoint==0){
		field.length=0;
		field.options[0] = new Option('Select One '+ slcttxt + '',0,1,1);
		for (var i=0; i< avAccessPoints.length; i++){
			if (avAccessPoints[i].launchpoint==lcnhpt){
				nIndex=field.options.length;
				field.options[nIndex]=new Option(avAccessPoints[i].name, avAccessPoints[i].accessid);
				if (avAccessPoints[i].restricted == 1){
					field.options[nIndex].style.backgroundColor = "#ffc1c1" ;
				}
				else if (avAccessPoints[i].recommended == 1) {
					field.options[nIndex].style.backgroundColor = "#00ff99" ;
				}
			}
		}
	}
}
function chgNumGuides() {
	document.PSForm.NumberofGuides.disabled = false ;
	document.PSForm.NumberofClients.disabled = false ;
	var numboaters = document.PSForm.NumberofBoaters.options[document.PSForm.NumberofBoaters.selectedIndex].value ;
	document.PSForm.NumberofGuides.length=0 ;
	document.PSForm.NumberofGuides.options[0]=new Option(0, '');
	for (g=1; g <= numboaters; g++) {
		nIndex=document.PSForm.NumberofGuides.options.length;
		document.PSForm.NumberofGuides.options[nIndex]=new Option(g, g);
	}
	document.PSForm.NumberofClients.length=0 ;
	document.PSForm.NumberofClients.options[0]=new Option(0, '');
	for (b=1; b <= numboaters; b++) {
		nIndex=document.PSForm.NumberofClients.options.length;
		document.PSForm.NumberofClients.options[nIndex]=new Option(b, b);
	}
}
function chgNumGuides2() {
	var thenum = document.PSForm.NumberofClients.options[document.PSForm.NumberofClients.selectedIndex].value ;
	var bolselected = 0 ;
	var numboaters = document.PSForm.NumberofBoaters.options[document.PSForm.NumberofBoaters.selectedIndex].value ;
	var maxguides = parseInt(numboaters-thenum) ;
	document.PSForm.NumberofGuides.length=0 ;
	document.PSForm.NumberofGuides.options[0]=new Option(0, '');
	for (b=1; b <= maxguides; b++) {
		nIndex=document.PSForm.NumberofGuides.options.length;
		if (b==maxguides) { bolselected = 1; }
		document.PSForm.NumberofGuides.options[nIndex]=new Option(b, b, bolselected, bolselected);
	}
}
function chgNumClients() {
	var thenum = document.PSForm.NumberofGuides.options[document.PSForm.NumberofGuides.selectedIndex].value ;
	var bolselected = 0 ;
	var numboaters = document.PSForm.NumberofBoaters.options[document.PSForm.NumberofBoaters.selectedIndex].value ;
	var maxclients = parseInt(numboaters-thenum) ;
	document.PSForm.NumberofClients.length=0 ;
	document.PSForm.NumberofClients.options[0]=new Option(0, '');
	for (b=1; b <= maxclients; b++) {
		nIndex=document.PSForm.NumberofClients.options.length;
		if (b==maxclients) { bolselected = 1; }
		document.PSForm.NumberofClients.options[nIndex]=new Option(b, b, bolselected, bolselected);
	}
}
function ifCommercial() {
	if (document.PSForm.CommercialUse[0].checked) {
		document.PSForm.GuideName1.disabled = true ;
		document.PSForm.NumberofGuides.disabled = true ;
		document.PSForm.NumberofClients.disabled = true ;
		document.getElementById('commercialdiv1').style.display='none';
		document.getElementById('commercialdiv2').style.display='none';
	}
	else if (document.PSForm.CommercialUse[1].checked) {
		document.PSForm.GuideName1.disabled = false ;
		document.getElementById('commercialdiv1').style.display='block';
		document.getElementById('commercialdiv2').style.display='block';
	}
}
function validateForm() {
	var validatepass = valpass() ;
	var enfrcpast = true ;
	if (!validatepass) {
		return validatepass;
	}
	else if (validatepass === undefined) {
		enfrcpast = false ;
	}
	if (! document.PSForm.CommercialUse[0].checked && ! document.PSForm.CommercialUse[1].checked) {
		alert("You must enter a use type.") ;
		document.PSForm.CommercialUse[0].focus() ;
		return false ;
	}
	if (document.PSForm.CommercialUse[1].checked && isEmpty(document.PSForm.GuideName1.value)) {
		alert("You must enter a company name if commercial use.") ;
		document.PSForm.GuideName1.focus() ;
		return false ;
	}
	if (document.PSForm.CommercialUse[1].checked &&  isNaN(parseInt(document.PSForm.NumberofGuides.options[document.PSForm.NumberofGuides.selectedIndex].value))) {
		alert("You must enter the number of guides.") ;
		document.PSForm.NumberofGuides.focus() ;
		return false ;
	}
	if (document.PSForm.CommercialUse[1].checked && isNaN(parseInt(document.PSForm.NumberofClients.options[document.PSForm.NumberofClients.selectedIndex].value))) {
		alert("You must enter the number of clients.") ;
		document.PSForm.NumberofClients.focus() ;
		return false ;
	}
	if (document.PSForm.NumberofBoaters.type == 'select' && isNaN(parseInt(document.PSForm.NumberofBoaters.options[document.PSForm.NumberofBoaters.selectedIndex].value))) {
		alert("You must enter the number of boaters.") ;
		document.PSForm.NumberofBoaters.focus() ;
		return false ;
	}
	if (! document.PSForm.Motorized[0].checked && ! document.PSForm.Motorized[1].checked) {
		alert("You must enter if your trip is motorized.") ;
		document.PSForm.Motorized[0].focus() ;
		return false ;
	}
	if (document.PSForm.LaunchPointID.options[document.PSForm.LaunchPointID.selectedIndex].value==0) {
		alert("You must enter a Launch Point.") ;
		document.PSForm.LaunchPointID.focus() ;
		return false ;
	}
	if (document.PSForm.TakeoutPointID.options[document.PSForm.TakeoutPointID.selectedIndex].value==0) {
		alert("You must enter a Take Out Point.") ;
		document.PSForm.TakeoutPointID.focus() ;
		return false ;
	}
	if (! isDate(document.PSForm.CheckinDate)) {
		alert("You must enter a Date In.") ;
		document.PSForm.CheckinDate.focus() ;
		return false;
	}
	if (! isDate(document.PSForm.CheckoutDate)) {
		alert("You must enter a Date Out.") ;
		document.PSForm.CheckoutDate.focus() ;
		return false;
	}
	var oneDay= 1000 * 60 * 60 * 24;
	var checkout=new Date(document.PSForm.CheckoutDate.value);
	var checkin=new Date(document.PSForm.CheckinDate.value);
	//make sure start date is valid; greater than mindate
	if (checkin.getTime()-mindate.getTime() < 0  && enfrcpast) {
		alert("Date In can not be before "+ mindatestr +"!");
		document.PSForm.CheckinDate.focus();
		return false;
	}
	//make sure start date is valid; less than maxdate
	if (checkin.getTime()-maxdate.getTime() > 0) {
		alert("Date In can not be after "+ maxdatestr +"!");
		document.PSForm.CheckinDate.focus();
		return false;
	}
	//make sure end date is valid; less than maxdate2
	if (checkout.getTime()-maxdate2.getTime() > 0) {
		alert("Date Out can not be after "+ maxdate2str +"!");
		document.PSForm.CheckoutDate.focus();
		return false;
	}
	//end date must be greater than or equal to startdate
	if (checkout.getTime()-checkin.getTime() < 0 ){
		alert("Date Out must be greater than or equal to the Date In!");
		document.PSForm.CheckoutDate.focus();
		return false;
	}
	//max trip duration
	if (checkout.getTime()-checkin.getTime() > (mxdys-1) * oneDay){
		alert("Maximum trip duration is " +mxdys+" days!");
		document.PSForm.CheckoutDate.focus();
		return false;
	}
	if (! document.PSForm.DirectionofTravel[0].checked && ! document.PSForm.DirectionofTravel[1].checked && ! document.PSForm.DirectionofTravel[2].checked) {
		alert("You must enter a direction of travel.") ;
		document.PSForm.DirectionofTravel[0].focus() ;
		return false ;
	}	
	if (isEmpty(document.PSForm.FirstName.value)){
		alert("You must enter the Trip Leader's First Name.");
		document.PSForm.FirstName.focus();
		return false;
	}
	if (isEmpty(document.PSForm.LastName.value)){
		alert("You must enter the Trip Leader's Last Name.");
		document.PSForm.LastName.focus();
		return false;
	}
	if (! isDate(document.PSForm.DateofBirth)) {
		alert("You must enter a Date of Birth.") ;
		document.PSForm.DateofBirth.focus() ;
		return false;
	}
	var bdate=new Date(document.PSForm.DateofBirth.value);
	//make sure birth date is valid; greater than maxdate - 100 years
	if (bdate.getTime()-DOBmaxdate.getTime() < 0 ) {
		alert("Birth date can not be before "+ DOBmaxdatestr +"!");
		document.PSForm.DateofBirth.focus();
		return false;
	}
	//make sure birth date is valid; less than mindate - 18 years
	if (bdate.getTime()-DOBmindate.getTime() > 0) {
		alert("Birth date can not be after "+ DOBmindatestr +"!\nLeader must be at least 18 years old.");
		document.PSForm.DateofBirth.focus();
		return false;
	}
	if (isEmpty(document.PSForm.Address1.value)){
		alert("You must enter the Trip Leader's Mailing Address.");
		document.PSForm.Address1.focus();
		return false;
	}
	if (isEmpty(document.PSForm.ZipCode.value)){
		alert("You must enter the Trip Leader's Zip Code.");
		document.PSForm.ZipCode.focus();
		return false;
	}
	if (isEmpty(document.PSForm.City.value)){
		alert("You must enter the Trip Leader's Mailing City.");
		document.PSForm.City.focus();
		return false;
	}
	if (isEmpty(document.PSForm.Phone.value)){
		alert("You must enter the Trip Leader's Phone Number.");
		document.PSForm.Phone.focus();
		return false;
	}
	if (isEmpty(document.PSForm.Email.value)){
		alert("You must enter the Trip Leader's Email.");
		document.PSForm.Email.focus();
		return false;
	}
	if (isEmpty(document.PSForm.Email2.value)){
		alert("You must verify the Trip Leader's Email.");
		document.PSForm.Email2.focus();
		return false;
	}
	if (document.PSForm.Email.value != document.PSForm.Email2.value){
		alert("Your email addresses do not match, please verify your email.");
		document.PSForm.Email.focus();
		return false;
	}
	if (isNaN(parseInt(document.PSForm.NumberofRaft.value))) {
		document.PSForm.NumberofRaft.value = 0;
	}
	if (isNaN(parseInt(document.PSForm.NumberofDrift.value))) {
		document.PSForm.NumberofDrift.value = 0;
	}
	if (isNaN(parseInt(document.PSForm.NumberofPontoon.value))) {
		document.PSForm.NumberofPontoon.value = 0;
	}
	if (isNaN(parseInt(document.PSForm.NumberofKayak.value))) {
		document.PSForm.NumberofKayak.value = 0;
	}
	if (isNaN(parseInt(document.PSForm.NumberofCanoe.value))) {
		document.PSForm.NumberofCanoe.value = 0;
	}
	if (isNaN(parseInt(document.PSForm.NumberofJet.value))) {
		document.PSForm.NumberofJet.value = 0;
	}
	if (isNaN(parseInt(document.PSForm.NumberofOtherMotor.value))) {
		document.PSForm.NumberofOtherMotor.value = 0;
	}
	if (isNaN(parseInt(document.PSForm.NumberofElectricMotor.value))) {
		document.PSForm.NumberofElectricMotor.value = 0;
	}
	//make sure at least one boat type is specified
	if ((document.PSForm.NumberofRaft.value + document.PSForm.NumberofDrift.value + document.PSForm.NumberofPontoon.value + document.PSForm.NumberofKayak.value + document.PSForm.NumberofCanoe.value + document.PSForm.NumberofJet.value + document.PSForm.NumberofOtherMotor.value + document.PSForm.NumberofElectricMotor.value) == 0 ) {
		alert('You must specify the type and number of boats.');
		document.PSForm.NumberofRaft.focus();
		return false;
	}
	//check the alt leader info
	for (var h=1; h < 4; h++ ) {
		if (/*document.getElementById("altleaderdiv" + h).style.display != 'none' 
			&&*/ ! isEmpty(document.getElementById('Alt' + h + 'FirstName').value) 
			&& ! isEmpty(document.getElementById('Alt' + h + 'LastName').value)) {
			if (! isDate(document.getElementById('Alt' + h + 'DateofBirth'))) {
				alert("You must enter a Date of Birth for Alternate Leader " + h + ".") ;
				document.getElementById('Alt' + h + 'DateofBirth').focus() ;
				return false;break;
			}
			var bdate=new Date(document.getElementById('Alt' + h + 'DateofBirth').value);
			//make sure birth date is valid; greater than maxdate - 100 years
			if (bdate.getTime()-DOBmaxdate.getTime() < 0 ) {
				alert("Birth date can not be before "+ DOBmaxdatestr +" for Alternate Leader " + h + ".");
				document.getElementById('Alt' + h + 'DateofBirth').focus();
				return false;break;
			}
			//make sure birth date is valid; less than mindate - 18 years
			if (bdate.getTime()-DOBmindate.getTime() > 0) {
				alert("Birth date can not be after "+ DOBmindatestr +"for Alternate Leader " + h + ".\nLeader must be at least 18 years old.");
				document.getElementById('Alt' + h + 'DateofBirth').focus();
				return false;break;
			}
			if (isEmpty(document.getElementById('Alt' + h + 'Address').value)){
				alert("You must enter the Mailing Address for Alternate Leader " + h + ".");
				document.getElementById('Alt' + h + 'Address').focus();
				return false;break;
			}
			if (isEmpty(document.getElementById('Alt' + h + 'ZipCode').value)){
				alert("You must enter the Zip Code for Alternate Leader " + h + ".");
				document.getElementById('Alt' + h + 'ZipCode').focus();
				return false;break;
			}
			if (isEmpty(document.getElementById('Alt' + h + 'City').value)){
				alert("You must enter the Mailing City for Alternate Leader " + h + ".");
				document.getElementById('Alt' + h + 'City').focus();
				return false;break;
			}
			if (isEmpty(document.getElementById('Alt' + h + 'Phone').value)){
				alert("You must enter the Phone Number for Alternate Leader " + h + ".");
				document.getElementById('Alt' + h + 'Phone').focus();
				return false;break;
			}
			if (isEmpty(document.getElementById('Alt' + h + 'Email').value)){
				alert("You must enter the Email for Alternate Leader " + h + ".");
				document.getElementById('Alt' + h + 'Email').focus();
				return false;break;
			}
		}
	}
	if (document.getElementById("randomquestionanswer") && isEmpty(document.getElementById("randomquestionanswer").value)){
		alert("You must enter the answer to the question.");
		document.PSForm.randomquestionanswer.focus();
		return false;
	}
	return true ;
}
function dateStatus(date) { 
	if (date.getTime() < mindate.getTime() || date.getTime() > maxdate.getTime()) {
		return true; // true means disable 
	} else {
		return false; // leave other dates enabled
	}
}
function dateStatus2(date) { 
	if (date.getTime() < mindate.getTime() || date.getTime() > maxdate2.getTime()) {
		return true; // true means disable 
	} else {
		return false; // leave other dates enabled
	}
}
function dateStatus3(date) { 
	var tmpdt = new Date();
	if (date.getTime() < mindate.getTime() || date.getTime() > tmpdt.getTime()) {
		return true; // true means disable 
	} else {
		return false; // leave other dates enabled
	}
}
function dateStatus4(date) { 
	if (date.getTime() < DOBmaxdate.getTime() || date.getTime() > DOBmindate.getTime()) {
		return true; // true means disable 
	} else {
		return false; // leave other dates enabled
	}
}
function chkzip(fld) {
	var tmpzip = document.getElementById(fld + 'ZipCode').value ;
	if (reg1.test(tmpzip)) {
		getZip(fld);
	} else {
		alert('Please enter a 5 digit zip code.');
		document.getElementById(fld + 'ZipCode').value='';
		document.getElementById(fld + 'City').value='';
		for (i=0; i < document.getElementById(fld + 'State').options.length; i++) {
			if (document.getElementById(fld + 'State').options[i].value == "OR") {
				document.getElementById(fld + 'State').options[i].selected = true ;
			}
		}
		document.getElementById(fld + 'ZipCode').focus();
		return false; //cancel the tab to the next field
	}
}
function addAltLeader() {
	var nnmppl = 3 ;
	if (document.PSForm.NumberofBoaters.type == 'select-one' && ! isNaN(parseInt(document.PSForm.NumberofBoaters.options[document.PSForm.NumberofBoaters.selectedIndex].value))) {
		nnmppl = parseInt(document.PSForm.NumberofBoaters.options[document.PSForm.NumberofBoaters.selectedIndex].value) ;
	} else if (document.getElementById("NumberofBoatersActualUse")) {
		nnmppl = parseInt(document.getElementById("NumberofBoatersActualUse").value) ;
	}
	for (var t=1; t < 4; t++) { 
		if (t >= nnmppl) {
			alert('Your group size does not indicate that you need another alternate trip leader.');
			break;
		} 
		else {
			if (document.getElementById('altleaderdiv' + t).style.display == 'none') {
				document.getElementById('altleaderdiv' + t).style.display = 'inline' ;
				if (t > 1) {
					if (document.getElementById('addaltlink' + (t-1)).style.display != 'none') {
						document.getElementById('addaltlink' + (t-1)).style.display = 'none' ;
						document.getElementById('altldrrmvlink' + (t-1)).style.display = 'none' ;
					}
				} else {
					if (document.getElementById('addaltlink').style.display != 'none') {
						document.getElementById('addaltlink').style.display = 'none' ;
						document.getElementById('linedivabovealts').style.display = 'none' ;
					}
				}
				// refill the form fields with defaults
				if (document.getElementById('Alt' + t + 'LeaderID')) {
					document.getElementById('Alt' + t + 'LeaderID').value = document.getElementById('Alt' + t + 'LeaderID').defaultValue ;
				}
				document.getElementById('Alt' + t + 'FirstName').value = document.getElementById('Alt' + t + 'FirstName').defaultValue ;
				document.getElementById('Alt' + t + 'MiddleName').value = document.getElementById('Alt' + t + 'MiddleName').defaultValue ;
				document.getElementById('Alt' + t + 'LastName').value = document.getElementById('Alt' + t + 'LastName').defaultValue ;
				document.getElementById('Alt' + t + 'DateofBirth').value = document.getElementById('Alt' + t + 'DateofBirth').defaultValue ;
				document.getElementById('Alt' + t + 'Address').value = document.getElementById('Alt' + t + 'Address').defaultValue ;
				document.getElementById('Alt' + t + 'City').value = document.getElementById('Alt' + t + 'City').defaultValue ;
				document.getElementById('Alt' + t + 'ZipCode').value = document.getElementById('Alt' + t + 'ZipCode').defaultValue ;
				document.getElementById('Alt' + t + 'Phone').value = document.getElementById('Alt' + t + 'Phone').defaultValue ;
				document.getElementById('Alt' + t + 'Email').value = document.getElementById('Alt' + t + 'Email').defaultValue ;
				document.getElementById('Alt' + t + 'FirstName').focus();
				break;
			}
		}
	}
}
function removeAltLeader() {
	for (var t=3; t > 0; t--) { 
		if (document.getElementById('altleaderdiv' + t).style.display != 'none') {
			document.getElementById('altleaderdiv' + t).style.display = 'none' ;
			// clear the form fields
			if (document.getElementById('Alt' + t + 'LeaderID')) {
				document.getElementById('Alt' + t + 'LeaderID').value = '' ;
			}
			document.getElementById('Alt' + t + 'FirstName').value = '' ;
			document.getElementById('Alt' + t + 'MiddleName').value = '' ;
			document.getElementById('Alt' + t + 'LastName').value = '' ;
			document.getElementById('Alt' + t + 'DateofBirth').value = '' ;
			document.getElementById('Alt' + t + 'Address').value = '' ;
			document.getElementById('Alt' + t + 'City').value = '' ;
			document.getElementById('Alt' + t + 'ZipCode').value = '' ;
			document.getElementById('Alt' + t + 'Phone').value = '' ;
			document.getElementById('Alt' + t + 'Email').value = '' ;
			if (t > 1) {
				if (document.getElementById('addaltlink' + (t-1)).style.display == 'none') {
					document.getElementById('addaltlink' + (t-1)).style.display = 'inline' ;
					document.getElementById('altldrrmvlink' + (t-1)).style.display = 'inline' ;
				}
			} else {
				if (document.getElementById('addaltlink').style.display == 'none') {
					document.getElementById('addaltlink').style.display = 'inline' ;
					document.getElementById('linedivabovealts').style.display = 'block' ;
				}
			}
			break;
		}
	}
}
