﻿//Postcode look up functions

function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var thisform = this;
    // Checks if browser is Mozilla or Safari
    if (window.XMLHttpRequest) { thisform.xmlHttpReq = new XMLHttpRequest(); }
    // Checks if browser is Internet Explorer
    else if (window.ActiveXObject) { thisform.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); }

    thisform.xmlHttpReq.open('POST', strURL, true);
    thisform.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    thisform.xmlHttpReq.onreadystatechange = function() {
        if (thisform.xmlHttpReq.readyState == 4) { updatepage(thisform.xmlHttpReq.responseText); }
    }
    thisform.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var postcode = document.getElementById('Postcode').value;
    qstr = 'postcode=' + escape(postcode);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str) {
    if (str.lastIndexOf("error") >= 0) {
        //alert('ERROR \n' + str);
        document.getElementById('ErrorMessage').style.display = 'block';
        document.getElementById('ErrorMessage').innerHTML = str;
    }
    else {
        var myArray = str.split("#");
        var myAddress = myArray[0].split("|");
        if (myAddress.length > 2) {
            AddItem('ddl1', '--please select a value--', '0');
            for (var i = 0, len = myAddress.length - 1; i < len; ++i) {
                AddItem('ddl1', myAddress[i], myAddress[i]);
            }
            document.getElementById('result').style.display = 'block';
        }
        else {
            document.getElementById("Address1").value = myAddress[0];
        }
        document.getElementById("Address2").value = myArray[1];
        //document.getElementById("Address3").value = myArray[2];
        //document.getElementById("City").value = myArray[3];
        document.getElementById("City").value = myArray[4];
        document.getElementById("County").value = myArray[5];
        
        if (document.getElementById('CountryID')) {
            //set the country drop down box to uk as we have done a look up
            setSelectedIndex(document.getElementById('CountryID'), '224');
        }
    }
}
function changebox(item) {
    document.getElementById('Address1').value = item.value;
    document.getElementById('result').style.display = 'none';
    
}
function AddItem(ddl, Text, Value) {
    // Create an Option object        
    var opt = document.createElement("option");
    // Add an Option object to Drop Down/List Box
    document.getElementById(ddl).options.add(opt);
    // Assign text and value to Option object
    opt.text = Text;
    opt.value = Value;

}
function removeAllOptions(selectbox) {
    var i;
    for (i = selectbox.options.length - 1; i >= 0; i--) {
        selectbox.remove(i);
    }
}
function clearfullform() {
    clearform();
    document.getElementById('Postcode').value = '';
}
function clearform() {
    if (document.getElementById('ddl1'))
        removeAllOptions(document.getElementById('ddl1'));
    document.getElementById('ErrorMessage').style.display = 'none';
    document.getElementById('result').style.display = 'none';
    document.getElementById("Address1").value = '';
    document.getElementById("Address2").value = '';
    //document.getElementById("Address3").value = '';
    //document.getElementById("Address4").value = '';
    document.getElementById("City").value = '';
    document.getElementById("County").value = '';
    //document.getElementById("PostcodeB").value = '';
}

function setSelectedIndex(s, v) {
    for (var i = 0; i < s.options.length; i++) {
        if (s.options[i].value == v) {
            s.options[i].selected = true;
            return;
        }
    }
}
