// JavaScript Document

function init() {
    var error = '';
    try {
        http = new XMLHttpRequest();
    }
    catch (err) {
        error += 'XMLHttpRequest(): ' + err + '\n';
        try {
            http = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (err) {
            error += 'Msxml2.XMLHTTP: ' + err + '\n';
            try {
                http = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (err) {

                alert('No HTTP request interface:\n' + error + 'Microsoft.XMLHTTP: ' + err);
            }
        }
    }
}

function checkpostcode() {
    var pc = document.makeForm.postcode.value;
    if (pc == '') {
        alert('Please enter a postcode');
    }
    else {
        http.open('GET', '/cgi-bin/postcodes.cgi?postcode=' + escape(pc), true);
        http.onreadystatechange = parseSearchResponse;
        http.send(null);
    }
    return false;
}

function parseSearchResponse() {
    with (document.makeForm) {
        if (http.readyState == 4) {
            if (http.responseText == 'failed') {
                alert('We could not find your postcode');
            }
            else {
                address_options.length = 0;
                address_options[0] = new Option("Select Address", "");
                var idadd = http.responseText.split(':');
                for (var ele = 0; ele < idadd.length; ele++) {
                    var bit = idadd[ele].split('#');
                    address_options[ele + 1] = new Option(bit[1], bit[0]);
                }

                var fillAddress;

                try {
                    // fillAddress() isn't used on all forms this js is
                    fillAddress = fillAddress();
                }
                catch(err) { };

				if(fillAddress){
		            address_options.selectedIndex = fillAddress;
					//getaddress();
					document.makeForm.address1.value = document.makeForm.address_options[address_options.selectedIndex].text
				} else {
				address_options.selectedIndex = 0;
				}
            }
        }
    }
}

function getaddress() {
    with (document.makeForm) {
        var pc = postcode.value;
        if (address_options[address_options.selectedIndex].value == '') {
            return false;
        }
        else {
            http.open('GET', '/cgi-bin/postcodes.cgi?postcode=' + escape(pc) + '&id=' + address_options[address_options.selectedIndex].value, true);
            http.onreadystatechange = parseDetailsResponse;
            http.send(null);
        }
        return false;
    }
}

function parseDetailsResponse() {
    if (http.readyState == 4) {
        var addeles = http.responseText.split(':');
        for (var ele = 0; ele < addeles.length; ele++) {
//            var tf = document.getElementById('address' + (ele + 1));
			var tf = eval("document.makeForm.address" + (ele + 1));
            tf.value = addeles[ele];
        }
    }
}

// #################### collection #####################

function checkpostcode_c() {
    var pc = document.makeForm.postcode_c.value;
    if (pc == '') {
        alert('Please enter a postcode');
    }
    else {
        http.open('GET', '/cgi-bin/postcodes.cgi?postcode=' + escape(pc), true);
        http.onreadystatechange = parseSearchResponse_c;
        http.send(null);
    }
    return false;
}

function parseSearchResponse_c() {
	with (document.makeForm) {
	    if (http.readyState == 4) {
			
            if (http.responseText == 'failed') {
                alert('We could not find your postcode');
            }
            else {
				
                address_options_c.length = 0;
                address_options_c[0] = new Option("Select Address", "");
                var idadd = http.responseText.split(':');
                for (var ele = 0; ele < idadd.length; ele++) {
                    var bit = idadd[ele].split('#');
                    address_options_c[ele + 1] = new Option(bit[1], bit[0]);
                }
				if(fillAddress_c()){
					
		            address_options_c.selectedIndex = fillAddress_c();
					//getaddress();
					document.makeForm.address1_c.value = document.makeForm.address_options_c[address_options_c.selectedIndex].text
				} else {
				address_options_c.selectedIndex = 0;
				}
            }
        }
    }
}

function getaddress_c() {
    with (document.makeForm) {
        var pc = postcode_c.value;
        if (address_options_c[address_options_c.selectedIndex].value == '') {
            return false;
        }
        else {
            http.open('GET', '/cgi-bin/postcodes.cgi?postcode=' + escape(pc) + '&id=' + address_options_c[address_options_c.selectedIndex].value, true);
            http.onreadystatechange = parseDetailsResponse_c;
            http.send(null);
        }
        return false;
    }
}

function parseDetailsResponse_c() {
    if (http.readyState == 4) {
        var addeles = http.responseText.split(':');
        for (var ele = 0; ele < addeles.length; ele++) {
//            var tf = document.getElementById('address' + (ele + 1));
			var tf = eval("document.makeForm.address" + (ele + 1)+"_c");
            tf.value = addeles[ele];
        }
    }
}


// #################### delivery #####################

function checkpostcode_d() {
    var pc = document.makeForm.postcode_d.value;
    if (pc == '') {
        alert('Please enter a postcode');
    }
    else {
        http.open('GET', '/cgi-bin/postcodes.cgi?postcode=' + escape(pc), true);
        http.onreadystatechange = parseSearchResponse_d;
        http.send(null);
    }
    return false;
}

function parseSearchResponse_d() {
    with (document.makeForm) {
        if (http.readyState == 4) {
            if (http.responseText == 'failed') {
                alert('We could not find your postcode');
            }
            else {
                address_options_d.length = 0;
                address_options_d[0] = new Option("Select Address", "");
                var idadd = http.responseText.split(':');
                for (var ele = 0; ele < idadd.length; ele++) {
                    var bit = idadd[ele].split('#');
                    address_options_d[ele + 1] = new Option(bit[1], bit[0]);
                }
				if(fillAddress_d()){
		            address_options_d.selectedIndex = fillAddress_d();
					//getaddress();
					document.makeForm.address1_d.value = document.makeForm.address_options_d[address_options_d.selectedIndex].text
				} else {
				address_options_d.selectedIndex = 0;
				}
            }
        }
    }
}

function getaddress_d() {
    with (document.makeForm) {
        var pc = postcode_d.value;
        if (address_options_d[address_options_d.selectedIndex].value == '') {
            return false;
        }
        else {
            http.open('GET', '/cgi-bin/postcodes.cgi?postcode=' + escape(pc) + '&id=' + address_options_d[address_options_d.selectedIndex].value, true);
            http.onreadystatechange = parseDetailsResponse_d;
            http.send(null);
        }
        return false;
    }
}

function parseDetailsResponse_d() {
    if (http.readyState == 4) {
        var addeles = http.responseText.split(':');
        for (var ele = 0; ele < addeles.length; ele++) {
//            var tf = document.getElementById('address' + (ele + 1));
			var tf = eval("document.makeForm.address" + (ele + 1)+"_d");
            tf.value = addeles[ele];
        }
    }
}
