
// inline help
function field_help(helpid){

	//alert();
	$("#"+helpid).toggle('fast');

}






// radio button toggleable code
var radioState = new Array; 

function toggleRadio(button) { 

if (radioState[button.name] && radioState[button.name] == button.value) { 
button.checked = false; 
radioState[button.name] = ''; 
} 
else { 
button.checked = true; 
radioState[button.name] = button.value; 
} 

return true; 
} 


// Page 1


// need to knoock out the PAF validatin for non uk countries in Admin mode.
function country_select(option){

	// alert(option.value.split("|")[1]);
	if (option.value.split("|")[1] != "UNITED KINGDOM"  )
	{
		// reveal the address fields
			// make the address block visible
			
			$("#addressBlock1").show();
			$("#addressBlock2").show();
			$("#addressBlock3").show();
			$("#addressBlock4").show();
			$("#addressBlock5").show();
			$("#paf_finder_button").hide();

	
	}else // its the uk so do the normal thing
	{
			$("#addressBlock1").hide();
			$("#addressBlock2").hide();
			$("#addressBlock3").hide();
			$("#addressBlock4").hide();
			$("#addressBlock5").hide();
			$("#paf_finder_button").show();
	
	}

}




// if an age of 19 is selected then display an alert.
function check_year(option){
	
    var year = option.options[option.selectedIndex].value;
	// if the user is over 20 display an alert
	var d = new Date();
	var ageDiff = d.getFullYear() - year;
	if (ageDiff > 19)
	{
		alert("Altitude is a club just for 13 to 18 year olds, so you're too old to join.\nBut don't worry, you can find plenty of information on the\nRAF Careers site at www.raf.mod.uk/careers");
	
	}

	
	
}



function populate_AltitudeLevel(){

	alert("something");

	//document.altform.aboutAltitudeLevel2.disabled=
	$("#aboutAltitudeLevel2").load("lookup.cfm");

}

function populate_aboutAltitudeLevel2(element,lookupType,filter)
{
		$("#"+element).html("<option value=\"\">no data loaded</option>");
		$("#specificSource").hide();
		
		$("#SpecificSourceManditory").val("false");
		$.get("alt_registration_getAjaxLookup.cfm", { type: "select",	lookupType: lookupType,filter: filter }, 
		function(data)
		{
			
			
			// alert("Data Loaded: " + trim(data)); 
			// split up data and populate fields
			if(trim(data)!= "Error: no data for this lookup"){
			  $("#"+element).html(data);
			  $("#SpecificSourceManditory").val("true");
			  $("#specificSource").show();
			}
		
		}
	);


}

// check post
function check_mail()
{
	//alert($("#mailOptout:checked").val() != "true");
	if($("#mailOptout:checked").val() != "true"){
	 if(!confirm("Are you sure?\n If you choose not to receive Altitude information by post, you won't get the free Altitude magazine - \n which is packed with exciting RAF articles, stories and photos.\n You won't receive information about exclusive RAF experiences and competitions either."))
		{
		 $("#mailOptout").attr("checked","checked");	
			
		}
	}

}

function p1submit()
{
	// pop open the address fields so any validation messages can be seen
		$("#addressBlock1").show();
		$("#addressBlock2").show();
		$("#addressBlock3").show();
		$("#addressBlock4").show();
		$("#addressBlock5").show();


}



function alt_ajax_populate_select(element,lookupType,filter)
{
	
	
	//alert("alt_registration_getAjaxLookup.cfm?type=select&lookupType="+ lookupType +"&filter=" + filter);
	//ducument.write("alt_registration_getAjaxLookup.cfm?type=select&lookupType="+ lookupType +"&filter=" + escape(filter));
	$("#"+element).html("<option>Loading...</option>");
	$("#"+element).load("alt_registration_getAjaxLookup.cfm?type=select&lookupType="+ lookupType +"&filter=" +escape(filter)+"");

}




// page 2 step 1

