	
	
	function switchInputType(elem){
		// is there a corresponding div with the same ID as the selected value?
		var myDiv			= document.getElementById(elem.value);
		var optionsLength	= elem.length;

		for( var i=0; i<optionsLength; i++ ){
			//alert(elem.options[i].value);
			toggleDiv(elem.options[i].value, 2);
		}
		toggleDiv(elem.value, 1);
	}







	function switchHowHeards(elem)
	{
		// is there a corresponding div with the same ID as the selected value?
		var num		= elem.id.split("_")[1];

		var myDiv			= document.getElementById(elem.value + '_' + num);
		var optionsLength	= elem.length;

		for( var i=0; i<optionsLength; i++ ){
			//alert(elem.options[i].value + '_' + num);
			toggleDiv(elem.options[i].value + '_' + num, 2);
		}
		toggleDiv(elem.value + '_' + num, 1);
	}







	function switchGrantType(elem, len)
	{
		// is there a corresponding div with the same ID as the selected value?
		var bits	= elem.id.split("_");
		var length	= bits.length;
		var id		= bits[(length-1)];

		var myDiv			= document.getElementById('grant_type_div_' + id);

		for( var i=1; i<=len; i++ ){
			//console.log('grant_type_div_'+i);
			toggleDiv('grant_type_div_'+i, 2);
		}
		toggleDiv('grant_type_div_'+id, 1);
	}







	function addDocument(target, appid, max_per_application)
	{
		console.log(target+' :: '+appid+' :: '+max_per_application);

		target_cell = document.getElementById(target);
		inputs = target_cell.getElementsByTagName('input');

		if( max_per_application == 0  ||  inputs.length < max_per_application )
		{
console.log('add');
			var inp = document.createElement('input');
			inp.setAttribute('name','grant_document['+appid+'][]');
			inp.setAttribute('type','file');
			inp.setAttribute('class','upload');
			target_cell.appendChild(inp);

			var br = document.createElement('br');
			target_cell.appendChild(br);
		}
	}







	function addPaidTitleInputs(target, ptft)
	{
		var tg		= document.getElementById(target);
		var divs	= tg.getElementsByTagName('div');
		var len		= divs.length;
		var tp		= ptft == 'ft' ? 0 : 1;
		//console.log(target+' :: '+ptft);

		for( i=0; i<len; i++ )
		{
			div = divs[i];
			iter = i+1;
			div.setAttribute('id', 'paid_row_'+ptft+'_'+iter);
			//console.log(div);
			anchors = div.getElementsByTagName('a');
			anchor = anchors[0];
			anchor.setAttribute('href', "javascript: removePaidTitleInputs('positions_paid_"+ptft+"_div','paid_row_"+ptft+"_"+iter+"');");
		}
		iter = i+1;
		var div = document.createElement('div');
		div.setAttribute('id', 'paid_row_'+ptft+'_'+iter);

		var inp = document.createElement('input');
		inp.setAttribute('name','positions['+tp+'][]');
		inp.setAttribute('type','text');
		inp.setAttribute('size','40');
		inp.setAttribute('maxlength','64');
		inp.setAttribute('value','');

		var anchor = document.createElement('a');
		anchor.setAttribute('href', "javascript: removePaidTitleInputs('positions_paid_"+ptft+"_div','paid_row_"+ptft+"_"+iter+"');");

		var img = document.createElement('img');
		img.setAttribute('src','/images/ButtonRemove.png');
		img.setAttribute('alt','remove staff title');

		anchor.appendChild(img);

		div.appendChild(inp);
		div.appendChild(anchor);

		tg.appendChild(div);
	}







	function removePaidTitleInputs(container, toremove)
	{
		//console.log(container+' :: '+toremove);
		var cont	= document.getElementById(container);
		var row		= document.getElementById(toremove);

		cont.removeChild(row);
	}







	function addVolunteerTitleInputs(target)
	{
		var tg		= document.getElementById(target);
		var divs	= tg.getElementsByTagName('div');
		var len		= divs.length;

		for( i=0; i<len; i++ )
		{
			div = divs[i];
			iter = i+1;
			div.setAttribute('id', 'volunteer_row_'+iter);
			//console.log(div);
			anchors = div.getElementsByTagName('a');
			anchor = anchors[0];
			anchor.setAttribute('href', "javascript: removeVolunteerTitleInputs('positions_volunteer_div','volunteer_row_"+iter+"');");
		}
		iter = i+1;
		var div = document.createElement('div');
		div.setAttribute('id', 'volunteer_row_'+iter);

		var inp = document.createElement('input');
		inp.setAttribute('name','positions[2][]');
		inp.setAttribute('type','text');
		inp.setAttribute('size','40');
		inp.setAttribute('maxlength','64');
		inp.setAttribute('value','');

		var anchor = document.createElement('a');
		anchor.setAttribute('href', "javascript: removeVolunteerTitleInputs('positions_volunteer_div','volunteer_row_"+iter+"');");

		var img = document.createElement('img');
		img.setAttribute('src','/images/ButtonRemove.png');
		img.setAttribute('alt','remove volunteer title');

		anchor.appendChild(img);

		div.appendChild(inp);
		div.appendChild(anchor);

		tg.appendChild(div);
	}







	function removeVolunteerTitleInputs(container, toremove)
	{
		var cont	= document.getElementById(container);
		var row		= document.getElementById(toremove);

		cont.removeChild(row);
	}







	function switchRegType(elemid){
		// is there a corresponding div with the same ID as the selected value?
		var elem			= document.getElementById(elemid);
		var value			= elem.value;
		var optionsLength	= elem.length;
		if( value == 2 ){
			toggleDiv('family', 1);
		}else{
			toggleDiv('family', 2);
		}
	}







	function switchAcknowledgement(value)
	{
		if( value == 1 )
		{
			toggleDiv('AckYes', 1);
		}else if( value == 0 )
		{
			toggleDiv('AckYes', 2);
		}
	}







	function showBikeClubMembers(num){
		for( var i=1; i<=6; i++ ){
			if( i <= num ){
				toggleDiv('member'+i, 1);
			}else{
				toggleDiv('member'+i, 2);
			}
		}
	}





	function auto_fill_kids(elem){
		var srcname = elem.name;
		var tgtname = srcname.substring(6);

		for( var i=0; i<=5; i++ ){
			var target = tgtname + '[' + i + ']';
			if( elem.type == 'select-one' ){
				document.registration_form.elements[target].selectedIndex = elem.selectedIndex;
			}else if( elem.type == 'text' ){
				document.registration_form.elements[target].value = elem.value;
			}
		}
	}





	function copy_value(source, target)
	{
		var src = document.getElementById(source);
		var tgt = document.getElementById(target);
		tgt.value = src.value;
	}





	function copy_address(id)
	{
		copy_value( 'Address_0', 'Address_'+id );
		copy_value( 'Address2_0', 'Address2_'+id );
		copy_value( 'City_0', 'City_'+id );
		copy_value( 'Postal_0', 'Postal_'+id  );
		copy_value( 'PhoneArea_0', 'PhoneArea_'+id );
		copy_value( 'PhoneExchange_0', 'PhoneExchange_'+id );
		copy_value( 'PhoneNumber_0', 'PhoneNumber_'+id );

		document.getElementById('ProvinceCode_'+id).selectedIndex = document.getElementById('ProvinceCode_0').selectedIndex;
	}





	function check_receipt_eligible(elem, elemid){
		var amount = document.getElementById(elemid).value;
		var value = elem.value;
		if( amount < 15 && value == 'y'){
			alert('Only donations of $25 or more will receive a tax receipt.');
			elem.checked='';
		}
	}




	function numbers_only(val){
		if( val )
		{
			val = strip_dollar(val);
			val = parseInt(val,10);
			if( isNaN(val) )
			{
				val = '';
			}
		}
		return val
	}




	function strip_dollar(val){
		val = val.replace("$",'');
		return val
	}





	function select_option(target,value)
	{
		var elem	= document.getElementById(target);
		var length	= elem.options.length;
alert(length);
/*
		for( var i=0; i<=5; i++ ){
			var target = tgtname + '[' + i + ']';
			if( elem.type == 'select-one' ){
				document.registration_form.elements[target].selectedIndex = elem.selectedIndex;
			}else if( elem.type == 'text' ){
				document.registration_form.elements[target].value = elem.value;
			}
		}
*/
	}





	function toggleDiv(id, type){
		var x = null;
		if (document.getElementById){
			x = document.getElementById(id);
		}else if (document.all){
			x = document.all[id];
		}else if (document.layers){
			x = document.layers[id];
		}
		if (x){
			if (type == 0){
				if (x.style){
					if (x.style.display == 'none'){
						x.style.display = '';
					}else{
						x.style.display = 'none';
					}
				}else{
					x.visibility = 'show';
				}
			}else{
				if (type == 1){
					if (x.style){
						x.style.display = '';
					}else{
						x.visibility = 'show';
					}
				}
				if (type == 2){
					if (x.style){
						x.style.display = 'none';
					}else{
						x.visibility = '';
					}
				}
			}
		}
	}









	function checkSpinningAgeRestriction(ageid, rideselid)
	{
		var age		= document.getElementById(ageid).value;
		var ridesel	= document.getElementById(rideselid).value;
		var bits	= rideselid.split("_");
		var ind		= bits[1];

		if( ridesel != 5 || age >= 14 )
		{
			return true;
		}
		document.getElementById(rideselid).selectedIndex="0";
		toggleDiv('bike_slots_'+ind, 2);

		alert('All riders must be at least 14 years of age to participate in the spinning selection.');
		return false;
	}









	function countBikeSlots(slot)
	{
		var count = 0;
		var container = document.getElementById('bike_slots_'+slot);

		var inputs = container.getElementsByTagName("input"); 
		for(var i = 0; i < inputs.length; i++)
		{ 
			if( inputs[i].type == 'checkbox')
			{
				if( inputs[i].checked == true )
				{
					count++;
				}
			}

		}
		return count;
	}









	function kidUpgradeShirt(ageElem)
	{
		var age		= document.getElementById(ageElem).value;
		if( age <= 17 )
		{
			alert('All jerseys come in adult sizes only.');
		}
		return true;
	}








	function calculateMinDonation(RegType, Adults){
		var MinDonations	= document.getElementById('MinDonation');
		var MinDonValue		= 0;

		// single rider
		if (RegType == '1') {
			var RideSel	= document.getElementById('RideSelectionID');
			var age		= document.getElementById('Age').value;

			if( RideSel.value != 5 )
				toggleDiv('bike_slots_0', 2);

			if( RideSel.value != 6 )
				//toggleDiv('sidekick_info', 2);

			if( RideSel.value == 0 ){
				MinDonValue = 50;
			}else if( RideSel.value == 1 ){
				MinDonValue = 125;
			}else if( RideSel.value == 2 ){
				MinDonValue = 250;
			}else if( RideSel.value == 3 ){
				MinDonValue = 500;
			}else if( RideSel.value == 4 ){
				MinDonValue = 800;
			}else if( RideSel.value == 5 ){
				// how many hours @ $50 / hour
				toggleDiv('bike_slots_0', 1);
				var slots = countBikeSlots(0);
				MinDonValue = slots*50;
			}
			// if 17 or under override the pledge level with $25
			if( age <= 17 && age != '' )
			{
				MinDonValue = 25;
			}
			if( RideSel.value == 6 )
			{
				MinDonValue = 0;
			}

		// family rider
		}else if(RegType == '2'){
			var ridercount = document.getElementById('RiderCount').value;
//alert('riders='+ridercount);
			if( ridercount > 0 ){
				Adults = 1;
				for( var i=0; i < ridercount; i++ ){
					var ridesel = document.getElementById('RideSelectionID_' + i).value;
					var age		= document.getElementById('Age_' + i).value;

					if( ridesel == 6 )
					{
						MinDonValue += 0;
					}
					else
					{
						if( ! age ){
							age = 18;
						}
						if( age >= 18 ){
							if( ridesel != 5 ){
								toggleDiv('bike_slots_'+i, 2);
							}
							if( ridesel == 0 ){
								MinDonValue += 50;
							}else if( ridesel == 1 ){
								MinDonValue += 125;
							}else if( ridesel == 2 ){
								MinDonValue += 250;
							}else if( ridesel == 3 ){
								MinDonValue += 500;
							}else if( ridesel == 4 ){
								MinDonValue += 800;
							}
						}
						else
						{
							MinDonValue += 25;
						}
						if( ridesel == 5 && age >= 14 ){
							// how many hours @ $50 / hour
							var slots = countBikeSlots(i);
							MinDonValue += slots*50;
							toggleDiv('bike_slots_'+i, 1);
						}
					}
				}
			}

		// team rider
		}else if(RegType == '3'){
			var RideSel		= document.getElementById('RideSelectionID');

			if ( document.getElementById('Age') )
			{
				var age		= document.getElementById('Age').value;
			}

			if( RideSel.value != 5 ){
				toggleDiv('bike_slots_0', 2);
			}
			if( RideSel.value == 0 ){
				MinDonValue = Adults * 50;
			}else if( RideSel.value == 1 ){
				MinDonValue = Adults * 125;
			}else if( RideSel.value == 2 ){
				MinDonValue = Adults * 250;
			}else if( RideSel.value == 3 ){
				MinDonValue = Adults * 500;
			}else if( RideSel.value == 4 ){
				MinDonValue = Adults * 800;
			}else if( RideSel.value == 5 ){
				// how many hours @ $50 / hour
				toggleDiv('bike_slots_0', 1);
				var slots = countBikeSlots(0);
				MinDonValue = slots*50;
			}
			// if 17 or under override the pledge level with $25
			if( age <= 17 && age != '' )
			{
				MinDonValue = 25;
			}
			if( RideSel.value == 6 )
			{
				MinDonValue = 0;
			}
		}
		MinDonations.value = MinDonValue;
		return true;
	}






	function calculateTeamRegistrationTotal(){
		// default registration amount for 1 rider
		var Total				= 50;
		// registration total element
		var RegistrationTotal	= document.getElementById('RegistrationTotal');
		var RideSelection		= document.getElementById('RideSelectionID').value;
		

		// is 'captain pays for team' checked?
		for (i=0;i<document.registration_form.TeamPay.length;i++) {
			if (document.registration_form.TeamPay[i].checked) {
				var teamPay = document.registration_form.TeamPay[i].value;
			}
		}
		var teamRiderCount	= document.getElementById('TeamRiderCount').value;

		// is captain paying for team jerseys?
		/*
		for (i=0;i<document.registration_form.TeamJerseyPay.length;i++) {
			if (document.registration_form.TeamJerseyPay[i].checked) {
				var teamJerseyPay = document.registration_form.TeamJerseyPay[i].value;
			}
		}
		var teamJerseyCount		= document.getElementById('TeamJerseyCount').value;
		*/

		// captain pays
		if( teamPay == 1 ){
			Total = teamRiderCount * 50;
			/*
			// captain is paying for some jerseys
			if( teamJerseyPay == 1 )
				Total += teamJerseyCount * 35;
			*/
		}
		// if captain is a 'sidekick', registration is $25 not 50 for captain
		if( RideSelection == 6 )
		{
			Total -= 25;
		}
		//alert('calculate team reg\n'+'riders='+teamRiderCount+'\n'+'ride selected='+RideSelection+'\n'+'Total is='+Total);
		RegistrationTotal.value = Total;
		return true;
	}

