﻿var sControlId

function Init(ControlId) {
    alert('1');
    sControlId=ControlId;
}

function AddressSearch()
{
	var sURL;
	var lHeight;
	var lWidth;
	var lTop;
	var lLeft;
	var sFeatures;
	
	
	sURL="address/search.asp?Add1=" + document.getElementById(sControlId + 'txtAdd1').value + "&Town=" + document.getElementById(sControlId + 'txtTown').value + "&Postcode=" + document.getElementById(sControlId + 'txtPostcode').value;

	lHeight=500;
	lWidth=500;
	lTop=(screen.availHeight-lHeight)/2;
	lLeft=(screen.availWidth-lWidth)/2;
	
	sFeatures="modal=yes,width=" + lWidth + ",height=" + lHeight + ",top=" + lTop + ",left=" + lLeft;
	window.open(sURL,"popupAddressSearch",sFeatures);

}
	
function AddressSelected(AddressString)
{
    //Update Postcode field first
	document.getElementById(sControlId + 'txtPostcode').value=AddressString.split("#")[4];
	
	//and trigger any change event handlers
	triggerEvent(document.getElementById(sControlId + 'txtPostcode'), "change");
	
	//And update all other address fields
	document.getElementById(sControlId + 'txtAdd1').value=AddressString.split("#")[0];
	document.getElementById(sControlId + 'txtAdd2').value=AddressString.split("#")[1];
	document.getElementById(sControlId + 'txtTown').value=AddressString.split("#")[2];
	document.getElementById(sControlId + 'txtCounty').value=AddressString.split("#")[3];
	document.getElementById(sControlId + 'txtOSGridRefEasting').value=AddressString.split("#")[5];
	document.getElementById(sControlId + 'txtOSGridRefNorthing').value=AddressString.split("#")[6];
	document.getElementById(sControlId + 'cboCountryId').value=AddressString.split("#")[7];
	
	CountryIdChanged();
	document.getElementById(sControlId + 'txtLocalAuthority').value=AddressString.split("#")[8];
	document.getElementById(sControlId + 'txtWardCode').value=AddressString.split("#")[9];
	
	// Unhide the address fields
	document.getElementById(sControlId + 'dvAddressDetails').style.display='block';
	
}



function CountryIdChanged()
{
	var lCountryId;
	
	lCountryId=document.getElementById(sControlId + 'cboCountryId').value;
	
	switch(lCountryId) {
	    case 2:
	        // Scotland
	        document.getElementById(sControlId + 'cboConveyancingTypeId').value=1;
	        break;    
	    case 4:
	        // Northern Ireland
	        document.getElementById(sControlId + 'cboConveyancingTypeId').value=2;
	        break;
	    default:
	        // E&W
	        document.getElementById(sControlId + 'cboConveyancingTypeId').value=0;    
	        break;
	}
	
}
	
function FormatPostcode(Field)
{
    var sPostcode;

    sPostcode=Field.value.toUpperCase().trim().replace(' ','');

    if(sPostcode.length>=5)
    {
        sPostcode=sPostcode.substr(0,sPostcode.length-3) + ' ' + sPostcode.substr(sPostcode.length-3,3)
    }

    Field.value=sPostcode;
}

function triggerEvent(Source, EventName)
{
    
    if(Source.dispatchEvent)
    {
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(EventName, true, false);
        
        Source.dispatchEvent(evt);
    }
    else if(Source.fireEvent)
    {
        Source.fireEvent('on' + EventName);
    }
}

function PostcodeChanged()
{
	//Reset all other address fields
	document.getElementById(sControlId + 'txtAdd1').value="";
	document.getElementById(sControlId + 'txtAdd2').value="";
	document.getElementById(sControlId + 'txtTown').value="";
	document.getElementById(sControlId + 'txtCounty').value="";
	document.getElementById(sControlId + 'txtOSGridRefEasting').value="-1";
	document.getElementById(sControlId + 'txtOSGridRefNorthing').value="-1";
	document.getElementById(sControlId + 'cboCountryId').value="1";
	CountryIdChanged();
	document.getElementById(sControlId + 'txtLocalAuthority').value="";
	document.getElementById(sControlId + 'txtWardCode').value="";
}