function alt_page2_select_country(selected_text)
{
	
	

	// switch off the prepopulated divs
	$("#gcse_multi_select_prepop").hide();
	$("#foundation_multi_select_prepop").hide();
	$("#higher_multi_select_prepop").hide();
	$("gcse_grade_select_prepop").hide();
	$("foundation_multi_select_grade_prepop").hide();
	$("higher_multi_select_grade_prepop").hide();
	
	alt_ajax_populate_select('EducationYear','EducationYear_Lookup',selected_text);
	alt_ajax_populate_select('EducationStudyingFor','EducationStudyingFor_Lookup',selected_text);
	alt_ajax_populate_select('EducationAimedFor','EducationAimedFor_Lookup',selected_text); 

	// populate the gcse list based on the country selected
	// OLD:changes have happened to the way that GCSE lookups are handled
	//$("#gcse_multi_select").load("alt_registration_getAjaxLookup.cfm?type=multi&lookupType=GCSE_Lookup&filter=" +escape(selected_text)+"");
	//$("#gcse_grade_select").load("alt_registration_getAjaxLookup.cfm?type=grade&lookupType=GCSE_Lookup&filter=" +escape(selected_text)+"");
	if (selected_text == "Scotland")
	{
		// alert($("#title_quals_study_for").html()):
		// set the country specific titles	
		$("#title_quals_study_for").html("<em>*</em> What Highers are you studying for?</h3>");
		
		// gcse
		$("#gcse_multi_select").load(				"alt_registration_getAjaxLookup.cfm?type=multi&lookupType=SubjectsScotland_Lookup&filter=GCSE");
		$("#foundation_multi_select").load(			"alt_registration_getAjaxLookup.cfm?type=multi&lookupType=SubjectsScotland_Lookup&filter=Foundation%20Diploma");
		$("#higher_multi_select").load(				"alt_registration_getAjaxLookup.cfm?type=multi&lookupType=SubjectsScotland_Lookup&filter=Higher%20Diploma");
		// grades
		$("#gcse_grade_select").load(				"alt_registration_getAjaxLookup.cfm?type=grade&lookupType=SubjectsScotland_Lookup&filter=GCSE");
		$("#foundation_multi_select_grade").load(	"alt_registration_getAjaxLookup.cfm?type=grade&lookupType=SubjectsScotland_Lookup&filter=Foundation%20Diploma");
		$("#higher_multi_select_grade").load(		"alt_registration_getAjaxLookup.cfm?type=grade&lookupType=SubjectsScotland_Lookup&filter=Higher%20Diploma");
	
	}else // its England / Wales or other
	{
		// set the country specific titles	
		$("#title_quals_study_for").html("<em>*</em> What GCSEs are you studying for?</h3>");
		
		// gcses
		$("#gcse_multi_select").load(				"alt_registration_getAjaxLookup.cfm?type=multi&lookupType=Subjects_Lookup&filter=GCSE");
		$("#foundation_multi_select").load(			"alt_registration_getAjaxLookup.cfm?type=multi&lookupType=Subjects_Lookup&filter=Foundation%20Diploma");
		$("#higher_multi_select").load(				"alt_registration_getAjaxLookup.cfm?type=multi&lookupType=Subjects_Lookup&filter=Higher%20Diploma");
		// grades
		$("#gcse_grade_select").load(				"alt_registration_getAjaxLookup.cfm?type=grade&lookupType=Subjects_Lookup&filter=GCSE");
		$("#foundation_multi_select_grade").load(	"alt_registration_getAjaxLookup.cfm?type=grade&lookupType=Subjects_Lookup&filter=Foundation%20Diploma");
		$("#higher_multi_select_grade").load(		"alt_registration_getAjaxLookup.cfm?type=grade&lookupType=Subjects_Lookup&filter=Higher%20Diploma");
	}

	



	//$("gcse_multi_select").html("<h1>123</h1>");

	// reveal the school_year div
	$("#school_year").show();

	// hide the previous selections
	$("#education_studying_for").hide();
	$("#education_studying_for_other").hide();
	$("#education_aimed_for").hide();
	$("#education_studying_for_other").hide();
	$("#gcse_studying").hide();
	$("#gcse_grades").hide();

}

// page 2 step 2
function alt_page2_selected_year(selected_text)
{
	//TODO: take the selected text, do a lookup and populate the what GCSE's
	
	$("#education_studying_for").show();
	$("#education_studying_for_other").hide();
	$("#education_aimed_for").hide();
	$("#education_studying_for_other").hide();
	$("#gcse_studying").hide();
	$("#gcse_grades").hide();


	
}

