// JavaScript Document

//////////////////////////////////////////////////////////////////////////////////
function doMailto(thePage) {
	
	var theform = document.getElementById("gbcmailtoform");
	var theMailto = 'mailto:';

	// quick hack for special custom check of age dropdowns on contact page
	if (thePage == 1) {
		
		var camperTwoName = document.getElementById("gbcmailtocamper2name");
		var camperTwoAge = document.getElementById("gbcmailtocamper2age");
		var camperThreeName = document.getElementById("gbcmailtocamper3name");
		var camperThreeAge = document.getElementById("gbcmailtocamper3age");
		
		if (camperTwoName.value) {
			//alert(camperTwoAge.selectedIndex);
			if ((camperTwoName.value.length > 1) && (camperTwoAge.selectedIndex == 0) ) {
				alert("Please provide an age for your second camper.");
				return;
			}
		}
		if (camperThreeName.value) {
				//alert(camperTwoAge.selectedIndex);
			if ((camperThreeName.value.length > 1) && (camperThreeAge.selectedIndex == 0) ) {
					alert("Please provide an age for your third camper.");
					return;
			}
		}
	}
	
	
	// construct the recipient address
	var msgRecipient = theform.elements['gbcrecipientusername'].value + '@' + theform.elements['gbcrecipientdomain'].value;
	
	// construct the message body
	var msgBody = "Name\n" + theform.elements['gbcmailtofirst'].value + ' '  + theform.elements['gbcmailtolast'].value + "\n\n"; 
	msgBody = msgBody + "Address\n" + theform.elements['gbcmailtoaddress'].value + "\n"  + theform.elements['gbcmailtocity'].value + ', '  + theform.elements['gbcmailtoprovince'].value  + "\n" +  theform.elements['gbcmailtopostal'].value + "\n\n";
	msgBody = msgBody + "Telephone\n" + theform.elements['gbcmailtotelephone'].value + "\n\n";
	msgBody = msgBody + "Email address\n" + theform.elements['gbcmailtosender'].value;
	
	// material for the Contact Us page
	if (thePage == 1) {
			msgBody = msgBody + "\n\nCamper[s]\n" + theform.elements['gbcmailtocamper1name'].value + ' - ' + theform.elements['gbcmailtocamper1age'].value +  " yrs";
		
			if (theform.elements['gbcmailtocamper2name'].value != '') {
					msgBody = msgBody + "\n" + theform.elements['gbcmailtocamper2name'].value + ' - ' + theform.elements['gbcmailtocamper2age'].value +  " yrs";
			}
		
			if (theform.elements['gbcmailtocamper3name'].value != '') {
					msgBody = msgBody + "\n" + theform.elements['gbcmailtocamper3name'].value + ' - ' + theform.elements['gbcmailtocamper3age'].value +  " yrs";
			}
			
	}
	
	
	
	// mateial for the Alumni page
	if (thePage == 0) {
			
			if (theform.elements['gbcmailtoalumnicamper'].value != '') {
					msgBody = msgBody + "\n\nYears attended as a camper\n" + theform.elements['gbcmailtoalumnicamper'].value;
			}
			
			if (theform.elements['gbcmailtoalumnistaff'].value != '') {
					msgBody = msgBody + "\n\nYears attended as staff\n" + theform.elements['gbcmailtoalumnistaff'].value ;
			}
			
		   var alumNews = '';
		   if (theform.gbcmailtoalumninews[0].checked) {
			   alumNews = "\n\nSend newsletter by email.";
		   }
	
		   if (theform.gbcmailtoalumninews[1].checked) {
			   alumNews = "\n\nSend newsletter by regular mail.";
		   }
		   
		  msgBody = msgBody + alumNews;
			
	}
	
	
	// add the comments
	if (theform.elements['gbcmailtocomments'].value != '') {
			var msgBody = msgBody + " \n\nAdditional comments\n" + theform.elements['gbcmailtocomments'].value + "\n\n";
	}
	
	// urlencode subject and message body for mailto url string
	msgBody = urlencode(msgBody);
	var msgSubject = theform.elements['gbcsubject'].value;
	msgSubject = urlencode(msgSubject);
	
	// contruct mailto url
	theMailto = theMailto + msgRecipient + '?subject=' + msgSubject + '&body=' + msgBody;
	
	// attempt to open mail in a new window
	window.location = theMailto;
	
} // end doMailto function

////////////////////////////////////////////////////////////////
// escape characters for url-encoding of mailto
function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '%20').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}
