// Delivery Address object definition
function delAddress(id, company, mailstop, line1, line2, line3, city, postcode, country, countryCode, telephone, region, regionCode, residential){
	this.id = id;
	this.company = company;
	this.mailstop = mailstop;
	this.line1 = line1;
	this.line2 = line2;
	this.line3 = line3;
	this.city = city;
	this.postcode = postcode;
	this.country = country;
	this.countryCode = countryCode;
	this.telephone = telephone;
	this.region = region;
	this.regionCode = regionCode;
	this.residential = residential;
}

// Function to change the delviery address to the details from the select box. 
// This method also resets the delivery options checkbox to deliver to address.
function changeAddress( i_type, i_elementname, i_addresses, i_doeditable ) {
	
	// change the delivery options
	if( i_type == "D" )
		document.getElementById("todeladdress").checked = true ;
	else if( i_type == "B" )
		document.getElementById("collection").checked = true ;
	else if( i_type == "P" ) 
		document.getElementById("manual").checked = true ;
	
	// get the drop down box
	var addSel = document.getElementById( i_elementname );
	
	// work out the selected index, if 0 then set to 1 for the first actual address
	var ind = addSel == null || addSel.selectedIndex == 0 ? 1 : addSel.selectedIndex;
	
	// update the address fields with the new index
	updateAddressFields(i_addresses[ind].mailstop,
											i_addresses[ind].line1,
											i_addresses[ind].line2,
											i_addresses[ind].line3,
											i_addresses[ind].city,
											i_addresses[ind].postcode,
											i_addresses[ind].country,
											i_addresses[ind].countryCode,
											i_addresses[ind].region,
											i_addresses[ind].regionCode,
											i_addresses[ind].residential);
											
	// always set the hidden fields for company and telephone
	if( i_addresses[ind].company && document.getElementById('delcompany_hidden') )
		document.getElementById('delcompany_hidden').value = i_addresses[ind].company;
	if( i_addresses[ind].telephone && document.getElementById('deltel_hidden') )
		document.getElementById('deltel_hidden').value 		= i_addresses[ind].telephone;
	
	if( i_doeditable ){
		if( i_addresses[ind].company && document.getElementById('delcompany') )
			document.getElementById('delcompany').value = i_addresses[ind].company;
		if( i_addresses[ind].telephone )
			document.getElementById('deltel').value 		= i_addresses[ind].telephone;
	}
	
	if(  document.getElementById('delcompany') )
		document.getElementById('delcompany').disabled 			= i_type == "D" || i_type == "P" ? false : true ;
//  if(  document.getElementById('delcontactname') )
//		document.getElementById('delcontactname').disabled 	= i_type == "D" || i_type == "P" ? false : true ;
	if(  document.getElementById('deldepartment') )
		document.getElementById('deldepartment').disabled 	= i_type == "D" || i_type == "P" ? false : true ;
	
  	document.getElementById('deltel').disabled 						= i_type == "D" || i_type == "P" ? false : true ;
	
}

function isDeliveryToAddress(){
	return document.getElementById("todeladdress").checked ;
}

lastPAFAddress = "";
function updateLastPafAddress() {
	if( document.getElementById('delcompany') )
		document.getElementById('delcompany').disabled 	= false ;
	
	document.getElementById('deltel').disabled 			= false ;
	
	var comp = "" ;
	if( document.getElementById('delcompany') )
		comp = document.getElementById('delcompany').value ;
		
	var region = "" ;
	if( document.getElementById('delregion') )
		region = document.getElementById('delregion').value ;
		
	var regionCode = "" ;
	if( document.getElementById('delregionCode') )
		regionCode = document.getElementById('delregioncode').value ;
		
	lastPAFAddress = new delAddress('',  comp,
									document.getElementById('del_address1').value,
									document.getElementById('del_address2').value,
									document.getElementById('del_address3').value,
									document.getElementById('del_city').value,
									document.getElementById('del_postcode').value,
									document.getElementById('del_country').value,
									document.getElementById('del_countryCode').value,
									document.getElementById('deltel').value,
									region,
									regionCode,
									document.getElementById('del_isres').value );
}