// page 2 step 3
function alt_page2_selected_education1(selected_text)
{
	
	//alert("ed1x: " + selected_text);
	// if other selected reveal the other box
	if(selected_text == "Other")
	{
		
		$("#education_studying_for_other").show();
	}else
	{
		$("#education_studying_for_other").hide();
	}

	$("#education_aimed_for").show();

}

// page 2 step 4
function alt_page2_selected_education_aimed(selected_text)
{
	if(selected_text == "Other")
	{
		
		$("#education_aimed_for_other").show();
	}else
	{
		$("#education_aimed_for_other").hide();
	}
	
	
	// show either the gcse studying or the gcse grades dependant on school year#
	// >=11 grades | < 11 grades
	
	educationYear = $('#EducationYear :selected').text();

	//alert(educationYear);
	 
	if (	
		
		educationYear == "Secondary 5" |
		educationYear == "Secondary 6" |
		educationYear == "12" |
		educationYear == "13" 
	)
	{
		$("#gcse_grades").show();
		$("[@name=validate]").val("grades");
	}else	 
	{
		$("#gcse_studying").show();
		$("[@name=validate]").val("gcse");
	}

	// reveal the next previous buttons
	$("#p2nextPrevious").show();

	$('#gcse_studying span').vAlign();
	$('#gcse_studying input').vAlign();
	$('#gcse_grades span').vAlign();
	$('#gcse_grades input').vAlign();



}

// page 3 show careers
function showCareers()
{
	$("#interestCareers").show();
	$('#gcse_studying span').vAlign();
	$('#gcse_studying input').vAlign();
	$('#gcse_grades span').vAlign();
	$('#gcse_grades input').vAlign();
}






function paf_finder()
{
	//do some validation of the postcode
	if(!/^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} [0-9][A-Za-z]{2}$/.test($("#Postcode").val())){
		alert("You need to enter a valid postcode to lookup your address\nPlease check it contains a space.");
		return false;
	}
	
	// get the postcode
	var postcode = $("#Postcode").val();
	
	// populate the addressChooserList

	$("#AddressLine1").val("");
	$("#AddressLine2").val("");
	$("#town").val("");
			
	$("#addressChooserList").load("alt_ajax_getAddressList.cfm?postcode="+escape(postcode));

	$("#addressChooser").show();



}
function paf_populate()
{
	var lookupVal = $("#addressChooserList :selected").val();
	
	//$("#AddressLine1").val("alt_ajax_getAddressDetail.cfm?postcode="+escape(lookupVal));
	// populate the details

	$.get("alt_ajax_getAddressDetail.cfm", { postcode: lookupVal }, 
		function(data)
		{
			//alert("Data Loaded: " + trim(data)); 
			// split up data and populate fields
			var details = trim(data);
			var detailsAR = details.split("|");		
			
			$("#AddressLine1").val(trim(detailsAR[1]));
			$("#AddressLine2").val(trim(detailsAR[2]));
			$("#town").val(trim(detailsAR[3]));

			// make the address block visible
			$("#addressBlock1").show();
			$("#addressBlock2").show();
			$("#addressBlock3").show();
			$("#addressBlock4").show();
			$("#addressBlock5").show();
		}
	);
	
	$("#addressChooser").hide();

}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

// this need a new image loding through ajax
function changeCaptcha()
{
	//alert("change");

	$.get("alt_ajax_generateCaptcha.cfm", 
		function(data)
		{
			
			//alert("Data Loaded: " + trim(data)); 
			//$("#captchaImg").src(trim(data));
			$("#captchaImg").attr("src", trim(data));
		
		}
	);

}



// Clearbyte form validation/layout functions

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
 return this.each(function(i){
 var ah = $(this).height();
 var ph = $(this).parent().height();
 var mh = (ph - ah) / 2;
 $(this).css('margin-top', mh);
 });
};
})(jQuery);




$().ready(function() {
	
	
	$('#gcse_studying span').vAlign();
	$('#gcse_studying input').vAlign();
	$('#gcse_grades span').vAlign();
	$('#gcse_grades input').vAlign();
	$('#gcse_grades .grade label').vAlign();
	$('#gcse_grades .grade h4').vAlign();


});




