							var addressArray;

							var currentPostfix = null;
							var postcodeField = null;
							var countyField = null;
							var townField = null;
							var addressLine1Field = null;
							var addressLine2Field = null;
							var addressLine3Field = null;
					
							var houseNoField = null;
							var houseNoFieldName = null;
							var addressTypeField = null;
									
							var tableAddressBlock = null;
							var trNoRecordBlock = null;
							var trCantFindBlock = null;
					        var trQuaBlock = null;                                           
					                    
					        function useFieldsWithPostfix(fieldPostFix)
							{
								currentPostfix = fieldPostFix;
							    postcodeField = eval("myForm.postcode" + fieldPostFix);
							    countyField = eval("myForm.county" + fieldPostFix);
							    townField = eval("myForm.town" + fieldPostFix);
							    addressLine1Field = eval("myForm.addressLine1" + fieldPostFix);
							    addressLine2Field = eval("myForm.addressLine2" + fieldPostFix);
							    addressLine3Field = eval("myForm.addressLine3" + fieldPostFix);
							    
								houseNoField = eval("myForm.houseNo" + fieldPostFix);
								houseNoFieldName = "houseNoFieldNameId" + fieldPostFix;
							    addressTypeField = eval("myForm.addressType" + fieldPostFix);
											
								tableAddressBlock = document.getElementById("address-block" + fieldPostFix);
								trNoRecordBlock = document.getElementById("no-record-block" + fieldPostFix);
								trCantFindBlock = document.getElementById("cant-find-block" + fieldPostFix);
					            trQuaBlock = document.getElementById("qas-block" + fieldPostFix); 
							}

					        function addressLineFocus(fieldPostFix)
							{
							    var addressLine1FieldFocus = eval("myForm.addressLine1" + fieldPostFix);
								addressLine1FieldFocus.focus();
							}


                            function showAddressBlock()
							{
                                if (tableAddressBlock) {
                                    tableAddressBlock.style.display = "block";
                                }
								hideQasCantFindButton();
								if (window.resizeParentFrame) {
									resizeParentFrame();
								}
                            }

                            function hideAddressBlock()
							{
                                if (tableAddressBlock) {
                                    tableAddressBlock.style.display = "none";
                                }
                            }

							function hideNoRecordText()
							{
                                if (trNoRecordBlock) {
								    trNoRecordBlock.style.display = "none";
                                }
							}
							function hideQasCantFindButton()
							{
                                if (trCantFindBlock) {
								    trCantFindBlock.style.display = "none";
                                }
							}

							function hideQasTable()
							{
								if (trQuaBlock) {
									trQuaBlock.innerHTML = "";
								    trQuaBlock.style.display = "none";
                                }
							}
							function showNoRecordText()
							{						
                                if (trNoRecordBlock) {
								    trNoRecordBlock.style.display = "block";
                                }								
								hideQasTable();
								showAddressBlock();
								emptyAddressSection();
								addressLine1Field.focus();								
							}
							function showQasCantFindButton()
							{
                                if (trCantFindBlock) {
								    trCantFindBlock.style.display = "block";
                                }
							}
							function locateAddress()
							{
								hideQasTable();
								hideQasCantFindButton();
								hideNoRecordText();
								// using one frame -- it can't mix the address data
								self.qasFrame.location = "/bmwuk/forms/qas_address_search_results/?postcode=" + postcodeField.value + "&houseNo=" + houseNoField.value + "&addressType=" + addressTypeField.value;
								if (window.locateDealersHidden) {
									locateDealersHidden();
								}
							}
							
							function locateAddressJoy()
							{
								hideQasCantFindButton();
								hideNoRecordText();
								// using one frame -- it can't mix the address data
								self.qasFrame.location = "/bmwuk/forms/qas_address_search_results/?postcode=" + postcodeField.value + "&houseNo=" + houseNoField.value + "&addressType=" + addressTypeField.value;
								if (window.locateDealersHidden) {
									locateDealersHidden();
								}
							}
							
							function showAddresses(newAddressArray)
							{					
                                if (trQuaBlock) {
									addressArray = newAddressArray;
									var qasTable = "<div id='tabdata' class='qasData'><table style=\"width: 383px;\" border=\"0\" cellspacing=\"2\" class=\"tabdata-stripy-table\">";

									qasTable += "<tr>";
									qasTable += "<td scope='col' colspan='4'>Please select your address from the list below:</td>";
									qasTable += "</tr>";

									qasTable += "<tr>";
									qasTable += "<th style=\"width: 135px;\" scope='col'>Address Line</th>";
									qasTable += "<th style=\"width: 90px;\" scope='col'>Town/City</th>";
									qasTable += "<th scope='col'>County</th>";
									qasTable += "<th scope='col' style='background:white;'>&nbsp;</th>";
									qasTable += "</tr>";

									var c = 1;
									for (i = 0; i < addressArray.length; i++)
									{
										var aAttrs = ' href="#" onclick="selectAddress(' + i + '); return false;"';
										c = (c == 0 ?  1 : 0);
										qasTable += "<tr" + (c == 1 ? " class=\"tabdata_altrow\"" : " class=\"tabdata_standardrow\"")  + " valign='middle'>";
										qasTable += "<td style=\"width: 135px;\"><a" + aAttrs + ">" + addressArray[i][2] + "</a></td>";
										qasTable += "<td style=\"width: 90px;\"><a" + aAttrs + ">" + addressArray[i][1] + "</a></td>";
										qasTable += "<td style=\"width: 90px;\"><a" + aAttrs + ">" + addressArray[i][0] + "</a></td>";
										qasTable += "<td style=\"text-align: right;\"><span class=\"qas-arrowed-link\"><a" + aAttrs + ">Select</a></span></td>";
										qasTable += "</tr>";
									}
									qasTable += "</table></div>";

									trQuaBlock.innerHTML = qasTable;
								    trQuaBlock.style.display = "block";
								}
								showQasCantFindButton();
							}

							function addressSectionSet(county, town, addressLine1, addressLine2, addressLine3)
							{
								countyField.value = county;
								townField.value = town;
								addressLine1Field.value = addressLine1;
								addressLine2Field.value = addressLine2;
								addressLine3Field.value = addressLine3;
							}

							function emptyAddressSection()
							{
								addressSectionSet("", "", "", "", "");
							}

							function selectAddress(i)
							{
								addressSectionSet(addressArray[i][0], addressArray[i][1], addressArray[i][2], addressArray[i][3], addressArray[i][4]);
								hideQasTable();
								showAddressBlock();
								var url = location.href;
								var pos = url.indexOf('#');
								if (pos > 0 ) url = url.substr(0,pos);
								var isSafari = navigator.vendor && (navigator.vendor.indexOf('Apple') != -1);
								if (!isSafari) {
									location.replace(url + '#chng' + currentPostfix);
								}
							}

							var origHouseNo;
							var origTdClass;
							var origInputClass;
							var disabledTdClass = "disabled_form_header_color";
							var disabledInputClass = "disabled_form_field_color";

							function initAddressPage(addressType,
							                         houseNo,
							                         addressEnabled,
							                         amendEnabled)
							{
							    // It is requested by Natasha to let the address be changeable anytime
	
								origHouseNo = houseNo;
								origTdClass = document.getElementById(houseNoFieldName).className;
								origInputClass = houseNoField.className;
							    
								addressTypeSet(addressType);
								addressTypeChanged();

							}
							function addressTypeSet(addressType)
							{
							    var checkNdx = (addressType == 'Home' ? 0 : 1);

							    addressTypeField[checkNdx].checked = true;
							}

							function addressTypeChanged()
							{
								
    							var addressType = (addressTypeField[0].checked ? "Home" : "Work");
								// ToDo: set correct class names !!!!

							    var textClassName  = (addressType == 'Home' ? origTdClass : disabledTdClass);
							    var inputClassName = (addressType == 'Home' ? origInputClass : disabledInputClass);

							    var houseNoElement;
							    houseNoElement = document.getElementById("houseNoFieldNameLabelId");
							    if (houseNoElement) {
							    	houseNoElement.style.visibility = (addressType == 'Home' ? "visible" : "hidden");
							    }
							    houseNoElement = document.getElementById("houseNo");
							    if (houseNoElement) {
							    	houseNoElement.style.visibility = (addressType == 'Home' ? "visible" : "hidden");
							    }

							    var addrLine1Str   = (addressType == 'Home' ? "Address line 1: <span class='dnc-red-asterisk'>*</span>" : "Business name:*");

								document.getElementById(houseNoFieldName).className = textClassName;
							    houseNoField.className = inputClassName;			

							    document.getElementById("addressLine1FieldNameId" + currentPostfix).innerHTML = addrLine1Str;

							    if (addressType == 'Home')
							    {
							    	if( origHouseNo != "" && origHouseNo != undefined )
							    	{
							        	houseNoField.value    = origHouseNo;
							        }
							        houseNoField.disabled = false;
							    }
							    else
							    {
							        origHouseNo              = houseNoField.value;
							        houseNoField.value    = '';
							        houseNoField.disabled = true;
							    }
							    if( window.addressTypeChangedCustom )
							    {
							    	addressTypeChangedCustom();
							    }
							}
							
							function callUsePostcodeIfExists()
							{
								var methodName = "window.useAddressPostcode" + currentPostfix;
								if (eval(methodName))
								{
									eval(methodName + "('" + postcodeField.value + "');");
								}
							}