var currentAgeFieldsTotal = 0;
var arrInput = new Array(0);
var arrInputValue = new Array(0);

function check_partyType()
{
	x = document.getElementById('calculatorForm')
	groupSize = x.GroupSizeSelected.value
	partyTypeIn = x.PartyTypeSelected.value


	if(partyTypeIn == 1){
		//Set Individuals to 2 people if they have selected a couple
		x.GroupSizeSelected.value = 2
	}

	if(partyTypeIn == 2 && x.GroupSizeSelected.value < 3){
		//Set Individuals to 3 if the customer chooses a family
		x.GroupSizeSelected.value = 3
	}

	if(partyTypeIn == 3 && x.GroupSizeSelected.value < 2){
		//Single Parent Family - Set to 2 people if a  single parent family is selected
		x.GroupSizeSelected.value = 2
	}
}

function check_partyType_radio(partyTypeIn)
{
	x = document.getElementById('calculatorForm')
	groupSize = x.GroupSizeSelected.value
	//partyTypeIn = x.PartyTypeSelected.value

	if(partyTypeIn == 1){
		//Set Individuals to 2 people if they have selected a couple
		x.GroupSizeSelected.value = 2;
		updateTravellersAgesFields(2);
	}

	if(partyTypeIn == 2 && x.GroupSizeSelected.value < 3){
		//Set Individuals to 3 if the customer chooses a family
		x.GroupSizeSelected.value = 3;
		updateTravellersAgesFields(3);
	}

	if(partyTypeIn == 3 && x.GroupSizeSelected.value < 2){
		//Single Parent Family - Set to 2 people if a  single parent family is selected
		x.GroupSizeSelected.value = 2;
		updateTravellersAgesFields(2);
	}
}

function check_winterSports(){
	x = document.getElementById('calculatorForm')
	wintersports = x.WinterSportsCoverSelected.checked

	if(wintersports == true){
		x.WinterSportsLength.disabled = false
	}else{
		x.WinterSportsLength.disabled = true
	}


}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
	return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
	if(radioObj.checked)
	return radioObj.value;
	else
	return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function updateTravellersAgesFields(numberFields) {

	//Number of additional fields required
	numberFieldsDifference = numberFields - currentAgeFieldsTotal;

	//Add/Remove fields from array
	if(numberFieldsDifference == 0){	return;  }
	else if(numberFieldsDifference > 0){

		for (i=0;i<numberFieldsDifference;i++) {

			arrInputValue.push("");
		}


	}else if(numberFieldsDifference < 0){
		for (i=numberFieldsDifference;i < 0;i++) {

			arrInputValue.pop();
		}

	}

	document.getElementById('agesDiv').innerHTML="";
	for (intI=0;intI<numberFields;intI++) {
		document.getElementById('agesDiv').innerHTML+=createInput(intI, arrInputValue[intI]);
	}

	currentAgeFieldsTotal = numberFields;
}

function saveValue(intId,strValue) {
	arrInputValue[intId]=strValue;
}

function createInput(id,value) {
	return "<input type='text' id='AgesIn["+ id +"]' name='AgesIn["+id+"]' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"' />";
}


function check_dateType(TripTypeIn) {
	if(TripTypeIn == 1){
		// change date type to singletrip if on annual
		Element.hide('annual_dates');
		Element.show('singletrip_dates');
		document.getElementById('StartDateSingletrip').disabled = false;
		document.getElementById('StartDateAnnual').disabled = true;
	}
	if(TripTypeIn == 2){
		// change date type to singletrip if on annual
		Element.hide('annual_dates');
		Element.show('singletrip_dates');
		document.getElementById('StartDateSingletrip').disabled = false;
		document.getElementById('StartDateAnnual').disabled = true;		
		
	}

	if(TripTypeIn == 3){
		// change date type to annual if on singletrip
		Element.hide('singletrip_dates');
		Element.show('annual_dates');
		document.getElementById('StartDateAnnual').disabled = false;
		document.getElementById('StartDateSingletrip').disabled = true;
	}
}


function check_where_travelling(TripTypeIn) {
	var party_type_id = getCheckedValue(document.forms['calculatorForm'].elements['PartyTypeSelected']);
	
	if(TripTypeIn == 1){
		// change where travelling if on singletrip
		Element.show('where_singletrip');
		Element.hide('where_longstay');
		Element.hide('where_annual');
		document.getElementById('RegionInLongstay').disabled = true;
		document.getElementById('RegionInAnnual').disabled = true;		
		enableRadioButtonGroup(document.forms['calculatorForm'].elements['PartyTypeSelected']);
		setCheckedValue(document.forms['calculatorForm'].elements['PartyTypeSelected'], party_type_id);
	}
	if(TripTypeIn == 2){
		// change where travelling if on longstay
		Element.hide('where_singletrip');
		Element.show('where_longstay');
		Element.hide('where_annual');
		document.getElementById('RegionInLongstay').disabled = false;
		document.getElementById('RegionInAnnual').disabled = true;		
		disableRadioButton(document.forms['calculatorForm'].elements['PartyTypeSelected'], '1');
		disableRadioButton(document.forms['calculatorForm'].elements['PartyTypeSelected'], '2');
		disableRadioButton(document.forms['calculatorForm'].elements['PartyTypeSelected'], '3');
		setCheckedValue(document.forms['calculatorForm'].elements['PartyTypeSelected'], '0');
		
	}

	if(TripTypeIn == 3){
		// change where travelling if on annual
		Element.hide('where_singletrip');
		Element.hide('where_longstay');
		Element.show('where_annual');		
		document.getElementById('RegionInAnnual').disabled = false;
		document.getElementById('RegionInLongstay').disabled = true;
		enableRadioButtonGroup(document.forms['calculatorForm'].elements['PartyTypeSelected']);
		setCheckedValue(document.forms['calculatorForm'].elements['PartyTypeSelected'], party_type_id);
	}
}

function check_winter_dates() {
	TripTypeIn = document.getElementById('TripTypeIn').value;
	WinterSportsCoverSelected = document.getElementById('WinterSportsCoverSelected').checked;		
	
	if(TripTypeIn == 1){
		// hide wintersports days dropdown if on singletrip
		Element.hide('show_wintersports_length');

	}
	if(TripTypeIn == 2){
		// hide wintersports days dropdown if on longstay
		Element.hide('show_wintersports_length');
	}

	if(TripTypeIn == 3){
		// show wintersports days dropdown if on annual and tickbox is selected
		if(WinterSportsCoverSelected == true)Element.show('show_wintersports_length');
		else Element.hide('show_wintersports_length');
	}
}


function show_calc_help(id,type) {

	var help_url = '/travel_insurance/display_definition.php?ajax_help=1&' + 'def=' + id + '&type=' + type;
	new Ajax.Updater('calculator_help', help_url, {asynchronous:true});

}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function disableRadioButton(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].disabled = true;
		}
	}
}

function enableRadioButtonGroup(radioObj) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;		
			radioObj[i].disabled = false;		
	}
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}